-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQuery.cpp
139 lines (117 loc) · 4.56 KB
/
Query.cpp
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
/*
Copyright (c) 2020 TOSHIBA Digital Solutions Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Query.h"
namespace griddb {
Napi::FunctionReference Query::constructor;
Napi::Object Query::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "Query",
{ InstanceMethod("fetch", &Query::fetch),
InstanceMethod("setFetchOptions", &Query::setFetchOptions),
InstanceMethod("getRowSet", &Query::getRowSet),
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("Query", func);
return exports;
}
Query::Query(const Napi::CallbackInfo &info) :
Napi::ObjectWrap<Query>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() != 3 || !info[0].IsExternal() || !info[1].IsExternal()
|| !info[2].IsExternal()) {
// Throw error
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
this->mQuery = info[0].As<Napi::External<GSQuery>>().Data();
this->mContainerInfo = info[1].As<Napi::External<GSContainerInfo>>().Data();
this->mRow = info[2].As<Napi::External<GSRow>>().Data();
}
Napi::Value Query::fetch(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
GSRowSet *gsRowSet;
// Call method from C-Api.
GSBool gsForUpdate = GS_FALSE;
GSResult ret = gsFetch(mQuery, gsForUpdate, &gsRowSet);
// Check ret, if error, throw exception
if (!GS_SUCCEEDED(ret)) {
PROMISE_REJECT_WITH_ERROR_CODE(deferred, env, ret, mQuery)
}
// Create new RowSet object
Napi::EscapableHandleScope scope(env);
auto rowsetPtr = Napi::External<GSRowSet>::New(env, gsRowSet);
auto gsContainerInfoPtr =
Napi::External<GSContainerInfo>::New(env, mContainerInfo);
auto gsRowPtr = Napi::External<GSRow>::New(env, mRow);
Napi::Value rowsetWrapper = scope.Escape(RowSet::constructor.New({
rowsetPtr, gsContainerInfoPtr, gsRowPtr })).ToObject();
deferred.Resolve(rowsetWrapper);
return deferred.Promise();
}
Query::~Query() {
if (mQuery) {
gsCloseQuery(&mQuery);
mQuery = NULL;
}
}
void Query::setFetchOptions(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() > 1 || !info[0].IsObject()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", mQuery)
return;
}
Napi::Object input = info[0].As<Napi::Object>();
int limit = 0;
if (input.Has("limit")) {
limit = input.Get("limit").As<Napi::Number>().Int32Value();
}
GSResult ret;
ret = gsSetFetchOption(mQuery, GS_FETCH_LIMIT, &limit, GS_TYPE_INTEGER);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, mQuery)
return;
}
#if GS_COMPATIBILITY_SUPPORT_4_0
bool partial = true; // default value rowKey = true
if (input.Has("partial")) {
partial = input.Get("partial").As<Napi::Boolean>().ToBoolean();
}
// Need to call gsSetFetchOption as many as the number of options
ret = gsSetFetchOption(mQuery, GS_FETCH_PARTIAL_EXECUTION, &partial,
GS_TYPE_BOOL);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, mQuery)
return;
}
#endif
}
Napi::Value Query::getRowSet(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
GSRowSet *gsRowSet;
GSResult ret = gsGetRowSet(mQuery, &gsRowSet);
// Check ret, if error, throw exception
if (!GS_SUCCEEDED(ret)) {
PROMISE_REJECT_WITH_ERROR_CODE(deferred, env, ret, mQuery)
}
auto rowset_ptr = Napi::External<GSRowSet>::New(env, gsRowSet);
Napi::EscapableHandleScope scope(env);
Napi::Value rowset_wrapper = scope.Escape(RowSet::constructor.New({
rowset_ptr })).ToObject();
deferred.Resolve(rowset_wrapper);
return deferred.Promise();
}
} // namespace griddb