Skip to content
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

Wasm feature #48

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0aaa040
latest v13 commit
pyramation Dec 22, 2022
676d1ab
pkg
pyramation Dec 22, 2022
5c2fe84
commits for 14
pyramation Dec 22, 2022
19c763f
v14
pyramation Dec 22, 2022
97c8066
v15
pyramation Dec 22, 2022
784b502
v15
pyramation Dec 22, 2022
2c1c7c5
v15
pyramation Dec 22, 2022
0db8bf0
v15
pyramation Dec 22, 2022
1eb3b60
readme
pyramation Dec 22, 2022
9c0101d
v15
pyramation Dec 22, 2022
7eaac50
feat: wasm support using emnapi + emscripten
gregnr Nov 3, 2023
7597744
feat: wasm webpack test
gregnr Nov 3, 2023
dc5904a
chore(wasm): better source caching and make commands
gregnr Nov 3, 2023
97f2f63
feat(makefile): support multiple platform-arch targets
gregnr Nov 3, 2023
f6ce4e2
feat(wasm): convert to esm + separate wasm file
gregnr Nov 3, 2023
b810a52
fix(wasm): yarn clean scripts
gregnr Nov 3, 2023
9639d9b
chore: remove packageManager from package.json
gregnr Nov 3, 2023
fec1ae2
fix: published files and conditional exports
gregnr Nov 6, 2023
26fd7c9
fix: add .node extension to native import
gregnr Nov 6, 2023
649d850
fix: support c++ exceptions
gregnr Nov 7, 2023
8b5a22e
feat: better makefile dependency management
gregnr Feb 20, 2024
d8038de
feat(wasm): lock libpg_query to specific version tag
gregnr Feb 21, 2024
256f406
refactor(wasm): makefile build/clean commands
gregnr Feb 21, 2024
0f42732
refactor(wasm): makefile command location
gregnr Feb 21, 2024
d22bece
readme, addon
pyramation Feb 21, 2024
c8f42ee
readme
pyramation Feb 21, 2024
dd3398f
readme
pyramation Feb 21, 2024
b22906b
udpate version
pyramation Feb 21, 2024
f351a42
clean
pyramation Feb 21, 2024
1c8617a
Merge pull request #34 from gregnr/feat/wasm
pyramation Feb 21, 2024
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
Expand Up @@ -4,3 +4,6 @@ libs/
npm-debug.log
libpg_query/**/*.a
libpg_query/**/*.h
wasm/libpg-query.js
*.wasm
.cache
91 changes: 91 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
WASM_OUT_DIR := wasm
WASM_OUT_NAME := libpg-query
WASM_MODULE_NAME := PgQueryModule
LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git
LIBPG_QUERY_TAG := 15-4.2.4
CACHE_DIR := .cache

OS ?= $(shell uname -s)
ARCH ?= $(shell uname -m)

ifdef EMSCRIPTEN
PLATFORM := emscripten
else ifeq ($(OS),Darwin)
PLATFORM := darwin
else ifeq ($(OS),Linux)
PLATFORM := linux
else
$(error Unsupported platform: $(OS))
endif

ifdef EMSCRIPTEN
ARCH := wasm
endif

