node-sql-parser
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -15,2 +15,25 @@ 'use strict' | ||
whiteListCheck(sql, whiteList) { | ||
if (!whiteList || whiteList.length === 0) return | ||
const tableAuthorityList = this.tableList(sql) | ||
let hasAuthority = true | ||
let denyTable = '' | ||
for (const tableAuthority of tableAuthorityList) { | ||
let hasTableAuthority = false | ||
for (const whiteAuthority of whiteList) { | ||
const regex = new RegExp(whiteAuthority) | ||
if (regex.test(tableAuthority)) { | ||
hasTableAuthority = true | ||
break | ||
} | ||
} | ||
if (!hasTableAuthority) { | ||
denyTable = tableAuthority | ||
hasAuthority = false | ||
break | ||
} | ||
} | ||
if (!hasAuthority) throw new Error(`SQL = '${sql}' is operating data on table with authority = '${denyTable}' that do not exist in whiteList`) | ||
} | ||
tableList(sql) { | ||
@@ -17,0 +40,0 @@ const astInfo = this.parse(sql) |
{ | ||
"name": "node-sql-parser", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "simple node sql parser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,3 +35,3 @@ # GanJiang SQL Parser | ||
- get the table list the sql visit | ||
- get the table list that the sql visited | ||
- the format is **{type}::{dbName}::{tableName}** // type could be select, update, delete or insert | ||
@@ -47,2 +47,12 @@ | ||
### Check the SQL with Authority List | ||
```javascript | ||
const { Parser } = require('node-sql-parser'); | ||
const parser = new Parser(); | ||
const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)' | ||
const whiteList = ['(select|update)::(.*)::(a|b)'] // array that contain multiple authorities | ||
parser.whiteListCheck(sql, whiteList) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined | ||
``` | ||
### Convert AST back to SQL | ||
@@ -49,0 +59,0 @@ |
314441
11318
82