-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_prepared_statement.cpp
More file actions
63 lines (54 loc) · 2.28 KB
/
node_prepared_statement.cpp
File metadata and controls
63 lines (54 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "include/node_prepared_statement.h"
Napi::Object NodePreparedStatement::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function t = DefineClass(env, "NodePreparedStatement",
{
InstanceMethod("initAsync", &NodePreparedStatement::InitAsync),
InstanceMethod("initSync", &NodePreparedStatement::InitSync),
InstanceMethod("isSuccess", &NodePreparedStatement::IsSuccess),
InstanceMethod("getErrorMessage", &NodePreparedStatement::GetErrorMessage),
});
exports.Set("NodePreparedStatement", t);
return exports;
}
NodePreparedStatement::NodePreparedStatement(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<NodePreparedStatement>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
NodeConnection* nodeConnection =
Napi::ObjectWrap<NodeConnection>::Unwrap(info[0].As<Napi::Object>());
connection = nodeConnection->connection;
queryString = info[1].ToString();
}
Napi::Value NodePreparedStatement::InitAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new PreparedStatementInitAsyncWorker(callback, this);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodePreparedStatement::InitSync(const Napi::CallbackInfo& info) {
InitCppPreparedStatement();
return info.Env().Undefined();
}
void NodePreparedStatement::InitCppPreparedStatement() {
preparedStatement = connection->prepare(queryString);
connection.reset();
}
Napi::Value NodePreparedStatement::IsSuccess(const Napi::CallbackInfo& info) {
if (preparedStatement == nullptr) {
return Napi::Boolean::New(info.Env(), false);
}
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Boolean::New(env, preparedStatement->isSuccess());
}
Napi::Value NodePreparedStatement::GetErrorMessage(const Napi::CallbackInfo& info) {
if (preparedStatement == nullptr) {
return Napi::String::New(info.Env(), "Prepared statement is not initialized.");
}
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::String::New(env, preparedStatement->getErrorMessage());
}