1
1
import emspl from './build/js/spl.js' ;
2
2
import result from './result' ;
3
- import { version as spatial_version } from '../package.json' ;
3
+ import { version as spl_version } from '../package.json' ;
4
4
import {
5
5
ISPLSync ,
6
6
IDBSync ,
@@ -95,50 +95,54 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
95
95
96
96
// TODO: save -> return buffer if no path given also in web; fix uncaught in promise; wal mode problem if opened from blob
97
97
98
- this . db = function ( sqlite3 ?: string | ArrayBuffer ) {
99
-
100
- // @ts -ignore
101
- if ( ! new . target ) return Object . freeze ( new _spl . db ( sqlite3 , options ) ) ;
102
-
103
- const _db = this ;
104
- let _result = result ( ) ;
105
- let tmpPtr = 0 ;
106
- let dbHandle = 0 ;
107
- let cache = 0 ;
108
- let filename = '' ;
109
- const ptr_initGaiaOutBuffer = stackAlloc ( 4 ) ;
110
- initGaiaOutBuffer ( ptr_initGaiaOutBuffer ) ;
111
- const gaiaOutBuffer = getValue ( ptr_initGaiaOutBuffer , 'i32' ) ;
112
-
113
- const memname = ( ) : string => `mem-${ ( Math . round ( Number . MAX_SAFE_INTEGER * Math . random ( ) ) ) } .db` ;
114
- const toGeoJSON = ( blob_ptr , size , precison , options ) => {
115
- let obj = null ;
116
- let buf_ptr = gaiaToJSON ( gaiaOutBuffer , blob_ptr , size , precison , options ) ;
117
- if ( buf_ptr ) {
118
- obj = JSON . parse ( UTF8ToString ( buf_ptr ) ) ;
119
- }
120
- gaiaOutBufferReset ( gaiaOutBuffer ) ;
121
- return obj ;
122
- } ;
123
- const isParam = obj => {
124
- return ! ! obj &&
125
- typeof ( obj ) === 'object' &&
126
- Object . keys ( obj ) . length &&
127
- Object . keys ( obj ) . every ( key => key . startsWith ( ':' ) || key . startsWith ( '$' ) || key . startsWith ( '@' ) ) ;
128
- } ;
129
- const maybeJSON = ( str : string ) => {
130
- return ( str . startsWith ( '"' ) && str . endsWith ( '"' ) ) ||
131
- str === 'null' ||
132
- ( str . startsWith ( '{' ) && str . endsWith ( '}' ) ) ||
133
- ( str . startsWith ( '[' ) && str . endsWith ( ']' ) ) ;
134
- } ;
135
-
136
- Object . defineProperty ( this , 'get' , { get : ( ) => _result } ) ;
137
-
138
- this . close = ( ) : ISPLSync => {
139
-
140
- if ( ! dbHandle || ! cache ) {
141
- throw new Error ( 'Database closed' ) ;
98
+ this . db = function ( sqlite3 ?: string | ArrayBuffer ) {
99
+
100
+ // @ts -ignore
101
+ if ( ! new . target ) return Object . freeze ( new _spl . db ( sqlite3 , options ) ) ;
102
+
103
+ const _db = this ;
104
+ let _result = result ( ) ;
105
+ let tmpPtr = 0 ;
106
+ let dbHandle = 0 ;
107
+ let cache = 0 ;
108
+ let filename = '' ;
109
+ const ptr_initGaiaOutBuffer = stackAlloc ( 4 ) ;
110
+ initGaiaOutBuffer ( ptr_initGaiaOutBuffer ) ;
111
+ const gaiaOutBuffer = getValue ( ptr_initGaiaOutBuffer , 'i32' ) ;
112
+
113
+ const memname = ( ) : string => `mem-${ ( Math . round ( Number . MAX_SAFE_INTEGER * Math . random ( ) ) ) } .db` ;
114
+ const toGeoJSON = ( blob_ptr , size , precison , options ) => {
115
+ let obj = null ;
116
+ let buf_ptr = gaiaToJSON ( gaiaOutBuffer , blob_ptr , size , precison , options ) ;
117
+ if ( buf_ptr ) {
118
+ obj = JSON . parse ( UTF8ToString ( buf_ptr ) ) ;
119
+ }
120
+ gaiaOutBufferReset ( gaiaOutBuffer ) ;
121
+ return obj ;
122
+ } ;
123
+ const isParam = obj => {
124
+ return ! ! obj &&
125
+ typeof ( obj ) === 'object' &&
126
+ Object . keys ( obj ) . length &&
127
+ Object . keys ( obj ) . every ( key => key . startsWith ( ':' ) || key . startsWith ( '$' ) || key . startsWith ( '@' ) ) ;
128
+ } ;
129
+ const maybeJSON = ( str : string ) => {
130
+ return ( str . startsWith ( '"' ) && str . endsWith ( '"' ) ) ||
131
+ str === 'null' ||
132
+ ( str . startsWith ( '{' ) && str . endsWith ( '}' ) ) ||
133
+ ( str . startsWith ( '[' ) && str . endsWith ( ']' ) ) ;
134
+ } ;
135
+ const freeMemory = ( ptrs : number [ ] ) => {
136
+ ptrs . forEach ( ptr => _free ( ptr ) ) ;
137
+ ptrs . length = 0 ;
138
+ } ;
139
+
140
+ Object . defineProperty ( this , 'get' , { get : ( ) => _result } ) ;
141
+
142
+ this . close = ( ) : ISPLSync => {
143
+
144
+ if ( ! dbHandle || ! cache ) {
145
+ throw new Error ( 'Database closed' ) ;
142
146
}
143
147
144
148
const ret = sqlite3_close_v2 ( dbHandle ) ;
@@ -255,18 +259,13 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
255
259
256
260
} ;
257
261
258
- function freeMem ( mem : number [ ] ) {
259
- mem . forEach ( ptr => _free ( ptr ) ) ;
260
- mem . length = 0 ;
261
- }
262
-
263
262
this . exec = ( sql , par ) => {
264
263
265
264
if ( ! dbHandle || ! cache ) {
266
265
throw new Error ( 'Database closed' ) ;
267
266
}
268
267
269
- let ret , mem = [ ] ;
268
+ let ret , ptrs = [ ] ;
270
269
const rows = [ ] ;
271
270
setValue ( tmpPtr , 0 , 'i32' ) ;
272
271
@@ -341,11 +340,11 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
341
340
len = lengthBytesUTF8 ( val ) + 1 ;
342
341
ptr = _malloc ( len ) ;
343
342
if ( ! ptr ) {
344
- freeMem ( mem ) ;
343
+ freeMemory ( ptrs ) ;
345
344
throw new Error ( "Unable to allocate memory" ) ;
346
345
}
347
346
stringToUTF8 ( val , ptr , len ) ;
348
- mem . push ( ptr ) ;
347
+ ptrs . push ( ptr ) ;
349
348
ret = sqlite3_bind_text ( stmt , idx , ptr , len - 1 , 0 ) ;
350
349
break ;
351
350
case 'boolean' :
@@ -359,22 +358,22 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
359
358
len = val . byteLength ;
360
359
ptr = _malloc ( len ) ;
361
360
if ( ! ptr ) {
362
- freeMem ( mem ) ;
361
+ freeMemory ( ptrs ) ;
363
362
throw new Error ( "Unable to allocate memory" ) ;
364
363
}
365
364
writeArrayToMemory ( new Uint8Array ( val ) , ptr ) ;
366
- mem . push ( ptr ) ;
365
+ ptrs . push ( ptr ) ;
367
366
ret = sqlite3_bind_blob ( stmt , idx , ptr , len ) ;
368
367
} else if ( ! ! val && splOptions . autoJSON ) {
369
368
const json = JSON . stringify ( val ) ;
370
369
len = lengthBytesUTF8 ( json ) + 1 ;
371
370
ptr = _malloc ( len ) ;
372
371
if ( ! ptr ) {
373
- freeMem ( mem ) ;
372
+ freeMemory ( ptrs ) ;
374
373
throw new Error ( "Unable to allocate memory" ) ;
375
374
}
376
375
stringToUTF8 ( json , ptr , len ) ;
377
- mem . push ( ptr ) ;
376
+ ptrs . push ( ptr ) ;
378
377
ret = sqlite3_bind_text ( stmt , idx , ptr , len , 0 ) ;
379
378
} else {
380
379
ret = sqlite3_bind_null ( stmt , idx ) ;
@@ -386,7 +385,7 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
386
385
} ;
387
386
if ( ret !== SQLITE . OK ) {
388
387
sqlite3_finalize ( stmt ) ;
389
- freeMem ( mem ) ;
388
+ freeMemory ( ptrs ) ;
390
389
throw new Error ( sqlite3_errmsg ( dbHandle ) ) ;
391
390
}
392
391
} ;
@@ -431,7 +430,7 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
431
430
}
432
431
rows . push ( row ) ;
433
432
} ;
434
- freeMem ( mem ) ;
433
+ freeMemory ( ptrs ) ;
435
434
sqlite3_reset ( stmt ) ;
436
435
sqlite3_clear_bindings ( stmt ) ;
437
436
@@ -500,7 +499,7 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
500
499
geos_version() geos,
501
500
proj_version() proj,
502
501
rttopo_version() rttopo,
503
- '${ spatial_version } ' 'spatial .js'` ) ;
502
+ '${ spl_version } ' 'spl .js'` ) ;
504
503
db_ . close ( ) ;
505
504
506
505
return ret [ 0 ] ;
0 commit comments