GeezerVison is showing github.com — sqlite: add getter to detect transactions · nodejs/node@da05add
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/sqlite.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -291,6 +291,15 @@ added: v23.11.0 | 291 | 291 |
| 292 | 292 | * {boolean} Whether the database is currently open or not. | 293 | 293 |
| | 294 | +### `database.isTransaction` | | 295 | + | | 296 | +<!-- YAML | | 297 | +added: REPLACEME | | 298 | +--> | | 299 | + | | 300 | +* {boolean} Whether the database is currently within a transaction. This method | | 301 | + is a wrapper around [`sqlite3_get_autocommit()`][]. | | 302 | + | 294 | 303 | ### `database.open()` | 295 | 304 |
| 296 | 305 | <!-- YAML | @@ -839,6 +848,7 @@ resolution handler passed to [`database.applyChangeset()`][]. See also | 839 | 848 | [`sqlite3_create_window_function()`]: https://www.sqlite.org/c3ref/create_function.html | 840 | 849 | [`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html | 841 | 850 | [`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html | | 851 | +[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html | 842 | 852 | [`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html | 843 | 853 | [`sqlite3_load_extension()`]: https://www.sqlite.org/c3ref/load_extension.html | 844 | 854 | [`sqlite3_prepare_v2()`]: https://www.sqlite.org/c3ref/prepare.html | |
Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -979,6 +979,15 @@ void DatabaseSync::IsOpenGetter(const FunctionCallbackInfo<Value>& args) { | 979 | 979 | args.GetReturnValue().Set(db->IsOpen()); | 980 | 980 | } | 981 | 981 |
| | 982 | +void DatabaseSync::IsTransactionGetter( | | 983 | + const FunctionCallbackInfo<Value>& args) { | | 984 | + DatabaseSync* db; | | 985 | + ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); | | 986 | + Environment* env = Environment::GetCurrent(args); | | 987 | + THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open"); | | 988 | + args.GetReturnValue().Set(sqlite3_get_autocommit(db->connection_) == 0); | | 989 | +} | | 990 | + | 982 | 991 | void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) { | 983 | 992 | DatabaseSync* db; | 984 | 993 | ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); | @@ -2623,6 +2632,10 @@ static void Initialize(Local<Object> target, | 2623 | 2632 | db_tmpl, | 2624 | 2633 | FIXED_ONE_BYTE_STRING(isolate, "isOpen"), | 2625 | 2634 | DatabaseSync::IsOpenGetter); | | 2635 | + SetSideEffectFreeGetter(isolate, | | 2636 | + db_tmpl, | | 2637 | + FIXED_ONE_BYTE_STRING(isolate, "isTransaction"), | | 2638 | + DatabaseSync::IsTransactionGetter); | 2626 | 2639 | SetConstructorFunction(context, target, "DatabaseSync", db_tmpl); | 2627 | 2640 | SetConstructorFunction(context, | 2628 | 2641 | target, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,6 +61,8 @@ class DatabaseSync : public BaseObject { | 61 | 61 | static void New(const v8::FunctionCallbackInfo<v8::Value>& args); | 62 | 62 | static void Open(const v8::FunctionCallbackInfo<v8::Value>& args); | 63 | 63 | static void IsOpenGetter(const v8::FunctionCallbackInfo<v8::Value>& args); | | 64 | + static void IsTransactionGetter( | | 65 | + const v8::FunctionCallbackInfo<v8::Value>& args); | 64 | 66 | static void Close(const v8::FunctionCallbackInfo<v8::Value>& args); | 65 | 67 | static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args); | 66 | 68 | static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-database-sync.js +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -324,3 +324,40 @@ suite('DatabaseSync.prototype.exec()', () => { | 324 | 324 | }); | 325 | 325 | }); | 326 | 326 | }); | | 327 | + | | 328 | +suite('DatabaseSync.prototype.isTransaction', () => { | | 329 | + test('correctly detects a committed transaction', (t) => { | | 330 | + const db = new DatabaseSync(':memory:'); | | 331 | + | | 332 | + t.assert.strictEqual(db.isTransaction, false); | | 333 | + db.exec('BEGIN'); | | 334 | + t.assert.strictEqual(db.isTransaction, true); | | 335 | + db.exec('CREATE TABLE foo (id INTEGER PRIMARY KEY)'); | | 336 | + t.assert.strictEqual(db.isTransaction, true); | | 337 | + db.exec('COMMIT'); | | 338 | + t.assert.strictEqual(db.isTransaction, false); | | 339 | + }); | | 340 | + | | 341 | + test('correctly detects a rolled back transaction', (t) => { | | 342 | + const db = new DatabaseSync(':memory:'); | | 343 | + | | 344 | + t.assert.strictEqual(db.isTransaction, false); | | 345 | + db.exec('BEGIN'); | | 346 | + t.assert.strictEqual(db.isTransaction, true); | | 347 | + db.exec('CREATE TABLE foo (id INTEGER PRIMARY KEY)'); | | 348 | + t.assert.strictEqual(db.isTransaction, true); | | 349 | + db.exec('ROLLBACK'); | | 350 | + t.assert.strictEqual(db.isTransaction, false); | | 351 | + }); | | 352 | + | | 353 | + test('throws if database is not open', (t) => { | | 354 | + const db = new DatabaseSync(nextDb(), { open: false }); | | 355 | + | | 356 | + t.assert.throws(() => { | | 357 | + return db.isTransaction; | | 358 | + }, { | | 359 | + code: 'ERR_INVALID_STATE', | | 360 | + message: /database is not open/, | | 361 | + }); | | 362 | + }); | | 363 | +}); |
|