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

wasm OO API #1: added DB.callInTransaction() and Stmt.stepFinalize().

FossilOrigin-Name: e37dddc1dd9c0530e4b1c6cb0ca7cba7451caa37734d383c9b47f378d7222242
This commit is contained in:
stephan
2022-08-13 13:46:19 +00:00
parent 2cf599cff8
commit f79451eea7
3 changed files with 67 additions and 14 deletions

View File

@ -575,8 +575,8 @@
while(stmt.step()){
stmt._isLocked = true;
const row = arg.cbArg(stmt);
if(callback) callback(row, stmt);
if(resultRows) resultRows.push(row);
if(callback) callback(row, stmt);
stmt._isLocked = false;
}
rowMode = undefined;
@ -819,6 +819,28 @@
return this.pointer ? Object.keys(__stmtMap.get(this)).length : 0;
},
/**
Starts a transaction, calls the given callback, and then either
rolls back or commits the transaction, depending on whether the
callback throw. The callback is pass this db object as its only
argument. On success, returns the result of the callback.
Throws on error.
*/
callInTransaction: function(callback){
affirmDbOpen(this);
let err, rc;
this.exec("BEGIN");
try { rc = callback(this); }
catch(e){
err = e;
throw e;
}finally{
if(err) this.exec("ROLLBACK");
else this.exec("COMMIT");
}
return rc;
},
/**
This function currently does nothing and always throws. It
WILL BE REMOVED pending other refactoring, to eliminate a hard
@ -1042,7 +1064,7 @@
console.warn("Unsupported bind() argument type:",val);
toss3("Unsupported bind() argument type: "+(typeof val));
}
if(rc) checkDbRc(stmt.db.pointer, rc);
if(rc) DB.checkRc(stmt.db.pointer, rc);
return stmt;
};
@ -1228,9 +1250,10 @@
return this;
},
/**
Steps the statement one time. If the result indicates that
a row of data is available, true is returned. If no row of
data is available, false is returned. Throws on error.
Steps the statement one time. If the result indicates that a
row of data is available, a truthy value is returned.
If no row of data is available, a falsy
value is returned. Throws on error.
*/
step: function(){
affirmUnlocked(this, 'step()');
@ -1242,8 +1265,38 @@
this._mayGet = false;
console.warn("sqlite3_step() rc=",rc,"SQL =",
capi.sqlite3_sql(this.pointer));
checkDbRc(this.db.pointer, rc);
};
DB.checkRc(this.db.pointer, rc);
}
},
/**
Functions like step() except that
it finalizes this statement immediately after stepping unless
the step cannot be performed because the statement is
locked. Throws on error, but any error other than the
statement-is-locked case will also trigger finalization of this
statement.
On success, it returns true if the step indicated that a row of
data was available, else it returns false.
This is intended to simplify use cases such as:
```
aDb.prepare("insert in foo(a) values(?)").bind(123).stepFinalize();
```
*/
stepFinalize: function(){
affirmUnlocked(this, 'step()');
const rc = capi.sqlite3_step(affirmStmtOpen(this).pointer);
switch(rc){
case capi.SQLITE_DONE: this.finalize(); return false;
case capi.SQLITE_ROW: this.finalize(); return true;
default:
this.finalize();
console.warn("sqlite3_step() rc=",rc,"SQL =",
capi.sqlite3_sql(this.pointer));
DB.checkRc(this.db.pointer, rc);
}
},
/**
Fetches the value from the given 0-based column index of