pgsql-parser 13.7.3 → 13.8.1
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 +39 -13
- package/main/cli.js +21 -27
- package/main/index.js +27 -96
- package/main/utils/index.js +84 -104
- package/module/cli.js +12 -15
- package/module/index.js +19 -24
- package/module/utils/index.js +80 -80
- package/package.json +43 -31
- package/src/cli.js +25 -0
- package/src/index.ts +30 -0
- package/src/utils/index.ts +92 -0
- package/types/cli.d.ts +2 -0
- package/types/index.d.ts +5 -0
- package/types/utils/index.d.ts +4 -0
- package/CHANGELOG.md +0 -488
package/README.md
CHANGED
|
@@ -14,9 +14,7 @@
|
|
|
14
14
|
<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>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
The real PostgreSQL parser for
|
|
18
|
-
|
|
19
|
-
The main functionality provided by this module is deparsing, which PostgreSQL does not have internally.
|
|
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.
|
|
20
18
|
|
|
21
19
|
## Installation
|
|
22
20
|
|
|
@@ -24,15 +22,22 @@ The main functionality provided by this module is deparsing, which PostgreSQL do
|
|
|
24
22
|
npm install pgsql-parser
|
|
25
23
|
```
|
|
26
24
|
|
|
25
|
+
## Key Features
|
|
26
|
+
|
|
27
|
+
- **True PostgreSQL Parsing:** Utilizes the real PostgreSQL source code for accurate parsing.
|
|
28
|
+
- **Symmetric Parsing and Deparsing:** Convert SQL to AST and back, enabling query manipulation.
|
|
29
|
+
- **AST Manipulation:** Easily modify parts of a SQL statement through the AST.
|
|
30
|
+
|
|
27
31
|
## Parser Example
|
|
28
32
|
|
|
29
33
|
Rewrite part of a SQL query:
|
|
30
34
|
|
|
31
35
|
```js
|
|
32
|
-
|
|
36
|
+
import { parse, deparse } from 'pgsql-parser';
|
|
33
37
|
|
|
34
38
|
const stmts = parse('SELECT * FROM test_table');
|
|
35
39
|
|
|
40
|
+
// Assuming the structure of stmts is known and matches the expected type
|
|
36
41
|
stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
37
42
|
|
|
38
43
|
console.log(deparse(stmts));
|
|
@@ -42,19 +47,25 @@ console.log(deparse(stmts));
|
|
|
42
47
|
|
|
43
48
|
## Deparser Example
|
|
44
49
|
|
|
45
|
-
The deparser
|
|
50
|
+
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.
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
const { parse } = require('pgsql-parser');
|
|
49
|
-
const { deparse } = require('pgsql-deparser');
|
|
52
|
+
Here's how you can use the deparser in your TypeScript code:
|
|
50
53
|
|
|
51
|
-
|
|
54
|
+
```ts
|
|
55
|
+
import { deparse } from 'pgsql-deparser';
|
|
56
|
+
|
|
57
|
+
// Assuming `stmts` is an AST object for the query 'SELECT * FROM test_table'
|
|
58
|
+
// This could have been obtained from any source, not necessarily the pgsql-parser
|
|
59
|
+
const stmts = getAstFromSomewhere();
|
|
52
60
|
|
|
61
|
+
// Modify the AST as needed
|
|
53
62
|
stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
54
63
|
|
|
64
|
+
// Deparse the modified AST back to a SQL string
|
|
55
65
|
console.log(deparse(stmts));
|
|
56
66
|
|
|
57
|
-
// SELECT * FROM "another_table"
|
|
67
|
+
// Output: SELECT * FROM "another_table"
|
|
68
|
+
|
|
58
69
|
```
|
|
59
70
|
|
|
60
71
|
## CLI
|
|
@@ -121,16 +132,31 @@ Our latest is built with `13-latest` branch from libpg_query
|
|
|
121
132
|
|
|
122
133
|
## Related
|
|
123
134
|
|
|
124
|
-
* [
|
|
135
|
+
* [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.
|
|
136
|
+
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/master/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
|
|
137
|
+
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/master/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`
|
|
138
|
+
* [@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.
|
|
139
|
+
* [@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.
|
|
140
|
+
* [@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.
|
|
141
|
+
* [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.
|
|
142
|
+
* [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.
|
|
143
|
+
|
|
144
|
+
Thanks @lfittl for building the core `libpg_query` suite:
|
|
145
|
+
|
|
125
146
|
* [libpg_query](https://github.com/pganalyze/libpg_query)
|
|
126
147
|
* [pg_query](https://github.com/lfittl/pg_query)
|
|
127
148
|
* [pg_query.go](https://github.com/lfittl/pg_query.go)
|
|
128
149
|
|
|
129
150
|
## Credits
|
|
130
151
|
|
|
152
|
+
Thanks to @zhm for the OG parser that started it all:
|
|
153
|
+
|
|
131
154
|
* [pg-query-parser](https://github.com/zhm/pg-query-parser)
|
|
132
155
|
* [pg-query-native](https://github.com/zhm/node-pg-query-native)
|
|
156
|
+
* [Original LICENSE](https://github.com/zhm/pg-query-parser/blob/master/LICENSE.md)
|
|
157
|
+
|
|
158
|
+
## Disclaimer
|
|
133
159
|
|
|
134
|
-
|
|
160
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
135
161
|
|
|
136
|
-
|
|
162
|
+
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.
|
package/main/cli.js
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var _utils = require("./utils");
|
|
11
|
-
|
|
12
|
-
var argv = require('minimist')(process.argv.slice(2));
|
|
13
|
-
|
|
14
|
-
var args = argv._;
|
|
15
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const index_1 = require("./index");
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
const argv = require('minimist')(process.argv.slice(2));
|
|
9
|
+
const args = argv._;
|
|
16
10
|
if (args.length !== 1) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
console.log(argv);
|
|
12
|
+
console.warn('Usage: pgsql-parser <sqlfile>');
|
|
13
|
+
process.exit(1);
|
|
20
14
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
const pth = args[0].startsWith('/')
|
|
16
|
+
? args[0]
|
|
17
|
+
: (0, path_1.resolve)((0, path_1.join)(process.cwd(), args[0]));
|
|
18
|
+
const content = (0, fs_1.readFileSync)(pth, 'utf-8');
|
|
19
|
+
let query;
|
|
26
20
|
if (argv.pl) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} else {
|
|
30
|
-
query = (0, _index.parse)(content);
|
|
21
|
+
// plpgsql ONLY
|
|
22
|
+
query = (0, index_1.parseFunction)(content);
|
|
31
23
|
}
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
else {
|
|
25
|
+
query = (0, index_1.parse)(content);
|
|
26
|
+
}
|
|
27
|
+
process.stdout.write(JSON.stringify((0, utils_1.cleanTreeWithStmt)(query), null, 2));
|
package/main/index.js
CHANGED
|
@@ -1,100 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(exports, "
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return _pgsqlDeparser.deparse;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "enums", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function get() {
|
|
23
|
-
return _pgsqlEnums.enums;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
Object.defineProperty(exports, "parseFunction", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
get: function get() {
|
|
29
|
-
return _libpgQuery.parsePlPgSQLSync;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
exports.parseAsync = exports.parse = void 0;
|
|
33
|
-
|
|
34
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
35
|
-
|
|
36
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
37
|
-
|
|
38
|
-
var _pgsqlDeparser = require("pgsql-deparser");
|
|
39
|
-
|
|
40
|
-
var _pgsqlEnums = require("pgsql-enums");
|
|
41
|
-
|
|
42
|
-
var _libpgQuery = require("libpg-query");
|
|
43
|
-
|
|
44
|
-
function mapStmt(_ref) {
|
|
45
|
-
var stmt = _ref.stmt,
|
|
46
|
-
stmt_len = _ref.stmt_len,
|
|
47
|
-
stmt_location = _ref.stmt_location;
|
|
48
|
-
return {
|
|
49
|
-
RawStmt: {
|
|
50
|
-
stmt: stmt,
|
|
51
|
-
stmt_len: stmt_len,
|
|
52
|
-
stmt_location: stmt_location || 0
|
|
53
|
-
}
|
|
54
|
-
};
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseFunction = exports.Deparser = exports.deparse = exports.parseAsync = exports.parse = void 0;
|
|
4
|
+
const pgsql_deparser_1 = require("pgsql-deparser");
|
|
5
|
+
Object.defineProperty(exports, "Deparser", { enumerable: true, get: function () { return pgsql_deparser_1.Deparser; } });
|
|
6
|
+
Object.defineProperty(exports, "deparse", { enumerable: true, get: function () { return pgsql_deparser_1.deparse; } });
|
|
7
|
+
const libpg_query_1 = require("libpg-query");
|
|
8
|
+
Object.defineProperty(exports, "parseFunction", { enumerable: true, get: function () { return libpg_query_1.parsePlPgSQLSync; } });
|
|
9
|
+
function mapStmt({ stmt, stmt_len, stmt_location }) {
|
|
10
|
+
return {
|
|
11
|
+
RawStmt: {
|
|
12
|
+
stmt,
|
|
13
|
+
stmt_len,
|
|
14
|
+
stmt_location: stmt_location || 0
|
|
15
|
+
}
|
|
16
|
+
};
|
|
55
17
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
18
|
+
const parse = (sql) => {
|
|
19
|
+
if (!sql)
|
|
20
|
+
throw new Error('no SQL provided to parser');
|
|
21
|
+
const result = (0, libpg_query_1.parseQuerySync)(sql);
|
|
22
|
+
return result.stmts.map(mapStmt);
|
|
61
23
|
};
|
|
62
|
-
|
|
63
24
|
exports.parse = parse;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
case 0:
|
|
72
|
-
if (sql) {
|
|
73
|
-
_context.next = 2;
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
throw new Error('no SQL provided to parser');
|
|
78
|
-
|
|
79
|
-
case 2:
|
|
80
|
-
_context.next = 4;
|
|
81
|
-
return (0, _libpgQuery.parseQuery)(sql);
|
|
82
|
-
|
|
83
|
-
case 4:
|
|
84
|
-
result = _context.sent;
|
|
85
|
-
return _context.abrupt("return", result.stmts.map(mapStmt));
|
|
86
|
-
|
|
87
|
-
case 6:
|
|
88
|
-
case "end":
|
|
89
|
-
return _context.stop();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}, _callee);
|
|
93
|
-
}));
|
|
94
|
-
|
|
95
|
-
return function parseAsync(_x) {
|
|
96
|
-
return _ref2.apply(this, arguments);
|
|
97
|
-
};
|
|
98
|
-
}();
|
|
99
|
-
|
|
100
|
-
exports.parseAsync = parseAsync;
|
|
25
|
+
const parseAsync = async (sql) => {
|
|
26
|
+
if (!sql)
|
|
27
|
+
throw new Error('no SQL provided to parser');
|
|
28
|
+
const result = await (0, libpg_query_1.parseQuery)(sql);
|
|
29
|
+
return result.stmts.map(mapStmt);
|
|
30
|
+
};
|
|
31
|
+
exports.parseAsync = parseAsync;
|
package/main/utils/index.js
CHANGED
|
@@ -1,117 +1,97 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.cleanTreeWithStmt = exports.cleanTree = exports.transform = exports.cleanLines = void 0;
|
|
9
|
-
|
|
10
|
-
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
11
|
-
|
|
12
2
|
/* eslint-disable no-restricted-syntax */
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.cleanTreeWithStmt = exports.cleanTree = exports.transform = exports.cleanLines = void 0;
|
|
5
|
+
const cleanLines = (sql) => {
|
|
6
|
+
return sql
|
|
7
|
+
.split('\n')
|
|
8
|
+
.map((l) => l.trim())
|
|
9
|
+
.filter((a) => a)
|
|
10
|
+
.join('\n');
|
|
19
11
|
};
|
|
20
|
-
|
|
21
12
|
exports.cleanLines = cleanLines;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return obj;
|
|
28
|
-
} // Handle Date
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (obj instanceof Date) {
|
|
32
|
-
copy = new Date();
|
|
33
|
-
copy.setTime(obj.getTime());
|
|
34
|
-
return copy;
|
|
35
|
-
} // Handle Array
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (obj instanceof Array) {
|
|
39
|
-
copy = [];
|
|
40
|
-
|
|
41
|
-
for (var i = 0, len = obj.length; i < len; i++) {
|
|
42
|
-
copy[i] = transform(obj[i], props);
|
|
13
|
+
const transform = (obj, props) => {
|
|
14
|
+
let copy = null;
|
|
15
|
+
// Handle the 3 simple types, and null or undefined
|
|
16
|
+
if (obj == null || typeof obj !== 'object') {
|
|
17
|
+
return obj;
|
|
43
18
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (typeof props[attr] === 'function') {
|
|
56
|
-
copy[attr] = props[attr](obj[attr]);
|
|
57
|
-
} else if (props[attr].hasOwnProperty(obj[attr])) {
|
|
58
|
-
copy[attr] = props[attr][obj[attr]];
|
|
59
|
-
} else {
|
|
60
|
-
copy[attr] = transform(obj[attr], props);
|
|
61
|
-
}
|
|
62
|
-
} else {
|
|
63
|
-
copy[attr] = transform(obj[attr], props);
|
|
19
|
+
// Handle Date
|
|
20
|
+
if (obj instanceof Date) {
|
|
21
|
+
copy = new Date();
|
|
22
|
+
copy.setTime(obj.getTime());
|
|
23
|
+
return copy;
|
|
24
|
+
}
|
|
25
|
+
// Handle Array
|
|
26
|
+
if (obj instanceof Array) {
|
|
27
|
+
copy = [];
|
|
28
|
+
for (let i = 0, len = obj.length; i < len; i++) {
|
|
29
|
+
copy[i] = (0, exports.transform)(obj[i], props);
|
|
64
30
|
}
|
|
65
|
-
|
|
66
|
-
copy[attr] = transform(obj[attr], props);
|
|
67
|
-
}
|
|
31
|
+
return copy;
|
|
68
32
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
33
|
+
// Handle Object
|
|
34
|
+
if (obj instanceof Object || typeof obj === 'object') {
|
|
35
|
+
copy = {};
|
|
36
|
+
for (const attr in obj) {
|
|
37
|
+
if (obj.hasOwnProperty(attr)) {
|
|
38
|
+
if (props.hasOwnProperty(attr)) {
|
|
39
|
+
if (typeof props[attr] === 'function') {
|
|
40
|
+
copy[attr] = props[attr](obj[attr]);
|
|
41
|
+
}
|
|
42
|
+
else if (props[attr].hasOwnProperty(obj[attr])) {
|
|
43
|
+
copy[attr] = props[attr][obj[attr]];
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
copy[attr] = (0, exports.transform)(obj[attr], props);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
copy[attr] = (0, exports.transform)(obj[attr], props);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
copy[attr] = (0, exports.transform)(obj[attr], props);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return copy;
|
|
58
|
+
}
|
|
59
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
74
60
|
};
|
|
75
|
-
|
|
76
61
|
exports.transform = transform;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
62
|
+
const noop = () => undefined;
|
|
63
|
+
const cleanTree = (tree) => {
|
|
64
|
+
return (0, exports.transform)(tree, {
|
|
65
|
+
stmt_len: noop,
|
|
66
|
+
stmt_location: noop,
|
|
67
|
+
location: noop,
|
|
68
|
+
DefElem: (obj) => {
|
|
69
|
+
if (obj.defname === 'as') {
|
|
70
|
+
if (Array.isArray(obj.arg) && obj.arg.length) {
|
|
71
|
+
// function
|
|
72
|
+
obj.arg[0].String.str = obj.arg[0].String.str.trim();
|
|
73
|
+
}
|
|
74
|
+
else if (obj.arg.List && obj.arg.List.items) {
|
|
75
|
+
// function
|
|
76
|
+
obj.arg.List.items[0].String.str = obj.arg.List.items[0].String.str.trim();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// do stmt
|
|
80
|
+
obj.arg.String.str = obj.arg.String.str.trim();
|
|
81
|
+
}
|
|
82
|
+
return (0, exports.cleanTree)(obj);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return (0, exports.cleanTree)(obj);
|
|
86
|
+
}
|
|
98
87
|
}
|
|
99
|
-
|
|
100
|
-
return cleanTree(obj);
|
|
101
|
-
} else {
|
|
102
|
-
return cleanTree(obj);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
});
|
|
88
|
+
});
|
|
106
89
|
};
|
|
107
|
-
|
|
108
90
|
exports.cleanTree = cleanTree;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
});
|
|
91
|
+
const cleanTreeWithStmt = (tree) => {
|
|
92
|
+
return (0, exports.transform)(tree, {
|
|
93
|
+
stmt_location: noop,
|
|
94
|
+
location: noop
|
|
95
|
+
});
|
|
115
96
|
};
|
|
116
|
-
|
|
117
|
-
exports.cleanTreeWithStmt = cleanTreeWithStmt;
|
|
97
|
+
exports.cleanTreeWithStmt = cleanTreeWithStmt;
|
package/module/cli.js
CHANGED
|
@@ -3,26 +3,23 @@ import { resolve, join } from 'path';
|
|
|
3
3
|
import { readFileSync } from 'fs';
|
|
4
4
|
import { parse, parseFunction } from './index';
|
|
5
5
|
import { cleanTreeWithStmt } from './utils';
|
|
6
|
-
|
|
7
6
|
const argv = require('minimist')(process.argv.slice(2));
|
|
8
|
-
|
|
9
7
|
const args = argv._;
|
|
10
|
-
|
|
11
8
|
if (args.length !== 1) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
console.log(argv);
|
|
10
|
+
console.warn('Usage: pgsql-parser <sqlfile>');
|
|
11
|
+
process.exit(1);
|
|
15
12
|
}
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const pth = args[0].startsWith('/')
|
|
14
|
+
? args[0]
|
|
15
|
+
: resolve(join(process.cwd(), args[0]));
|
|
18
16
|
const content = readFileSync(pth, 'utf-8');
|
|
19
17
|
let query;
|
|
20
|
-
|
|
21
18
|
if (argv.pl) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
} else {
|
|
25
|
-
query = parse(content);
|
|
19
|
+
// plpgsql ONLY
|
|
20
|
+
query = parseFunction(content);
|
|
26
21
|
}
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
else {
|
|
23
|
+
query = parse(content);
|
|
24
|
+
}
|
|
25
|
+
process.stdout.write(JSON.stringify(cleanTreeWithStmt(query), null, 2));
|
package/module/index.js
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
1
|
import { Deparser, deparse } from 'pgsql-deparser';
|
|
2
|
-
import { enums } from 'pgsql-enums';
|
|
3
2
|
import { parseQuery, parseQuerySync, parsePlPgSQLSync as parseFunction } from 'libpg-query';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
stmt,
|
|
13
|
-
stmt_len,
|
|
14
|
-
stmt_location: stmt_location || 0
|
|
15
|
-
}
|
|
16
|
-
};
|
|
3
|
+
function mapStmt({ stmt, stmt_len, stmt_location }) {
|
|
4
|
+
return {
|
|
5
|
+
RawStmt: {
|
|
6
|
+
stmt,
|
|
7
|
+
stmt_len,
|
|
8
|
+
stmt_location: stmt_location || 0
|
|
9
|
+
}
|
|
10
|
+
};
|
|
17
11
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
export const parse = (sql) => {
|
|
13
|
+
if (!sql)
|
|
14
|
+
throw new Error('no SQL provided to parser');
|
|
15
|
+
const result = parseQuerySync(sql);
|
|
16
|
+
return result.stmts.map(mapStmt);
|
|
23
17
|
};
|
|
24
|
-
export const parseAsync = async sql => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
export const parseAsync = async (sql) => {
|
|
19
|
+
if (!sql)
|
|
20
|
+
throw new Error('no SQL provided to parser');
|
|
21
|
+
const result = await parseQuery(sql);
|
|
22
|
+
return result.stmts.map(mapStmt);
|
|
28
23
|
};
|
|
29
|
-
export { deparse, Deparser,
|
|
24
|
+
export { deparse, Deparser, parseFunction };
|