@crvouga/sqlite-mem 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,9 @@
1
1
  import type { Statement as ParsedStatement } from "../ast/nodes.js";
2
+ import { type ParsedUnit } from "./parser.js";
2
3
  /** Parsed SQL statement AST (`SELECT`, `INSERT`, `CREATE TABLE`, …). */
3
4
  export type { Statement as ParsedStatement } from "../ast/nodes.js";
4
- export { parseTokens } from "./parser.js";
5
+ export type { ParsedUnit } from "./parser.js";
6
+ export { parseTokens, parseTokenUnits } from "./parser.js";
5
7
  /**
6
8
  * Tokenize `sql` and parse every semicolon-separated statement.
7
9
  *
@@ -9,3 +11,7 @@ export { parseTokens } from "./parser.js";
9
11
  * @returns AST statements in source order.
10
12
  */
11
13
  export declare function parse(sql: string): ParsedStatement[];
14
+ /**
15
+ * Parse SQL into AST + per-statement source slices (for `sqlite_master.sql`).
16
+ */
17
+ export declare function parseUnits(sql: string): ParsedUnit[];
@@ -1,9 +1,15 @@
1
1
  import type { Expr, SelectStmt, Statement, WithClause } from "../ast/nodes.js";
2
2
  import type { Token } from "../lexer/tokenize.js";
3
+ export interface ParsedUnit {
4
+ statement: Statement;
5
+ /** Source slice for this statement (no trailing semicolon); used for sqlite_master.sql. */
6
+ sql: string;
7
+ }
3
8
  export declare class Parser {
4
9
  private readonly tokens;
10
+ private readonly source;
5
11
  private pos;
6
- constructor(tokens: Token[]);
12
+ constructor(tokens: Token[], source?: string);
7
13
  private current;
8
14
  private peek;
9
15
  private at;
@@ -20,6 +26,8 @@ export declare class Parser {
20
26
  private parseDetachStmt;
21
27
  private optionalAlias;
22
28
  parseStatements(): Statement[];
29
+ /** Parse statements with per-statement source slices for catalog `sql` text. */
30
+ parseUnits(): ParsedUnit[];
23
31
  parseStatement(): Statement;
24
32
  private parseAnalyzeStmt;
25
33
  private parseReindexStmt;
@@ -101,4 +109,6 @@ export declare class Parser {
101
109
  private parseFrameSpec;
102
110
  private parseFrameBound;
103
111
  }
104
- export declare function parseTokens(tokens: Token[]): Statement[];
112
+ export declare function parseTokens(tokens: Token[], source?: string): Statement[];
113
+ /** Parse SQL into AST statements paired with source slices. */
114
+ export declare function parseTokenUnits(tokens: Token[], source: string): ParsedUnit[];
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Normalize CREATE source toward SQLite's `sqlite_master.sql` conventions:
3
+ * strip TEMP/TEMPORARY, IF NOT EXISTS, and schema qualifiers on the object name.
4
+ * Body text (from after the object name) is preserved byte-for-byte from the input.
5
+ */
6
+ export declare function normalizeMasterSql(sql: string): string;
7
+ /** Append an ALTER TABLE ADD COLUMN definition into CREATE TABLE master sql (SQLite style). */
8
+ export declare function appendAddColumnToMasterSql(originalSql: string | null, alterSql: string | null): string | null;
9
+ /** Synthesize CTAS catalog SQL: `CREATE TABLE name(col1, col2, ...)`. */
10
+ export declare function synthesizeCtasMasterSql(tableName: string, columnNames: string[]): string;
package/dist/unstable.js CHANGED
@@ -3890,10 +3890,12 @@ var PREC = {
3890
3890
  JSON_ARROW: 80
3891
3891
  };
3892
3892
  var Parser = class {
3893
- constructor(tokens) {
3893
+ constructor(tokens, source = "") {
3894
3894
  this.tokens = tokens;
3895
+ this.source = source;
3895
3896
  }
3896
3897
  tokens;
3898
+ source;
3897
3899
  pos = 0;
3898
3900
  current() {
3899
3901
  return this.tokens[this.pos] ?? this.tokens[this.tokens.length - 1];
@@ -3977,13 +3979,21 @@ var Parser = class {
3977
3979
  }
3978
3980
  // ── Statements ──────────────────────────────────────────────────────────
3979
3981
  parseStatements() {
3980
- const stmts = [];
3982
+ return this.parseUnits().map((unit) => unit.statement);
3983
+ }
3984
+ /** Parse statements with per-statement source slices for catalog `sql` text. */
3985
+ parseUnits() {
3986
+ const units = [];
3981
3987
  while (!this.at("EOF")) {
3982
3988
  if (this.match("SEMI")) continue;
3983
- stmts.push(this.parseStatement());
3989
+ const start = this.current().start;
3990
+ const statement = this.parseStatement();
3991
+ const end = this.pos > 0 ? this.tokens[this.pos - 1].end : this.current().end;
3984
3992
  this.match("SEMI");
3993
+ const sql = this.source ? this.source.slice(start, end).trimEnd() : "";
3994
+ units.push({ statement, sql });
3985
3995
  }
3986
- return stmts;
3996
+ return units;
3987
3997
  }
3988
3998
  parseStatement() {
3989
3999
  if (this.match("EXPLAIN")) {
@@ -5609,14 +5619,16 @@ var Parser = class {
5609
5619
  return { kind: "following", expr };
5610
5620
  }
5611
5621
  };
5612
- function parseTokens(tokens) {
5613
- return new Parser(tokens).parseStatements();
5622
+ function parseTokenUnits(tokens, source) {
5623
+ return new Parser(tokens, source).parseUnits();
5614
5624
  }
5615
5625
 
5616
5626
  // src/parser/index.ts
5617
5627
  function parse(sql) {
5618
- const tokens = tokenize(sql);
5619
- return parseTokens(tokens);
5628
+ return parseUnits(sql).map((unit) => unit.statement);
5629
+ }
5630
+ function parseUnits(sql) {
5631
+ return parseTokenUnits(tokenize(sql), sql);
5620
5632
  }
5621
5633
 
5622
5634
  // src/runtime/clock.ts