Skip to content

Commit

Permalink
Merge pull request #32 from tomquist/tom/deparse
Browse files Browse the repository at this point in the history
Add bindings for pg_query_deparse_protobuf
  • Loading branch information
pyramation authored May 2, 2024
2 parents 03f3f15 + 16cbb4c commit 9b6a671
Show file tree
Hide file tree
Showing 17 changed files with 97,007 additions and 239 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ libs/
npm-debug.log
libpg_query/**/*.a
libpg_query/**/*.h
libpg_query/**/*.proto
wasm/libpg-query.js
*.wasm
.cache
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function parseQuery(sql: string): Promise<any>;
export function deparse(parseTree: any): Promise<string>;
export function parsePlPgSQL(funcsSql: string): Promise<any>;
export function parseQuerySync(sql: string): any;
export function deparseSync(parseTree: any): any;
export function parsePlPgSQLSync(funcsSql: string): any;
export function fingerprint(sql: string): Promise<string>;
export function fingerprintSync(sql: string): string;
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const PgQuery = require('./build/Release/queryparser.node');
const PgQuery = require('./build/Release/queryparser');
const { pg_query } = require('./proto');


module.exports = {
parseQuery(query) {
Expand All @@ -9,6 +11,16 @@ module.exports = {
});
},

deparse(parseTree) {
const msg = pg_query.ParseResult.fromObject(parseTree);
const data = pg_query.ParseResult.encode(msg).finish();
return new Promise((resolve, reject) => {
PgQuery.deparseAsync(data, (err, result) => {
err ? reject(err) : resolve(result);
});
});
},

parsePlPgSQL(query) {
return new Promise((resolve, reject) => {
PgQuery.parsePlPgSQLAsync(query, (err, result) => {
Expand All @@ -21,6 +33,12 @@ module.exports = {
return JSON.parse(PgQuery.parseQuerySync(query));
},

deparseSync(parseTree) {
const msg = pg_query.ParseResult.fromObject(parseTree);
const data = pg_query.ParseResult.encode(msg).finish();
return PgQuery.deparseSync(data);
},

parsePlPgSQLSync(query) {
return JSON.parse(PgQuery.parsePlPgSQLSync(query));
},
Expand Down
Empty file added libpg_query/protobuf/.gitkeep
Empty file.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
},
"scripts": {
"proto": "node_modules/protobufjs-cli/bin/pbjs --keep-case -t static-module -o proto.js libpg_query/protobuf/pg_query.proto",
"clean": "rimraf build",
"configure": "node-pre-gyp configure",
"install": "node-pre-gyp install --fallback-to-build --loglevel verbose",
Expand All @@ -54,11 +55,13 @@
"chai": "^3.5.0",
"emnapi": "^0.43.1",
"lodash": "^4.17.15",
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"protobufjs-cli": "https://gitpkg.now.sh/michaelbgreen/protobuf.js/cli?6254efb1bd9263978e9a90da3b05c4ccbcd6564a"
},
"dependencies": {
"@emnapi/runtime": "^0.43.1",
"@mapbox/node-pre-gyp": "^1.0.8",
"protobufjs": "git+https://github.com/michaelbgreen/protobuf.js.git#c0b68ac5c8fbb1ded261c0924bbce0538370333e"
"node-addon-api": "^7.0.0",
"node-gyp": "^10.0.1"
},
Expand Down
Loading

0 comments on commit 9b6a671

Please sign in to comment.