Skip to content

Commit 9bfac7c

Browse files
committed
support multiple database
1 parent 536e2c1 commit 9bfac7c

File tree

12 files changed

+2259
-35
lines changed

12 files changed

+2259
-35
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ node_modules/
22
build/
33
coverage/
44
lib/
5-
index.d.ts
65
.nyc_output/
76
.coveralls.yml

README.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ or
3636
yarn add node-sql-parser
3737
```
3838

39-
**Install the following type module for typescript usage**
39+
**(Deprecated)Install the following type module for typescript usage**
4040

4141
```bash
4242
npm install @types/node-sql-parser --save-dev
@@ -85,20 +85,28 @@ console.log(ast);
8585
### Convert AST back to SQL
8686

8787
```javascript
88+
const opt = {
89+
database: 'MySQL' // MySQL is the default database
90+
}
8891
const { Parser } = require('node-sql-parser');
8992
const parser = new Parser()
90-
const ast = parser.astify('SELECT * FROM t');
91-
const sql = parse.sqlify(ast);
93+
// opt is optional
94+
const ast = parser.astify('SELECT * FROM t', opt);
95+
const sql = parse.sqlify(ast, opt);
9296

9397
console.log(sql); // SELECT * FROM `t`
9498
```
9599

96100
### Get TableList, ColumnList, Ast by `parse` function
97101

98102
```javascript
103+
const opt = {
104+
database: 'MariaDB' // MySQL is the default database
105+
}
99106
const { Parser } = require('node-sql-parser');
100107
const parser = new Parser()
101-
const { tableList, columnList, ast } = parser.parse('SELECT * FROM t');
108+
// opt is optional
109+
const { tableList, columnList, ast } = parser.parse('SELECT * FROM t', opt);
102110
```
103111

104112
### Get the SQL visited tables
@@ -107,9 +115,13 @@ const { tableList, columnList, ast } = parser.parse('SELECT * FROM t');
107115
- the format is **{type}::{dbName}::{tableName}** // type could be select, update, delete or insert
108116

109117
```javascript
118+
const opt = {
119+
database: 'MySQL'
120+
}
110121
const { Parser } = require('node-sql-parser');
111122
const parser = new Parser();
112-
const tableList = parser.tableList('SELECT * FROM t');
123+
// opt is optional
124+
const tableList = parser.tableList('SELECT * FROM t', opt);
113125

114126
console.log(tableList); // ["select::null::t"]
115127
```
@@ -121,24 +133,33 @@ console.log(tableList); // ["select::null::t"]
121133
- for `select *`, `delete` and `insert into tableName values()` without specified columns, the `.*` column authority regex is required
122134

123135
```javascript
136+
const opt = {
137+
database: 'MySQL'
138+
}
124139
const { Parser } = require('node-sql-parser');
125140
const parser = new Parser();
126-
const columnList = parser.columnList('SELECT t.id FROM t');
141+
// opt is optional
142+
const columnList = parser.columnList('SELECT t.id FROM t', opt);
127143

128144
console.log(columnList); // ["select::t::id"]
129145
```
130146

131147
### Check the SQL with Authority List
132148

133149
- check table authority
134-
- `whiteListCheck` function check on `table` mode by default
150+
- `whiteListCheck` function check on `table` mode and `MySQL` database by default
135151

136152
```javascript
137153
const { Parser } = require('node-sql-parser');
138154
const parser = new Parser();
139155
const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)'
140156
const whiteTableList = ['(select|update)::(.*)::(a|b)'] // array that contain multiple authorities
141-
parser.whiteListCheck(sql, whiteTableList, 'table') // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
157+
const opt = {
158+
database: 'MySQL',
159+
type: 'table',
160+
}
161+
// opt is optional
162+
parser.whiteListCheck(sql, whiteTableList, opt) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
142163
```
143164

