Skip to content

Commit

Permalink
Throwing exception if cursor state is null
Browse files Browse the repository at this point in the history
  • Loading branch information
geelen committed Jul 26, 2023
1 parent 32777c7 commit 4cbb82d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/workerd/api/sql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ async function test(storage) {
assert.deepEqual(rawResults[3], [4,5,6,1,2,3])

// Once an iterator is consumed, it can no longer access the columnNames.
assert.deepEqual(iterator.columnNames, [])
requireException(() => {
iterator.columnNames
}, "Error: Cannot call .getColumnNames after Cursor iterator has been consumed.")

// Also works with cursors returned from .exec
const execIterator = sql.exec(`SELECT * FROM abc, cde`)
Expand Down
5 changes: 4 additions & 1 deletion src/workerd/api/sql.c++
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ jsg::Ref<SqlStorage::Cursor::RawIterator> SqlStorage::Cursor::raw(jsg::Lock&) {
return jsg::alloc<RawIterator>(JSG_THIS);
}

// Returns the set of column names for the current Cursor. An exception will be thrown if the
// iterator has already been fully consumed. The resulting columns may contain duplicate entries,
// for instance a `SELECT *` across a join of two tables that share a column name.
kj::Array<jsg::V8Ref<v8::String>> SqlStorage::Cursor::getColumnNames(jsg::Lock& js) {
KJ_IF_MAYBE(s, state) {
cachedColumnNames.ensureInitialized(js, (*s)->query);
return KJ_MAP(name, this->cachedColumnNames.get()) {
return name.addRef(js);
};
} else {
return kj::Array<jsg::V8Ref<v8::String>>();
kj::throwFatalException(JSG_KJ_EXCEPTION(FAILED, Error, "Cannot call .getColumnNames after Cursor iterator has been consumed."));
}
}

Expand Down

0 comments on commit 4cbb82d

Please sign in to comment.