@@ -15,7 +15,7 @@ import type {
1515 InArgs ,
1616 Replicated ,
1717} from "@libsql/core/api" ;
18- import { LibsqlError } from "@libsql/core/api" ;
18+ import { LibsqlError , LibsqlBatchError } from "@libsql/core/api" ;
1919import type { ExpandedConfig } from "@libsql/core/config" ;
2020import { expandConfig , isInMemoryConfig } from "@libsql/core/config" ;
2121import {
@@ -148,18 +148,39 @@ export class Sqlite3Client implements Client {
148148 const db = this . #getDb( ) ;
149149 try {
150150 executeStmt ( db , transactionModeToBegin ( mode ) , this . #intMode) ;
151- const resultSets = stmts . map ( ( stmt ) => {
152- if ( ! db . inTransaction ) {
153- throw new LibsqlError (
154- "The transaction has been rolled back" ,
155- "TRANSACTION_CLOSED" ,
151+ const resultSets = [ ] ;
152+ for ( let i = 0 ; i < stmts . length ; i ++ ) {
153+ try {
154+ if ( ! db . inTransaction ) {
155+ throw new LibsqlBatchError (
156+ "The transaction has been rolled back" ,
157+ i ,
158+ "TRANSACTION_CLOSED" ,
159+ ) ;
160+ }
161+ const stmt = stmts [ i ] ;
162+ const normalizedStmt : InStatement = Array . isArray ( stmt )
163+ ? { sql : stmt [ 0 ] , args : stmt [ 1 ] || [ ] }
164+ : stmt ;
165+ resultSets . push (
166+ executeStmt ( db , normalizedStmt , this . #intMode) ,
156167 ) ;
168+ } catch ( e ) {
169+ if ( e instanceof LibsqlBatchError ) {
170+ throw e ;
171+ }
172+ if ( e instanceof LibsqlError ) {
173+ throw new LibsqlBatchError (
174+ e . message ,
175+ i ,
176+ e . code ,
177+ e . rawCode ,
178+ e . cause instanceof Error ? e . cause : undefined ,
179+ ) ;
180+ }
181+ throw e ;
157182 }
158- const normalizedStmt : InStatement = Array . isArray ( stmt )
159- ? { sql : stmt [ 0 ] , args : stmt [ 1 ] || [ ] }
160- : stmt ;
161- return executeStmt ( db , normalizedStmt , this . #intMode) ;
162- } ) ;
183+ }
163184 executeStmt ( db , "COMMIT" , this . #intMode) ;
164185 return resultSets ;
165186 } finally {
@@ -175,15 +196,33 @@ export class Sqlite3Client implements Client {
175196 try {
176197 executeStmt ( db , "PRAGMA foreign_keys=off" , this . #intMode) ;
177198 executeStmt ( db , transactionModeToBegin ( "deferred" ) , this . #intMode) ;
178- const resultSets = stmts . map ( ( stmt ) => {
179- if ( ! db . inTransaction ) {
180- throw new LibsqlError (
181- "The transaction has been rolled back" ,
182- "TRANSACTION_CLOSED" ,
183- ) ;
199+ const resultSets = [ ] ;
200+ for ( let i = 0 ; i < stmts . length ; i ++ ) {
201+ try {
202+ if ( ! db . inTransaction ) {
203+ throw new LibsqlBatchError (
204+ "The transaction has been rolled back" ,
205+ i ,
206+ "TRANSACTION_CLOSED" ,
207+ ) ;
208+ }
209+ resultSets . push ( executeStmt ( db , stmts [ i ] , this . #intMode) ) ;
210+ } catch ( e ) {
211+ if ( e instanceof LibsqlBatchError ) {
212+ throw e ;
213+ }
214+ if ( e instanceof LibsqlError ) {
215+ throw new LibsqlBatchError (
216+ e . message ,
217+ i ,
218+ e . code ,
219+ e . rawCode ,
220+ e . cause instanceof Error ? e . cause : undefined ,
221+ ) ;
222+ }
223+ throw e ;
184224 }
185- return executeStmt ( db , stmt , this . #intMode) ;
186- } ) ;
225+ }
187226 executeStmt ( db , "COMMIT" , this . #intMode) ;
188227 return resultSets ;
189228 } finally {
@@ -291,13 +330,34 @@ export class Sqlite3Transaction implements Transaction {
291330 async batch (
292331 stmts : Array < InStatement | [ string , InArgs ?] > ,
293332 ) : Promise < Array < ResultSet > > {
294- return stmts . map ( ( stmt ) => {
295- this . #checkNotClosed( ) ;
296- const normalizedStmt : InStatement = Array . isArray ( stmt )
297- ? { sql : stmt [ 0 ] , args : stmt [ 1 ] || [ ] }
298- : stmt ;
299- return executeStmt ( this . #database, normalizedStmt , this . #intMode) ;
300- } ) ;
333+ const resultSets = [ ] ;
334+ for ( let i = 0 ; i < stmts . length ; i ++ ) {
335+ try {
336+ this . #checkNotClosed( ) ;
337+ const stmt = stmts [ i ] ;
338+ const normalizedStmt : InStatement = Array . isArray ( stmt )
339+ ? { sql : stmt [ 0 ] , args : stmt [ 1 ] || [ ] }
340+ : stmt ;
341+ resultSets . push (
342+ executeStmt ( this . #database, normalizedStmt , this . #intMode) ,
343+ ) ;
344+ } catch ( e ) {
345+ if ( e instanceof LibsqlBatchError ) {
346+ throw e ;
347+ }
348+ if ( e instanceof LibsqlError ) {
349+ throw new LibsqlBatchError (
350+ e . message ,
351+ i ,
352+ e . code ,
353+ e . rawCode ,
354+ e . cause instanceof Error ? e . cause : undefined ,
355+ ) ;
356+ }
357+ throw e ;
358+ }
359+ }
360+ return resultSets ;
301361 }
302362
303363 async executeMultiple ( sql : string ) : Promise < void > {
0 commit comments