144165
- check column authority
@@ -148,7 +169,12 @@ const { Parser } = require('node-sql-parser');
148169
const parser = new Parser();
149170
const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)'
150171
const whiteColumnList = ['select::null::name', 'update::a::id'] // array that contain multiple authorities
151-
parser.whiteListCheck(sql, whiteColumnList, 'column') // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
172+
const opt = {
173+
database: 'MySQL',
174+
type: 'column',
175+
}
176+
// opt is optional
177+
parser.whiteListCheck(sql, whiteColumnList, opt) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
152178
```
153179

154180
## :kissing_heart: Acknowledgement

build.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const peg = require('pegjs')
4+
5+
const parserFolder = path.join(__dirname, 'pegjs')
6+
const PARSER_FILE = /(.*)\.pegjs$/
7+
8+
fs.readdirSync(parserFolder)
9+
.filter(file => PARSER_FILE.test(file))
10+
.forEach(file => {
11+
const [, name] = file.match(PARSER_FILE)
12+
const source = fs.readFileSync(path.join(parserFolder, file), 'utf8')
13+
const parser = peg.generate(source, {
14+
format: 'commonjs',
15+
output: 'source',
16+
})
17+
fs.writeFileSync(path.join(__dirname, `build/${name}.js`), parser)
18+
})

index.d.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Type definitions for node-sql-parser 1.0
2+
// Project: https://github.com/taozhi8833998/node-sql-parser#readme
3+
// Definitions by: taozhi8833998 <https://github.com/taozhi8833998>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
// TypeScript Version: 2.4
6+
7+
export interface With {
8+
name: string;
9+
stmt: any[];
10+
columns?: any[];
11+
}
12+
export type WhilteListCheckMode = 'table' | 'column';
13+
export interface Option {
14+
database?: string,
15+
type?: string,
16+
}
17+
export interface TableColumnAst {
18+
tableList: string[];
19+
columnsList: string[];
20+
ast: AST[] | AST;
21+
}
22+
export interface From {
23+
db: string | null;
24+
table: string;
25+
as: string | null;
26+
}
27+
export interface Dual {
28+
type: 'dual';
29+
}
30+
export interface Limit {
31+
type: string;
32+
value: number;
33+
}
34+
export interface OrderBy {
35+
type: 'ASC' | 'DESC';
36+
expr: any;
37+
}
38+
export interface ColumnRef {
39+
type: 'column_ref';
40+
table: string | null;
41+
column: string;
42+
}
43+
export interface SetList {
44+
column: string;
45+
value: any;
46+
table: string | null;
47+
}
48+
export interface InsertReplaceValue {
49+
type: 'expr_list';
50+
value: any[];
51+
}
52+
export interface Select {
53+
with: With | null;
54+
type: 'select';
55+
options: any[] | null;
56+
distinct: 'DISTINCT' | null;
57+
columns: any[] | '*';
58+
from: Array<From | Dual> | null;
59+
where: any;
60+
groupby: ColumnRef[] | null;
61+
having: any[] | null;
62+
orderby: OrderBy[] | null;
63+
limit: Limit[] | null;
64+
}
65+
export interface Insert_Replace {
66+
type: 'replace' | 'insert';
67+
db: string | null;
68+
table: string;
69+
columns: string[] | null;
70+
values: InsertReplaceValue[];
71+
}
72+
export interface Update {
73+
type: 'udpate';
74+
db: string | null;
75+
table: string;
76+
set: SetList[];
77+
where: any;
78+
}
79+
export interface Delete {
80+
type: 'delete';
81+
tables: any;
82+
from: Array<From | Dual>;
83+
where: any;
84+
}
85+
86+
export interface Alter {
87+
type: 'alter',
88+
table: From,
89+
expr: any
90+
}
91+
92+
export type AST = Select | Insert_Replace | Update | Delete | Alter;
93+
94+
export class Parser {
95+
constructor();
96+
97+
parse(sql: string, opt?: Option): TableColumnAst;
98+
99+
astify(sql: string, opt?: Option): AST[] | AST;
100+
101+
sqlify(ast: AST[] | AST, opt?: Option): string;
102+
103+
whiteListCheck(sql: string, whiteList: string[], opt?: Option): Error | undefined;
104+
105+
tableList(sql: string, opt?: Option): string[];
106+
107+
columnList(sql: string, opt?: Option): string[];
108+
}

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "1.5.2",
3+
"version": "1.6.0",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8-
"build": "mkdir -p build && pegjs -o build/pegjs-parser.js sql.pegjs && npm run compile",
9-
"test": "NODE_ENV=test npm run lint && mocha --require @babel/register",
8+
"build": "mkdir -p build && node build.js && npm run compile",
9+
"test": "NODE_ENV=test npm run lint && mocha --recursive --require @babel/register",
1010
"compile": "babel src -d lib",
1111
"lint": "npm run build && eslint src/",
1212
"prepublishOnly": "npm run build",
13+
"coverLocal": "nyc npm run test",
1314
"cover": "nyc npm run test && nyc report --reporter=text-lcov | coveralls"
1415
},
1516
"repository": {
@@ -27,7 +28,8 @@
2728
"files": [
2829
"index.js",
2930
"lib/",
30-
"build/pegjs-parser.js",
31+
"index.d.ts",
32+
"build/*.js",
3133
"README.md",
3234
"LICENSE"
3335
],

0 commit comments

Comments
 (0)