diff options
author | stephan <stephan@noemail.net> | 2022-10-30 11:39:47 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-10-30 11:39:47 +0000 |
commit | 50ef01398d73f38d1204cf00b0371d188e8aef85 (patch) | |
tree | 7903486574ab370ab6e6d7188b7b64c8dd551b3e /ext/wasm/api/sqlite3-api-oo1.js | |
parent | 9163ef1f4dcd60ff2e7d77fcc130b523a9a6b4f3 (diff) | |
download | sqlite-50ef01398d73f38d1204cf00b0371d188e8aef85.tar.gz sqlite-50ef01398d73f38d1204cf00b0371d188e8aef85.zip |
Add oo1.DB.selectArray() and selectObject().
FossilOrigin-Name: 7660db2a2e9c4f3a6a9343d6929744ad0f4be6820976411f9080165491da59b7
Diffstat (limited to 'ext/wasm/api/sqlite3-api-oo1.js')
-rw-r--r-- | ext/wasm/api/sqlite3-api-oo1.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/ext/wasm/api/sqlite3-api-oo1.js b/ext/wasm/api/sqlite3-api-oo1.js index cbdf40f9b..b44dce690 100644 --- a/ext/wasm/api/sqlite3-api-oo1.js +++ b/ext/wasm/api/sqlite3-api-oo1.js @@ -430,6 +430,21 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ }; /** + Internal impl of the DB.selectRowArray() and + selectRowObject() methods. + */ + const __selectFirstRow = (db, sql, bind, getArg)=>{ + let stmt, rc; + try { + stmt = db.prepare(sql).bind(bind); + if(stmt.step()) rc = stmt.get(getArg); + }finally{ + if(stmt) stmt.finalize(); + } + return rc; + }; + + /** Expects to be given a DB instance or an `sqlite3*` pointer (may be null) and an sqlite3 API result code. If the result code is not falsy, this function throws an SQLite3Error with an error @@ -982,6 +997,38 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ } return rc; }, + /** + Prepares the given SQL, step()s it one time, and returns an + array containing the values of the first result row. If it has + no results, `undefined` is returned. + + If passed a second argument other than `undefined`, it is + treated like an argument to Stmt.bind(), so may be any type + supported by that function. + + Throws on error (e.g. malformed SQL). + */ + selectArray: function(sql,bind){ + return __selectFirstRow(this, sql, bind, []); + }, + + /** + Prepares the given SQL, step()s it one time, and returns an + object containing the key/value pairs of the first result + row. If it has no results, `undefined` is returned. + + Note that the order of returned object's keys is not guaranteed + to be the same as the order of the fields in the query string. + + If passed a second argument other than `undefined`, it is + treated like an argument to Stmt.bind(), so may be any type + supported by that function. + + Throws on error (e.g. malformed SQL). + */ + selectObject: function(sql,bind){ + return __selectFirstRow(this, sql, bind, {}); + }, /** Returns the number of currently-opened Stmt handles for this db |