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

add a statement.readOnly flag #13

Open
wants to merge 1 commit into
base: grist-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Napi::Object Statement::Init(Napi::Env env, Napi::Object exports) {
InstanceMethod("each", &Statement::Each, napi_default_method),
InstanceMethod("reset", &Statement::Reset, napi_default_method),
InstanceMethod("finalize", &Statement::Finalize_, napi_default_method),
InstanceAccessor("readOnly", &Statement::ReadOnlyGetter, nullptr),
});

exports.Set("Statement", t);
Expand Down Expand Up @@ -151,6 +152,9 @@ void Statement::Work_Prepare(napi_env e, void* data) {
stmt->message = std::string(sqlite3_errmsg(baton->db->_handle));
stmt->_handle = NULL;
}
// sqlite3_stmt_readonly is an instant operation (just reading out
// something already computed). And it can handle null.
stmt->readOnly = bool(sqlite3_stmt_readonly(stmt->_handle));

sqlite3_mutex_leave(mtx);
}
Expand Down Expand Up @@ -1026,6 +1030,11 @@ void Statement::Finalize_() {
db->Unref();
}

Napi::Value Statement::ReadOnlyGetter(const Napi::CallbackInfo& info) {
Statement *statement = this;
return Napi::Boolean::New(this->Env(), statement->readOnly);
}

void Statement::CleanQueue() {
Napi::Env env = this->Env();
Napi::HandleScope scope(env);
Expand Down
2 changes: 2 additions & 0 deletions src/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class Statement : public Napi::ObjectWrap<Statement> {
WORK_DEFINITION(AllMarshal);
WORK_DEFINITION(Each);
WORK_DEFINITION(Reset);
Napi::Value ReadOnlyGetter(const Napi::CallbackInfo& info);

Napi::Value Finalize_(const Napi::CallbackInfo& info);

Expand Down Expand Up @@ -251,6 +252,7 @@ class Statement : public Napi::ObjectWrap<Statement> {
bool prepared;
bool locked;
bool finalized;
bool readOnly;
std::queue<Call*> queue;
};

Expand Down
50 changes: 50 additions & 0 deletions test/readOnly.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const sqlite3 = require('..');
const assert = require('assert');

const testCases = [
{ sql: 'DELETE FROM foo', readOnly: false },
{ sql: 'SELECT * FROM foo', readOnly: true },
{ sql: 'UPDATE foo SET x = 10', readOnly: false },
{ sql: 'PRAGMA application_id', readOnly: true },
{ sql: 'PRAGMA application_id = 1', readOnly: false },
{ sql: 'CREATE TABLE bar(x, y)', readOnly: false },
{ sql: 'CREATE TABLE IF NOT EXISTS foo(x, y)', readOnly: false },

// Prepare only uses the first statement if there are multiple
// (the rest are returned in a tail string by the underlying
// sqlite3 function). The readOnly flag is for that first
// statement. Anything after the first statement could be
// readOnly or not readOnly, it just won't be running.
{ sql: 'SELECT * FROM foo; DELETE FROM foo', readOnly: true },

// Statements that only affect the connection don't count.
// This is too bad, wish there were a way to detect such things.
{ sql: 'PRAGMA query_only = false', readOnly: true },
{ sql: 'ATTACH DATABASE \'test\' AS test', readOnly: true },
];

describe('readOnly', function() {
let db;

before(function(done) {
db = new sqlite3.Database(':memory:', function(err) {
if (err) throw err;
db.run('CREATE TABLE foo(x, y)', function(err) {
if (err) throw err;
done();
});
});
});

for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
it("reports " + testCase.sql + " as " +
(testCase.readOnly ? "not " : "") + "readOnly", function(done) {
const stmt = db.prepare(testCase.sql, function(err) {
if (err) throw err;
assert.equal(stmt.readOnly, testCase.readOnly);
stmt.finalize(done);
});
});
}
});