|
| 1 | +using SqlServerSimulator.Parser; |
| 2 | +using SqlServerSimulator.Parser.Tokens; |
| 3 | +using SqlServerSimulator.Storage; |
| 4 | + |
| 5 | +namespace SqlServerSimulator; |
| 6 | + |
| 7 | +partial class Simulation |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Parses <c>CREATE TABLE</c>. Returns false if the leading <c>CREATE</c> |
| 11 | + /// isn't followed by <c>TABLE</c> (so the caller can route to the syntax |
| 12 | + /// error). Other malformed forms throw <see cref="SimulatedSqlException"/> |
| 13 | + /// directly with the matching SQL Server error. |
| 14 | + /// </summary> |
| 15 | + private bool TryParseCreate(ParserContext context) |
| 16 | + { |
| 17 | + if (context.GetNextRequired() is not ReservedKeyword { Keyword: Keyword.Table }) |
| 18 | + return false; |
| 19 | + |
| 20 | + if (context.GetNextRequired() is not Name tableName) |
| 21 | + return false; |
| 22 | + |
| 23 | + if (context.GetNextRequired() is not Operator { Character: '(' }) |
| 24 | + return false; |
| 25 | + |
| 26 | + var rawColumns = new List<(Name Name, Name TypeName, int? DeclaredMaxLength, int? DeclaredScale, bool Nullable, IdentityState? Identity)>(); |
| 27 | + bool suppressAdvanceToken; |
| 28 | + do |
| 29 | + { |
| 30 | + suppressAdvanceToken = false; |
| 31 | + var columnName = context.GetNextRequired<Name>(); |
| 32 | + var type = context.GetNextRequired<Name>(); |
| 33 | + |
| 34 | + int? declaredMaxLength = null; |
| 35 | + int? declaredScale = null; |
| 36 | + context.MoveNextRequired(); |
| 37 | + if (context.Token is Operator { Character: '(' }) |
| 38 | + { |
| 39 | + var lengthToken = context.GetNextRequired(); |
| 40 | + if (lengthToken is Numeric { Value: { IsNull: false } numericValue }) |
| 41 | + { |
| 42 | + declaredMaxLength = numericValue.AsInt32; |
| 43 | + } |
| 44 | + else if (context.MatchContextual(ContextualKeyword.Max)) |
| 45 | + { |
| 46 | + throw new NotSupportedException($"{type}(MAX) and other LOB types aren't modeled yet."); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 51 | + } |
| 52 | + |
| 53 | + switch (context.GetNextRequired()) |
| 54 | + { |
| 55 | + case Operator { Character: ',' }: |
| 56 | + if (context.GetNextRequired() is not Numeric { Value: { IsNull: false } scaleValue }) |
| 57 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 58 | + declaredScale = scaleValue.AsInt32; |
| 59 | + if (context.GetNextRequired() is not Operator { Character: ')' }) |
| 60 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 61 | + break; |
| 62 | + case Operator { Character: ')' }: |
| 63 | + break; |
| 64 | + default: |
| 65 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 66 | + } |
| 67 | + |
| 68 | + context.MoveNextRequired(); |
| 69 | + } |
| 70 | + |
| 71 | + IdentityState? identity = null; |
| 72 | + if (context.Token is ReservedKeyword { Keyword: Keyword.Identity }) |
| 73 | + { |
| 74 | + identity = ParseIdentitySpec(context, columnName.Value); |
| 75 | + } |
| 76 | + |
| 77 | + bool nullable; |
| 78 | + if (context.Token is ReservedKeyword next) |
| 79 | + { |
| 80 | + switch (next.Keyword) |
| 81 | + { |
| 82 | + case Keyword.Not: |
| 83 | + if (context.GetNextRequired() is not ReservedKeyword { Keyword: Keyword.Null }) |
| 84 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 85 | + |
| 86 | + nullable = false; |
| 87 | + break; |
| 88 | + case Keyword.Null: |
| 89 | + nullable = true; |
| 90 | + break; |
| 91 | + default: |
| 92 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + suppressAdvanceToken = true; |
| 98 | + nullable = identity is null; |
| 99 | + } |
| 100 | + |
| 101 | + rawColumns.Add((columnName, type, declaredMaxLength, declaredScale, nullable, identity)); |
| 102 | + } while ((suppressAdvanceToken ? context.Token : context.GetNextRequired()) is Operator { Character: ',' }); |
| 103 | + |
| 104 | + if (context.Token is not Operator { Character: ')' }) |
| 105 | + return false; |
| 106 | + |
| 107 | + var heapColumns = new HeapColumn[rawColumns.Count]; |
| 108 | + var fixedWidthSum = 0; |
| 109 | + var identityCount = 0; |
| 110 | + for (var i = 0; i < rawColumns.Count; i++) |
| 111 | + { |
| 112 | + var raw = rawColumns[i]; |
| 113 | + var (resolvedType, maxLength) = SqlType.GetByName(raw.TypeName, raw.DeclaredMaxLength, raw.DeclaredScale, i + 1, raw.Name.Value); |
| 114 | + if (raw.Identity is not null) |
| 115 | + { |
| 116 | + if (++identityCount > 1) |
| 117 | + throw SimulatedSqlException.MultipleIdentityColumns(tableName.Value); |
| 118 | + if (raw.Nullable) |
| 119 | + throw SimulatedSqlException.IdentityOnNullableColumn(raw.Name.Value, tableName.Value); |
| 120 | + if (resolvedType != SqlType.Int32 && resolvedType != SqlType.BigInt && resolvedType != SqlType.SmallInt && resolvedType != SqlType.TinyInt) |
| 121 | + throw SimulatedSqlException.IdentityInvalidType(raw.Name.Value); |
| 122 | + } |
| 123 | + heapColumns[i] = new(raw.Name.Value, resolvedType, maxLength, raw.Nullable, raw.Identity); |
| 124 | + if (resolvedType.IsFixedLength) |
| 125 | + fixedWidthSum += resolvedType.FixedLength; |
| 126 | + } |
| 127 | + |
| 128 | + // Schemas whose fixed-width columns alone exceed SQL Server's 8060-byte |
| 129 | + // in-row limit can never hold a row; reject at CREATE TABLE (Msg 1701). |
| 130 | + // The variable-width-aware warning path is deferred until warning |
| 131 | + // infrastructure exists. |
| 132 | + if (fixedWidthSum > Heap.MaxRowSize) |
| 133 | + throw SimulatedSqlException.RowSizeExceedsMaximum(tableName.Value, fixedWidthSum, Heap.MaxRowSize); |
| 134 | + |
| 135 | + var heapTable = new HeapTable(tableName.Value, heapColumns); |
| 136 | + return this.HeapTables.TryAdd(heapTable.Name, heapTable) |
| 137 | + ? true |
| 138 | + : throw SimulatedSqlException.ThereIsAlreadyAnObject(heapTable.Name); |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Parses the <c>IDENTITY [(seed, increment)]</c> property after a column's |
| 143 | + /// data type. Enters with <see cref="ParserContext.Token"/> on the |
| 144 | + /// <c>IDENTITY</c> keyword and leaves it on the next non-identity token |
| 145 | + /// (a nullability keyword, comma, or the column-list's closing paren). |
| 146 | + /// Bare <c>IDENTITY</c> is shorthand for <c>IDENTITY(1, 1)</c>. |
| 147 | + /// </summary> |
| 148 | + private static IdentityState ParseIdentitySpec(ParserContext context, string columnName) |
| 149 | + { |
| 150 | + long seed = 1; |
| 151 | + long increment = 1; |
| 152 | + var afterIdentity = context.GetNextRequired(); |
| 153 | + if (afterIdentity is Operator { Character: '(' }) |
| 154 | + { |
| 155 | + context.MoveNextRequired(); |
| 156 | + seed = EvaluateLiteralBigInt(Expression.Parse(context)); |
| 157 | + if (context.Token is not Operator { Character: ',' }) |
| 158 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 159 | + context.MoveNextRequired(); |
| 160 | + increment = EvaluateLiteralBigInt(Expression.Parse(context)); |
| 161 | + if (context.Token is not Operator { Character: ')' }) |
| 162 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 163 | + context.MoveNextRequired(); |
| 164 | + } |
| 165 | + return increment == 0 |
| 166 | + ? throw SimulatedSqlException.IdentityInvalidIncrement(columnName) |
| 167 | + : new IdentityState(seed, increment); |
| 168 | + } |
| 169 | + |
| 170 | + private static long EvaluateLiteralBigInt(Expression expression) => |
| 171 | + expression.Run(name => throw SimulatedSqlException.InvalidColumnName(name)).CoerceTo(SqlType.BigInt).AsInt64; |
| 172 | +} |
0 commit comments