Skip to content

Commit

Permalink
Merge pull request #51 from carlopi/fixexamples
Browse files Browse the repository at this point in the history
Avoid throws in the callbacks
  • Loading branch information
hannes authored Feb 20, 2024
2 parents 6dd34ef + 4fa2046 commit a62d096
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 83 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Then you can run a query:
```js
db.all('SELECT 42 AS fortytwo', function(err, res) {
if (err) {
throw err;
console.warn(err);
}
console.log(res[0].fortytwo)
});
Expand All @@ -26,7 +26,7 @@ Other available methods are `each`, where the callback is invoked for each row,
```js
db.all('SELECT ?::INTEGER AS fortytwo, ?::STRING as hello', 42, 'Hello, World', function(err, res) {
if (err) {
throw err;
console.warn(err);
}
console.log(res[0].fortytwo)
console.log(res[0].hello)
Expand All @@ -47,7 +47,7 @@ You can create multiple connections, each with their own transaction context.
```js
con.all('SELECT 42 AS fortytwo', function(err, res) {
if (err) {
throw err;
console.warn(err);
}
console.log(res[0].fortytwo)
});
Expand All @@ -64,7 +64,7 @@ To execute this statement, you can call for example `all()` on the `stmt` object
```js
stmt.all(42, function(err, res) {
if (err) {
throw err;
console.warn(err);
}
console.log(res[0].fortytwo)
});
Expand All @@ -81,7 +81,7 @@ for (var i = 0; i < 10; i++) {
stmt.finalize();
con.all('SELECT * FROM a', function(err, res) {
if (err) {
throw err;
console.warn(err);
}
console.log(res)
});
Expand All @@ -93,7 +93,7 @@ con.all('SELECT * FROM a', function(err, res) {
var stmt = con.prepare('select ?::INTEGER as fortytwo', function(err, stmt) {
stmt.all(42, function(err, res) {
if (err) {
throw err;
console.warn(err);
}
console.log(res[0].fortytwo)
});
Expand Down
4 changes: 2 additions & 2 deletions test/affected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('query properties', function() {
var j = 1;
for (var i = 0; i < 5000; i++) {
stmt.run(i, "demo", function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
// Relies on SQLite's row numbering to be gapless and starting
// from 1.
// @ts-ignore
Expand All @@ -26,7 +26,7 @@ describe('query properties', function() {

it.skip('should return the correct changes count', function(done) {
db.run("UPDATE foo SET id = id + 1 WHERE id % 2 = 0", function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
// FIXME assert.equal(2500, this.changes);
done();
});
Expand Down
4 changes: 2 additions & 2 deletions test/columns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Column Types', function() {
it('should prepare a statement and return the columns and their types', function(done) {
// we dont include the large_enum and small_enum since they are huge and test the same code path as the small_enum
var stmt = db.prepare("SELECT * EXCLUDE(medium_enum, large_enum) FROM test_all_types()", function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));

let cols = stmt.columns();

Expand Down Expand Up @@ -263,4 +263,4 @@ describe('Column Types', function() {
});
stmt.finalize(done);
});
});
});
4 changes: 2 additions & 2 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('user_agent', () => {

db.all('PRAGMA USER_AGENT', (err: null | Error, rows: TableData) => {
if (err) {
throw err;
done(new Error('Query failed unexpectedly'));
}
assert.match(rows[0].user_agent, /duckdb\/.*\(*\) nodejs/);
done();
Expand All @@ -21,7 +21,7 @@ describe('user_agent', () => {

db.all('PRAGMA USER_AGENT', (err: null | Error, rows: TableData) => {
if (err) {
throw err;
done(new Error('Query failed unexpectedly'));
}
assert.match(rows[0].user_agent, /duckdb\/.*\(*\) nodejs a_framework/);
done();
Expand Down
4 changes: 2 additions & 2 deletions test/each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('each', function() {


db.each('SELECT id, txt FROM foo WHERE ROWID < ?', total, function(err: null | Error, row: RowData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
retrieved++;

if(retrieved === total) {
Expand All @@ -29,7 +29,7 @@ describe('each', function() {
var retrieved = 0;

db.each('SELECT id, txt FROM foo WHERE ROWID < ?', total, function(err: null | Error, row: RowData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
retrieved++;
}, function(err: null | Error, num: RowData) {
assert.equal(retrieved, num);
Expand Down
4 changes: 2 additions & 2 deletions test/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('exec', function() {
it('Database#exec', function(done) {
var sql = fs.readFileSync('test/support/script.sql', 'utf8');
db.exec(sql, function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
done();
});
});

it('retrieve database structure', function(done) {
db.all("SELECT type, name FROM sqlite_master ORDER BY type, name", function(err: null | Error, rows: TableData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.deepEqual(rows, [
// { type: 'index', name: 'grid_key_lookup' },
// { type: 'index', name: 'grid_utfgrid_lookup' },
Expand Down
4 changes: 2 additions & 2 deletions test/named_columns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe.skip('named columns', function() {

it('should retrieve the values', function(done) {
db.get("SELECT txt, num FROM foo ORDER BY num", function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.txt, "Lorem Ipsum");
assert.equal(row.num, 1);
done();
Expand All @@ -30,7 +30,7 @@ describe.skip('named columns', function() {

it('should be able to retrieve rowid of last inserted value', function(done) {
db.get("SELECT last_insert_rowid() as last_id FROM foo", function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.last_id, 1);
done();
});
Expand Down
4 changes: 2 additions & 2 deletions test/null_error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ describe('null error', function() {

it('should insert rows with lots of null values', function(done) {
var stmt = db.prepare('INSERT INTO febp_data VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));

for (var i = 0; i < 100; i++) {
stmt.run([ '100005', 'Albertville City School District', 'ALABAMA', 'AL', '1', '856031', '753000', 'NULL', 'NULL', '6-Small Town', 'Town', 21, '130', '6624', '7140', '8731', '8520', '102', '88', '100', '94', '23352000', '27280000', '30106000', '33028000', '768478', '845886', '782696', '1096819', '1279663', '1168521', '561522', '657649', '684366', '687531', '710543', '727276', '726647', 'N/A', 'N/A', 'N/A', 'N/A', '986', '977', '1006', '1080250', '1202325', '1009962', '1109310', '70287', '93015', '14693.56', '13634.58', 'N/A', '0.230', '0.301', '0.268882175', '73', '26', '29', '3718', '3747', '3790', '2663', '2615', '2575', '75', '82', '89', '3', '2', '6', '11', '9', '8', '955', '1028', '1102', '1991', '2061', '2146', '649', '729', '770', '443', '278', '267', '0.860', '0.86', '0.8474', '0.84', '0.8235', '0.810', '0.84', '0.7729', '0.75', '0.7843', '1121', '1205', '0.74', '0.6862', '0.72', '0.7317', '0.78', '0.7766', '0.79', '0.7387', '0.84', '0.9255', '0.86', '0.9302', '0.88', '0.9308', '0.84', '0.8605' ]);
}

stmt.finalize(function(err) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
done();
});
});
Expand Down
54 changes: 28 additions & 26 deletions test/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('prepare', function() {
err.message === 'Parser: syntax error at or near "CRATE' */) {
done();
}
else throw err;
else {
done(new Error('Query failed unexpectedly'));
}
});
});

Expand Down Expand Up @@ -56,24 +58,24 @@ describe('prepare', function() {
i * Math.PI,
null,
function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
inserted++;
}
).finalize(function(err) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
if (inserted == count) done();
});
}
});

it('should prepare a statement and return values again', function(done) {
var stmt = db.prepare("SELECT txt, num, flt, blb FROM foo ORDER BY num", function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(stmt.sql, 'SELECT txt, num, flt, blb FROM foo ORDER BY num');
});

stmt.each(function(err: null | Error, row: RowData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.txt, 'String ' + retrieved);
assert.equal(row.num, retrieved);
assert.equal(row.flt, retrieved * Math.PI);
Expand All @@ -93,13 +95,13 @@ describe('prepare', function() {
/* // get() is an abomination and should be killed
it('should prepare a statement and run it ' + (count + 5) + ' times', function(done) {
var stmt = db.prepare("SELECT txt, num, flt, blb FROM foo ORDER BY num", function(err) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(stmt.sql, 'SELECT txt, num, flt, blb FROM foo ORDER BY num');
});
for (var i = 0; i < count + 5; i++) (function(i) {
stmt.get(function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
if (retrieved >= 1000) {
assert.equal(row, undefined);
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('prepare', function() {

it('should insert two rows', function(done) {
db.prepare('INSERT INTO foo VALUES(4)').run(function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
inserted++;
}).run(undefined, function (err: null | Error) {
// The second time we pass undefined as a parameter. This is
Expand All @@ -149,20 +151,20 @@ describe('prepare', function() {
};
inserted++;
}).finalize(function(err) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
if (inserted == 2) done();
});
});

/*
it('should retrieve the data', function(done) {
var stmt = db.prepare("SELECT num FROM foo", function(err) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
});
for (var i = 0; i < 2; i++) (function(i) {
stmt.get(function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert(row);
assert.equal(row.num, 4);
retrieved++;
Expand All @@ -175,11 +177,11 @@ describe('prepare', function() {

it('should retrieve the data', function(done) {
var stmt = db.prepare("SELECT num FROM foo", function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
});

stmt.each(function(err: null | Error, row: RowData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.ok(row);
assert.equal(row.num, 4);
retrieved++;
Expand Down Expand Up @@ -211,7 +213,7 @@ describe('prepare', function() {
for (var i = 0; i < 10; i++) {
stmt.reset();
stmt.get(function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.txt, 'String 0');
assert.equal(row.num, 0);
assert.equal(row.flt, 0.0);
Expand Down Expand Up @@ -245,7 +247,7 @@ describe('prepare', function() {
for (var i = 0; i < 10; i++) (function(i) {
stmt.get(i * 10 + 1, function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
var val = i * 10 + 1;
assert.equal(row.txt, 'String ' + val);
assert.equal(row.num, val);
Expand Down Expand Up @@ -279,7 +281,7 @@ describe('prepare', function() {
/* it('should retrieve particular rows', function(done) {
db.prepare("SELECT txt, num, flt, blb FROM foo WHERE num = ? AND txt = ?", 10, 'String 10')
.get(function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.txt, 'String 10');
assert.equal(row.num, 10);
assert.equal(row.flt, 10 * Math.PI);
Expand All @@ -292,7 +294,7 @@ describe('prepare', function() {
it('should retrieve particular rows', function(done) {
db.prepare("SELECT txt, num, flt, blb FROM foo WHERE num = ? AND txt = ?")
.each(10, 'String 10', function(err: null | Error, row: RowData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.txt, 'String 10');
assert.equal(row.num, 10);
// assert.equal(row.flt, 10 * Math.PI);
Expand Down Expand Up @@ -323,7 +325,7 @@ describe('prepare', function() {
it('should retrieve particular rows', function(done) {
db.prepare("SELECT txt, num, flt, blb FROM foo WHERE num < ? ORDER BY num")
.all(count, function(err: null | Error, rows: TableData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
for (var i = 0; i < rows.length; i++) {
assert.equal(rows[i].txt, 'String ' + i);
assert.equal(rows[i].num, i);
Expand Down Expand Up @@ -353,7 +355,7 @@ describe('prepare', function() {
it('should directly execute first statements', function(done) {
db.prepare("insert into foo values (3); insert into foo values (4); select * from foo")
.all(function(err: null | Error, rows: TableData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(rows[0].a, 3);
assert.equal(rows[1].a, 4);
})
Expand Down Expand Up @@ -392,7 +394,7 @@ describe('prepare', function() {
it('should retrieve particular rows', function(done) {
db.prepare("SELECT txt, num, flt, blb FROM foo WHERE num > 5000")
.all(function(err: null | Error, rows: TableData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.ok(rows.length === 0);
})
.finalize(done);
Expand Down Expand Up @@ -435,10 +437,10 @@ describe('prepare', function() {
for (var i = 0; i < data.length; i++) {
var stmt = db.prepare("INSERT INTO foo VALUES(?, ?, ?, ?)");
stmt.run(data[i][0], data[i][1], data[i][2], data[i][3], function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
inserted++;
}).finalize(function(err) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
if (inserted == data.length) done();
});
}
Expand All @@ -447,7 +449,7 @@ describe('prepare', function() {
it('should retrieve all values', function(done) {
db.prepare("SELECT txt, num, flt, blb FROM foo")
.all(function(err: null | Error, rows: TableData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));

for (var i = 0; i < rows.length; i++) {
assert.ok(retrieved_marks[rows[i].num] !== true);
Expand Down Expand Up @@ -485,7 +487,7 @@ describe('prepare', function() {
it('should get a row', function(done) {
db.get("SELECT txt, num, flt, blb FROM foo WHERE num = ? AND txt = ?", 10, 'String 10', function(err, row) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
assert.equal(row.txt, 'String 10');
assert.equal(row.num, 10);
assert.equal(row.flt, 10 * Math.PI);
Expand Down Expand Up @@ -524,7 +526,7 @@ describe('prepare', function() {
i * Math.PI,
null,
function(err: null | Error) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
inserted++;
if (inserted == count) done();
}
Expand All @@ -534,7 +536,7 @@ describe('prepare', function() {

it('should retrieve all rows', function(done) {
db.all("SELECT txt, num, flt, blb FROM foo ORDER BY num", function(err: null | Error, rows: TableData) {
if (err) throw err;
if (err) done(new Error('Query failed unexpectedly'));
for (var i = 0; i < rows.length; i++) {
assert.equal(rows[i].txt, 'String ' + i);
assert.equal(rows[i].num, i);
Expand Down
Loading

0 comments on commit a62d096

Please sign in to comment.