Skip to content

Commit 0ab36e1

Browse files
geeksilva97aduh95
authored andcommitted
sqlite: aggregate constants in a single property
PR-URL: #56213 Fixes: #56193 Refs: #56193 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 118cd99 commit 0ab36e1

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

doc/api/sqlite.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,19 @@ exception.
472472
| `TEXT` | {string} |
473473
| `BLOB` | {Uint8Array} |
474474

475-
## SQLite constants
475+
## `sqlite.constants`
476476

477-
The following constants are exported by the `node:sqlite` module.
477+
<!-- YAML
478+
added: REPLACEME
479+
-->
480+
481+
* {Object}
482+
483+
An object containing commonly used constants for SQLite operations.
484+
485+
### SQLite constants
478486

479-
### SQLite Session constants
487+
The following constants are exported by the `sqlite.constants` object.
480488

481489
#### Conflict-resolution constants
482490

@@ -497,7 +505,7 @@ The following constants are meant for use with [`database.applyChangeset()`](#da
497505
</tr>
498506
<tr>
499507
<td><code>SQLITE_CHANGESET_ABORT</code></td>
500-
<td>Abort when a change encounters a conflict and roll back databsase.</td>
508+
<td>Abort when a change encounters a conflict and roll back database.</td>
501509
</tr>
502510
</table>
503511

src/node_sqlite.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,12 @@ void Session::Delete() {
16581658
session_ = nullptr;
16591659
}
16601660

1661+
void DefineConstants(Local<Object> target) {
1662+
NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_OMIT);
1663+
NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_REPLACE);
1664+
NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_ABORT);
1665+
}
1666+
16611667
static void Initialize(Local<Object> target,
16621668
Local<Value> unused,
16631669
Local<Context> context,
@@ -1668,6 +1674,9 @@ static void Initialize(Local<Object> target,
16681674
NewFunctionTemplate(isolate, DatabaseSync::New);
16691675
db_tmpl->InstanceTemplate()->SetInternalFieldCount(
16701676
DatabaseSync::kInternalFieldCount);
1677+
Local<Object> constants = Object::New(isolate);
1678+
1679+
DefineConstants(constants);
16711680

16721681
SetProtoMethod(isolate, db_tmpl, "open", DatabaseSync::Open);
16731682
SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close);
@@ -1690,9 +1699,7 @@ static void Initialize(Local<Object> target,
16901699
"StatementSync",
16911700
StatementSync::GetConstructorTemplate(env));
16921701

1693-
NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_OMIT);
1694-
NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_REPLACE);
1695-
NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_ABORT);
1702+
target->Set(context, OneByteString(isolate, "constants"), constants).Check();
16961703
}
16971704

16981705
} // namespace sqlite

test/parallel/test-sqlite-session.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
require('../common');
44
const {
55
DatabaseSync,
6-
SQLITE_CHANGESET_OMIT,
7-
SQLITE_CHANGESET_REPLACE,
8-
SQLITE_CHANGESET_ABORT
6+
constants,
97
} = require('node:sqlite');
108
const { test, suite } = require('node:test');
119

@@ -165,7 +163,7 @@ suite('conflict resolution', () => {
165163
test('database.applyChangeset() - conflict with SQLITE_CHANGESET_ABORT', (t) => {
166164
const { database2, changeset } = prepareConflict();
167165
const result = database2.applyChangeset(changeset, {
168-
onConflict: SQLITE_CHANGESET_ABORT
166+
onConflict: constants.SQLITE_CHANGESET_ABORT
169167
});
170168
// When changeset is aborted due to a conflict, applyChangeset should return false
171169
t.assert.strictEqual(result, false);
@@ -177,7 +175,7 @@ suite('conflict resolution', () => {
177175
test('database.applyChangeset() - conflict with SQLITE_CHANGESET_REPLACE', (t) => {
178176
const { database2, changeset } = prepareConflict();
179177
const result = database2.applyChangeset(changeset, {
180-
onConflict: SQLITE_CHANGESET_REPLACE
178+
onConflict: constants.SQLITE_CHANGESET_REPLACE
181179
});
182180
// Not aborted due to conflict, so should return true
183181
t.assert.strictEqual(result, true);
@@ -189,7 +187,7 @@ suite('conflict resolution', () => {
189187
test('database.applyChangeset() - conflict with SQLITE_CHANGESET_OMIT', (t) => {
190188
const { database2, changeset } = prepareConflict();
191189
const result = database2.applyChangeset(changeset, {
192-
onConflict: SQLITE_CHANGESET_OMIT
190+
onConflict: constants.SQLITE_CHANGESET_OMIT
193191
});
194192
// Not aborted due to conflict, so should return true
195193
t.assert.strictEqual(result, true);
@@ -199,12 +197,6 @@ suite('conflict resolution', () => {
199197
});
200198
});
201199

202-
test('session related constants are defined', (t) => {
203-
t.assert.strictEqual(SQLITE_CHANGESET_OMIT, 0);
204-
t.assert.strictEqual(SQLITE_CHANGESET_REPLACE, 1);
205-
t.assert.strictEqual(SQLITE_CHANGESET_ABORT, 2);
206-
});
207-
208200
test('database.createSession() - filter changes', (t) => {
209201
const database1 = new DatabaseSync(':memory:');
210202
const database2 = new DatabaseSync(':memory:');

test/parallel/test-sqlite.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { spawnPromisified } = require('../common');
33
const tmpdir = require('../common/tmpdir');
44
const { join } = require('node:path');
5-
const { DatabaseSync } = require('node:sqlite');
5+
const { DatabaseSync, constants } = require('node:sqlite');
66
const { suite, test } = require('node:test');
77
let cnt = 0;
88

@@ -85,6 +85,12 @@ test('in-memory databases are supported', (t) => {
8585
);
8686
});
8787

88+
test('sqlite constants are defined', (t) => {
89+
t.assert.strictEqual(constants.SQLITE_CHANGESET_OMIT, 0);
90+
t.assert.strictEqual(constants.SQLITE_CHANGESET_REPLACE, 1);
91+
t.assert.strictEqual(constants.SQLITE_CHANGESET_ABORT, 2);
92+
});
93+
8894
test('PRAGMAs are supported', (t) => {
8995
const db = new DatabaseSync(nextDb());
9096
t.after(() => { db.close(); });

typings/internalBinding/constants.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ export interface ConstantsBinding {
130130
PRIORITY_HIGHEST: -20;
131131
};
132132
};
133+
sqlite: {
134+
SQLITE_CHANGESET_OMIT: 0;
135+
SQLITE_CHANGESET_REPLACE: 1;
136+
SQLITE_CHANGESET_ABORT: 2;
137+
};
133138
fs: {
134139
UV_FS_SYMLINK_DIR: 1;
135140
UV_FS_SYMLINK_JUNCTION: 2;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy