1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-27 20:41:58 +03:00

Add oo1.DB.selectArray() and selectObject().

FossilOrigin-Name: 7660db2a2e9c4f3a6a9343d6929744ad0f4be6820976411f9080165491da59b7
This commit is contained in:
stephan
2022-10-30 11:39:47 +00:00
parent 9163ef1f4d
commit 50ef01398d
4 changed files with 73 additions and 8 deletions

View File

@ -429,6 +429,21 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
return out;
};
/**
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
@ -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