Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0b09125
remove bindings and node-gyp from package.json, when installing alway…
matus123 Jun 6, 2016
b68e5b9
updated travis badge, updated readme
matus123 Jun 6, 2016
0727522
preinstall changed to postinstall
matus123 Jun 6, 2016
2894741
changed to preinstall
matus123 Jun 7, 2016
9824673
run node-gyp in postinstall
matus123 Jun 7, 2016
8232d9a
Merge remote-tracking branch 'upstream/master'
matus123 Sep 29, 2016
94e7bb9
added async pg parsing, remove static windows,osx lib, rewrite to v8
matus123 Sep 29, 2016
940c436
fix travis
matus123 Sep 29, 2016
5318de1
fix async tests
matus123 Sep 29, 2016
7424d87
fix buildScript, remove nan from V8 version
matus123 Jun 4, 2017
3e75119
Update .travis.yml
matus123 Jun 4, 2017
f0aa7f0
add osx build
matus123 May 24, 2018
7416266
fix osx build
matus123 May 24, 2018
c53ebbc
Merge remote-tracking branch 'matus123/master'
ethanresnick Jul 22, 2019
7655511
refactor: api changes
ethanresnick Jul 22, 2019
38381f9
Fix squash
ethanresnick Jul 22, 2019
f15f094
feat: add plpgqsql parsing
ethanresnick Jul 23, 2019
fc6fa68
refactor: rewrite with node-addon-api
ethanresnick Jul 23, 2019
0e01fc8
republish
ethanresnick Jul 23, 2019
92f9981
Merge remote-tracking branch 'origin/master'
ethanresnick Jul 23, 2019
f8a0f06
docs: new features, package rename
ethanresnick Jul 23, 2019
3085575
fix: build script errors, update pg parse
ethanresnick Jul 23, 2019
c6e688f
feat: add typings
ethanresnick Jul 24, 2019
71a3f8f
Bump version to republish
ethanresnick Jul 26, 2019
5a6241d
Rename package (to move to @sqlutils org)
ethanresnick Jul 30, 2019
b4150b9
Update README.md
ethanresnick Jul 30, 2019
62f0b92
linux build
pyramation Jul 3, 2020
b5b559b
build info
pyramation Jul 3, 2020
df04a7b
bump
pyramation Jul 3, 2020
0134c4b
buildAddon tmp dirs
pyramation Jul 3, 2020
6a0cefb
buildAddon
pyramation Jul 3, 2020
dd7ffb2
readme
pyramation Jul 3, 2020
3c6147d
update
pyramation Jul 9, 2020
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules/
build/
libs/
npm-debug.log
libpg_query/**/*.a
libpg_query/**/*.h
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: node_js
node_js:
- "0.12"
- "4"
- "5"
- "8"
addons:
apt:
sources:
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# 1.2.1
* Rename package

# 1.2.0
* Add TS typings

# 1.1.2 (07/23/19)
* Fix build script

# 1.1.0 (Republish, 07/23/19)
* Rewrite to use Node-Addons-API (C++ wrapper for the stable N-API)
* Support PL/PGSql Parsing
* Support async parsing

## 0.0.5 (November 19, 2015)
* Update libpg_query to include latest A_CONST fixes

Expand Down
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
# pg-query-native [![Build Status](https://travis-ci.org/zhm/node-pg-query-native.svg?branch=master)](https://travis-ci.org/zhm/node-pg-query-native)
# @sqlutils/parse

The real PostgreSQL parser for nodejs.
The real PostgreSQL parser, exposed for nodejs.

