Skip to content

Commit 001ad1f

Browse files
committed
@pgsql/parser for PG16 — no deparse yet supported
1 parent a4751a2 commit 001ad1f

Some content is hidden

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

59 files changed

+1178
-92
lines changed

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 is a version of `pgsql-parser` that only contains the parser, for those who don't need deparsing. This is for PG16 only.
18+
19+
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.
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.

packages/enums-json/README.md packages/pgsql-enums/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ console.log(getConstraintFromConstrType('CONSTR_CHECK')); // Outputs 'CHECK'
110110

111111
* [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.
112112
* [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`.
113-
* [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`
113+
* [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`
114114
* [@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.
115115
* [@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.
116116
* [@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.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/pgsql-parser/.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

packages/pgsql-parser/.eslintignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
dist/
3+
main/
4+
module/
5+
coverage/

0 commit comments

Comments
 (0)