pgsql-deparser 13.6.3 → 13.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -74
- package/main/deparser.js +3332 -4218
- package/main/index.js +9 -18
- package/main/utils/index.js +84 -104
- package/module/deparser.js +3456 -4005
- package/module/index.js +1 -1
- package/module/utils/index.js +80 -80
- package/package.json +42 -31
- package/src/deparser.ts +4122 -0
- package/src/index.ts +3 -0
- package/src/utils/index.ts +92 -0
- package/types/deparser.d.ts +118 -0
- package/types/index.d.ts +3 -0
- package/types/utils/index.d.ts +4 -0
- package/CHANGELOG.md +0 -483
package/README.md
CHANGED
|
@@ -8,102 +8,52 @@
|
|
|
8
8
|
<a href="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml">
|
|
9
9
|
<img height="20" src="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
10
|
</a>
|
|
11
|
-
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/npm/dw/pgsql-deparser"/></a>
|
|
12
11
|
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/npm/dt/pgsql-deparser"></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/npm/dw/pgsql-deparser"/></a>
|
|
13
13
|
<a href="https://github.com/launchql/pgsql-parser/blob/main/LICENSE-MIT"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
|
|
14
|
-
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-
|
|
14
|
+
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-deparser?filename=packages%2Fparser%2Fpackage.json"/></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
The main functionality provided by this module is deparsing, which PostgreSQL does not have internally.
|
|
17
|
+
`pgsql-deparser` is a streamlined tool designed to convert PostgreSQL Abstract Syntax Trees (AST) back into SQL queries. It is a companion module to [`pgsql-parser`](https://github.com/launchql/pgsql-parser), which is capable of both parsing SQL queries into ASTs and deparsing these ASTs back into SQL. However, unlike `pgsql-parser`, which incorporates the full PostgreSQL parser, `pgsql-deparser` focuses solely on the deparser functionality. This makes it an excellent choice for scenarios where only AST-to-SQL conversion is needed, avoiding the extra overhead associated with the full parsing capabilities.
|
|
20
18
|
|
|
21
19
|
## Installation
|
|
22
20
|
|
|
23
21
|
```sh
|
|
24
|
-
npm install pgsql-
|
|
22
|
+
npm install pgsql-deparser
|
|
25
23
|
```
|
|
26
24
|
|
|
27
|
-
##
|
|
28
|
-
|
|
29
|
-
The deparser can be used separately, which removes many deps required for the parser:
|
|
30
|
-
|
|
31
|
-
```js
|
|
32
|
-
const { parse } = require('pgsql-parser');
|
|
33
|
-
const { deparse } = require('pgsql-deparser');
|
|
34
|
-
|
|
35
|
-
const stmts = parse('SELECT * FROM test_table');
|
|
36
|
-
|
|
37
|
-
stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
25
|
+
## Key Features
|
|
38
26
|
|
|
39
|
-
|
|
27
|
+
- **Lightweight and Pure TypeScript**: Built entirely in TypeScript, `pgsql-deparser` is lightweight and doesn't rely on native dependencies, facilitating easier integration and deployment across various environments.
|
|
28
|
+
- **Focused Functionality**: By concentrating on decompiling ASTs to SQL, `pgsql-deparser` offers a specialized, efficient solution for those who need to generate SQL statements from ASTs without the full parsing mechanism.
|
|
29
|
+
- **Compatibility**: Ideal for use cases where the AST is already obtained from another source or process, allowing for seamless SQL generation from AST representations.
|
|
40
30
|
|
|
41
|
-
|
|
42
|
-
```
|
|
31
|
+
## Deparser Example
|
|
43
32
|
|
|
44
|
-
|
|
33
|
+
The deparser functionality is provided as a standalone module, enabling you to serialize AST (Abstract Syntax Tree) objects back to SQL statements without the need for the full parsing engine. This separation is particularly beneficial for environments where the native dependencies of the full parser are problematic or unnecessary. For instance, if you already have an AST representation of your SQL query and merely need to convert it back to a SQL string, you can use the pgsql-deparser module directly. This module is implemented in pure TypeScript, avoiding the need for native bindings and thereby simplifying deployment and compatibility across different environments.
|
|
45
34
|
|
|
46
|
-
|
|
35
|
+
Here's how you can use the deparser in your TypeScript code:
|
|
47
36
|
|
|
48
|
-
```
|
|
49
|
-
|
|
37
|
+
```ts
|
|
38
|
+
import { deparse } from 'pgsql-deparser';
|
|
50
39
|
|
|
51
|
-
|
|
40
|
+
// Assuming `stmts` is an AST object for the query 'SELECT * FROM test_table'
|
|
41
|
+
// This could have been obtained from any source, not necessarily the pgsql-parser
|
|
42
|
+
const stmts = getAstFromSomewhere();
|
|
52
43
|
|
|
44
|
+
// Modify the AST as needed
|
|
53
45
|
stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
54
46
|
|
|
47
|
+
// Deparse the modified AST back to a SQL string
|
|
55
48
|
console.log(deparse(stmts));
|
|
56
49
|
|
|
57
|
-
// SELECT * FROM "another_table"
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## CLI
|
|
61
|
-
|
|
62
|
-
```
|
|
63
|
-
npm install -g pgsql-parser
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### usage
|
|
67
|
-
|
|
68
|
-
```sh
|
|
69
|
-
pgsql-parser <sqlfile>
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Documentation
|
|
73
|
-
|
|
74
|
-
### `parser.parse(sql)`
|
|
75
|
-
|
|
76
|
-
Parses the query and returns a parse object.
|
|
77
|
-
|
|
78
|
-
### Parameters
|
|
79
|
-
|
|
80
|
-
| parameter | type | description |
|
|
81
|
-
| -------------------- | ------------------ | --------------------------------------------------------- |
|
|
82
|
-
| `query` | String | SQL query |
|
|
83
|
-
|
|
84
|
-
Returns an object in the format:
|
|
50
|
+
// Output: SELECT * FROM "another_table"
|
|
85
51
|
|
|
86
52
|
```
|
|
87
|
-
{ query: <query|Object>,
|
|
88
|
-
error: { message: <message|String>,
|
|
89
|
-
fileName: <fileName|String>,
|
|
90
|
-
lineNumber: <line|Number>,
|
|
91
|
-
cursorPosition: <cursor|Number> }
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### `parser.deparse(query)`
|
|
95
|
-
|
|
96
|
-
Deparses the query tree and returns a formatted SQL statement. This function takes as input a query AST
|
|
97
|
-
in the same format as the `query` property of on the result of the `parse` method. This is the primary
|
|
98
|
-
functionality of this module.
|
|
99
53
|
|
|
100
|
-
|
|
54
|
+
## Why Use `pgsql-deparser`?
|
|
101
55
|
|
|
102
|
-
|
|
103
|
-
| -------------------- | ------------------ | --------------------------------------------------------- |
|
|
104
|
-
| `query` | Object | Query tree obtained from `parse` |
|
|
105
|
-
|
|
106
|
-
Returns a normalized formatted SQL string.
|
|
56
|
+
`pgsql-deparser` is particularly useful in development environments where native dependencies are problematic or in applications where only the deparser functionality is required. Its independence from the full `pgsql-parser` package allows for more focused and lightweight SQL generation tasks.
|
|
107
57
|
|
|
108
58
|
## Versions
|
|
109
59
|
|
|
@@ -118,18 +68,34 @@ Our latest is built with `13-latest` branch from libpg_query
|
|
|
118
68
|
| 11 | (n/a) | Not supported |
|
|
119
69
|
| 10 | 10-latest | Not supported | `@1.3.1` ([tree](https://github.com/launchql/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
|
|
120
70
|
|
|
71
|
+
|
|
121
72
|
## Related
|
|
122
73
|
|
|
123
|
-
* [
|
|
74
|
+
* [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.
|
|
75
|
+
* [pgsql-deparser](https://github.com/launchql/pgsql-deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
|
|
76
|
+
* [pgsql-enums](https://github.com/launchql/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`
|
|
77
|
+
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/master/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
|
|
78
|
+
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/master/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
|
|
79
|
+
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/master/packages/utils): A utility library for working with PostgreSQL AST node enumerations in a type-safe way, easing enum name and value conversions.
|
|
80
|
+
* [pg-proto-parser](https://github.com/launchql/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
81
|
+
* [libpg-query-node](https://github.com/launchql/libpg-query-node): The real PostgreSQL parser exposed for Node.js, used primarily in `pgsql-parser` for parsing and deparsing SQL queries.
|
|
82
|
+
|
|
83
|
+
Thanks @lfittl for building the core `libpg_query` suite:
|
|
84
|
+
|
|
124
85
|
* [libpg_query](https://github.com/pganalyze/libpg_query)
|
|
125
86
|
* [pg_query](https://github.com/lfittl/pg_query)
|
|
126
87
|
* [pg_query.go](https://github.com/lfittl/pg_query.go)
|
|
127
88
|
|
|
128
89
|
## Credits
|
|
129
90
|
|
|
91
|
+
Thanks to @zhm for the OG parser that started it all:
|
|
92
|
+
|
|
130
93
|
* [pg-query-parser](https://github.com/zhm/pg-query-parser)
|
|
131
94
|
* [pg-query-native](https://github.com/zhm/node-pg-query-native)
|
|
95
|
+
* [Original LICENSE](https://github.com/zhm/pg-query-parser/blob/master/LICENSE.md)
|
|
96
|
+
|
|
97
|
+
## Disclaimer
|
|
132
98
|
|
|
133
|
-
|
|
99
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
134
100
|
|
|
135
|
-
|
|
101
|
+
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|