From 8fea6f1ae8d50ce5012f6026484dd600d87b0ec9 Mon Sep 17 00:00:00 2001 From: Edy Silva Date: Tue, 10 Dec 2024 13:59:07 -0300 Subject: [PATCH 1/5] sqlite: aggregate constants in a single property --- src/node_sqlite.cc | 15 ++++++++++++--- test/parallel/test-sqlite-session.js | 16 ++++------------ test/parallel/test-sqlite.js | 8 +++++++- typings/internalBinding/constants.d.ts | 5 +++++ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index dd78f4e6dacf59..4ba83b92b80cdd 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -1293,6 +1293,12 @@ void Session::Delete() { session_ = nullptr; } +void DefineConstants(Local target) { + NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_OMIT); + NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_REPLACE); + NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_ABORT); +} + static void Initialize(Local target, Local unused, Local context, @@ -1303,6 +1309,9 @@ static void Initialize(Local target, NewFunctionTemplate(isolate, DatabaseSync::New); db_tmpl->InstanceTemplate()->SetInternalFieldCount( DatabaseSync::kInternalFieldCount); + Local constants = Object::New(isolate); + + DefineConstants(constants); SetProtoMethod(isolate, db_tmpl, "open", DatabaseSync::Open); SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close); @@ -1318,9 +1327,9 @@ static void Initialize(Local target, "StatementSync", StatementSync::GetConstructorTemplate(env)); - NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_OMIT); - NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_REPLACE); - NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_ABORT); + target->Set(context, + OneByteString(isolate, "constants"), + constants).Check(); } } // namespace sqlite diff --git a/test/parallel/test-sqlite-session.js b/test/parallel/test-sqlite-session.js index 306f439939e2e0..617c0c2aa71181 100644 --- a/test/parallel/test-sqlite-session.js +++ b/test/parallel/test-sqlite-session.js @@ -3,9 +3,7 @@ require('../common'); const { DatabaseSync, - SQLITE_CHANGESET_OMIT, - SQLITE_CHANGESET_REPLACE, - SQLITE_CHANGESET_ABORT + constants, } = require('node:sqlite'); const { test, suite } = require('node:test'); @@ -165,7 +163,7 @@ suite('conflict resolution', () => { test('database.applyChangeset() - conflict with SQLITE_CHANGESET_ABORT', (t) => { const { database2, changeset } = prepareConflict(); const result = database2.applyChangeset(changeset, { - onConflict: SQLITE_CHANGESET_ABORT + onConflict: constants.SQLITE_CHANGESET_ABORT }); // When changeset is aborted due to a conflict, applyChangeset should return false t.assert.strictEqual(result, false); @@ -177,7 +175,7 @@ suite('conflict resolution', () => { test('database.applyChangeset() - conflict with SQLITE_CHANGESET_REPLACE', (t) => { const { database2, changeset } = prepareConflict(); const result = database2.applyChangeset(changeset, { - onConflict: SQLITE_CHANGESET_REPLACE + onConflict: constants.SQLITE_CHANGESET_REPLACE }); // Not aborted due to conflict, so should return true t.assert.strictEqual(result, true); @@ -189,7 +187,7 @@ suite('conflict resolution', () => { test('database.applyChangeset() - conflict with SQLITE_CHANGESET_OMIT', (t) => { const { database2, changeset } = prepareConflict(); const result = database2.applyChangeset(changeset, { - onConflict: SQLITE_CHANGESET_OMIT + onConflict: constants.SQLITE_CHANGESET_OMIT }); // Not aborted due to conflict, so should return true t.assert.strictEqual(result, true); @@ -199,12 +197,6 @@ suite('conflict resolution', () => { }); }); -test('session related constants are defined', (t) => { - t.assert.strictEqual(SQLITE_CHANGESET_OMIT, 0); - t.assert.strictEqual(SQLITE_CHANGESET_REPLACE, 1); - t.assert.strictEqual(SQLITE_CHANGESET_ABORT, 2); -}); - test('database.createSession() - filter changes', (t) => { const database1 = new DatabaseSync(':memory:'); const database2 = new DatabaseSync(':memory:'); diff --git a/test/parallel/test-sqlite.js b/test/parallel/test-sqlite.js index 825e44fb2965f7..87162526ffadcd 100644 --- a/test/parallel/test-sqlite.js +++ b/test/parallel/test-sqlite.js @@ -2,7 +2,7 @@ const { spawnPromisified } = require('../common'); const tmpdir = require('../common/tmpdir'); const { join } = require('node:path'); -const { DatabaseSync } = require('node:sqlite'); +const { DatabaseSync, constants } = require('node:sqlite'); const { suite, test } = require('node:test'); let cnt = 0; @@ -85,6 +85,12 @@ test('in-memory databases are supported', (t) => { ); }); +test('sqlite constants are defined', (t) => { + t.assert.strictEqual(constants.SQLITE_CHANGESET_OMIT, 0); + t.assert.strictEqual(constants.SQLITE_CHANGESET_REPLACE, 1); + t.assert.strictEqual(constants.SQLITE_CHANGESET_ABORT, 2); +}); + test('PRAGMAs are supported', (t) => { const db = new DatabaseSync(nextDb()); t.after(() => { db.close(); }); diff --git a/typings/internalBinding/constants.d.ts b/typings/internalBinding/constants.d.ts index 89d2a53aae2118..dc4657080ba54b 100644 --- a/typings/internalBinding/constants.d.ts +++ b/typings/internalBinding/constants.d.ts @@ -130,6 +130,11 @@ export interface ConstantsBinding { PRIORITY_HIGHEST: -20; }; }; + sqlite: { + SQLITE_CHANGESET_OMIT: 0; + SQLITE_CHANGESET_REPLACE: 1; + SQLITE_CHANGESET_ABORT: 2; + }; fs: { UV_FS_SYMLINK_DIR: 1; UV_FS_SYMLINK_JUNCTION: 2; From 298fa0a7a4e9ad30cd846b9e2b988696ab9029a7 Mon Sep 17 00:00:00 2001 From: Edy Silva Date: Tue, 10 Dec 2024 14:08:37 -0300 Subject: [PATCH 2/5] sqlite,doc: add contants section --- doc/api/sqlite.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 897242c8f16ba2..344de2df2864a5 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -429,6 +429,18 @@ exception. | `TEXT` | {string} | | `BLOB` | {Uint8Array} | +## `node:sqlite` module methods and properties + +### `sqlite.constants` + + + +* {Object} + +An object containing commonly used constants for SQLite operations. + ## SQLite constants The following constants are exported by the `node:sqlite` module. From 258b643a0fdbb3e7ed621fd9177f77c0709fe5f0 Mon Sep 17 00:00:00 2001 From: Edy Silva Date: Tue, 10 Dec 2024 14:13:32 -0300 Subject: [PATCH 3/5] chore: fix formatting --- src/node_sqlite.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 4ba83b92b80cdd..92c7960d5bb060 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -1327,9 +1327,7 @@ static void Initialize(Local target, "StatementSync", StatementSync::GetConstructorTemplate(env)); - target->Set(context, - OneByteString(isolate, "constants"), - constants).Check(); + target->Set(context, OneByteString(isolate, "constants"), constants).Check(); } } // namespace sqlite From 2232b8fbeb02095967a2398f5804f4047c87d9c1 Mon Sep 17 00:00:00 2001 From: Edy Silva Date: Tue, 10 Dec 2024 16:00:25 -0300 Subject: [PATCH 4/5] sqlite,doc: rearrange sections --- doc/api/sqlite.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 344de2df2864a5..bc7d105c78357d 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -429,9 +429,7 @@ exception. | `TEXT` | {string} | | `BLOB` | {Uint8Array} | -## `node:sqlite` module methods and properties - -### `sqlite.constants` +## `sqlite.constants`