Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite: aggregate constants in a single property #56213

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/sqlite.md
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,18 @@ exception.
| `TEXT` | {string} |
| `BLOB` | {Uint8Array} |

## `node:sqlite` module methods and properties
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved

### `sqlite.constants`
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: REPLACEME
-->

* {Object}

An object containing commonly used constants for SQLite operations.

## SQLite constants

The following constants are exported by the `node:sqlite` module.
Expand Down
13 changes: 10 additions & 3 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,12 @@ void Session::Delete() {
session_ = nullptr;
}

void DefineConstants(Local<Object> 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<Object> target,
Local<Value> unused,
Local<Context> context,
Expand All @@ -1303,6 +1309,9 @@ static void Initialize(Local<Object> target,
NewFunctionTemplate(isolate, DatabaseSync::New);
db_tmpl->InstanceTemplate()->SetInternalFieldCount(
DatabaseSync::kInternalFieldCount);
Local<Object> constants = Object::New(isolate);

DefineConstants(constants);

SetProtoMethod(isolate, db_tmpl, "open", DatabaseSync::Open);
SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close);
Expand All @@ -1318,9 +1327,7 @@ static void Initialize(Local<Object> 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
Expand Down
16 changes: 4 additions & 12 deletions test/parallel/test-sqlite-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
require('../common');
const {
DatabaseSync,
SQLITE_CHANGESET_OMIT,
SQLITE_CHANGESET_REPLACE,
SQLITE_CHANGESET_ABORT
constants,
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
} = require('node:sqlite');
const { test, suite } = require('node:test');

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});

geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
test('database.createSession() - filter changes', (t) => {
const database1 = new DatabaseSync(':memory:');
const database2 = new DatabaseSync(':memory:');
Expand Down
8 changes: 7 additions & 1 deletion test/parallel/test-sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(); });
Expand Down
5 changes: 5 additions & 0 deletions typings/internalBinding/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading