forked from launchql/libpg-query-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryparser.cc
235 lines (190 loc) · 7.41 KB
/
queryparser.cc
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <node.h>
#include <uv.h>
#include "pg_query.h"
#include <string>
using namespace v8;
Local<Object> CreateError(Isolate* isolate, const PgQueryError& err)
{
v8::EscapableHandleScope handleScope(isolate);
Local<Object> error = Object::New(isolate);
error->Set(String::NewFromUtf8(isolate, "message"), String::NewFromUtf8(isolate, err.message));
error->Set(String::NewFromUtf8(isolate, "fileName"), String::NewFromUtf8(isolate, err.filename));
error->Set(String::NewFromUtf8(isolate, "functionName"), String::NewFromUtf8(isolate, err.funcname));
error->Set(String::NewFromUtf8(isolate, "lineNumber"), Integer::New(isolate, err.lineno));
error->Set(String::NewFromUtf8(isolate, "cursorPosition"), Integer::New(isolate, err.cursorpos));
if (err.context) {
error->Set(String::NewFromUtf8(isolate, "context"), String::NewFromUtf8(isolate, err.context));
}
else {
error->Set(String::NewFromUtf8(isolate, "context"), Null(isolate));
}
return handleScope.Escape(error);
}
Local<Object> QueryParseResponse(Isolate* isolate, const PgQueryParseResult& result)
{
v8::EscapableHandleScope handleScope(isolate);
Local<Object> obj = Object::New(isolate);
if (result.error) {
obj->Set(
String::NewFromUtf8(isolate, "error"),
CreateError(isolate, *result.error)
);
}
if (result.parse_tree) {
Local<Value> parseResult;
bool parseSucceeded = JSON::Parse(
isolate,
String::NewFromUtf8(isolate, result.parse_tree)
).ToLocal(&parseResult);
if(parseSucceeded == true)
obj->Set(String::NewFromUtf8(isolate, "query"),parseResult);
}
if (result.stderr_buffer) {
obj->Set(
String::NewFromUtf8(isolate, "stderr"),
String::NewFromUtf8(isolate, result.stderr_buffer)
);
}
pg_query_free_parse_result(result);
return handleScope.Escape(obj);
}
Local<Object> PlPgSQLParseResponse(Isolate* isolate, const PgQueryPlpgsqlParseResult& result)
{
v8::EscapableHandleScope handleScope(isolate);
Local<Object> obj = Object::New(isolate);
if (result.error) {
obj->Set(
String::NewFromUtf8(isolate, "error"),
CreateError(isolate, *result.error)
);
}
if (result.plpgsql_funcs) {
Local<Value> parseResult;
bool parseSucceeded = JSON::Parse(
isolate,
String::NewFromUtf8(isolate, result.plpgsql_funcs)
).ToLocal(&parseResult);
if(parseSucceeded == true)
obj->Set(String::NewFromUtf8(isolate, "functions"),parseResult);
}
pg_query_free_plpgsql_parse_result(result);
return handleScope.Escape(obj);
}
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
String::Utf8Value query(args[0]->ToString());
PgQueryParseResult result = pg_query_parse(*query);
args.GetReturnValue().Set(QueryParseResponse(isolate, result));
}
void MethodPlPgSQL(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
String::Utf8Value query(args[0]->ToString());
PgQueryPlpgsqlParseResult result = pg_query_parse_plpgsql(*query);
args.GetReturnValue().Set(PlPgSQLParseResponse(isolate, result));
}
struct Work {
uv_work_t request;
Persistent<Function> callback;
std::string ss;
PgQueryParseResult result;
};
struct WorkPlPgSQL {
uv_work_t request;
Persistent<Function> callback;
std::string ss;
PgQueryPlpgsqlParseResult result;
};
// called by libuv worker in separate thread
static void WorkAsync(uv_work_t* req)
{
Work* work = static_cast<Work*>(req->data);
work->result = pg_query_parse(work->ss.data());
}
static void WorkPlPgSQLAsync(uv_work_t* req)
{
WorkPlPgSQL* work = static_cast<WorkPlPgSQL*>(req->data);
work->result = pg_query_parse_plpgsql(work->ss.data());
}
// called by libuv in event loop when async function completes
static void WorkAsyncComplete(uv_work_t *req, int status)
{
Isolate* isolate = Isolate::GetCurrent();
Work* work = static_cast<Work*>(req->data);
// Set up return arguments.
// Rather than calling back to node with an error if the parse failed,
// as would be more correct, we just call back object that may have an
// error property; caller can deal with it.
v8::HandleScope handleScope(isolate);
Handle<Value> argv[1] = {
QueryParseResponse(isolate, work->result)
};
// execute the callback
// https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065
Local<Function>::New(isolate, work->callback)->Call(
isolate->GetEnteredOrMicrotaskContext()->Global(),
1,
argv
);
// Free up the persistent function callback
work->callback.Reset();
delete work;
}
static void WorkPlPgSQLAsyncComplete(uv_work_t *req, int status)
{
Isolate* isolate = Isolate::GetCurrent();
WorkPlPgSQL* work = static_cast<WorkPlPgSQL*>(req->data);
// Set up return arguments.
// Rather than calling back to node with an error if the parse failed,
// as would be more correct, we just call back object that may have an
// error property; caller can deal with it.
v8::HandleScope handleScope(isolate);
Handle<Value> argv[1] = {
PlPgSQLParseResponse(isolate, work->result)
};
// execute the callback
Local<Function>::New(isolate, work->callback)->Call(
isolate->GetEnteredOrMicrotaskContext()->Global(),
1,
argv
);
// Free up the persistent function callback
work->callback.Reset();
delete work;
}
void MethodAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
v8::EscapableHandleScope handleScope(isolate);
// Set up the work object on the heap so that we can do our calculations
// in WorkAsync, and put our callback there too for use in WorkAsynComplete.
Work* work = new Work();
work->request.data = work;
String::Utf8Value query(args[0]->ToString());
work->ss.assign(*query);
// TODO: should check that this is actually a function.
Local<Function> callback = Local<Function>::Cast(args[1]);
work->callback.Reset(isolate, callback);
// kick of the worker thread
uv_queue_work(uv_default_loop(),&work->request,WorkAsync,WorkAsyncComplete);
args.GetReturnValue().Set(handleScope.Escape(Undefined(isolate)));
}
void MethodPlPgSQLAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
WorkPlPgSQL* work = new WorkPlPgSQL();
work->request.data = work;
String::Utf8Value query(args[0]->ToString());
work->ss.assign(*query);
// store the callback from JS in the work package so we can invoke it later
// TODO: should check that this is actually a function.
Local<Function> callback = Local<Function>::Cast(args[1]);
work->callback.Reset(isolate, callback);
// kick of the worker thread
uv_queue_work(uv_default_loop(),&work->request,WorkPlPgSQLAsync,WorkPlPgSQLAsyncComplete);
args.GetReturnValue().Set(Undefined(isolate));
}
void init(Handle<Object> exports, Handle<Object> module) {
NODE_SET_METHOD(exports, "parseQuery", Method);
NODE_SET_METHOD(exports, "parseQueryAsync", MethodAsync);
NODE_SET_METHOD(exports, "parsePlPgSQL", MethodPlPgSQL);
NODE_SET_METHOD(exports, "parsePlPgSQLAsync", MethodPlPgSQLAsync);
}
NODE_MODULE(addon, init)