-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
db.iterateStatements allows giving sql.js an SQL string and iterating over statements objects created from that string * Initial working version of statement iteration. * Tests written and running green. * Resolved linter issues. * Modified approach based on PR feedback; simple testing works, automated tests and documentation to be written. * Testing and documentation written. * Undid prior commit (accidentally committed change from sql-wasm.js to sql-wasm-debug.js) * Applied all suggested modifications. * Documentation fixes. * Improve the documentation of db#iterateStatements * Add @implements annotations for StatementIterator * Reformat test code * Fix the type definition of StatementIterator.StatementIteratorResult Co-authored-by: ophir <pere.jobs@gmail.com>
- Loading branch information
1 parent
0cfeaef
commit e20bb74
Showing
6 changed files
with
300 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,4 @@ | |
document.getElementById('error').innerHTML = error; | ||
}; | ||
</script> | ||
</body> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"cwrap", | ||
"stackAlloc", | ||
"stackSave", | ||
"stackRestore" | ||
"stackRestore", | ||
"UTF8ToString" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
exports.test = function (SQL, assert) { | ||
// Create a database | ||
var db = new SQL.Database(); | ||
|
||
// Multiline SQL | ||
var sqlstr = "CREATE TABLE test (x text, y integer);\n" | ||
+ "INSERT INTO test\n" | ||
+ "VALUES ('hello', 42), ('goodbye', 17);\n" | ||
+ "SELECT * FROM test;\n" | ||
+ " -- nothing here"; | ||
var sqlstart = "CREATE TABLE test (x text, y integer);" | ||
|
||
// Manual iteration | ||
// Get an iterator | ||
var it = db.iterateStatements(sqlstr); | ||
|
||
// Get first item | ||
var x = it.next(); | ||
assert.equal(x.done, false, "Valid iterator object produced"); | ||
assert.equal(x.value.getSQL(), sqlstart, "Statement is for first query only"); | ||
assert.equal(it.getRemainingSQL(), sqlstr.slice(sqlstart.length), "Remaining sql retrievable"); | ||
|
||
// execute the first query | ||
x.value.step(); | ||
|
||
// get and execute the second query | ||
x = it.next(); | ||
assert.equal(x.done, false, "Second query found"); | ||
x.value.step(); | ||
|
||
// get and execute the third query | ||
x = it.next(); | ||
assert.equal(x.done, false, "Third query found"); | ||
x.value.step(); | ||
assert.deepEqual(x.value.getColumnNames(), ['x', 'y'], "Third query is SELECT"); | ||
|
||
// check for additional queries | ||
x = it.next(); | ||
assert.deepEqual(x, { done: true }, "Done reported after last query"); | ||
|
||
// additional iteration does nothing | ||
x = it.next(); | ||
assert.deepEqual(x, { done: true }, "Done reported when iterating past completion"); | ||
|
||
db.run("DROP TABLE test;"); | ||
|
||
// for...of | ||
var count = 0; | ||
for (let statement of db.iterateStatements(sqlstr)) { | ||
statement.step(); | ||
count = count + 1; | ||
} | ||
assert.equal(count, 3, "For loop iterates correctly"); | ||
|
||
var badsql = "SELECT 1 as x;garbage in, garbage out"; | ||
|
||
// bad sql will stop iteration | ||
it = db.iterateStatements(badsql); | ||
x = it.next(); | ||
x.value.step(); | ||
assert.deepEqual(x.value.getAsObject(), { x: 1 }, "SQL before bad statement executes successfully"); | ||
assert.throws(function () { it.next() }, /syntax error/, "Bad SQL stops iteration with exception"); | ||
assert.deepEqual(it.next(), { done: true }, "Done reported when iterating after exception"); | ||
|
||
// valid SQL executes, remaining SQL accessible after exception | ||
it = db.iterateStatements(badsql); | ||
var remains = ''; | ||
try { | ||
for (let statement of it) { | ||
statement.step(); | ||
} | ||
} catch { | ||
remains = it.getRemainingSQL(); | ||
} | ||
assert.equal(remains, "garbage in, garbage out", "Remaining SQL accessible after exception"); | ||
|
||
// From the doc example on the iterateStatements method | ||
const results = []; | ||
const sql_queries = "SELECT 1 AS x; SELECT '2' as y"; | ||
for (const statement of db.iterateStatements(sql_queries)) { | ||
statement.step(); // Fetch one line of result from the statement | ||
const sql = statement.getSQL(); | ||
const result = statement.getAsObject(); | ||
results.push({ sql, result }); | ||
} | ||
console.log(results); | ||
assert.deepEqual(results, [ | ||
{ sql: 'SELECT 1 AS x;', result: { x: 1 } }, | ||
{ sql: " SELECT '2' as y", result: { y: '2' } } | ||
], "The code example from the documentation works"); | ||
}; | ||
|
||
if (module == require.main) { | ||
const target_file = process.argv[2]; | ||
const sql_loader = require('./load_sql_lib'); | ||
sql_loader(target_file).then((sql) => { | ||
require('test').run({ | ||
'test statement iterator': function (assert) { | ||
exports.test(sql, assert); | ||
} | ||
}); | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
assert.fail(e); | ||
}); | ||
} |