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

javascript: rename and simplify DB.callInTransaction() as DB.transaction(). Add DB.savepoint(), which works the same as transaction() but uses savepoints. Correct concatenation of arguments to SQLite3Error to use spaces instead of commas. Test that demo-oo1.js works with persistent OPFS storage (output differs due to persistent rows, but the demo works).

FossilOrigin-Name: e8c323f12b6223bc9dcbbec40698969c7917128aa50cf599c247df23f607ae61
This commit is contained in:
stephan
2022-08-18 12:21:58 +00:00
parent fed91737f5
commit d869a21d13
4 changed files with 83 additions and 39 deletions

View File

@ -59,12 +59,16 @@
enabling clients to unambiguously identify such exceptions.
*/
class SQLite3Error extends Error {
/**
Constructs this object with a message equal to all arguments
concatenated with a space between each one.
*/
constructor(...args){
super(...args);
super(args.join(' '));
this.name = 'SQLite3Error';
}
};
const toss3 = (...args)=>{throw new SQLite3Error(args)};
const toss3 = (...args)=>{throw new SQLite3Error(...args)};
sqlite3.SQLite3Error = SQLite3Error;
/**
@ -195,7 +199,7 @@
};
/**
Expects to be passed (arguments) from DB.exec() and
Expects to be passed the `arguments` object from DB.exec() and
DB.execMulti(). Does the argument processing/validation, throws
on error, and returns a new object on success:
@ -842,26 +846,47 @@
/**
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.
rolls back or commits the savepoint, depending on whether the
callback throws. The callback is passed this db object as its
only argument. On success, returns the result of the
callback. Throws on error.
Note that transactions may not be nested, so this will throw if
it is called recursively. For nested transactions, use the
savepoint() method or manually manage SAVEPOINTs using exec().
*/
callInTransaction: function(callback){
affirmDbOpen(this);
let err, rc;
this.exec("BEGIN");
try { rc = callback(this); }
catch(e){
err = e;
transaction: function(callback){
affirmDbOpen(this).exec("BEGIN");
try {
const rc = callback(this);
this.exec("COMMIT");
return rc;
}catch(e){
this.exec("ROLLBACK");
throw e;
}finally{
if(err) this.exec("ROLLBACK");
else this.exec("COMMIT");
}
return rc;
},
/**
This works similarly to transaction() but uses sqlite3's SAVEPOINT
feature. This function starts a savepoint (with an unspecified name)
and calls the given callback function, passing it this db object.
If the callback returns, the savepoint is released (committed). If
the callback throws, the savepoint is rolled back. If it does not
throw, it returns the result of the callback.
*/
savepoint: function(callback){
affirmDbOpen(this).exec("SAVEPOINT oo1");
try {
const rc = callback(this);
this.exec("RELEASE oo1");
return rc;
}catch(e){
this.execMulti("ROLLBACK to SAVEPOINT oo1; RELEASE SAVEPOINT oo1");
throw e;
}
},
/**
This function currently does nothing and always throws. It
WILL BE REMOVED pending other refactoring, to eliminate a hard