Skip to content

Commit

Permalink
Fix large sql allocation issue (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvail authored May 17, 2024
1 parent 586266b commit c7ce2f0
Show file tree
Hide file tree
Showing 6 changed files with 7,057 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/spl-node.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/spl-web.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/spl.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const spl = function (wasmBinary=null, options = {}) {

const sqlite3_open = _emspl.cwrap('sqlite3_open', 'number', ['string', 'number']),
sqlite3_close_v2 = _emspl.cwrap('sqlite3_close_v2', 'number', ['number']),
sqlite3_prepare_v2 = _emspl.cwrap('sqlite3_prepare_v2', 'number', ['number', 'string', 'number', 'number', 'number']),
sqlite3_prepare_v2 = _emspl.cwrap('sqlite3_prepare_v2', 'number', ['number', 'number', 'number', 'number', 'number']),
sqlite3_bind_text = _emspl.cwrap('sqlite3_bind_text', 'number', ['number', 'number', 'number', 'number', 'number']),
sqlite3_bind_blob = _emspl.cwrap('sqlite3_bind_blob', 'number', ['number', 'number', 'number', 'number', 'number']),
sqlite3_bind_double = _emspl.cwrap('sqlite3_bind_double', 'number', ['number', 'number', 'number']),
Expand Down Expand Up @@ -271,8 +271,11 @@ const spl = function (wasmBinary=null, options = {}) {
let ret, ptrs = [];
const rows = [];
setValue(tmpPtr, 0, 'i32');
const sqlSize = lengthBytesUTF8(sql) + 1
const sqlPtr = _malloc(sqlSize);
stringToUTF8(sql, sqlPtr, sqlSize);

ret = sqlite3_prepare_v2(dbHandle, sql, -1, tmpPtr, 0);
ret = sqlite3_prepare_v2(dbHandle, sqlPtr, -1, tmpPtr, 0);
if (ret !== SQLITE.OK) {
throw new Error(sqlite3_errmsg(dbHandle));
}
Expand Down Expand Up @@ -448,6 +451,7 @@ const spl = function (wasmBinary=null, options = {}) {

// const readonly = sqlite3_stmt_readonly(stmt);
sqlite3_finalize(stmt);
_free(sqlPtr);

_result = result(columns, rows);

Expand Down
50 changes: 50 additions & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,53 @@ tape('extensions', async t => {
);

});

tape('json', async t => {

t.plan(4);

let db = (await SPL({
autoGeoJSON: {
precision: 0,
options: 0
}
})).db();

t.deepEqual(
await db.exec('select json(@js)', { '@js': { hello: 'json' }}).get.first,
{ hello: 'json' }
);

t.deepEqual(
await db.exec('select geomfromtext(?)', [ 'POINT(11.1 11.1)' ]).get.first,
{ type: 'Point', 'coordinates': [11, 11] }
)

db.close();

// https://github.com/jvail/spl.js/issues/33
db = (await SPL({
autoGeoJSON: {
precision: 8,
options: 0
}
})).db();

const [a, b] = await fetch('files/json/precision.json').then(res => res.json());

t.doesNotThrow(
async () => await db.exec(
'SELECT CastToMulti(ST_Union(GeomFromGeoJSON(@a), GeomFromGeoJSON(@b)))',
{ '@a': a, '@b': b }
).get.first
);

t.doesNotThrow(
async () => await db.exec(
`SELECT CastToMulti(ST_Union(GeomFromGeoJSON('${JSON.stringify(a)}'), GeomFromGeoJSON('${JSON.stringify(b)}'))) as a`
).get.first
);

db.close();

});
Loading

0 comments on commit c7ce2f0

Please sign in to comment.