Skip to content

Commit 4131887

Browse files
authored
Merge pull request #117 from launchql/16-latest
16 latest
2 parents a4751a2 + 701ca23 commit 4131887

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1189
-92
lines changed

.github/workflows/run-tests.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ jobs:
5757
POSTGRES_HOST: localhost
5858
POSTGRES_PORT: 5432
5959

60+
- name: pgsql-parser
61+
run: cd ./packages/pgsql-parser && yarn test
62+
env:
63+
POSTGRES_USER: postgres
64+
POSTGRES_PASSWORD: password
65+
POSTGRES_HOST: localhost
66+
POSTGRES_PORT: 5432
67+
6068
- name: deparser
6169
run: cd ./packages/deparser && yarn test
6270
env:
@@ -65,6 +73,9 @@ jobs:
6573
POSTGRES_HOST: localhost
6674
POSTGRES_PORT: 5432
6775

76+
- name: pgsql-enums
77+
run: cd ./packages/pgsql-enums && yarn test
78+
6879
- name: enums
6980
run: cd ./packages/enums && yarn test
7081

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Our latest is built with `13-latest` branch from libpg_query
150150

151151
* [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
152152
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
153-
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums-json): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
153+
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
154154
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
155155
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
156156
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.

packages/deparser/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Our latest is built with `13-latest` branch from libpg_query
8989

9090
* [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
9191
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
92-
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums-json): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
92+
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
9393
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
9494
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
9595
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.

packages/enums/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ In this example, `A_Expr_Kind` is an enum imported from `@pgsql/enums`, and expr
5050

5151
* [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
5252
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
53-
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums-json): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
53+
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
5454
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
5555
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
5656
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.

packages/parser/README.md

+12-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pgsql-parser
1+
# @pgsql/parser
22

33
<p align="center" width="100%">
44
<img height="120" src="https://github.com/launchql/pgsql-parser/assets/545047/6440fa7d-918b-4a3b-8d1b-755d85de8bea" />
@@ -14,12 +14,14 @@
1414
<a href="https://www.npmjs.com/package/pgsql-parser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Fparser%2Fpackage.json"/></a>
1515
</p>
1616

