-
Notifications
You must be signed in to change notification settings - Fork 40
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
use simpler batch input #255
Changes from all commits
bc49824
d2f637d
5c6da67
79094ae
0fffe73
0cfe870
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,22 @@ async function example() { | |
], | ||
"write", | ||
); | ||
|
||
await db.batch( | ||
[ | ||
{ | ||
sql: "INSERT INTO users (email) VALUES (?)", | ||
args: ["[email protected]"], | ||
}, | ||
["INSERT INTO users (email) VALUES (?)", ["[email protected]"]], | ||
{ | ||
sql: "INSERT INTO users (email) VALUES (:email)", | ||
args: { email: "[email protected]" }, | ||
}, | ||
], | ||
"write", | ||
); | ||
|
||
const rs = await db.execute("SELECT * FROM users"); | ||
console.log(rs); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import type { | |
ResultSet, | ||
Transaction, | ||
TransactionMode, | ||
InArgs, | ||
} from "@libsql/core/api"; | ||
import { LibsqlError } from "@libsql/core/api"; | ||
import type { SqlCache } from "./sql_cache.js"; | ||
|
@@ -255,7 +256,7 @@ export async function executeHranaBatch( | |
disableForeignKeys: boolean = false, | ||
): Promise<Array<ResultSet>> { | ||
if (disableForeignKeys) { | ||
batch.step().run("PRAGMA foreign_keys=off") | ||
batch.step().run("PRAGMA foreign_keys=off"); | ||
} | ||
const beginStep = batch.step(); | ||
const beginPromise = beginStep.run(transactionModeToBegin(mode)); | ||
|
@@ -287,7 +288,7 @@ export async function executeHranaBatch( | |
.condition(hrana.BatchCond.not(hrana.BatchCond.ok(commitStep))); | ||
rollbackStep.run("ROLLBACK").catch((_) => undefined); | ||
if (disableForeignKeys) { | ||
batch.step().run("PRAGMA foreign_keys=on") | ||
batch.step().run("PRAGMA foreign_keys=on"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Diff noise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prettier ran on commit, have the rules changed? |
||
} | ||
|
||
await batch.execute(); | ||
|
@@ -309,17 +310,27 @@ export async function executeHranaBatch( | |
return resultSets; | ||
} | ||
|
||
export function stmtToHrana(stmt: InStatement): hrana.Stmt { | ||
if (typeof stmt === "string") { | ||
return new hrana.Stmt(stmt); | ||
} | ||
export function stmtToHrana(stmt: InStatement | [string, InArgs?]): hrana.Stmt { | ||
let sql: string; | ||
let args: InArgs | undefined; | ||
|
||
const hranaStmt = new hrana.Stmt(stmt.sql); | ||
if (Array.isArray(stmt.args)) { | ||
hranaStmt.bindIndexes(stmt.args); | ||
if (Array.isArray(stmt)) { | ||
[sql, args] = stmt; | ||
} else if (typeof stmt === "string") { | ||
sql = stmt; | ||
} else { | ||
for (const [key, value] of Object.entries(stmt.args)) { | ||
hranaStmt.bindName(key, value); | ||
sql = stmt.sql; | ||
args = stmt.args; | ||
} | ||
|
||
const hranaStmt = new hrana.Stmt(sql); | ||
if (args) { | ||
if (Array.isArray(args)) { | ||
hranaStmt.bindIndexes(args); | ||
} else { | ||
for (const [key, value] of Object.entries(args)) { | ||
hranaStmt.bindName(key, value); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,7 @@ export class Sqlite3Client implements Client { | |
} | ||
|
||
async batch( | ||
stmts: Array<InStatement>, | ||
stmts: Array<InStatement | [string, InArgs?]>, | ||
mode: TransactionMode = "deferred", | ||
): Promise<Array<ResultSet>> { | ||
this.#checkNotClosed(); | ||
|
@@ -156,7 +156,10 @@ export class Sqlite3Client implements Client { | |
"TRANSACTION_CLOSED", | ||
); | ||
} | ||
return executeStmt(db, stmt, this.#intMode); | ||
const normalizedStmt: InStatement = Array.isArray(stmt) | ||
? { sql: stmt[0], args: stmt[1] || [] } | ||
: stmt; | ||
return executeStmt(db, normalizedStmt, this.#intMode); | ||
}); | ||
executeStmt(db, "COMMIT", this.#intMode); | ||
return resultSets; | ||
|
@@ -167,9 +170,7 @@ export class Sqlite3Client implements Client { | |
} | ||
} | ||
|
||
async migrate( | ||
stmts: Array<InStatement>, | ||
): Promise<Array<ResultSet>> { | ||
async migrate(stmts: Array<InStatement>): Promise<Array<ResultSet>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Diff noise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prettier ran on commit, have the rules changed? |
||
this.#checkNotClosed(); | ||
const db = this.#getDb(); | ||
try { | ||
|
@@ -276,10 +277,15 @@ export class Sqlite3Transaction implements Transaction { | |
return executeStmt(this.#database, stmt, this.#intMode); | ||
} | ||
|
||
async batch(stmts: Array<InStatement>): Promise<Array<ResultSet>> { | ||
async batch( | ||
stmts: Array<InStatement | [string, InArgs?]>, | ||
): Promise<Array<ResultSet>> { | ||
return stmts.map((stmt) => { | ||
this.#checkNotClosed(); | ||
return executeStmt(this.#database, stmt, this.#intMode); | ||
const normalizedStmt: InStatement = Array.isArray(stmt) | ||
? { sql: stmt[0], args: stmt[1] || [] } | ||
: stmt; | ||
return executeStmt(this.#database, normalizedStmt, this.#intMode); | ||
}); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diff noise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prettier ran on commit, have the rules changed?