Skip to content

Commit 839a68f

Browse files
committed
fix(sqlite): split multi-statement run() for better-sqlite3 (Node)
better-sqlite3 rejects multiple statements per prepare(); bun:sqlite allows them. Node path now splits on ';' when Bun is undefined. Remove semicolon from createIndexes comment (PK; broke naive split).
1 parent aa3073d commit 839a68f

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export function createIndexes(db: CodemapDatabase) {
143143
CREATE INDEX IF NOT EXISTS idx_components_name ON components(name, file_path, props_type, hooks_used);
144144
CREATE INDEX IF NOT EXISTS idx_components_file ON components(file_path, name);
145145
146-
-- WITHOUT ROWID tables already have a clustered PK; this covers reverse lookups
146+
-- WITHOUT ROWID tables already have a clustered PK this covers reverse lookups
147147
CREATE INDEX IF NOT EXISTS idx_dependencies_to ON dependencies(to_path, from_path);
148148
149149
CREATE INDEX IF NOT EXISTS idx_markers_kind ON markers(kind, file_path, line_number, content);

src/sqlite-db.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ type SqliteInner = {
3232
close(): void;
3333
};
3434

35+
/**
36+
* `better-sqlite3` allows only one statement per `prepare()`; `bun:sqlite` accepts several.
37+
* On Node we split on `;` — do not put `;` inside `--` line comments in `db.ts` SQL strings.
38+
*/
39+
function runSql(inner: SqliteInner, sql: string, params?: BindValues): void {
40+
if (params !== undefined && params.length > 0) {
41+
inner.run(sql, params);
42+
return;
43+
}
44+
if (typeof Bun !== "undefined") {
45+
inner.run(sql);
46+
return;
47+
}
48+
const parts = sql
49+
.split(";")
50+
.map((s) => s.trim())
51+
.filter(Boolean);
52+
if (parts.length <= 1) {
53+
inner.run(sql.trim());
54+
} else {
55+
for (const p of parts) {
56+
inner.run(p);
57+
}
58+
}
59+
}
60+
3561
function openRaw(path: string): SqliteInner {
3662
if (typeof Bun !== "undefined") {
3763
// eslint-disable-next-line @typescript-eslint/no-require-imports
@@ -78,11 +104,7 @@ function openRaw(path: string): SqliteInner {
78104
function wrap(inner: SqliteInner): CodemapDatabase {
79105
return {
80106
run(sql: string, params?: BindValues) {
81-
if (params !== undefined && params.length > 0) {
82-
inner.run(sql, params);
83-
} else {
84-
inner.run(sql);
85-
}
107+
runSql(inner, sql, params);
86108
},
87109
query<T>(sql: string) {
88110
return {

0 commit comments

Comments
 (0)