This is based on the output of [libpg_query](https://github.com/lfittl/libpg_query). This wraps the static library output and links it into a node module for use in js.

All credit for the hard problems goes to [Lukas Fittl](https://github.com/lfittl).

## Requirements

Install node-gyp globally

```sh
npm install node-gyp -g
```

## Installation

```sh
npm install pg-query-native-latest
npm install @sqlutils/parse
```

### Documentation

### `query.parse(query)`
### `query.parseQuery(sql)`/`parseQuerySync`

Parses the query and returns the parse tree.
Parses the sql and returns a Promise for the parse tree (or returns the parse tree directly in the sync version). May reject with/throw a parse error.

### Parameters
The return value is an array, as multiple queries may be provided in a single string (semicolon-delimited, as Postgres expects).

| parameter | type | description |
| -------------------- | ------------------ | --------------------------------------------------------- |
| `query` | String | SQL query |
### `query.parsePlPgSQL(funcsSql)`/`query.parsePlPgSQLSync(funcsSql)`

Returns an object in the format:

```
{ query: <query|Object>,
error: { message: <message|String>,
fileName: <fileName|String>,
lineNumber: <line|Number>,
cursorPosition: <cursor|Number> }
```
Parses the contents of a PL/PGSql function, from a `CREATE FUNCTION` declaration, and returns a Promise for the parse tree (or returns the parse tree directly in the sync version). May reject with/throw a parse error.

## Example

```js
var parse = require('pg-query-native').parse;

console.log(parse('select 1').query);
const parser = require('@sqlutils/parse');
parser.parseQuery('select 1').then(console.log);
```

## Related
Expand Down
43 changes: 31 additions & 12 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
{
"targets": [
{
"target_name": "pg-query",
"sources": [ "pg-query.cc", "functions.cc" ],
"include_dirs" : [
"<!(node -e \"require('nan')\")",
"libpg_query/include"
],
"target_name": "queryparser",
"sources": [
"src/addon.cc",
"src/helpers.cc",
"src/sync.cc",
"src/async.cc"
],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'include_dirs': [
"libpg_query/include",
"<!@(node -p \"require('node-addon-api').include\")"
],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'conditions': [
['OS=="linux"', {
"libraries": [ "-L<!(pwd)/libpg_query/linux", "-lpg_query" ]
} ],
"libraries": [ "-L<!(pwd)/libpg_query/linux", "-lpg_query" ]
}],
['OS=="mac"', {
"libraries": [ "-L<!(pwd)/libpg_query/osx", "-lpg_query" ]
} ],
['OS=="win"', { }]
"libraries": [ "-L<!(pwd)/libpg_query/osx", "-lpg_query" ],
"xcode_settings": {
"CLANG_CXX_LIBRARY": "libc++",
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'MACOSX_DEPLOYMENT_TARGET': '10.7'
}
}],
['OS=="win"', {
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": 1
}
}
}]
]
}
],
]
}
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '2'
services:
pg_query:
build: ./docker
image: pyramation/libpg_query
volumes:
- ./libpg_query:/pg_query
8 changes: 8 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:12.18.2-alpine3.11
RUN apk --no-cache add git
RUN apk add --no-cache bash
RUN apk add --no-cache make
RUN apk add --update alpine-sdk
RUN apk --no-cache add python

ENTRYPOINT ["/bin/bash"]
77 changes: 77 additions & 0 deletions docker/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 0 build on mac first

build the binary on mac

# 1 build the docker image

run `docker-compose build`

# 2 start the server

```sh
docker run -d \
-it \
--name build_pg_query \
--mount type=bind,source="$(pwd)"/libpg_query,target=/pg_query \
pyramation/libpg_query
```

# 3 jump inside

`ssh` into the box

```sh
docker exec -it build_pg_query /bin/bash
```

# publishing from inside

```sh
mkdir git
cd git
git clone https://github.com/pyramation/pg-plpgsql-query-native
cd pg-plpgsql-query-native/
yarn

# get the OSX version you build before...
cp /pg_query/osx/libpg_query.a ./libpg_query/osx/

# add your creds real quick... (look in your ~/.npmrc)
vi .npmrc
vi package.json
npm publish
```

# building libpg_query

not necessary, but for fun:

```sh
mkdir git
cd git/
git clone git clone [email protected]:lfittl/libpg_query.git
git clone [email protected]:lfittl/libpg_query.git
git clone https://github.com/lfittl/libpg_query.git
cd libpg_query/
make
cp libpg_query.a /pg_query/linux/
```

you should see `.a` files now :)

```
libpg_query/
libpg_query//osx
libpg_query//osx/libpg_query.a
libpg_query//osx/.gitkeep
libpg_query//include
libpg_query//include/.gitkeep
libpg_query//include/pg_query.h
libpg_query//linux
libpg_query//linux/libpg_query.a
libpg_query//linux/.gitkeep
libpg_query//windows
libpg_query//windows/.gitkeep
```

make sure you grab the `pg_query.h` if you don't have it ;)
66 changes: 0 additions & 66 deletions functions.cc

This file was deleted.

9 changes: 0 additions & 9 deletions functions.h

This file was deleted.

4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function parseQuery(sql: string): Promise<any>;
export function parsePlPgSQL(funcsSql: string): Promise<any>;
export function parseQuerySync(sql: string): any;
export function parsePlPgSQLSync(funcsSql: string): any;
38 changes: 20 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
var PgQuery = require('bindings')('pg-query');
const PgQuery = require('./build/Release/queryparser');

module.exports = {
parse: function(query) {
var result = PgQuery.parse(query);
parseQuery(query) {
return new Promise((resolve, reject) => {
PgQuery.parseQueryAsync(query, (err, result) => {
err ? reject(err) : resolve(JSON.parse(result));
});
});
},

if (result.query) {
result.query = JSON.parse(result.query);
}
parsePlPgSQL(query) {
return new Promise((resolve, reject) => {
PgQuery.parsePlPgSQLAsync(query, (err, result) => {
err ? reject(err) : resolve(JSON.parse(result));
});
});
},

if (result.error) {
var err = new Error(result.error.message);
parseQuerySync(query) {
return JSON.parse(PgQuery.parseQuerySync(query));
},

err.fileName = result.error.fileName;
err.lineNumber = result.error.lineNumber;
err.cursorPosition = result.error.cursorPosition;
err.functionName = result.error.functionName;
err.context = result.error.context;

result.error = err;
}

return result;
parsePlPgSQLSync(query) {
return JSON.parse(PgQuery.parsePlPgSQLSync(query));
}
};
Empty file added libpg_query/include/.gitkeep
Empty file.
Loading