@@ -3,9 +3,22 @@ import { SqliteParam, SqliteParams, SqliteRow, paramsToStringArray, throwError }
33
44type Db = android . database . sqlite . SQLiteDatabase ;
55
6- type FromCursor < T > = ( cursor : android . database . Cursor ) => T ;
6+ type FromCursor < T > = ( cursor : android . database . Cursor , transformBlobs ?: boolean ) => T ;
77
8- const dataFromCursor = ( cursor : android . database . Cursor ) => {
8+ export function byteArrayToBuffer ( value ) {
9+ if ( ! value ) {
10+ return null ;
11+ }
12+ const length = value . length ;
13+ const ret = new Uint8Array ( length ) ;
14+ const isString = typeof value === 'string' ;
15+ for ( let i = 0 ; i < length ; i ++ ) {
16+ ret [ i ] = isString ? value . charCodeAt ( i ) : value [ i ] ;
17+ }
18+ return ret ;
19+ }
20+
21+ const dataFromCursor = ( cursor : android . database . Cursor , transformBlobs ?: boolean ) => {
922 const colCount = cursor . getColumnCount ( ) ;
1023 const data : SqliteRow = { } ;
1124 for ( let i = 0 ; i < colCount ; i ++ ) {
@@ -25,7 +38,7 @@ const dataFromCursor = (cursor: android.database.Cursor) => {
2538 break ;
2639
2740 case android . database . Cursor . FIELD_TYPE_BLOB :
28- data [ name ] = cursor . getBlob ( i ) as any ;
41+ data [ name ] = transformBlobs ? byteArrayToBuffer ( cursor . getBlob ( i ) ) : cursor . getBlob ( i ) ;
2942 break ;
3043
3144 case android . database . Cursor . FIELD_TYPE_NULL :
@@ -38,8 +51,7 @@ const dataFromCursor = (cursor: android.database.Cursor) => {
3851 }
3952 return data ;
4053} ;
41-
42- const arrayFromCursor = ( cursor : android . database . Cursor ) => {
54+ const arrayFromCursor = ( cursor : android . database . Cursor , transformBlobs ?: boolean ) => {
4355 const colCount = cursor . getColumnCount ( ) ;
4456 const data : SqliteParam [ ] = [ ] ;
4557 for ( let i = 0 ; i < colCount ; i ++ ) {
@@ -61,7 +73,7 @@ const arrayFromCursor = (cursor: android.database.Cursor) => {
6173 data . push ( cursor . getDouble ( i ) ) ;
6274 break ;
6375 case android . database . Cursor . FIELD_TYPE_BLOB :
64- data . push ( cursor . getBlob ( i ) as any ) ;
76+ data . push ( transformBlobs ? byteArrayToBuffer ( cursor . getBlob ( i ) ) : cursor . getBlob ( i ) ) ;
6577 break ;
6678
6779 case android . database . Cursor . FIELD_TYPE_NULL :
@@ -75,20 +87,20 @@ const arrayFromCursor = (cursor: android.database.Cursor) => {
7587 return data ;
7688} ;
7789
78- const rawSql = < T > ( onCursor : FromCursor < T > ) => ( db : Db ) => ( sql : string , params ?: SqliteParams ) => {
90+ const rawSql = < T > ( onCursor : FromCursor < T > ) => ( db : Db , transformBlobs ?: boolean ) => ( sql : string , params ?: SqliteParams ) => {
7991 const parameters = paramsToStringArray ( params ) as string [ ] ;
8092 const cursor = db . rawQuery ( sql , parameters ) ;
8193 try {
8294 const result : T [ ] = [ ] ;
8395 while ( cursor . moveToNext ( ) ) {
84- result . push ( onCursor ( cursor ) ) ;
96+ result . push ( onCursor ( cursor , transformBlobs ) ) ;
8597 }
8698 return result ;
8799 } finally {
88100 cursor . close ( ) ;
89101 }
90102} ;
91- const eachRaw = < T > ( onCursor : FromCursor < T > ) => ( db : Db ) => (
103+ const eachRaw = < T > ( onCursor : FromCursor < T > ) => ( db : Db , transformBlobs ?: boolean ) => (
92104 sql : string ,
93105 params : SqliteParams ,
94106 callback : ( error : Error , result : T ) => void ,
@@ -100,7 +112,7 @@ const eachRaw = <T>(onCursor: FromCursor<T>) => (db: Db) => (
100112 . then ( ( ) => {
101113 const count = 0 ;
102114 while ( cursor . moveToNext ( ) ) {
103- const result = onCursor ( cursor ) ;
115+ const result = onCursor ( cursor , transformBlobs ) ;
104116 callback ( null , result ) ;
105117 }
106118 cursor . close ( ) ;
@@ -139,13 +151,16 @@ const messagePromises: { [key: string]: { resolve: Function; reject: Function; t
139151export class SQLiteDatabaseBase {
140152 db : android . database . sqlite . SQLiteDatabase ;
141153 flags ;
154+ transformBlobs : boolean ;
142155 constructor ( public filePath : string , options ?: {
143156 threading ?: boolean ;
144157 readOnly ?: boolean ;
145158 flags ?: number ;
159+ transformBlobs ?: boolean ;
146160 } ) {
147161 this . threading = options && options . threading === true ;
148162 this . flags = options ?. flags ;
163+ this . transformBlobs = ! options || options . transformBlobs !== false ;
149164 }
150165 _isInTransaction = false ;
151166 threading = false ;
@@ -211,6 +226,9 @@ export class SQLiteDatabaseBase {
211226 {
212227 type : 'call' ,
213228 id,
229+ dbOptions : {
230+ transformBlobs :this . transformBlobs
231+ } ,
214232 nativeDataKeys : keys ,
215233 } ,
216234 messageData
@@ -254,7 +272,7 @@ export class SQLiteDatabaseBase {
254272 }
255273 return this . db . execSQL ( query , paramsToStringArray ( params ) ) ;
256274 }
257- async get ( query : string , params ?: SqliteParams ) {
275+ async get ( query : string , params ?: SqliteParams , transformBlobs ?: boolean ) {
258276 if ( this . threading ) {
259277 return this . sendMessageToWorker (
260278 {
@@ -266,9 +284,9 @@ export class SQLiteDatabaseBase {
266284 }
267285 ) ;
268286 }
269- return rawSql ( dataFromCursor ) ( this . db ) ( query , params ) [ 0 ] || null ;
287+ return rawSql ( dataFromCursor ) ( this . db , transformBlobs ?? this . transformBlobs ) ( query , params ) [ 0 ] || null ;
270288 }
271- async getArray ( query : string , params ?: SqliteParams ) {
289+ async getArray ( query : string , params ?: SqliteParams , transformBlobs ?: boolean ) {
272290 if ( this . threading ) {
273291 return this . sendMessageToWorker (
274292 {
@@ -280,9 +298,9 @@ export class SQLiteDatabaseBase {
280298 }
281299 ) ;
282300 }
283- return rawSql ( arrayFromCursor ) ( this . db ) ( query , params ) [ 0 ] || null ;
301+ return rawSql ( arrayFromCursor ) ( this . db , transformBlobs ?? this . transformBlobs ) ( query , params ) [ 0 ] || null ;
284302 }
285- async select ( query : string , params ?: SqliteParams ) {
303+ async select ( query : string , params ?: SqliteParams , transformBlobs ?: boolean ) {
286304 if ( this . threading ) {
287305 return this . sendMessageToWorker (
288306 {
@@ -294,9 +312,9 @@ export class SQLiteDatabaseBase {
294312 }
295313 ) ;
296314 }
297- return rawSql ( dataFromCursor ) ( this . db ) ( query , params ) ;
315+ return rawSql ( dataFromCursor ) ( this . db , transformBlobs ?? this . transformBlobs ) ( query , params ) ;
298316 }
299- async selectArray ( query : string , params ?: SqliteParams ) {
317+ async selectArray ( query : string , params ?: SqliteParams , transformBlobs ?: boolean ) {
300318 if ( this . threading ) {
301319 return this . sendMessageToWorker (
302320 {
@@ -308,15 +326,16 @@ export class SQLiteDatabaseBase {
308326 }
309327 ) ;
310328 }
311- return rawSql ( arrayFromCursor ) ( this . db ) ( query , params ) ;
329+ return rawSql ( arrayFromCursor ) ( this . db , transformBlobs ?? this . transformBlobs ) ( query , params ) ;
312330 }
313331 async each (
314332 query : string ,
315333 params : SqliteParams ,
316334 callback : ( error : Error , result : any ) => void ,
317- complete : ( error : Error , count : number ) => void
335+ complete : ( error : Error , count : number ) => void ,
336+ transformBlobs ?: boolean
318337 ) {
319- return eachRaw ( dataFromCursor ) ( this . db ) ( query , params , callback , complete ) ;
338+ return eachRaw ( dataFromCursor ) ( this . db , transformBlobs ?? this . transformBlobs ) ( query , params , callback , complete ) ;
320339 }
321340 async transaction < T = any > ( action : ( cancel ?: ( ) => void ) => Promise < T > ) : Promise < T > {
322341 return transactionRaw ( this . db , action ) ;
0 commit comments