17-
The real PostgreSQL parser for Node.js, `pgsql-parser` provides symmetric parsing and deparsing of SQL statements using the actual [PostgreSQL parser](https://github.com/pganalyze/libpg_query). It allows you to parse SQL queries into AST and modify or reconstruct SQL queries from the AST.
17+
**NOTE:** This version of `@pgsql/parser` only contains the parsing functionality, optimized for users who do not require deparsing capabilities. This version is specifically tailored for PostgreSQL 16.
18+
19+
The real PostgreSQL parser for Node.js, `@pgsql/parser` offers the ability to parse SQL statements into ASTs using the actual [PostgreSQL parser](https://github.com/pganalyze/libpg_query). It enables users to parse SQL queries into AST format for further manipulation and analysis. Deparsing functionality is (yet) not included in this package.
1820

1921
## Installation
2022

2123
```sh
22-
npm install pgsql-parser
24+
npm install @pgsql/parser
2325
```
2426

2527
## Key Features
@@ -33,61 +35,15 @@ npm install pgsql-parser
3335
Rewrite part of a SQL query:
3436

3537
```js
36-
import { parse, deparse } from 'pgsql-parser';
38+
import { parse } from '@pgsql/parser';
3739

3840
const stmts = parse('SELECT * FROM test_table');
39-
40-
// Assuming the structure of stmts is known and matches the expected type
41-
stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
42-
43-
console.log(deparse(stmts));
44-
45-
// SELECT * FROM "another_table"
46-
```
47-
48-
## Deparser Example
49-
50-
The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding the full parser's native dependencies. It's useful when only SQL string conversion from ASTs is needed, and is written in pure TypeScript for easy cross-environment deployment.
51-
52-
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/launchql/pgsql-parser/tree/main/packages/utils) to create an AST for `deparse`:
53-
54-
```ts
55-
import ast, { SelectStmt } from '@pgsql/utils';
56-
import { deparse } from 'pgsql-deparser';
57-
58-
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
59-
const stmt: SelectStmt = ast.selectStmt({
60-
targetList: [
61-
ast.resTarget({
62-
val: ast.columnRef({
63-
fields: [ast.aStar()]
64-
})
65-
})
66-
],
67-
fromClause: [
68-
ast.rangeVar({
69-
relname: 'some_table',
70-
inh: true,
71-
relpersistence: 'p'
72-
})
73-
],
74-
limitOption: 'LIMIT_OPTION_DEFAULT',
75-
op: 'SETOP_NONE'
76-
});
77-
78-
// Modify the AST if needed
79-
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
80-
81-
// Deparse the modified AST back to a SQL string
82-
console.log(deparse(stmts));
83-
84-
// Output: SELECT * FROM another_table
8541
```
8642

8743
## CLI
8844

8945
```
90-
npm install -g pgsql-parser
46+
npm install -g @pgsql/parser
9147
```
9248

9349
### usage
@@ -140,7 +96,10 @@ Our latest is built with `13-latest` branch from libpg_query
14096

14197
| PostgreSQL Major Version | libpg_query | Status | npm
14298
|--------------------------|-------------|---------------------|---------|
143-
| 13 | 13-latest | Active development | `latest`
99+
| 16 | 16-latest | Active development | `latest`
100+
| 15 | (n/a) | Not supported |
101+
| 14 | (n/a) | Not supported |
102+
| 13 | 13-latest | use `pgsql-parser` |
144103
| 12 | (n/a) | Not supported |
145104
| 11 | (n/a) | Not supported |
146105
| 10 | 10-latest | Not supported | `@1.3.1` ([tree](https://github.com/launchql/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
@@ -150,7 +109,7 @@ Our latest is built with `13-latest` branch from libpg_query
150109

151110
* [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
152111
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
153-
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums-json): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
112+
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
154113
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
155114
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
156115
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.

packages/parser/__tests__/__snapshots__/async.test.ts.snap

+9-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ exports[`works 1`] = `
4646
"fields": [
4747
{
4848
"String": {
49-
"str": "health_grade",
49+
"sval": "health_grade",
5050
},
5151
},
5252
],
@@ -57,17 +57,15 @@ exports[`works 1`] = `
5757
"name": [
5858
{
5959
"String": {
60-
"str": "=",
60+
"sval": "=",
6161
},
6262
},
6363
],
6464
"rexpr": {
6565
"A_Const": {
6666
"location": undefined,
67-
"val": {
68-
"String": {
69-
"str": "A",
70-
},
67+
"sval": {
68+
"sval": "A",
7169
},
7270
},
7371
},
@@ -81,7 +79,7 @@ exports[`works 1`] = `
8179
"fields": [
8280
{
8381
"String": {
84-
"str": "average_rating",
82+
"sval": "average_rating",
8583
},
8684
},
8785
],
@@ -92,18 +90,16 @@ exports[`works 1`] = `
9290
"name": [
9391
{
9492
"String": {
95-
"str": ">=",
93+
"sval": ">=",
9694
},
9795
},
9896
],
9997
"rexpr": {
10098
"A_Const": {
101-
"location": undefined,
102-
"val": {
103-
"Float": {
104-
"str": "4.5",
105-
},
99+
"fval": {
100+
"fval": "4.5",
106101
},
102+
"location": undefined,
107103
},
108104
},
109105
},

packages/parser/__tests__/__snapshots__/funcs.test.ts.snap

+16-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ exports[`works 1`] = `
1111
"PLpgSQL_stmt_assign": {
1212
"expr": {
1313
"PLpgSQL_expr": {
14-
"query": "SELECT ast.jsonb_set(result, '{IndexElem, name}', to_jsonb(v_name))",
14+
"parseMode": 3,
15+
"query": "result = ast.jsonb_set(result, '{IndexElem, name}', to_jsonb(v_name))",
1516
},
1617
},
1718
"lineno": 5,
@@ -22,7 +23,8 @@ exports[`works 1`] = `
2223
"PLpgSQL_stmt_assign": {
2324
"expr": {
2425
"PLpgSQL_expr": {
25-
"query": "SELECT ast.jsonb_set(result, '{IndexElem, ordering}', to_jsonb(v_ordering))",
26+
"parseMode": 3,
27+
"query": "result = ast.jsonb_set(result, '{IndexElem, ordering}', to_jsonb(v_ordering))",
2628
},
2729
},
2830
"lineno": 6,
@@ -33,7 +35,8 @@ exports[`works 1`] = `
3335
"PLpgSQL_stmt_assign": {
3436
"expr": {
3537
"PLpgSQL_expr": {
36-
"query": "SELECT ast.jsonb_set(result, '{IndexElem, nulls_ordering}', to_jsonb(v_nulls_ordering))",
38+
"parseMode": 3,
39+
"query": "result = ast.jsonb_set(result, '{IndexElem, nulls_ordering}', to_jsonb(v_nulls_ordering))",
3740
},
3841
},
3942
"lineno": 7,
@@ -44,7 +47,8 @@ exports[`works 1`] = `
4447
"PLpgSQL_stmt_assign": {
4548
"expr": {
4649
"PLpgSQL_expr": {
47-
"query": "SELECT ast.jsonb_set(result, '{IndexElem, expr}', v_expr)",
50+
"parseMode": 3,
51+
"query": "result = ast.jsonb_set(result, '{IndexElem, expr}', v_expr)",
4852
},
4953
},
5054
"lineno": 8,
@@ -55,7 +59,8 @@ exports[`works 1`] = `
5559
"PLpgSQL_stmt_assign": {
5660
"expr": {
5761
"PLpgSQL_expr": {
58-
"query": "SELECT ast.jsonb_set(result, '{IndexElem, opclass}', v_opclass)",
62+
"parseMode": 3,
63+
"query": "result = ast.jsonb_set(result, '{IndexElem, opclass}', v_opclass)",
5964
},
6065
},
6166
"lineno": 9,
@@ -66,7 +71,8 @@ exports[`works 1`] = `
6671
"PLpgSQL_stmt_assign": {
6772
"expr": {
6873
"PLpgSQL_expr": {
69-
"query": "SELECT ast.jsonb_set(result, '{IndexElem, collation}', v_collation)",
74+
"parseMode": 3,
75+
"query": "result = ast.jsonb_set(result, '{IndexElem, collation}', v_collation)",
7076
},
7177
},
7278
"lineno": 10,
@@ -77,7 +83,8 @@ exports[`works 1`] = `
7783
"PLpgSQL_stmt_return": {
7884
"expr": {
7985
"PLpgSQL_expr": {
80-
"query": "SELECT result",
86+
"parseMode": 2,
87+
"query": "result",
8188
},
8289
},
8390
"lineno": 11,
@@ -167,7 +174,8 @@ exports[`works 1`] = `
167174
},
168175
"default_val": {
169176
"PLpgSQL_expr": {
170-
"query": "SELECT '{"IndexElem":{}}'::jsonb",
177+
"parseMode": 2,
178+
"query": "'{"IndexElem":{}}'::jsonb",
171179
},
172180
},
173181
"lineno": 3,

packages/parser/package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "pgsql-parser",
3-
"version": "13.15.0",
2+
"name": "@pgsql/parser",
3+
"version": "16.0.0-testing",
44
"description": "The real PostgreSQL query parser",
55
"author": "Dan Lynch <[email protected]>",
66
"homepage": "https://github.com/launchql/pgsql-parser",
@@ -84,9 +84,8 @@
8484
"typescript": "^5.0.4"
8585
},
8686
"dependencies": {
87-
"libpg-query": "13.3.2",
87+
"libpg-query": "16.1.0",
8888
"minimist": "^1.2.6",
89-
"pgsql-deparser": "^13.14.0",
90-
"pgsql-enums": "^13.9.0"
89+
"@pgsql/types": "13.8.0"
9190
}
9291
}

packages/parser/src/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Deparser, deparse } from 'pgsql-deparser';
1+
import { Node } from '@pgsql/types';
2+
23
import {
34
parseQuery,
45
parseQuerySync,
@@ -15,7 +16,7 @@ function mapStmt({ stmt, stmt_len, stmt_location }) {
1516
};
1617
}
1718

18-
export const parse = (sql) => {
19+
export const parse = (sql): Node => {
1920
if (!sql) throw new Error('no SQL provided to parser');
2021
const result = parseQuerySync(sql);
2122
return result.stmts.map(mapStmt);
@@ -27,4 +28,4 @@ export const parseAsync = async (sql) => {
2728
return result.stmts.map(mapStmt);
2829
};
2930

30-
export { deparse, Deparser, parseFunction };
31+
export { parseFunction };
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)