Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pragma foreign_keys in batch() #174

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions packages/libsql-client/src/hrana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,28 @@ export abstract class HranaTransaction implements Transaction {
}
}

export function isHranaPragmaStmt(stmt: hrana.Stmt): boolean {
if (typeof stmt.sql === "string") {
return stmt.sql.toUpperCase().startsWith("PRAGMA ");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's too eager - pragma foreign_keys is explicitly documented to not work in transaction context, but there are lots of pragmas that do, and sometimes you may need them to be atomic. Think pragmas that check if the schema is in some particular shape

}
throw new Error("unexpected");
}

export async function executeHranaBatch(
mode: TransactionMode,
version: hrana.ProtocolVersion,
batch: hrana.Batch,
hranaStmts: Array<hrana.Stmt>,
): Promise<Array<ResultSet>> {
if (hranaStmts.length == 0) {
return [];
}
let pragmaPromise = undefined;
if (isHranaPragmaStmt(hranaStmts[0])) {
const pragmaStmt = hranaStmts.shift()!;
const pragmaStep = batch.step();
const pragmaPromise = pragmaStep.run(pragmaStmt);
}
const beginStep = batch.step();
const beginPromise = beginStep.run(transactionModeToBegin(mode));

Expand Down Expand Up @@ -263,6 +279,9 @@ export async function executeHranaBatch(
await batch.execute();

const resultSets = [];
if (pragmaPromise !== undefined) {
await pragmaPromise;
}
await beginPromise;
for (const stmtPromise of stmtPromises) {
const hranaRows = await stmtPromise;
Expand Down
4 changes: 4 additions & 0 deletions packages/libsql-client/src/sql_cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as hrana from "@libsql/hrana-client";
import { isHranaPragmaStmt } from "./hrana.js";

export class SqlCache {
#owner: hrana.SqlOwner;
Expand Down Expand Up @@ -29,6 +30,9 @@ export class SqlCache {
if (typeof hranaStmt.sql !== "string") {
continue;
}
if (isHranaPragmaStmt(hranaStmt)) {
continue;
}
const sqlText = hranaStmt.sql;

let sqlObj = this.#sqls.get(sqlText);
Expand Down
Loading