/** * metona-sqlark Query Compiler — AST → 查询计划 * @module query/compiler * * 将 AST 语句编译为引擎可执行的 QueryPlan。 * v0.0.1: 简单直接映射,未来可加入索引选择、过滤下推等优化。 */ import type { QueryPlan } from '../constants'; import { DatabaseError } from '../constants'; import type { Statement, SelectStatement, DeleteStatement, UpdateStatement } from './ast'; // --------------------------------------------------------------------------- // 编译 AST → QueryPlan // --------------------------------------------------------------------------- /** * 编译 SELECT / DELETE / UPDATE 语句为 QueryPlan。 * INSERT 和 DDL 语句不需要 QueryPlan。 */ export function compileStatement(stmt: Statement): QueryPlan { switch (stmt.type) { case 'SELECT': return compileSelect(stmt); case 'DELETE': return compileDelete(stmt); case 'UPDATE': return compileUpdate(stmt); default: throw new DatabaseError(`Cannot compile statement type "${stmt.type}" to QueryPlan`, 'COMPILE_ERROR'); } } function compileSelect(stmt: SelectStatement): QueryPlan { return { table: stmt.from, columns: stmt.columns, where: stmt.where, orderBy: stmt.orderBy?.length ? stmt.orderBy : undefined, limit: stmt.limit, offset: stmt.offset, }; } function compileDelete(stmt: DeleteStatement): QueryPlan { return { table: stmt.from, where: stmt.where, }; } function compileUpdate(stmt: UpdateStatement): QueryPlan { return { table: stmt.table, where: stmt.where, }; }