Skip to content

Commit 3779fc9

Browse files
committed
read-only statements that do not return data can now be executed with .run()
1 parent 840ab40 commit 3779fc9

File tree

10 files changed

+22
-22
lines changed

10 files changed

+22
-22
lines changed

src/objects/database/create-statement.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ NAN_METHOD(Database::CreateStatement) {
4040
return Nan::ThrowTypeError("The supplied SQL string contains more than one statement.");
4141
}
4242

43-
// Determine if the sqlite3_stmt is read-only or not.
44-
if (!sqlite3_stmt_readonly(stmt->st_handle)) {
43+
// Determine if the sqlite3_stmt returns data or not.
44+
int column_count = sqlite3_column_count(stmt->st_handle);
45+
if (!sqlite3_stmt_readonly(stmt->st_handle) || column_count < 1) {
4546
stmt->column_count = 0;
4647
} else {
47-
stmt->column_count = sqlite3_column_count(stmt->st_handle);
48-
if (stmt->column_count < 1) {
49-
return Nan::ThrowTypeError("This read-only SQL statement returns no result columns.");
50-
}
48+
stmt->column_count = column_count;
49+
stmt->state |= RETURNS_DATA
5150
}
5251
Nan::ForceSet(statement, NEW_INTERNAL_STRING_FAST("source"), source, FROZEN);
5352
Nan::ForceSet(statement, NEW_INTERNAL_STRING_FAST("database"), info.This(), FROZEN);

src/objects/statement/all.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
NAN_METHOD(Statement::All) {
44
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This());
5-
if (stmt->column_count == 0) {
6-
return Nan::ThrowTypeError("This statement is not read-only. Use run() instead.");
5+
if (!(stmt->state & RETURNS_DATA)) {
6+
return Nan::ThrowTypeError("This statement does not return data. Use run() instead.");
77
}
88
QUERY_START(stmt, statement, STATEMENT_BIND, SQLITE_STATIC, info, info.Length());
99

src/objects/statement/each.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
NAN_METHOD(Statement::Each) {
44
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This());
5-
if (stmt->column_count == 0) {
6-
return Nan::ThrowTypeError("This statement is not read-only. Use run() instead.");
5+
if (!(stmt->state & RETURNS_DATA)) {
6+
return Nan::ThrowTypeError("This statement does not return data. Use run() instead.");
77
}
88
REQUIRE_LAST_ARGUMENT_FUNCTION(func_index, callback);
99
QUERY_START(stmt, statement, STATEMENT_BIND, SQLITE_TRANSIENT, info, func_index);

src/objects/statement/get.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
NAN_METHOD(Statement::Get) {
44
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This());
5-
if (stmt->column_count == 0) {
6-
return Nan::ThrowTypeError("This statement is not read-only. Use run() instead.");
5+
if (!(stmt->state & RETURNS_DATA)) {
6+
return Nan::ThrowTypeError("This statement does not return data. Use run() instead.");
77
}
88
QUERY_START(stmt, statement, STATEMENT_BIND, SQLITE_STATIC, info, info.Length());
99

src/objects/statement/pluck.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
NAN_METHOD(Statement::Pluck) {
44
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This());
5-
if (stmt->column_count == 0) {
6-
return Nan::ThrowTypeError("The pluck() method can only be used by read-only statements.");
5+
if (!(stmt->state & RETURNS_DATA)) {
6+
return Nan::ThrowTypeError("The pluck() method can only be used by statements that return data.");
77
}
88

99
if (info.Length() == 0 || info[0]->BooleanValue() == true) {

src/objects/statement/run.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
NAN_METHOD(Statement::Run) {
44
Statement* stmt = Nan::ObjectWrap::Unwrap<Statement>(info.This());
5-
if (stmt->column_count != 0) {
6-
return Nan::ThrowTypeError("This statement is read-only. Use get(), all(), or each() instead.");
5+
if (stmt->state & RETURNS_DATA) {
6+
return Nan::ThrowTypeError("This statement returns data. Use get(), all(), or each() instead.");
77
}
88
QUERY_START(stmt, statement, STATEMENT_BIND, SQLITE_STATIC, info, info.Length());
99

src/objects/statement/statement.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void Statement::Init() {
3434
t->InstanceTemplate()->SetInternalFieldCount(1);
3535
t->SetClassName(Nan::New("Statement").ToLocalChecked());
3636

37-
Nan::SetAccessor(t->InstanceTemplate(), Nan::New("readonly").ToLocalChecked(), Readonly);
37+
Nan::SetAccessor(t->InstanceTemplate(), Nan::New("returnsData").ToLocalChecked(), ReturnsData);
3838
Nan::SetPrototypeMethod(t, "safeIntegers", SafeIntegers);
3939
Nan::SetPrototypeMethod(t, "bind", Bind);
4040
Nan::SetPrototypeMethod(t, "pluck", Pluck);

src/objects/statement/statement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Statement : public Nan::ObjectWrap, public Query {
2828
private:
2929
static CONSTRUCTOR(constructor);
3030
static NAN_METHOD(New);
31-
static NAN_GETTER(Readonly);
31+
static NAN_GETTER(ReturnsData);
3232
static NAN_METHOD(SafeIntegers);
3333
static NAN_METHOD(Bind);
3434
static NAN_METHOD(Pluck);
@@ -41,7 +41,7 @@ class Statement : public Nan::ObjectWrap, public Query {
4141
// Sqlite3 interfacing and state
4242
Database* db;
4343
sqlite3_stmt* st_handle;
44-
int column_count; // If this is 0, the statement is a write statement
44+
int column_count;
4545
uint8_t state;
4646

4747
// Unique Statement Id

src/objects/statement/util.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ v8::Local<v8::Object> Statement::GetBindMap() {
2828
return namedParams;
2929
}
3030

31-
// get .readonly -> boolean
32-
NAN_GETTER(Statement::Readonly) {
33-
info.GetReturnValue().Set(Nan::ObjectWrap::Unwrap<Statement>(info.This())->column_count != 0);
31+
// get .returnsData -> boolean
32+
NAN_GETTER(Statement::ReturnsData) {
33+
info.GetReturnValue().Set((Nan::ObjectWrap::Unwrap<Statement>(info.This())->state & RETURNS_DATA) ? true : false);
3434
}
3535

3636
// .safeIntegers(boolean) -> this

src/util/macros.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define HAS_BIND_MAP 0x8
1616
#define SAFE_INTS 0x10
1717
#define PLUCK_COLUMN 0x20
18+
#define RETURNS_DATA 0x40
1819

1920
// Given a v8::String, returns a pointer to a heap-allocated C-String clone.
2021
inline char* C_STRING(v8::Local<v8::String> string) {

0 commit comments

Comments
 (0)