Skip to content

Commit ff838ff

Browse files
committed
add remaining methods
1 parent 2ae8782 commit ff838ff

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/node_sqlite.cc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "node_sqlite.h"
22
#include <path.h>
3+
#include <functional>
34
#include "base_object-inl.h"
45
#include "debug_utils-inl.h"
56
#include "env-inl.h"
@@ -2343,25 +2344,41 @@ bool Statement::BindParams(const FunctionCallbackInfo<Value>& args) {
23432344
return true;
23442345
}
23452346

2346-
void Statement::Run(const FunctionCallbackInfo<Value>& args) {
2347+
int StatementRun(sqlite3_stmt* stmt) {
2348+
sqlite3_step(stmt);
2349+
return sqlite3_reset(stmt);
2350+
}
2351+
2352+
void Statement::Get(const FunctionCallbackInfo<Value>& args) {}
2353+
2354+
void Statement::All(const FunctionCallbackInfo<Value>& args) {
23472355
Statement* stmt;
23482356
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
23492357
Environment* env = Environment::GetCurrent(args);
23502358
THROW_AND_RETURN_ON_BAD_STATE(
23512359
env, stmt->IsFinalized(), "statement has been finalized");
2360+
Isolate* isolate = env->isolate();
23522361
int r = sqlite3_reset(stmt->statement_);
23532362
CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void());
23542363
if (!stmt->BindParams(args)) {
23552364
return;
23562365
}
2366+
}
23572367

2358-
auto task = [args, stmt, env]() -> int {
2359-
sqlite3_step(stmt->statement_);
2360-
return sqlite3_reset(stmt->statement_);
2361-
};
2368+
void Statement::Run(const FunctionCallbackInfo<Value>& args) {
2369+
Statement* stmt;
2370+
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
2371+
Environment* env = Environment::GetCurrent(args);
2372+
THROW_AND_RETURN_ON_BAD_STATE(
2373+
env, stmt->IsFinalized(), "statement has been finalized");
2374+
int r = sqlite3_reset(stmt->statement_);
2375+
CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void());
2376+
if (!stmt->BindParams(args)) {
2377+
return;
2378+
}
23622379

23632380
if (!stmt->async_) {
2364-
int r = task();
2381+
int r = StatementRun(stmt->statement_);
23652382
args.GetReturnValue().Set(Integer::New(env->isolate(), r));
23662383
return;
23672384
}
@@ -2413,7 +2430,7 @@ void Statement::Run(const FunctionCallbackInfo<Value>& args) {
24132430

24142431
args.GetReturnValue().Set(resolver->GetPromise());
24152432
// TODO(myself): todo: save work somewhere for cleaning it up
2416-
SQLiteAsyncWork *work = new SQLiteAsyncWork(env, stmt->db_.get(), resolver, task, after);
2433+
SQLiteAsyncWork *work = new SQLiteAsyncWork(env, stmt->db_.get(), resolver, std::bind(StatementRun, stmt->statement_), after);
24172434
work->ScheduleWork();
24182435
}
24192436

@@ -2659,6 +2676,8 @@ Local<FunctionTemplate> Statement::GetConstructorTemplate(Environment* env) {
26592676
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "Statement"));
26602677
tmpl->InstanceTemplate()->SetInternalFieldCount(
26612678
Statement::kInternalFieldCount);
2679+
SetProtoMethod(isolate, tmpl, "all", Statement::All);
2680+
SetProtoMethod(isolate, tmpl, "get", Statement::Get);
26622681
SetProtoMethod(isolate, tmpl, "run", Statement::Run);
26632682
env->set_sqlite_statement_constructor_template(tmpl);
26642683
}

src/node_sqlite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class Statement : public BaseObject {
129129
BaseObjectPtr<DatabaseSync> db,
130130
sqlite3_stmt* stmt,
131131
bool async);
132+
static void All(const v8::FunctionCallbackInfo<v8::Value>& args);
133+
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
132134
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
133135
bool BindValue(const v8::Local<v8::Value>& value, const int index);
134136
bool BindParams(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)