PLATFORM_ARCH := $(PLATFORM)-$(ARCH)
SRC_FILES := $(wildcard src/*.cc)
LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG)
LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a
LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h
CXXFLAGS := -O3

ifdef EMSCRIPTEN
OUT_FILES := $(foreach EXT,.js .wasm,$(WASM_OUT_DIR)/$(WASM_OUT_NAME)$(EXT))
else
OUT_FILES := build/Release/queryparser.node $(wildcard build/*)
endif

# Clone libpg_query source (lives in CACHE_DIR)
$(LIBPG_QUERY_DIR):
mkdir -p $(CACHE_DIR)
git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR)

$(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR)

# Build libpg_query
$(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR)
cd $(LIBPG_QUERY_DIR); $(MAKE) build

# Build libpg-query-node (based on platform)
$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES)
ifdef EMSCRIPTEN
@ $(CXX) \
$(CXXFLAGS) \
-DNAPI_HAS_THREADS \
-I$(LIBPG_QUERY_DIR) \
-I./node_modules/emnapi/include \
-I./node_modules/node-addon-api \
-L./node_modules/emnapi/lib/wasm32-emscripten \
-L$(LIBPG_QUERY_DIR) \
--js-library=./node_modules/emnapi/dist/library_napi.js \
-sEXPORTED_FUNCTIONS="['_malloc','_free','_napi_register_wasm_v1','_node_api_module_get_api_version_v1']" \
-sEXPORT_NAME="$(WASM_MODULE_NAME)" \
-sENVIRONMENT="web" \
-sMODULARIZE=1 \
-sEXPORT_ES6=1 \
-fexceptions \
-lpg_query \
-lemnapi-basic \
-o $@ \
$(SRC_FILES)
else
# if not wasm, defer to node-gyp
yarn rebuild
endif

# Commands
build: $(OUT_FILES)

build-cache: $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER)

rebuild: clean build

rebuild-cache: clean-cache build-cache

clean:
-@ rm -r $(OUT_FILES) > /dev/null 2>&1

clean-cache:
-@ rm -rf $(LIBPG_QUERY_DIR)

.PHONY: build build-cache rebuild rebuild-cache clean clean-cache
69 changes: 54 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# libpg-query

<p align="center" width="100%">
<img src="https://github.com/launchql/libpg-query-node/assets/545047/7f7053b8-9f52-4f4e-95b0-201d656902ed" alt="webincubator" width="100">
</p>

<p align="center" width="100%">
<a href="https://www.npmjs.com/package/libpg-query"><img height="20" src="https://img.shields.io/npm/dt/libpg-query"></a>
<a href="https://www.npmjs.com/package/libpg-query"><img height="20" src="https://img.shields.io/npm/dw/libpg-query"/></a>
<a href="https://github.com/launchql/libpg-query/blob/main/LICENSE-MIT"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
<a href="https://www.npmjs.com/package/libpg-query"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/libpg-query-node"/></a>
</p>

The real PostgreSQL parser, exposed for nodejs.

Primarily used for the node.js parser and deparser [pgsql-parser](https://github.com/pyramation/pgsql-parser)

## Building a binary distribution

- Install requirements (`npm i`)
- `npx node-pre-gyp rebuild package`
- With appropriate AWS credentials configured, `npx node-pre-gyp publish`
## Table of Contents

Or you can run the scripts
1. [Installation](#installation)
2. [Example](#example)
5. [Documentation](#documentation)
3. [Versions](#versions)
4. [Building a binary distribution](#building-a-binary-distribution)
6. [Related Projects](#related-projects)
7. [Credit](#credit)

```
npm run binary:build
npm run binary:publish
```

## Installation

```sh
npm install libpg-query
```

## Example

```js
const parser = require('libpg-query');
parser.parseQuery('select 1').then(console.log);
```

### Documentation

### `query.parseQuery(sql)`/`parseQuerySync`
Expand All @@ -35,14 +52,36 @@ The return value is an array, as multiple queries may be provided in a single st

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
## Versions

```js
const parser = require('libpg-query');
parser.parseQuery('select 1').then(console.log);
Our latest is built with `16-latest` branch from libpg_query


| PG Major Version | libpg_query | Branch | npm
|--------------------------|-------------|------------------------------------------------------------------------------------------------|---------|
| 16 | 16-latest | [`16-latest`](https://github.com/launchql/libpg-query-node/tree/16-latest) | [`[email protected]`](https://www.npmjs.com/package/libpg-query/v/latest)
| 15 | 15-latest | [`15-latest`](https://github.com/launchql/libpg-query-node/tree/15-latest) | [`[email protected]`](https://www.npmjs.com/package/libpg-query/v/15.0.3)
| 14 | 14-latest | [`14-latest`](https://github.com/launchql/libpg-query-node/tree/14-latest) | [`[email protected]`](https://www.npmjs.com/package/libpg-query/v/14.0.0)
| 13 | 13-latest | [`13-latest`](https://github.com/launchql/libpg-query-node/tree/13-latest) | [`[email protected]`](https://www.npmjs.com/package/libpg-query/v/13.3.1)
| 12 | (n/a) | |
| 11 | (n/a) | |
| 10 | 10-latest | | `@1.3.1` ([tree](https://github.com/pyramation/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |


## Building a binary distribution

- Install requirements (`npm i`)
- `npx node-pre-gyp rebuild package`
- With appropriate AWS credentials configured, `npx node-pre-gyp publish`

Or you can run the scripts

```
npm run binary:build
npm run binary:publish
```

## Related
## Related Projects

* [libpg_query](https://github.com/pganalyze/libpg_query)
* [pgsql-parser](https://github.com/pyramation/pgsql-parser)
Expand All @@ -55,6 +94,6 @@ This is based on the output of [libpg_query](https://github.com/pganalyze/libpg_

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

Additional thanks for node binding [Ethan Resnick](github.com/ethanresnick).
Additional thanks for node binding [Ethan Resnick](https://github.com/ethanresnick).

Original [Code](https://github.com/zhm/node-pg-query-native) and [License](https://github.com/zhm/node-pg-query-native/blob/master/LICENSE.md)
73 changes: 30 additions & 43 deletions docker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,42 @@ build the binary on mac
yarn
```

# 1 build the docker image

run `docker-compose build`

# 2 start the server
# 1 build and run the docker image

```sh
docker run -d \
-it \
--name build_pg_query \
docker run \
--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
--rm -it \
$(docker build -q --file docker/Dockerfile .)
```

# publishing from inside
# build inside the docker image

```sh
mkdir git && cd git && git clone https://github.com/pyramation/libpg-query-node
branch=15-latest
mkdir git_clone_dir && cd git_clone_dir
git clone -b $branch --single-branch https://github.com/launchql/libpg-query-node
cd libpg-query-node/
yarn

# get the OSX version you build before...
cp /pg_query/osx/libpg_query.a ./libpg_query/osx/
# get the linux version and sent it to docker host
cp ./libpg_query/linux/libpg_query.a /pg_query/linux/
```

# add your creds real quick... (look in your ~/.npmrc)
vi .npmrc
vi package.json
Now on the docker host machine, you should be able to publish:

```sh
npm publish
```

# building libpg_query

not necessary, but for fun:
to build manually using `libpg_query`

```sh
mkdir git
cd git/
git clone git clone [email protected]:pganalyze/libpg_query.git
git clone [email protected]:pganalyze/libpg_query.git
git clone https://github.com/pganalyze/libpg_query.git
branch=15-latest
mkdir git_clone_dir && cd git_clone_dir
git clone -b $branch --single-branch https://github.com/pganalyze/libpg_query.git
cd libpg_query/
make
cp libpg_query.a /pg_query/linux/
Expand All @@ -62,18 +50,17 @@ 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
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 ;)
make sure you grab the `pg_query.h` if you don't have it ;)
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const PgQuery = require('./build/Release/queryparser');
const PgQuery = require('./build/Release/queryparser.node');

module.exports = {
parseQuery(query) {
Expand Down Expand Up @@ -26,14 +26,14 @@ module.exports = {
},

fingerprint(query) {
return new Promise((resolve, reject) =>{
return new Promise((resolve, reject) => {
PgQuery.fingerprintAsync(query, (err, result) => {
err ? reject(err) : resolve(result);
})
});
});
},

fingerprintSync(query) {
return PgQuery.fingerprintSync(query);
}
},
};
Loading