Skip to content

Commit a7dae96

Browse files
committed
Fix indentation and a bit of refactoring
1 parent 55a85d0 commit a7dae96

File tree

1 file changed

+59
-60
lines changed

1 file changed

+59
-60
lines changed

src/spl.ts

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import emspl from './build/js/spl.js';
22
import result from './result';
3-
import { version as spatial_version } from '../package.json';
3+
import { version as spl_version } from '../package.json';
44
import {
55
ISPLSync,
66
IDBSync,
@@ -95,50 +95,54 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
9595

9696
// TODO: save -> return buffer if no path given also in web; fix uncaught in promise; wal mode problem if opened from blob
9797

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');
142146
}
143147

144148
const ret = sqlite3_close_v2(dbHandle);
@@ -255,18 +259,13 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
255259

256260
};
257261

258-
function freeMem(mem: number[]) {
259-
mem.forEach(ptr => _free(ptr));
260-
mem.length = 0;
261-
}
262-
263262
this.exec = (sql, par) => {
264263

265264
if (!dbHandle || !cache) {
266265
throw new Error('Database closed');
267266
}
268267

269-
let ret, mem = [];
268+
let ret, ptrs = [];
270269
const rows = [];
271270
setValue(tmpPtr, 0, 'i32');
272271

@@ -341,11 +340,11 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
341340
len = lengthBytesUTF8(val) + 1;
342341
ptr = _malloc(len);
343342
if (!ptr) {
344-
freeMem(mem);
343+
freeMemory(ptrs);
345344
throw new Error("Unable to allocate memory");
346345
}
347346
stringToUTF8(val, ptr, len);
348-
mem.push(ptr);
347+
ptrs.push(ptr);
349348
ret = sqlite3_bind_text(stmt, idx, ptr, len - 1, 0);
350349
break;
351350
case 'boolean':
@@ -359,22 +358,22 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
359358
len = val.byteLength;
360359
ptr = _malloc(len);
361360
if (!ptr) {
362-
freeMem(mem);
361+
freeMemory(ptrs);
363362
throw new Error("Unable to allocate memory");
364363
}
365364
writeArrayToMemory(new Uint8Array(val), ptr);
366-
mem.push(ptr);
365+
ptrs.push(ptr);
367366
ret = sqlite3_bind_blob(stmt, idx, ptr, len);
368367
} else if (!!val && splOptions.autoJSON) {
369368
const json = JSON.stringify(val);
370369
len = lengthBytesUTF8(json) + 1;
371370
ptr = _malloc(len);
372371
if (!ptr) {
373-
freeMem(mem);
372+
freeMemory(ptrs);
374373
throw new Error("Unable to allocate memory");
375374
}
376375
stringToUTF8(json, ptr, len);
377-
mem.push(ptr);
376+
ptrs.push(ptr);
378377
ret = sqlite3_bind_text(stmt, idx, ptr, len, 0);
379378
} else {
380379
ret = sqlite3_bind_null(stmt, idx);
@@ -386,7 +385,7 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
386385
};
387386
if (ret !== SQLITE.OK) {
388387
sqlite3_finalize(stmt);
389-
freeMem(mem);
388+
freeMemory(ptrs);
390389
throw new Error(sqlite3_errmsg(dbHandle));
391390
}
392391
};
@@ -431,7 +430,7 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
431430
}
432431
rows.push(row);
433432
};
434-
freeMem(mem);
433+
freeMemory(ptrs);
435434
sqlite3_reset(stmt);
436435
sqlite3_clear_bindings(stmt);
437436

@@ -500,7 +499,7 @@ const spl = function (wasmBinary=null, options: ISplOptions | {} ={}): ISPLSync
500499
geos_version() geos,
501500
proj_version() proj,
502501
rttopo_version() rttopo,
503-
'${spatial_version}' 'spatial.js'`);
502+
'${spl_version}' 'spl.js'`);
504503
db_.close();
505504

506505
return ret[0];

0 commit comments

Comments
 (0)