|
1 | 1 | #include "node_sqlite.h" |
2 | 2 | #include <path.h> |
| 3 | +#include <functional> |
3 | 4 | #include "base_object-inl.h" |
4 | 5 | #include "debug_utils-inl.h" |
5 | 6 | #include "env-inl.h" |
@@ -2343,25 +2344,41 @@ bool Statement::BindParams(const FunctionCallbackInfo<Value>& args) { |
2343 | 2344 | return true; |
2344 | 2345 | } |
2345 | 2346 |
|
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) { |
2347 | 2355 | Statement* stmt; |
2348 | 2356 | ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This()); |
2349 | 2357 | Environment* env = Environment::GetCurrent(args); |
2350 | 2358 | THROW_AND_RETURN_ON_BAD_STATE( |
2351 | 2359 | env, stmt->IsFinalized(), "statement has been finalized"); |
| 2360 | + Isolate* isolate = env->isolate(); |
2352 | 2361 | int r = sqlite3_reset(stmt->statement_); |
2353 | 2362 | CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void()); |
2354 | 2363 | if (!stmt->BindParams(args)) { |
2355 | 2364 | return; |
2356 | 2365 | } |
| 2366 | +} |
2357 | 2367 |
|
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 | + } |
2362 | 2379 |
|
2363 | 2380 | if (!stmt->async_) { |
2364 | | - int r = task(); |
| 2381 | + int r = StatementRun(stmt->statement_); |
2365 | 2382 | args.GetReturnValue().Set(Integer::New(env->isolate(), r)); |
2366 | 2383 | return; |
2367 | 2384 | } |
@@ -2413,7 +2430,7 @@ void Statement::Run(const FunctionCallbackInfo<Value>& args) { |
2413 | 2430 |
|
2414 | 2431 | args.GetReturnValue().Set(resolver->GetPromise()); |
2415 | 2432 | // 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); |
2417 | 2434 | work->ScheduleWork(); |
2418 | 2435 | } |
2419 | 2436 |
|
@@ -2659,6 +2676,8 @@ Local<FunctionTemplate> Statement::GetConstructorTemplate(Environment* env) { |
2659 | 2676 | tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "Statement")); |
2660 | 2677 | tmpl->InstanceTemplate()->SetInternalFieldCount( |
2661 | 2678 | Statement::kInternalFieldCount); |
| 2679 | + SetProtoMethod(isolate, tmpl, "all", Statement::All); |
| 2680 | + SetProtoMethod(isolate, tmpl, "get", Statement::Get); |
2662 | 2681 | SetProtoMethod(isolate, tmpl, "run", Statement::Run); |
2663 | 2682 | env->set_sqlite_statement_constructor_template(tmpl); |
2664 | 2683 | } |
|
0 commit comments