Skip to content

Commit 9b6a671

Browse files
authored
Merge pull request #32 from tomquist/tom/deparse
Add bindings for pg_query_deparse_protobuf
2 parents 03f3f15 + 16cbb4c commit 9b6a671

17 files changed

+97007
-239
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ libs/
44
npm-debug.log
55
libpg_query/**/*.a
66
libpg_query/**/*.h
7+
libpg_query/**/*.proto
78
wasm/libpg-query.js
89
*.wasm
910
.cache

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export function parseQuery(sql: string): Promise<any>;
2+
export function deparse(parseTree: any): Promise<string>;
23
export function parsePlPgSQL(funcsSql: string): Promise<any>;
34
export function parseQuerySync(sql: string): any;
5+
export function deparseSync(parseTree: any): any;
46
export function parsePlPgSQLSync(funcsSql: string): any;
57
export function fingerprint(sql: string): Promise<string>;
68
export function fingerprintSync(sql: string): string;

index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const PgQuery = require('./build/Release/queryparser.node');
1+
const PgQuery = require('./build/Release/queryparser');
2+
const { pg_query } = require('./proto');
3+
24

35
module.exports = {
46
parseQuery(query) {
@@ -9,6 +11,16 @@ module.exports = {
911
});
1012
},
1113

14+
deparse(parseTree) {
15+
const msg = pg_query.ParseResult.fromObject(parseTree);
16+
const data = pg_query.ParseResult.encode(msg).finish();
17+
return new Promise((resolve, reject) => {
18+
PgQuery.deparseAsync(data, (err, result) => {
19+
err ? reject(err) : resolve(result);
20+
});
21+
});
22+
},
23+
1224
parsePlPgSQL(query) {
1325
return new Promise((resolve, reject) => {
1426
PgQuery.parsePlPgSQLAsync(query, (err, result) => {
@@ -21,6 +33,12 @@ module.exports = {
2133
return JSON.parse(PgQuery.parseQuerySync(query));
2234
},
2335

36+
deparseSync(parseTree) {
37+
const msg = pg_query.ParseResult.fromObject(parseTree);
38+
const data = pg_query.ParseResult.encode(msg).finish();
39+
return PgQuery.deparseSync(data);
40+
},
41+
2442
parsePlPgSQLSync(query) {
2543
return JSON.parse(PgQuery.parsePlPgSQLSync(query));
2644
},

libpg_query/protobuf/.gitkeep

Whitespace-only changes.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}
3131
},
3232
"scripts": {
33+
"proto": "node_modules/protobufjs-cli/bin/pbjs --keep-case -t static-module -o proto.js libpg_query/protobuf/pg_query.proto",
3334
"clean": "rimraf build",
3435
"configure": "node-pre-gyp configure",
3536
"install": "node-pre-gyp install --fallback-to-build --loglevel verbose",
@@ -54,11 +55,13 @@
5455
"chai": "^3.5.0",
5556
"emnapi": "^0.43.1",
5657
"lodash": "^4.17.15",
57-
"mocha": "^5.2.0"
58+
"mocha": "^5.2.0",
59+
"protobufjs-cli": "https://gitpkg.now.sh/michaelbgreen/protobuf.js/cli?6254efb1bd9263978e9a90da3b05c4ccbcd6564a"
5860
},
5961
"dependencies": {
6062
"@emnapi/runtime": "^0.43.1",
6163
"@mapbox/node-pre-gyp": "^1.0.8",
64+
"protobufjs": "git+https://github.com/michaelbgreen/protobuf.js.git#c0b68ac5c8fbb1ded261c0924bbce0538370333e"
6265
"node-addon-api": "^7.0.0",
6366
"node-gyp": "^10.0.1"
6467
},

0 commit comments

Comments
 (0)