-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_connection.cpp
More file actions
159 lines (143 loc) · 6.38 KB
/
node_connection.cpp
File metadata and controls
159 lines (143 loc) · 6.38 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "include/node_connection.h"
#include <iostream>
#include "include/node_database.h"
#include "include/node_query_result.h"
#include "include/node_util.h"
#include "main/lbug.h"
Napi::Object NodeConnection::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function t = DefineClass(env, "NodeConnection",
{InstanceMethod("initAsync", &NodeConnection::InitAsync),
InstanceMethod("initSync", &NodeConnection::InitSync),
InstanceMethod("executeAsync", &NodeConnection::ExecuteAsync),
InstanceMethod("queryAsync", &NodeConnection::QueryAsync),
InstanceMethod("executeSync", &NodeConnection::ExecuteSync),
InstanceMethod("querySync", &NodeConnection::QuerySync),
InstanceMethod("setMaxNumThreadForExec", &NodeConnection::SetMaxNumThreadForExec),
InstanceMethod("setQueryTimeout", &NodeConnection::SetQueryTimeout),
InstanceMethod("close", &NodeConnection::Close)});
exports.Set("NodeConnection", t);
return exports;
}
NodeConnection::NodeConnection(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<NodeConnection>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
NodeDatabase* nodeDatabase = Napi::ObjectWrap<NodeDatabase>::Unwrap(info[0].As<Napi::Object>());
database = nodeDatabase->database;
}
Napi::Value NodeConnection::InitAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new ConnectionInitAsyncWorker(callback, this);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodeConnection::InitSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
InitCppConnection();
} catch (const std::exception& exc) {
Napi::Error::New(env, exc.what()).ThrowAsJavaScriptException();
}
return env.Undefined();
}
void NodeConnection::InitCppConnection() {
this->connection = std::make_shared<Connection>(database.get());
ProgressBar::Get(*connection->getClientContext())
->setDisplay(std::make_shared<NodeProgressBarDisplay>());
// After the connection is initialized, we do not need to hold a reference to the database.
database.reset();
}
void NodeConnection::SetMaxNumThreadForExec(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t numThreads = info[0].ToNumber().Int64Value();
try {
this->connection->setMaxNumThreadForExec(numThreads);
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}
void NodeConnection::SetQueryTimeout(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
size_t timeout = info[0].ToNumber().Int64Value();
try {
this->connection->setQueryTimeOut(timeout);
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}
void NodeConnection::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->connection.reset();
}
Napi::Value NodeConnection::ExecuteAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto nodePreparedStatement =
Napi::ObjectWrap<NodePreparedStatement>::Unwrap(info[0].As<Napi::Object>());
auto nodeQueryResult = Napi::ObjectWrap<NodeQueryResult>::Unwrap(info[1].As<Napi::Object>());
auto callback = info[3].As<Napi::Function>();
try {
auto params = Util::TransformParametersForExec(info[2].As<Napi::Array>());
auto asyncWorker = new ConnectionExecuteAsyncWorker(callback, connection,
nodePreparedStatement->preparedStatement, nodeQueryResult, std::move(params), info[4]);
asyncWorker->Queue();
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return info.Env().Undefined();
}
Napi::Value NodeConnection::QuerySync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto statement = info[0].As<Napi::String>().Utf8Value();
auto nodeQueryResult = Napi::ObjectWrap<NodeQueryResult>::Unwrap(info[1].As<Napi::Object>());
try {
auto result = connection->query(statement);
auto* resultRaw = result.get();
nodeQueryResult->AdoptQueryResult(std::move(result));
if (!resultRaw->isSuccess()) {
Napi::Error::New(env, resultRaw->getErrorMessage()).ThrowAsJavaScriptException();
}
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
Napi::Value NodeConnection::ExecuteSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto nodePreparedStatement =
Napi::ObjectWrap<NodePreparedStatement>::Unwrap(info[0].As<Napi::Object>());
auto nodeQueryResult = Napi::ObjectWrap<NodeQueryResult>::Unwrap(info[1].As<Napi::Object>());
try {
auto params = Util::TransformParametersForExec(info[2].As<Napi::Array>());
auto result = connection->executeWithParams(nodePreparedStatement->preparedStatement.get(),
std::move(params));
auto* resultRaw = result.get();
nodeQueryResult->AdoptQueryResult(std::move(result));
if (!resultRaw->isSuccess()) {
Napi::Error::New(env, resultRaw->getErrorMessage()).ThrowAsJavaScriptException();
}
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
Napi::Value NodeConnection::QueryAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto statement = info[0].As<Napi::String>().Utf8Value();
auto nodeQueryResult = Napi::ObjectWrap<NodeQueryResult>::Unwrap(info[1].As<Napi::Object>());
auto callback = info[2].As<Napi::Function>();
auto asyncWorker =
new ConnectionQueryAsyncWorker(callback, connection, statement, nodeQueryResult, info[3]);
asyncWorker->Queue();
return info.Env().Undefined();
}