|
| 1 | +using SqlServerSimulator.Storage; |
| 2 | + |
| 3 | +namespace SqlServerSimulator.Parser.Expressions; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// SQL <c>COLUMNPROPERTY(table_or_proc_id, 'column_or_param_name', 'property')</c>: |
| 7 | +/// per-column metadata flags / counts for a table column. Returns <c>int</c>; |
| 8 | +/// unknown property / column / id / NULL on any arg → NULL (matches real |
| 9 | +/// SQL Server, probe-confirmed 2026-05-23). Property names and column names |
| 10 | +/// are both case-insensitive. |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// <para> |
| 14 | +/// Shipped properties (all probe-confirmed against SQL Server 2025): |
| 15 | +/// <list type="bullet"> |
| 16 | +/// <item><description><c>AllowsNull</c> — 1 / 0 from <see cref="HeapColumn.Nullable"/>.</description></item> |
| 17 | +/// <item><description><c>IsIdentity</c> — 1 / 0 from <see cref="HeapColumn.Identity"/>.</description></item> |
| 18 | +/// <item><description><c>IsComputed</c> — 1 / 0 from <see cref="HeapColumn.Computed"/>.</description></item> |
| 19 | +/// <item><description><c>IsRowGuidCol</c> — always 0 (ROWGUIDCOL not modeled).</description></item> |
| 20 | +/// <item><description><c>Precision</c> — type-dependent: integer family yields |
| 21 | +/// (decimal-equivalent precision), <c>varchar(N)</c> / <c>nvarchar(N)</c> yield |
| 22 | +/// <c>N</c>, money family yields 19 / 10.</description></item> |
| 23 | +/// <item><description><c>Scale</c> — type-dependent: integer family yields 0, |
| 24 | +/// money yields 4, decimal yields the declared scale.</description></item> |
| 25 | +/// <item><description><c>CharMaxLen</c> — <c>N</c> for <c>varchar(N)</c> / |
| 26 | +/// <c>nvarchar(N)</c>; NULL for non-character types (matches real, which |
| 27 | +/// returns -1 on the catalog view but NULL through COLUMNPROPERTY).</description></item> |
| 28 | +/// <item><description><c>ColumnId</c> — 1-based declaration ordinal in |
| 29 | +/// <see cref="HeapTable.Columns"/>.</description></item> |
| 30 | +/// <item><description><c>UsesAnsiTrim</c> — 1 for character types (the |
| 31 | +/// simulator's ANSI-trim behavior is always on), 0 otherwise.</description></item> |
| 32 | +/// </list> |
| 33 | +/// Unsupported / physical-storage properties (<c>IsDeterministic</c>, |
| 34 | +/// <c>IsIndexable</c>, <c>IsPrecise</c>, <c>IsSparse</c>, <c>IsColumnSet</c>, |
| 35 | +/// <c>StatisticalSemantics</c>, <c>GeneratedAlwaysType</c>) return NULL — |
| 36 | +/// callers reading them on a real server with no stats / column-set also get |
| 37 | +/// NULL, so this matches the common case. |
| 38 | +/// </para> |
| 39 | +/// </remarks> |
| 40 | +internal sealed class ColumnProperty : Expression |
| 41 | +{ |
| 42 | + private readonly Expression idArg; |
| 43 | + private readonly Expression columnArg; |
| 44 | + private readonly Expression propertyArg; |
| 45 | + |
| 46 | + public ColumnProperty(ParserContext context) |
| 47 | + { |
| 48 | + this.idArg = Parse(context); |
| 49 | + if (context.Token is not Tokens.Operator { Character: ',' }) |
| 50 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 51 | + this.columnArg = Parse(context.MoveNextRequiredReturnSelf()); |
| 52 | + if (context.Token is not Tokens.Operator { Character: ',' }) |
| 53 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 54 | + this.propertyArg = Parse(context.MoveNextRequiredReturnSelf()); |
| 55 | + if (context.Token is not Tokens.Operator { Character: ')' }) |
| 56 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 57 | + } |
| 58 | + |
| 59 | + public override SqlValue Run(RuntimeContext runtime) |
| 60 | + { |
| 61 | + var idValue = this.idArg.Run(runtime); |
| 62 | + var columnValue = this.columnArg.Run(runtime); |
| 63 | + var propValue = this.propertyArg.Run(runtime); |
| 64 | + if (idValue.IsNull || columnValue.IsNull || propValue.IsNull) |
| 65 | + return SqlValue.Null(SqlType.Int32); |
| 66 | + var id = idValue.CoerceTo(SqlType.Int32).AsInt32; |
| 67 | + var columnName = columnValue.CoerceTo(SqlType.NVarchar).AsString; |
| 68 | + var prop = propValue.CoerceTo(SqlType.NVarchar).AsString; |
| 69 | + |
| 70 | + if (ObjectProperty.FindObject(runtime.Batch.CurrentDatabase, id) is not HeapTable table) |
| 71 | + return SqlValue.Null(SqlType.Int32); |
| 72 | + |
| 73 | + var (column, ordinal) = FindColumn(table, columnName); |
| 74 | + return column is null |
| 75 | + ? SqlValue.Null(SqlType.Int32) |
| 76 | + : EvaluateColumnProperty(column, ordinal, prop) is int result |
| 77 | + ? SqlValue.FromInt32(result) |
| 78 | + : SqlValue.Null(SqlType.Int32); |
| 79 | + } |
| 80 | + |
| 81 | + private static (HeapColumn? Column, int Ordinal) FindColumn(HeapTable table, string columnName) |
| 82 | + { |
| 83 | + for (var i = 0; i < table.Columns.Length; i++) |
| 84 | + { |
| 85 | + if (Collation.Baseline.Equals(table.Columns[i].Name, columnName)) |
| 86 | + return (table.Columns[i], i + 1); |
| 87 | + } |
| 88 | + return (null, 0); |
| 89 | + } |
| 90 | + |
| 91 | + private static int? EvaluateColumnProperty(HeapColumn column, int ordinal, string property) |
| 92 | + { |
| 93 | + Span<char> upper = stackalloc char[property.Length]; |
| 94 | + return property.AsSpan().ToUpperInvariant(upper) switch |
| 95 | + { |
| 96 | + 5 => upper switch { "SCALE" => GetScale(column.Type), _ => null }, |
| 97 | + 8 => upper switch |
| 98 | + { |
| 99 | + "COLUMNID" => ordinal, |
| 100 | + _ => null, |
| 101 | + }, |
| 102 | + 9 => upper switch |
| 103 | + { |
| 104 | + "PRECISION" => GetPrecision(column), |
| 105 | + _ => null, |
| 106 | + }, |
| 107 | + 10 => upper switch |
| 108 | + { |
| 109 | + "ALLOWSNULL" => column.Nullable ? 1 : 0, |
| 110 | + "ISCOMPUTED" => column.Computed is null ? 0 : 1, |
| 111 | + "ISIDENTITY" => column.Identity is null ? 0 : 1, |
| 112 | + "CHARMAXLEN" => GetCharMaxLen(column), |
| 113 | + _ => null, |
| 114 | + }, |
| 115 | + 12 => upper switch |
| 116 | + { |
| 117 | + "ISROWGUIDCOL" => 0, |
| 118 | + "USESANSITRIM" => SqlType.IsStringCategory(column.Type) ? 1 : 0, |
| 119 | + _ => null, |
| 120 | + }, |
| 121 | + _ => null, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + private static int GetPrecision(HeapColumn column) => column.Type switch |
| 126 | + { |
| 127 | + { Category: SqlTypeCategory.Integer } t => SqlType.IntegerAsDecimal(t).Precision, |
| 128 | + { Category: SqlTypeCategory.Money } t => SqlType.MoneyAsDecimal(t).Precision, |
| 129 | + DecimalSqlType d => d.precision, |
| 130 | + _ when SqlType.IsStringCategory(column.Type) |
| 131 | + && column.MaxLength is int n |
| 132 | + && n != SqlType.MaxLengthSentinel => n, |
| 133 | + _ => 0, |
| 134 | + }; |
| 135 | + |
| 136 | + private static int GetScale(SqlType type) => type switch |
| 137 | + { |
| 138 | + { Category: SqlTypeCategory.Money } t => SqlType.MoneyAsDecimal(t).Scale, |
| 139 | + DecimalSqlType d => d.scale, |
| 140 | + _ => 0, |
| 141 | + }; |
| 142 | + |
| 143 | + private static int? GetCharMaxLen(HeapColumn column) => |
| 144 | + SqlType.IsStringCategory(column.Type) && column.MaxLength is int n && n != SqlType.MaxLengthSentinel |
| 145 | + ? n |
| 146 | + : null; |
| 147 | + |
| 148 | + public override SqlType GetSqlType(BatchContext batch, Func<MultiPartName, SqlType> resolveColumnType) => SqlType.Int32; |
| 149 | + |
| 150 | + internal override string DebugDisplay() => |
| 151 | + $"COLUMNPROPERTY({this.idArg.DebugDisplay()}, {this.columnArg.DebugDisplay()}, {this.propertyArg.DebugDisplay()})"; |
| 152 | +} |
0 commit comments