pgsql-parser 13.7.3 → 13.8.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 +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/module/utils/index.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-syntax */
|
|
2
|
-
export const cleanLines = sql => {
|
|
3
|
-
|
|
2
|
+
export const cleanLines = (sql) => {
|
|
3
|
+
return sql
|
|
4
|
+
.split('\n')
|
|
5
|
+
.map((l) => l.trim())
|
|
6
|
+
.filter((a) => a)
|
|
7
|
+
.join('\n');
|
|
4
8
|
};
|
|
5
9
|
export const transform = (obj, props) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} // Handle Date
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (obj instanceof Date) {
|
|
14
|
-
copy = new Date();
|
|
15
|
-
copy.setTime(obj.getTime());
|
|
16
|
-
return copy;
|
|
17
|
-
} // Handle Array
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (obj instanceof Array) {
|
|
21
|
-
copy = [];
|
|
22
|
-
|
|
23
|
-
for (let i = 0, len = obj.length; i < len; i++) {
|
|
24
|
-
copy[i] = transform(obj[i], props);
|
|
10
|
+
let copy = null;
|
|
11
|
+
// Handle the 3 simple types, and null or undefined
|
|
12
|
+
if (obj == null || typeof obj !== 'object') {
|
|
13
|
+
return obj;
|
|
25
14
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
15
|
+
// Handle Date
|
|
16
|
+
if (obj instanceof Date) {
|
|
17
|
+
copy = new Date();
|
|
18
|
+
copy.setTime(obj.getTime());
|
|
19
|
+
return copy;
|
|
20
|
+
}
|
|
21
|
+
// Handle Array
|
|
22
|
+
if (obj instanceof Array) {
|
|
23
|
+
copy = [];
|
|
24
|
+
for (let i = 0, len = obj.length; i < len; i++) {
|
|
25
|
+
copy[i] = transform(obj[i], props);
|
|
26
|
+
}
|
|
27
|
+
return copy;
|
|
28
|
+
}
|
|
29
|
+
// Handle Object
|
|
30
|
+
if (obj instanceof Object || typeof obj === 'object') {
|
|
31
|
+
copy = {};
|
|
32
|
+
for (const attr in obj) {
|
|
33
|
+
if (obj.hasOwnProperty(attr)) {
|
|
34
|
+
if (props.hasOwnProperty(attr)) {
|
|
35
|
+
if (typeof props[attr] === 'function') {
|
|
36
|
+
copy[attr] = props[attr](obj[attr]);
|
|
37
|
+
}
|
|
38
|
+
else if (props[attr].hasOwnProperty(obj[attr])) {
|
|
39
|
+
copy[attr] = props[attr][obj[attr]];
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
copy[attr] = transform(obj[attr], props);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
copy[attr] = transform(obj[attr], props);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
copy[attr] = transform(obj[attr], props);
|
|
51
|
+
}
|
|
46
52
|
}
|
|
47
|
-
|
|
48
|
-
copy[attr] = transform(obj[attr], props);
|
|
49
|
-
}
|
|
53
|
+
return copy;
|
|
50
54
|
}
|
|
51
|
-
|
|
52
|
-
return copy;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
55
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
56
56
|
};
|
|
57
|
-
|
|
58
57
|
const noop = () => undefined;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
58
|
+
export const cleanTree = (tree) => {
|
|
59
|
+
return transform(tree, {
|
|
60
|
+
stmt_len: noop,
|
|
61
|
+
stmt_location: noop,
|
|
62
|
+
location: noop,
|
|
63
|
+
DefElem: (obj) => {
|
|
64
|
+
if (obj.defname === 'as') {
|
|
65
|
+
if (Array.isArray(obj.arg) && obj.arg.length) {
|
|
66
|
+
// function
|
|
67
|
+
obj.arg[0].String.str = obj.arg[0].String.str.trim();
|
|
68
|
+
}
|
|
69
|
+
else if (obj.arg.List && obj.arg.List.items) {
|
|
70
|
+
// function
|
|
71
|
+
obj.arg.List.items[0].String.str = obj.arg.List.items[0].String.str.trim();
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// do stmt
|
|
75
|
+
obj.arg.String.str = obj.arg.String.str.trim();
|
|
76
|
+
}
|
|
77
|
+
return cleanTree(obj);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return cleanTree(obj);
|
|
81
|
+
}
|
|
76
82
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
export const cleanTreeWithStmt = (tree) => {
|
|
86
|
+
return transform(tree, {
|
|
87
|
+
stmt_location: noop,
|
|
88
|
+
location: noop
|
|
89
|
+
});
|
|
84
90
|
};
|
|
85
|
-
export const cleanTreeWithStmt = tree => {
|
|
86
|
-
return transform(tree, {
|
|
87
|
-
stmt_location: noop,
|
|
88
|
-
location: noop
|
|
89
|
-
});
|
|
90
|
-
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pgsql-parser",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.8.0",
|
|
4
4
|
"description": "The real PostgreSQL query parser",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/launchql/pgsql-parser",
|
|
7
7
|
"license": "SEE LICENSE IN LICENSE",
|
|
8
8
|
"main": "main/index.js",
|
|
9
9
|
"module": "module/index.js",
|
|
10
|
+
"typings": "types/index.d.ts",
|
|
10
11
|
"directories": {
|
|
11
12
|
"lib": "src",
|
|
12
13
|
"test": "__tests__"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
|
-
"
|
|
16
|
-
"module"
|
|
16
|
+
"types",
|
|
17
|
+
"module",
|
|
18
|
+
"src",
|
|
19
|
+
"main"
|
|
17
20
|
],
|
|
18
21
|
"bin": {
|
|
19
22
|
"pgsql-parser": "main/cli.js"
|
|
@@ -35,13 +38,15 @@
|
|
|
35
38
|
"url": "https://github.com/launchql/pgsql-parser"
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
|
-
"build:main": "
|
|
39
|
-
"build:module": "
|
|
41
|
+
"build:main": "yarn tsc -p tsconfig.json --outDir main --module commonjs",
|
|
42
|
+
"build:module": "yarn tsc -p tsconfig.json --outDir module --module es2022",
|
|
40
43
|
"build": "npm run build:module && npm run build:main",
|
|
44
|
+
"buidl": "npm run build",
|
|
45
|
+
"buidl:clean": "npm run clean && npm run buidl",
|
|
46
|
+
"clean": "rimraf ./types",
|
|
41
47
|
"prepare": "npm run build",
|
|
42
|
-
"lint": "eslint
|
|
43
|
-
"
|
|
44
|
-
"watch": "cross-env NODE_ENV=development babel-watch src/index",
|
|
48
|
+
"lint": "eslint .",
|
|
49
|
+
"format": "eslint . --fix",
|
|
45
50
|
"test": "jest",
|
|
46
51
|
"test:watch": "jest --watch",
|
|
47
52
|
"test:debug": "node --inspect node_modules/.bin/jest --runInBand"
|
|
@@ -49,33 +54,40 @@
|
|
|
49
54
|
"bugs": {
|
|
50
55
|
"url": "https://github.com/launchql/pgsql-parser/issues"
|
|
51
56
|
},
|
|
57
|
+
"jest": {
|
|
58
|
+
"preset": "ts-jest",
|
|
59
|
+
"testEnvironment": "node",
|
|
60
|
+
"transform": {
|
|
61
|
+
"^.+\\.ts?$": "ts-jest"
|
|
62
|
+
},
|
|
63
|
+
"transformIgnorePatterns": [
|
|
64
|
+
"<rootDir>/node_modules/"
|
|
65
|
+
],
|
|
66
|
+
"testPathIgnorePatterns": [
|
|
67
|
+
"main/",
|
|
68
|
+
"module/",
|
|
69
|
+
"types/"
|
|
70
|
+
]
|
|
71
|
+
},
|
|
52
72
|
"devDependencies": {
|
|
53
|
-
"@
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"eslint": "^6.8.0",
|
|
66
|
-
"eslint-config-prettier": "^6.10.0",
|
|
67
|
-
"eslint-plugin-prettier": "^3.1.2",
|
|
68
|
-
"glob": "7.1.6",
|
|
69
|
-
"jest": "^25.1.0",
|
|
70
|
-
"prettier": "^2.1.2",
|
|
71
|
-
"regenerator-runtime": "^0.13.7"
|
|
73
|
+
"@types/jest": "^29.5.0",
|
|
74
|
+
"eslint": "8.38.0",
|
|
75
|
+
"eslint-config-prettier": "^8.8.0",
|
|
76
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
77
|
+
"esprima": "4.0.1",
|
|
78
|
+
"glob": "8.0.3",
|
|
79
|
+
"jest": "^29.5.0",
|
|
80
|
+
"prettier": "^2.8.7",
|
|
81
|
+
"rimraf": "5.0.5",
|
|
82
|
+
"ts-jest": "^29.1.0",
|
|
83
|
+
"ts-node": "10.9.2",
|
|
84
|
+
"typescript": "^5.0.4"
|
|
72
85
|
},
|
|
73
86
|
"dependencies": {
|
|
74
|
-
"@babel/runtime": "^7.11.2",
|
|
75
87
|
"libpg-query": "13.3.2",
|
|
76
88
|
"minimist": "^1.2.6",
|
|
77
|
-
"pgsql-deparser": "^13.
|
|
78
|
-
"pgsql-enums": "^13.
|
|
89
|
+
"pgsql-deparser": "^13.7.0",
|
|
90
|
+
"pgsql-enums": "^13.3.0"
|
|
79
91
|
},
|
|
80
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "7d1df1770583c50d0fa68dd0cf90beedc9d46e40"
|
|
81
93
|
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { resolve, join } from 'path';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import { parse, parseFunction } from './index';
|
|
5
|
+
import { cleanTreeWithStmt } from './utils';
|
|
6
|
+
const argv = require('minimist')(process.argv.slice(2));
|
|
7
|
+
const args = argv._;
|
|
8
|
+
if (args.length !== 1) {
|
|
9
|
+
console.log(argv);
|
|
10
|
+
console.warn('Usage: pgsql-parser <sqlfile>');
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
const pth = args[0].startsWith('/')
|
|
14
|
+
? args[0]
|
|
15
|
+
: resolve(join(process.cwd(), args[0]));
|
|
16
|
+
const content = readFileSync(pth, 'utf-8');
|
|
17
|
+
let query;
|
|
18
|
+
if (argv.pl) {
|
|
19
|
+
// plpgsql ONLY
|
|
20
|
+
query = parseFunction(content);
|
|
21
|
+
} else {
|
|
22
|
+
query = parse(content);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
process.stdout.write(JSON.stringify(cleanTreeWithStmt(query), null, 2));
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Deparser, deparse } from 'pgsql-deparser';
|
|
2
|
+
import {
|
|
3
|
+
parseQuery,
|
|
4
|
+
parseQuerySync,
|
|
5
|
+
parsePlPgSQLSync as parseFunction
|
|
6
|
+
} from 'libpg-query';
|
|
7
|
+
|
|
8
|
+
function mapStmt({ stmt, stmt_len, stmt_location }) {
|
|
9
|
+
return {
|
|
10
|
+
RawStmt: {
|
|
11
|
+
stmt,
|
|
12
|
+
stmt_len,
|
|
13
|
+
stmt_location: stmt_location || 0
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const parse = (sql) => {
|
|
19
|
+
if (!sql) throw new Error('no SQL provided to parser');
|
|
20
|
+
const result = parseQuerySync(sql);
|
|
21
|
+
return result.stmts.map(mapStmt);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const parseAsync = async (sql) => {
|
|
25
|
+
if (!sql) throw new Error('no SQL provided to parser');
|
|
26
|
+
const result = await parseQuery(sql);
|
|
27
|
+
return result.stmts.map(mapStmt);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { deparse, Deparser, parseFunction };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
|
|
3
|
+
export const cleanLines = (sql) => {
|
|
4
|
+
return sql
|
|
5
|
+
.split('\n')
|
|
6
|
+
.map((l) => l.trim())
|
|
7
|
+
.filter((a) => a)
|
|
8
|
+
.join('\n');
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const transform = (obj, props) => {
|
|
12
|
+
let copy = null;
|
|
13
|
+
// Handle the 3 simple types, and null or undefined
|
|
14
|
+
if (obj == null || typeof obj !== 'object') {
|
|
15
|
+
return obj;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Handle Date
|
|
19
|
+
if (obj instanceof Date) {
|
|
20
|
+
copy = new Date();
|
|
21
|
+
copy.setTime(obj.getTime());
|
|
22
|
+
return copy;
|
|
23
|
+
}
|
|
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] = transform(obj[i], props);
|
|
30
|
+
}
|
|
31
|
+
return copy;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Handle Object
|
|
35
|
+
if (obj instanceof Object || typeof obj === 'object') {
|
|
36
|
+
copy = {};
|
|
37
|
+
for (const attr in obj) {
|
|
38
|
+
if (obj.hasOwnProperty(attr)) {
|
|
39
|
+
if (props.hasOwnProperty(attr)) {
|
|
40
|
+
if (typeof props[attr] === 'function') {
|
|
41
|
+
copy[attr] = props[attr](obj[attr]);
|
|
42
|
+
} else if (props[attr].hasOwnProperty(obj[attr])) {
|
|
43
|
+
copy[attr] = props[attr][obj[attr]];
|
|
44
|
+
} else {
|
|
45
|
+
copy[attr] = transform(obj[attr], props);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
copy[attr] = transform(obj[attr], props);
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
copy[attr] = transform(obj[attr], props);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return copy;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const noop = () => undefined;
|
|
61
|
+
|
|
62
|
+
export const cleanTree = (tree) => {
|
|
63
|
+
return transform(tree, {
|
|
64
|
+
stmt_len: noop,
|
|
65
|
+
stmt_location: noop,
|
|
66
|
+
location: noop,
|
|
67
|
+
DefElem: (obj) => {
|
|
68
|
+
if (obj.defname === 'as') {
|
|
69
|
+
if (Array.isArray(obj.arg) && obj.arg.length) {
|
|
70
|
+
// function
|
|
71
|
+
obj.arg[0].String.str = obj.arg[0].String.str.trim();
|
|
72
|
+
} else if (obj.arg.List && obj.arg.List.items) {
|
|
73
|
+
// function
|
|
74
|
+
obj.arg.List.items[0].String.str = obj.arg.List.items[0].String.str.trim();
|
|
75
|
+
} else {
|
|
76
|
+
// do stmt
|
|
77
|
+
obj.arg.String.str = obj.arg.String.str.trim();
|
|
78
|
+
}
|
|
79
|
+
return cleanTree(obj);
|
|
80
|
+
} else {
|
|
81
|
+
return cleanTree(obj);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const cleanTreeWithStmt = (tree) => {
|
|
88
|
+
return transform(tree, {
|
|
89
|
+
stmt_location: noop,
|
|
90
|
+
location: noop
|
|
91
|
+
});
|
|
92
|
+
};
|
package/types/cli.d.ts
ADDED
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Deparser, deparse } from 'pgsql-deparser';
|
|
2
|
+
import { parsePlPgSQLSync as parseFunction } from 'libpg-query';
|
|
3
|
+
export declare const parse: (sql: any) => any;
|
|
4
|
+
export declare const parseAsync: (sql: any) => Promise<any>;
|
|
5
|
+
export { deparse, Deparser, parseFunction };
|