Skip to content

Commit d4c0438

Browse files
committed
Add deparse
1 parent 8bf1d4b commit d4c0438

File tree

17 files changed

+96968
-239
lines changed

17 files changed

+96968
-239
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ libs/
44
npm-debug.log
55
libpg_query/**/*.a
66
libpg_query/**/*.h
7+
libpg_query/**/*.proto

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const PgQuery = require('./build/Release/queryparser');
2+
const { pg_query } = require('./proto');
23

34
module.exports = {
45
parseQuery(query) {
@@ -9,6 +10,16 @@ module.exports = {
910
});
1011
},
1112

13+
deparse(parseTree) {
14+
const msg = pg_query.ParseResult.fromObject(parseTree);
15+
const data = pg_query.ParseResult.encode(msg).finish();
16+
return new Promise((resolve, reject) => {
17+
PgQuery.deparseAsync(data, (err, result) => {
18+
err ? reject(err) : resolve(result);
19+
});
20+
});
21+
},
22+
1223
parsePlPgSQL(query) {
1324
return new Promise((resolve, reject) => {
1425
PgQuery.parsePlPgSQLAsync(query, (err, result) => {
@@ -21,6 +32,12 @@ module.exports = {
2132
return JSON.parse(PgQuery.parseQuerySync(query));
2233
},
2334

35+
deparseSync(parseTree) {
36+
const msg = pg_query.ParseResult.fromObject(parseTree);
37+
const data = pg_query.ParseResult.encode(msg).finish();
38+
return PgQuery.deparseSync(data);
39+
},
40+
2441
parsePlPgSQLSync(query) {
2542
return JSON.parse(PgQuery.parsePlPgSQLSync(query));
2643
},

libpg_query/protobuf/.gitkeep

Whitespace-only changes.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"scripts": {
1212
"configure": "node-pre-gyp configure",
1313
"install": "node-pre-gyp install --fallback-to-build",
14+
"proto": "node_modules/protobufjs-cli/bin/pbjs --keep-case -t static-module -o proto.js libpg_query/protobuf/pg_query.proto",
1415
"rebuild": "node-pre-gyp configure rebuild",
1516
"test": "mocha --timeout 5000",
1617
"binary:build": "node-pre-gyp rebuild package",
@@ -25,12 +26,14 @@
2526
"devDependencies": {
2627
"chai": "^3.5.0",
2728
"lodash": "^4.17.15",
28-
"mocha": "^5.2.0"
29+
"mocha": "^5.2.0",
30+
"protobufjs-cli": "https://gitpkg.now.sh/michaelbgreen/protobuf.js/cli?6254efb1bd9263978e9a90da3b05c4ccbcd6564a"
2931
},
3032
"dependencies": {
3133
"@mapbox/node-pre-gyp": "^1.0.8",
3234
"node-addon-api": "^1.6.3",
33-
"node-gyp": "^8.0.0"
35+
"node-gyp": "^8.0.0",
36+
"protobufjs": "git+https://github.com/michaelbgreen/protobuf.js.git#c0b68ac5c8fbb1ded261c0924bbce0538370333e"
3437
},
3538
"keywords": [
3639
"sql",

0 commit comments

Comments
 (0)