Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/drivers/sqlite/sql-parse-table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,32 @@ describe("parse create table", () => {
constraints: [],
} as DatabaseTableSchema);
});

test("ignore sql comments inside create table", () => {
const sql = `CREATE TABLE IF NOT EXISTS test1 (
day TEXT PRIMARY KEY, -- 'yyyy-MM-dd'
total_size INTEGER /* bytes, -- not a line comment */
-- trailing comment
)`;

expect(p(sql)).toEqual({
tableName: "test1",
schemaName: "main",
autoIncrement: false,
pk: ["day"],
columns: [
{
name: "day",
type: "TEXT",
pk: true,
constraint: { primaryKey: true, autoIncrement: false },
},
{
name: "total_size",
type: "INTEGER",
},
],
constraints: [],
} as DatabaseTableSchema);
});
});
10 changes: 6 additions & 4 deletions src/drivers/sqlite/sql-parse-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { unescapeIdentity } from "./sql-helper";
export class CursorV2 {
private ptr: number = 0;

constructor(private tokens: Token[]) {
private tokens: Token[];

constructor(tokens: Token[]) {
this.tokens = tokens.filter((t) => t.type !== "COMMENT");

// Trim whitespace tokens from the beginning and end
while (this.tokens.length > 0 && this.tokens[0].type === "WHITESPACE") {
this.tokens.shift();
Expand All @@ -27,8 +31,6 @@ export class CursorV2 {
) {
this.tokens.pop();
}

this.tokens = tokens;
}

getPointer() {
Expand Down Expand Up @@ -563,7 +565,7 @@ export function parseCreateTableScript(
cursor.expectTokenOptional("TEMPORARY");
cursor.expectTokenOptional("VIRTUAL");
cursor.expectToken("TABLE");
cursor.expectTokensOptional(["IF", "NOT", "EXIST"]);
cursor.expectTokensOptional(["IF", "NOT", "EXISTS"]);

const tableName = cursor.consumeIdentifier();

Expand Down
Loading