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.
@@ -1,90 +1,90 @@
1
1
  /* eslint-disable no-restricted-syntax */
2
- export const cleanLines = sql => {
3
- return sql.split('\n').map(l => l.trim()).filter(a => a).join('\n');
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
- let copy = null; // Handle the 3 simple types, and null or undefined
7
-
8
- if (obj == null || typeof obj !== 'object') {
9
- return obj;
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
- return copy;
28
- } // Handle Object
29
-
30
-
31
- if (obj instanceof Object || typeof obj === 'object') {
32
- copy = {};
33
-
34
- for (const attr in obj) {
35
- if (obj.hasOwnProperty(attr)) {
36
- if (props.hasOwnProperty(attr)) {
37
- if (typeof props[attr] === 'function') {
38
- copy[attr] = props[attr](obj[attr]);
39
- } else if (props[attr].hasOwnProperty(obj[attr])) {
40
- copy[attr] = props[attr][obj[attr]];
41
- } else {
42
- copy[attr] = transform(obj[attr], props);
43
- }
44
- } else {
45
- copy[attr] = transform(obj[attr], props);
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
- } else {
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
- export const cleanTree = tree => {
61
- return transform(tree, {
62
- stmt_len: noop,
63
- stmt_location: noop,
64
- location: noop,
65
- DefElem: obj => {
66
- if (obj.defname === 'as') {
67
- if (Array.isArray(obj.arg) && obj.arg.length) {
68
- // function
69
- obj.arg[0].String.str = obj.arg[0].String.str.trim();
70
- } else if (obj.arg.List && obj.arg.List.items) {
71
- // function
72
- obj.arg.List.items[0].String.str = obj.arg.List.items[0].String.str.trim();
73
- } else {
74
- // do stmt
75
- obj.arg.String.str = obj.arg.String.str.trim();
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
- return cleanTree(obj);
79
- } else {
80
- return cleanTree(obj);
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.7.3",
3
+ "version": "13.8.1",
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
- "main",
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": "cross-env BABEL_ENV=production babel src --out-dir main --delete-dir-on-start",
39
- "build:module": "cross-env MODULE=true babel src --out-dir module --delete-dir-on-start",
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 src --fix",
43
- "dev": "cross-env NODE_ENV=development babel-node src/index",
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
- "@babel/cli": "7.11.6",
54
- "@babel/core": "7.11.6",
55
- "@babel/node": "^7.12.1",
56
- "@babel/plugin-proposal-class-properties": "7.10.4",
57
- "@babel/plugin-proposal-export-default-from": "7.10.4",
58
- "@babel/plugin-proposal-object-rest-spread": "7.11.0",
59
- "@babel/plugin-transform-runtime": "7.11.5",
60
- "@babel/preset-env": "7.11.5",
61
- "babel-core": "7.0.0-bridge.0",
62
- "babel-eslint": "10.1.0",
63
- "babel-watch": "^7.0.0",
64
- "cross-env": "^7.0.2",
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.6.3",
78
- "pgsql-enums": "^13.2.2"
89
+ "pgsql-deparser": "^13.7.1",
90
+ "pgsql-enums": "^13.3.1"
79
91
  },
80
- "gitHead": "36f81b1aa480303b6e778aa90353962439cbfb18"
92
+ "gitHead": "ac077a65935d537e10d78a8495f5c5f0393e45c4"
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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -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 };
@@ -0,0 +1,4 @@
1
+ export declare const cleanLines: (sql: any) => any;
2
+ export declare const transform: (obj: any, props: any) => any;
3
+ export declare const cleanTree: (tree: any) => any;
4
+ export declare const cleanTreeWithStmt: (tree: any) => any;