| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a4105db commit 0e999eb
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -200,6 +200,15 @@ added: v22.15.0 | |||
| 200 | 200 | ||
| 201 | 201 | * {boolean} Whether the database is currently open or not. | |
| 202 | 202 | ||
| 203 | + ### `database.isTransaction` | ||
| 204 | + | ||
| 205 | + <!-- YAML | ||
| 206 | + added: REPLACEME | ||
| 207 | + --> | ||
| 208 | + | ||
| 209 | + * {boolean} Whether the database is currently within a transaction. This method | ||
| 210 | + is a wrapper around [`sqlite3_get_autocommit()`][]. | ||
| 211 | + | ||
| 203 | 212 | ### `database.open()` | |
| 204 | 213 | ||
| 205 | 214 | <!-- YAML | |
@@ -628,6 +637,7 @@ resolution handler passed to [`database.applyChangeset()`][]. See also | |||
| 628 | 637 | [`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html | |
| 629 | 638 | [`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html | |
| 630 | 639 | [`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html | |
| 640 | + [`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html | ||
| 631 | 641 | [`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html | |
| 632 | 642 | [`sqlite3_load_extension()`]: https://www.sqlite.org/c3ref/load_extension.html | |
| 633 | 643 | [`sqlite3_prepare_v2()`]: https://www.sqlite.org/c3ref/prepare.html | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -537,6 +537,15 @@ void DatabaseSync::IsOpenGetter(const FunctionCallbackInfo<Value>& args) { | |||
| 537 | 537 | args.GetReturnValue().Set(db->IsOpen()); | |
| 538 | 538 | } | |
| 539 | 539 | ||
| 540 | + void DatabaseSync::IsTransactionGetter( | ||
| 541 | + const FunctionCallbackInfo<Value>& args) { | ||
| 542 | + DatabaseSync* db; | ||
| 543 | + ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); | ||
| 544 | + Environment* env = Environment::GetCurrent(args); | ||
| 545 | + THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open"); | ||
| 546 | + args.GetReturnValue().Set(sqlite3_get_autocommit(db->connection_) == 0); | ||
| 547 | + } | ||
| 548 | + | ||
| 540 | 549 | void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) { | |
| 541 | 550 | DatabaseSync* db; | |
| 542 | 551 | ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); | |
@@ -1876,6 +1885,10 @@ static void Initialize(Local<Object> target, | |||
| 1876 | 1885 | db_tmpl, | |
| 1877 | 1886 | FIXED_ONE_BYTE_STRING(isolate, "isOpen"), | |
| 1878 | 1887 | DatabaseSync::IsOpenGetter); | |
| 1888 | + SetSideEffectFreeGetter(isolate, | ||
| 1889 | + db_tmpl, | ||
| 1890 | + FIXED_ONE_BYTE_STRING(isolate, "isTransaction"), | ||
| 1891 | + DatabaseSync::IsTransactionGetter); | ||
| 1879 | 1892 | SetConstructorFunction(context, target, "DatabaseSync", db_tmpl); | |
| 1880 | 1893 | SetConstructorFunction(context, | |
| 1881 | 1894 | target, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,8 @@ class DatabaseSync : public BaseObject { | |||
| 55 | 55 | static void New(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 56 | 56 | static void Open(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 57 | 57 | static void IsOpenGetter(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 58 | + static void IsTransactionGetter( | ||
| 59 | + const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| 58 | 60 | static void Close(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 59 | 61 | static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 60 | 62 | static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -315,3 +315,40 @@ suite('DatabaseSync.prototype.exec()', () => { | |||
| 315 | 315 | }); | |
| 316 | 316 | }); | |
| 317 | 317 | }); | |
| 318 | + | ||
| 319 | + suite('DatabaseSync.prototype.isTransaction', () => { | ||
| 320 | + test('correctly detects a committed transaction', (t) => { | ||
| 321 | + const db = new DatabaseSync(':memory:'); | ||
| 322 | + | ||
| 323 | + t.assert.strictEqual(db.isTransaction, false); | ||
| 324 | + db.exec('BEGIN'); | ||
| 325 | + t.assert.strictEqual(db.isTransaction, true); | ||
| 326 | + db.exec('CREATE TABLE foo (id INTEGER PRIMARY KEY)'); | ||
| 327 | + t.assert.strictEqual(db.isTransaction, true); | ||
| 328 | + db.exec('COMMIT'); | ||
| 329 | + t.assert.strictEqual(db.isTransaction, false); | ||
| 330 | + }); | ||
| 331 | + | ||
| 332 | + test('correctly detects a rolled back transaction', (t) => { | ||
| 333 | + const db = new DatabaseSync(':memory:'); | ||
| 334 | + | ||
| 335 | + t.assert.strictEqual(db.isTransaction, false); | ||
| 336 | + db.exec('BEGIN'); | ||
| 337 | + t.assert.strictEqual(db.isTransaction, true); | ||
| 338 | + db.exec('CREATE TABLE foo (id INTEGER PRIMARY KEY)'); | ||
| 339 | + t.assert.strictEqual(db.isTransaction, true); | ||
| 340 | + db.exec('ROLLBACK'); | ||
| 341 | + t.assert.strictEqual(db.isTransaction, false); | ||
| 342 | + }); | ||
| 343 | + | ||
| 344 | + test('throws if database is not open', (t) => { | ||
| 345 | + const db = new DatabaseSync(nextDb(), { open: false }); | ||
| 346 | + | ||
| 347 | + t.assert.throws(() => { | ||
| 348 | + return db.isTransaction; | ||
| 349 | + }, { | ||
| 350 | + code: 'ERR_INVALID_STATE', | ||
| 351 | + message: /database is not open/, | ||
| 352 | + }); | ||
| 353 | + }); | ||
| 354 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments