/** * metona-sqlark Query AST — 查询抽象语法树类型定义 * @module query/ast * * QueryBuilder 和 SQL Parser 统一输出此 AST, * Executor 只认 AST,保证两种查询接口行为一致。 */ import type { WhereCondition, OrderBy } from '../constants'; // --------------------------------------------------------------------------- // AST 语句类型枚举 // --------------------------------------------------------------------------- export type StatementType = | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'CREATE_TABLE' | 'DROP_TABLE'; // --------------------------------------------------------------------------- // 列引用 // --------------------------------------------------------------------------- /** 列引用,'*' 表示所有列;支持 'table.column' 格式 */ export type ColumnRef = string; // --------------------------------------------------------------------------- // JOIN // --------------------------------------------------------------------------- /** JOIN 类型 */ export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'CROSS'; /** JOIN 子句 */ export interface JoinClause { type: JoinType; table: string; alias?: string; on: WhereCondition; } // --------------------------------------------------------------------------- // 聚合函数 // --------------------------------------------------------------------------- /** 聚合函数类型 */ export type AggregateFunc = 'COUNT' | 'SUM' | 'AVG' | 'MIN' | 'MAX'; /** 聚合表达式 */ export interface AggregateExpression { type: 'AGGREGATE'; func: AggregateFunc; column: string; // '*' for COUNT(*) alias?: string; } // --------------------------------------------------------------------------- // DDL: CREATE TABLE // --------------------------------------------------------------------------- export interface ASTColumnDef { name: string; type: string; primaryKey?: boolean; unique?: boolean; required?: boolean; default?: unknown; index?: boolean; maxLength?: number; min?: number; max?: number; } export interface CreateTableStatement { type: 'CREATE_TABLE'; name: string; columns: ASTColumnDef[]; } // --------------------------------------------------------------------------- // DDL: DROP TABLE // --------------------------------------------------------------------------- export interface DropTableStatement { type: 'DROP_TABLE'; name: string; } // --------------------------------------------------------------------------- // DML: INSERT // --------------------------------------------------------------------------- export interface InsertStatement { type: 'INSERT'; into: string; columns?: string[]; values: unknown[][]; } // --------------------------------------------------------------------------- // DML: UPDATE // --------------------------------------------------------------------------- export interface UpdateStatement { type: 'UPDATE'; table: string; sets: Record; where: WhereCondition; } // --------------------------------------------------------------------------- // DML: DELETE // --------------------------------------------------------------------------- export interface DeleteStatement { type: 'DELETE'; from: string; where: WhereCondition; } // --------------------------------------------------------------------------- // DQL: SELECT // --------------------------------------------------------------------------- export interface SelectStatement { type: 'SELECT'; columns: ColumnRef[]; distinct?: boolean; from: string; /** 主表别名 */ alias?: string; /** JOIN 子句列表 */ joins?: JoinClause[]; where: WhereCondition; /** GROUP BY */ groupBy?: string[]; /** HAVING */ having?: WhereCondition; orderBy?: OrderBy[]; limit?: number; offset?: number; } // --------------------------------------------------------------------------- // AST 联合类型 // --------------------------------------------------------------------------- export type Statement = | SelectStatement | InsertStatement | UpdateStatement | DeleteStatement | CreateTableStatement | DropTableStatement;