Skip to content

Expose deparseSync #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions queryparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ Local<Object> QueryParseResponse(Isolate* isolate, const PgQueryParseResult& res
return handleScope.Escape(obj);
}

Local<Object> QueryDeparseResponse(Isolate* isolate, const PgQueryDeparseResult& 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.query) {
Local<Value> parseResult;
bool parseSucceeded = JSON::Parse(
isolate,
String::NewFromUtf8(isolate, result.query)
).ToLocal(&parseResult);

if(parseSucceeded == true)
obj->Set(String::NewFromUtf8(isolate, "query"),parseResult);
}

pg_query_free_deparse_result(result);
return handleScope.Escape(obj);
}

Local<Object> PlPgSQLParseResponse(Isolate* isolate, const PgQueryPlpgsqlParseResult& result)
{
v8::EscapableHandleScope handleScope(isolate);
Expand Down Expand Up @@ -94,6 +121,13 @@ void Method(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(QueryParseResponse(isolate, result));
}

void MethodDeparse(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
String::Utf8Value parse_tree(args[0]->ToString());
PgQueryDeparseResult result = pg_query_deparse_protobuf(*parse_tree);
args.GetReturnValue().Set(QueryDeparseResponse(isolate, result));
}

void MethodPlPgSQL(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
String::Utf8Value query(args[0]->ToString());
Expand Down
2 changes: 1 addition & 1 deletion script/buildAddon.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

commit=1ada550d901ed4edbfb6bce2163d21f4b948ab2d
commit=fc5775e0622955f9e5d887dfaf0b7bbf7f9046f9

rDIR=$(pwd)
tmpDir=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmpdir.XXXX')
Expand Down
5 changes: 5 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

// Expose synchronous and asynchronous access to our parsing functions
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(
Napi::String::New(env, "deparseQuerySync"),
Napi::Function::New(env, DeparseQuerySync)
);

exports.Set(
Napi::String::New(env, "parseQuerySync"),
Napi::Function::New(env, ParseQuerySync)
Expand Down
15 changes: 14 additions & 1 deletion src/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ Napi::Error CreateError(Napi::Env env, const PgQueryError& err)
return error;
}

Napi::String QueryDeparseResult(Napi::Env env, const PgQueryDeparseResult& result)
{
if (result.error) {
auto throwVal = CreateError(env, *result.error);
pg_query_free_deparse_result(result);
throw throwVal;
}

auto returnVal = Napi::String::New(env, result.query);
pg_query_free_deparse_result(result);
return returnVal;
}

Napi::String QueryParseResult(Napi::Env env, const PgQueryParseResult& result)
{
if (result.error) {
Expand Down Expand Up @@ -52,4 +65,4 @@ Napi::String FingerprintResult(Napi::Env env, const PgQueryFingerprintResult & r
auto returnVal = Napi::String::New(env, result.fingerprint_str);
pg_query_free_fingerprint_result(result);
return returnVal;
}
}
1 change: 1 addition & 0 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <napi.h>

Napi::Error CreateError(Napi::Env env, const PgQueryError& err);
Napi::String QueryDeparseResult(Napi::Env env, const PgQueryDeparseResult& result);
Napi::String QueryParseResult(Napi::Env env, const PgQueryParseResult& result);
Napi::String PlPgSQLParseResult(Napi::Env env, const PgQueryPlpgsqlParseResult& result);
Napi::String FingerprintResult(Napi::Env env, const PgQueryFingerprintResult & result);
7 changes: 7 additions & 0 deletions src/sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#include "sync.h" // NOLINT(build/include)
#include "helpers.h" // NOLINT(build/include)

Napi::String DeparseQuerySync(const Napi::CallbackInfo& info) {
std::string parse_tree = info[0].As<Napi::String>();
PgQueryDeparseResult result = pg_query_deparse_protobuf(parse_tree.c_str());

return QueryDeparseResult(info.Env(), result);
}

Napi::String ParseQuerySync(const Napi::CallbackInfo& info) {
std::string query = info[0].As<Napi::String>();
PgQueryParseResult result = pg_query_parse(query.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/sync.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <napi.h>

Napi::String DeparseQuerySync(const Napi::CallbackInfo& info);
Napi::String ParseQuerySync(const Napi::CallbackInfo& info);
Napi::String ParsePlPgSQLSync(const Napi::CallbackInfo& info);
Napi::String FingerprintSync(const Napi::CallbackInfo& info);