22 lines
713 B
C#
22 lines
713 B
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Omika.HfSqlProbe;
|
|
|
|
internal static partial class SqlIdentifierValidator
|
|
{
|
|
/// <summary>Accepts only letters, digits, and underscores.</summary>
|
|
[GeneratedRegex(@"\A[A-Za-z_][A-Za-z0-9_]*\z")]
|
|
private static partial Regex IdentifierPattern();
|
|
|
|
public static bool IsValid(string name) => IdentifierPattern().IsMatch(name);
|
|
|
|
/// <summary>Validates and returns the identifier, or throws on invalid input.</summary>
|
|
public static string Validate(string name)
|
|
{
|
|
if (!IsValid(name))
|
|
throw new ArgumentException(
|
|
$"Invalid SQL identifier: '{name}'. Only [A-Za-z0-9_] allowed.");
|
|
return name;
|
|
}
|
|
}
|