|
| 1 | +using CosmoSQLClient.Core; |
| 2 | +using CosmoSQLClient.CosmoKv.Schema; |
| 3 | + |
| 4 | +namespace CosmoSQLClient.CosmoKv.Ast; |
| 5 | + |
| 6 | +/// <summary>Root of the AST hierarchy. One concrete subtype per statement form.</summary> |
| 7 | +internal abstract record Statement; |
| 8 | + |
| 9 | +// ── DDL ────────────────────────────────────────────────────────────────────── |
| 10 | + |
| 11 | +internal sealed record CreateTableStatement( |
| 12 | + string TableName, |
| 13 | + IReadOnlyList<ColumnSchema> Columns, |
| 14 | + int? PrimaryKeyColumnIndex, |
| 15 | + int? IdentityColumnIndex, |
| 16 | + bool IfNotExists) : Statement; |
| 17 | + |
| 18 | +internal sealed record CreateIndexStatement( |
| 19 | + string IndexName, |
| 20 | + string TableName, |
| 21 | + IReadOnlyList<string> Columns, |
| 22 | + bool IsUnique) : Statement; |
| 23 | + |
| 24 | +internal sealed record DropTableStatement(string TableName, bool IfExists) : Statement; |
| 25 | +internal sealed record DropIndexStatement(string IndexName, string? TableName, bool IfExists) : Statement; |
| 26 | + |
| 27 | +// ── DML ────────────────────────────────────────────────────────────────────── |
| 28 | + |
| 29 | +internal sealed record InsertStatement( |
| 30 | + string TableName, |
| 31 | + IReadOnlyList<string>? Columns, // null = all columns in declaration order |
| 32 | + IReadOnlyList<IReadOnlyList<Expression>> Rows) : Statement; |
| 33 | + |
| 34 | +internal sealed record SelectStatement( |
| 35 | + IReadOnlyList<SelectItem> Items, |
| 36 | + string TableName, |
| 37 | + Expression? Where, |
| 38 | + IReadOnlyList<Expression>? GroupBy, |
| 39 | + Expression? Having, |
| 40 | + IReadOnlyList<OrderByItem>? OrderBy, |
| 41 | + long? Top, |
| 42 | + long? Offset, |
| 43 | + long? Fetch) : Statement; |
| 44 | + |
| 45 | +internal sealed record SelectItem(Expression Expr, string? Alias); |
| 46 | + |
| 47 | +internal sealed record OrderByItem(Expression Expr, bool Descending); |
| 48 | + |
| 49 | +internal sealed record UpdateStatement( |
| 50 | + string TableName, |
| 51 | + IReadOnlyList<UpdateAssignment> Assignments, |
| 52 | + Expression? Where) : Statement; |
| 53 | + |
| 54 | +internal sealed record UpdateAssignment(string ColumnName, Expression Value); |
| 55 | + |
| 56 | +internal sealed record DeleteStatement( |
| 57 | + string TableName, |
| 58 | + Expression? Where) : Statement; |
| 59 | + |
| 60 | +// ── Transaction control ────────────────────────────────────────────────────── |
| 61 | + |
| 62 | +/// <summary><c>BEGIN TRAN[SACTION] [name]</c>. Name is accepted but not honoured.</summary> |
| 63 | +internal sealed record BeginTransactionStatement(string? Name) : Statement; |
| 64 | + |
| 65 | +/// <summary><c>COMMIT [TRAN[SACTION]] [name]</c>.</summary> |
| 66 | +internal sealed record CommitTransactionStatement(string? Name) : Statement; |
| 67 | + |
| 68 | +/// <summary><c>ROLLBACK [TRAN[SACTION]] [name]</c>. Phase 5: nested savepoints not supported.</summary> |
| 69 | +internal sealed record RollbackTransactionStatement(string? Name) : Statement; |
| 70 | + |
| 71 | +// ── Expressions ────────────────────────────────────────────────────────────── |
| 72 | + |
| 73 | +internal abstract record Expression; |
| 74 | + |
| 75 | +/// <summary>Literal — number, string, boolean, NULL, hex/binary.</summary> |
| 76 | +internal sealed record LiteralExpression(SqlValue Value) : Expression; |
| 77 | + |
| 78 | +/// <summary>Parameter reference like <c>@name</c>; bound from the caller's parameter list.</summary> |
| 79 | +internal sealed record ParameterExpression(string Name) : Expression; |
| 80 | + |
| 81 | +/// <summary>Bare column reference. Phase 1 only — no qualifier resolution.</summary> |
| 82 | +internal sealed record ColumnExpression(string Name) : Expression; |
| 83 | + |
| 84 | +/// <summary><c>SELECT *</c>'s sentinel. Resolved against the source table at execution time.</summary> |
| 85 | +internal sealed record StarExpression : Expression; |
| 86 | + |
| 87 | +/// <summary>Binary operator: arithmetic, comparison, logical AND/OR.</summary> |
| 88 | +internal sealed record BinaryExpression(BinaryOperator Op, Expression Left, Expression Right) : Expression; |
| 89 | + |
| 90 | +/// <summary>Unary operator: NOT (logical), - (numeric negation).</summary> |
| 91 | +internal sealed record UnaryExpression(UnaryOperator Op, Expression Operand) : Expression; |
| 92 | + |
| 93 | +/// <summary><c>expr IS [NOT] NULL</c>.</summary> |
| 94 | +internal sealed record IsNullExpression(Expression Operand, bool Negated) : Expression; |
| 95 | + |
| 96 | +/// <summary><c>expr [NOT] LIKE pattern</c>. <c>%</c> = any sequence, <c>_</c> = single char. Phase 2: no ESCAPE clause.</summary> |
| 97 | +internal sealed record LikeExpression(Expression Source, Expression Pattern, bool Negated) : Expression; |
| 98 | + |
| 99 | +/// <summary><c>expr [NOT] IN (v1, v2, …)</c>.</summary> |
| 100 | +internal sealed record InExpression(Expression Source, IReadOnlyList<Expression> Values, bool Negated) : Expression; |
| 101 | + |
| 102 | +/// <summary> |
| 103 | +/// A function call: <c>NAME(arg1, arg2, …)</c>. Aggregate vs scalar is |
| 104 | +/// resolved at execution time from the function registry — keeping that |
| 105 | +/// decision out of the parser lets new functions land without grammar churn. |
| 106 | +/// </summary> |
| 107 | +internal sealed record FunctionCallExpression(string Name, IReadOnlyList<Expression> Args) : Expression; |
| 108 | + |
| 109 | +/// <summary><c>COUNT(*)</c>. Counts rows including those with NULLs in every column.</summary> |
| 110 | +internal sealed record CountStarExpression : Expression; |
| 111 | + |
| 112 | +/// <summary> |
| 113 | +/// <c>CAST(expr AS type)</c> and <c>CONVERT(type, expr)</c>. Style argument |
| 114 | +/// (T-SQL CONVERT's third) is ignored by Phase 3; the cast follows the |
| 115 | +/// column-coercion rules in <see cref="CosmoSQLClient.CosmoKv.Execution.TypeCoercer"/>. |
| 116 | +/// </summary> |
| 117 | +internal sealed record CastExpression(Expression Operand, Schema.SqlColumnType TargetType) : Expression; |
| 118 | + |
| 119 | +internal enum BinaryOperator |
| 120 | +{ |
| 121 | + And, Or, |
| 122 | + Eq, Ne, Lt, Le, Gt, Ge, |
| 123 | + Add, Sub, Mul, Div, Mod, |
| 124 | +} |
| 125 | + |
| 126 | +internal enum UnaryOperator |
| 127 | +{ |
| 128 | + Not, Negate, |
| 129 | +} |
0 commit comments