GeezerVison is showing github.com — sqlite: cleanup ERM support and export Session class · nodejs/node@0fe5337
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 clipboard +1-14Lines changed: 1 addition & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | | -const { | 3 | | - SymbolDispose, | 4 | | -} = primordials; | 5 | 2 | const { emitExperimentalWarning } = require('internal/util'); | 6 | | -const binding = internalBinding('sqlite'); | 7 | 3 |
| 8 | 4 | emitExperimentalWarning('SQLite'); | 9 | 5 |
| 10 | | -// TODO(cjihrig): Move this to C++ once Symbol.dispose reaches Stage 4. | 11 | | -binding.DatabaseSync.prototype[SymbolDispose] = function() { | 12 | | - try { | 13 | | - this.close(); | 14 | | - } catch { | 15 | | - // Ignore errors. | 16 | | - } | 17 | | -}; | 18 | | - | 19 | | -module.exports = binding; | | 6 | +module.exports = internalBinding('sqlite'); |
Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1103,6 +1103,14 @@ void DatabaseSync::Close(const FunctionCallbackInfo<Value>& args) { | 1103 | 1103 | db->connection_ = nullptr; | 1104 | 1104 | } | 1105 | 1105 |
| | 1106 | +void DatabaseSync::Dispose(const v8::FunctionCallbackInfo<v8::Value>& args) { | | 1107 | + v8::TryCatch try_catch(args.GetIsolate()); | | 1108 | + Close(args); | | 1109 | + if (try_catch.HasCaught()) { | | 1110 | + CHECK(try_catch.CanContinue()); | | 1111 | + } | | 1112 | +} | | 1113 | + | 1106 | 1114 | void DatabaseSync::Prepare(const FunctionCallbackInfo<Value>& args) { | 1107 | 1115 | DatabaseSync* db; | 1108 | 1116 | ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); | @@ -3015,6 +3023,7 @@ Local<FunctionTemplate> Session::GetConstructorTemplate(Environment* env) { | 3015 | 3023 | SetProtoMethod( | 3016 | 3024 | isolate, tmpl, "patchset", Session::Changeset<sqlite3session_patchset>); | 3017 | 3025 | SetProtoMethod(isolate, tmpl, "close", Session::Close); | | 3026 | + SetProtoDispose(isolate, tmpl, Session::Dispose); | 3018 | 3027 | env->set_sqlite_session_constructor_template(tmpl); | 3019 | 3028 | } | 3020 | 3029 | return tmpl; | @@ -3059,6 +3068,14 @@ void Session::Close(const FunctionCallbackInfo<Value>& args) { | 3059 | 3068 | session->Delete(); | 3060 | 3069 | } | 3061 | 3070 |
| | 3071 | +void Session::Dispose(const v8::FunctionCallbackInfo<v8::Value>& args) { | | 3072 | + v8::TryCatch try_catch(args.GetIsolate()); | | 3073 | + Close(args); | | 3074 | + if (try_catch.HasCaught()) { | | 3075 | + CHECK(try_catch.CanContinue()); | | 3076 | + } | | 3077 | +} | | 3078 | + | 3062 | 3079 | void Session::Delete() { | 3063 | 3080 | if (!database_ || !database_->connection_ || session_ == nullptr) return; | 3064 | 3081 | sqlite3session_delete(session_); | @@ -3094,6 +3111,7 @@ static void Initialize(Local<Object> target, | 3094 | 3111 |
| 3095 | 3112 | SetProtoMethod(isolate, db_tmpl, "open", DatabaseSync::Open); | 3096 | 3113 | SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close); | | 3114 | + SetProtoDispose(isolate, db_tmpl, DatabaseSync::Dispose); | 3097 | 3115 | SetProtoMethod(isolate, db_tmpl, "prepare", DatabaseSync::Prepare); | 3098 | 3116 | SetProtoMethod(isolate, db_tmpl, "exec", DatabaseSync::Exec); | 3099 | 3117 | SetProtoMethod(isolate, db_tmpl, "function", DatabaseSync::CustomFunction); | @@ -3133,6 +3151,8 @@ static void Initialize(Local<Object> target, | 3133 | 3151 | target, | 3134 | 3152 | "StatementSync", | 3135 | 3153 | StatementSync::GetConstructorTemplate(env)); | | 3154 | + SetConstructorFunction( | | 3155 | + context, target, "Session", Session::GetConstructorTemplate(env)); | 3136 | 3156 |
| 3137 | 3157 | target->Set(context, env->constants_string(), constants).Check(); | 3138 | 3158 |
| |
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 |
|---|
@@ -123,6 +123,7 @@ class DatabaseSync : public BaseObject { | 123 | 123 | static void IsTransactionGetter( | 124 | 124 | const v8::FunctionCallbackInfo<v8::Value>& args); | 125 | 125 | static void Close(const v8::FunctionCallbackInfo<v8::Value>& args); | | 126 | + static void Dispose(const v8::FunctionCallbackInfo<v8::Value>& args); | 126 | 127 | static void Prepare(const v8::FunctionCallbackInfo<v8::Value>& args); | 127 | 128 | static void Exec(const v8::FunctionCallbackInfo<v8::Value>& args); | 128 | 129 | static void CreateTagStore(const v8::FunctionCallbackInfo<v8::Value>& args); | @@ -266,6 +267,7 @@ class Session : public BaseObject { | 266 | 267 | template <Sqlite3ChangesetGenFunc sqliteChangesetFunc> | 267 | 268 | static void Changeset(const v8::FunctionCallbackInfo<v8::Value>& args); | 268 | 269 | static void Close(const v8::FunctionCallbackInfo<v8::Value>& args); | | 270 | + static void Dispose(const v8::FunctionCallbackInfo<v8::Value>& args); | 269 | 271 | static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( | 270 | 272 | Environment* env); | 271 | 273 | static BaseObjectPtr<Session> Create(Environment* env, | |
Collapse file Copy file name to clipboardExpand all lines: src/util.cc +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -598,6 +598,32 @@ void SetMethodNoSideEffect(Isolate* isolate, | 598 | 598 | that->Set(name_string, t); | 599 | 599 | } | 600 | 600 |
| | 601 | +void SetProtoDispose(v8::Isolate* isolate, | | 602 | + v8::Local<v8::FunctionTemplate> that, | | 603 | + v8::FunctionCallback callback) { | | 604 | + Local<v8::Signature> signature = v8::Signature::New(isolate, that); | | 605 | + Local<v8::FunctionTemplate> t = | | 606 | + NewFunctionTemplate(isolate, | | 607 | + callback, | | 608 | + signature, | | 609 | + v8::ConstructorBehavior::kThrow, | | 610 | + v8::SideEffectType::kHasSideEffect); | | 611 | + that->PrototypeTemplate()->Set(v8::Symbol::GetDispose(isolate), t); | | 612 | +} | | 613 | + | | 614 | +void SetProtoAsyncDispose(v8::Isolate* isolate, | | 615 | + v8::Local<v8::FunctionTemplate> that, | | 616 | + v8::FunctionCallback callback) { | | 617 | + Local<v8::Signature> signature = v8::Signature::New(isolate, that); | | 618 | + Local<v8::FunctionTemplate> t = | | 619 | + NewFunctionTemplate(isolate, | | 620 | + callback, | | 621 | + signature, | | 622 | + v8::ConstructorBehavior::kThrow, | | 623 | + v8::SideEffectType::kHasSideEffect); | | 624 | + that->PrototypeTemplate()->Set(v8::Symbol::GetAsyncDispose(isolate), t); | | 625 | +} | | 626 | + | 601 | 627 | void SetProtoMethod(v8::Isolate* isolate, | 602 | 628 | Local<v8::FunctionTemplate> that, | 603 | 629 | const std::string_view name, | |
Collapse file Copy file name to clipboardExpand all lines: src/util.h +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -932,6 +932,16 @@ void SetMethodNoSideEffect(v8::Isolate* isolate, | 932 | 932 | const std::string_view name, | 933 | 933 | v8::FunctionCallback callback); | 934 | 934 |
| | 935 | +// Set the Symbol.dispose method on the prototype of the class. | | 936 | +void SetProtoDispose(v8::Isolate* isolate, | | 937 | + v8::Local<v8::FunctionTemplate> that, | | 938 | + v8::FunctionCallback callback); | | 939 | + | | 940 | +// Set the Symbol.asyncDispose method on the prototype of the class. | | 941 | +void SetProtoAsyncDispose(v8::Isolate* isolate, | | 942 | + v8::Local<v8::FunctionTemplate> that, | | 943 | + v8::FunctionCallback callback); | | 944 | + | 935 | 945 | enum class SetConstructorFunctionFlag { | 936 | 946 | NONE, | 937 | 947 | SET_CLASS_NAME, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-session.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -540,3 +540,18 @@ test('session.close() - closing twice', (t) => { | 540 | 540 | message: 'session is not open' | 541 | 541 | }); | 542 | 542 | }); | | 543 | + | | 544 | +test('session supports ERM', (t) => { | | 545 | + const database = new DatabaseSync(':memory:'); | | 546 | + let afterDisposeSession; | | 547 | + { | | 548 | + using session = database.createSession(); | | 549 | + afterDisposeSession = session; | | 550 | + const changeset = session.changeset(); | | 551 | + t.assert.ok(changeset instanceof Uint8Array); | | 552 | + t.assert.strictEqual(changeset.length, 0); | | 553 | + } | | 554 | + t.assert.throws(() => afterDisposeSession.changeset(), { | | 555 | + message: /session is not open/, | | 556 | + }); | | 557 | +}); |
|