@@ -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,38 @@ 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 normalizedStmt : InStatement = Array . isArray ( stmts [ i ] )
162+ ? { sql : stmts [ i ] [ 0 ] , args : stmts [ i ] [ 1 ] || [ ] }
163+ : stmts [ i ] ;
164+ resultSets . push (
165+ executeStmt ( db , normalizedStmt , this . #intMode) ,
156166 ) ;
167+ } catch ( e ) {
168+ if ( e instanceof LibsqlBatchError ) {
169+ throw e ;
170+ }
171+ if ( e instanceof LibsqlError ) {
172+ throw new LibsqlBatchError (
173+ e . message ,
174+ i ,
175+ e . code ,
176+ e . rawCode ,
177+ e . cause ,
178+ ) ;
179+ }
180+ throw e ;
157181 }
158- const normalizedStmt : InStatement = Array . isArray ( stmt )
159- ? { sql : stmt [ 0 ] , args : stmt [ 1 ] || [ ] }
160- : stmt ;
161- return executeStmt ( db , normalizedStmt , this . #intMode) ;
162- } ) ;
182+ }
163183 executeStmt ( db , "COMMIT" , this . #intMode) ;
164184 return resultSets ;
165185 } finally {
@@ -175,15 +195,33 @@ export class Sqlite3Client implements Client {
175195 try {
176196 executeStmt ( db , "PRAGMA foreign_keys=off" , this . #intMode) ;
177197 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- ) ;
198+ const resultSets = [ ] ;
199+ for ( let i = 0 ; i < stmts . length ; i ++ ) {
200+ try {
201+ if ( ! db . inTransaction ) {
202+ throw new LibsqlBatchError (
203+ "The transaction has been rolled back" ,
204+ i ,
205+ "TRANSACTION_CLOSED" ,
206+ ) ;
207+ }
208+ resultSets . push ( executeStmt ( db , stmts [ i ] , this . #intMode) ) ;
209+ } catch ( e ) {
210+ if ( e instanceof LibsqlBatchError ) {
211+ throw e ;
212+ }
213+ if ( e instanceof LibsqlError ) {
214+ throw new LibsqlBatchError (
215+ e . message ,
216+ i ,
217+ e . code ,
218+ e . rawCode ,
219+ e . cause ,
220+ ) ;
221+ }
222+ throw e ;
184223 }
185- return executeStmt ( db , stmt , this . #intMode) ;
186- } ) ;
224+ }
187225 executeStmt ( db , "COMMIT" , this . #intMode) ;
188226 return resultSets ;
189227 } finally {
@@ -291,13 +329,33 @@ export class Sqlite3Transaction implements Transaction {
291329 async batch (
292330 stmts : Array < InStatement | [ string , InArgs ?] > ,
293331 ) : 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- } ) ;
332+ const resultSets = [ ] ;
333+ for ( let i = 0 ; i < stmts . length ; i ++ ) {
334+ try {
335+ this . #checkNotClosed( ) ;
336+ const normalizedStmt : InStatement = Array . isArray ( stmts [ i ] )
337+ ? { sql : stmts [ i ] [ 0 ] , args : stmts [ i ] [ 1 ] || [ ] }
338+ : stmts [ i ] ;
339+ resultSets . push (
340+ executeStmt ( this . #database, normalizedStmt , this . #intMode) ,
341+ ) ;
342+ } catch ( e ) {
343+ if ( e instanceof LibsqlBatchError ) {
344+ throw e ;
345+ }
346+ if ( e instanceof LibsqlError ) {
347+ throw new LibsqlBatchError (
348+ e . message ,
349+ i ,
350+ e . code ,
351+ e . rawCode ,
352+ e . cause ,
353+ ) ;
354+ }
355+ throw e ;
356+ }
357+ }
358+ return resultSets ;
301359 }
302360
303361 async executeMultiple ( sql : string ) : Promise < void > {
0 commit comments