node-sql-parser
Advanced tools
Comparing version 1.0.0 to 1.1.0
'use strict' | ||
const parseFn = require('../build/pegjs-parser').parse | ||
const astToSQL = require('./sql') | ||
class Parser { | ||
sqlToAst(sql) { | ||
astify(sql) { | ||
const astInfo = this.parse(sql) | ||
@@ -11,2 +12,6 @@ return astInfo && astInfo.ast | ||
sqlify(ast) { | ||
return astToSQL(ast) | ||
} | ||
parse(sql) { | ||
@@ -13,0 +18,0 @@ return parseFn(sql) |
@@ -137,2 +137,7 @@ 'use strict' | ||
function intervalToSQL(expr) { | ||
const [intervalNum, unit] = expr.value | ||
return `INTERVAL ${intervalNum} ${unit}` | ||
} | ||
/** | ||
@@ -223,20 +228,12 @@ * Stringify column expressions | ||
function setToSQL(sets) { | ||
const baseTable = sets[0] | ||
if (!sets || sets.length === 0) return '' | ||
const clauses = [] | ||
let str = '' | ||
if (baseTable.column) str = `\`${baseTable.column}\`` | ||
str = baseTable.value && (str += ` = ${exprToSQL(baseTable.value)}`) | ||
clauses.push(str) | ||
for (let i = 1; i < sets.length; ++i) { | ||
const joinExpr = sets[i] | ||
if (joinExpr.column) str = `, \`${joinExpr.column}\`` | ||
str = joinExpr.value && (str += ` = ${exprToSQL(joinExpr.value)}`) | ||
for (const set of sets) { | ||
let str = '' | ||
const { table, column, value } = set | ||
if (column) str = table ? `\`${table}\`.\`${column}\`` : `\`${column}\`` | ||
if (value) str = `${str} = ${exprToSQL(value)}` | ||
clauses.push(str) | ||
} | ||
return clauses.join('') | ||
return clauses.join(', ') | ||
} | ||
@@ -301,3 +298,2 @@ | ||
if (Array.isArray(stmt.from)) clauses.push('FROM', tablesToSQL(stmt.from)) | ||
if (has(stmt, 'where') && stmt.where !== null) clauses.push(`WHERE ${exprToSQL(stmt.where)}`) | ||
@@ -311,3 +307,2 @@ | ||
if (has(stmt, 'table') && stmt.table !== null) clauses.push(identifierToSql(stmt.table, false)) | ||
if (Array.isArray(stmt.tables)) clauses.push(tablesToSQL(stmt.tables)) | ||
if (Array.isArray(stmt.set)) clauses.push('SET', setToSQL(stmt.set)) | ||
@@ -323,3 +318,2 @@ | ||
if (has(stmt, 'table') && stmt.table !== null) clauses.push(identifierToSql(stmt.table, false)) | ||
if (Array.isArray(stmt.tables)) clauses.push(tablesToSQL(stmt.tables)) | ||
if (Array.isArray(stmt.columns)) clauses.push(`(${stmt.columns.map(identifierToSql).join(', ')})`) | ||
@@ -367,2 +361,3 @@ if (Array.isArray(stmt.values)) clauses.push('VALUES', valuesToSQL(stmt.values)) | ||
function : funcToSQL, | ||
interval : intervalToSQL, | ||
unary_expr : unaryToSQL, | ||
@@ -369,0 +364,0 @@ expr_list : expr => { |
'use strict' | ||
const astToSQL = require('./sql') | ||
/** | ||
@@ -76,3 +74,2 @@ * @param {(Array|boolean|string|number|null)} value | ||
replaceParams : (ast, params) => replaceParams(JSON.parse(JSON.stringify(ast)), params), | ||
astToSQL, | ||
} |
{ | ||
"name": "node-sql-parser", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "simple node sql parser", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
@@ -45,7 +46,7 @@ "build": "mkdir -p build && pegjs -o build/pegjs-parser.js sql.pegjs", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.10.0", | ||
"eslint": "^5.12.1", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-strict": "^14.0.1", | ||
"eslint-plugin-filenames": "^1.3.2", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"istanbul": "^0.4.5", | ||
@@ -52,0 +53,0 @@ "mocha": "^5.2.0", |
# Nodejs SQL Parser | ||
[![Build Status](https://travis-ci.org/taozhi8833998/node-sql-parser.svg?branch=master)](https://travis-ci.org/taozhi8833998/node-sql-parser) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/dff0b2ee1b964d2d88fe6947c4f5c649)](https://app.codacy.com/app/taozhi8833998/node-sql-parser?utm_source=github.com&utm_medium=referral&utm_content=taozhi8833998/node-sql-parser&utm_campaign=Badge_Grade_Dashboard) | ||
[![Coverage Status](https://img.shields.io/coveralls/github/taozhi8833998/node-sql-parser/master.svg)](https://coveralls.io/github/taozhi8833998/node-sql-parser?branch=master) | ||
[![Dependencies](https://img.shields.io/david/taozhi8833998/node-sql-parser.svg)](https://img.shields.io/david/taozhi8833998/node-sql-parser) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/taozhi8833998/node-sql-parser/badge.svg?targetFile=package.json)](https://snyk.io/test/github/taozhi8833998/node-sql-parser?targetFile=package.json) | ||
[![](https://img.shields.io/badge/Powered%20by-ganjiang-brightgreen.svg)](https://github.com/taozhi8833998/node-sql-parser) | ||
[![Build Status](https://travis-ci.org/taozhi8833998/node-sql-parser.svg?branch=master)](https://travis-ci.org/taozhi8833998/node-sql-parser) | ||
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/taozhi8833998/node-sql-parser/blob/master/LICENSE) | ||
[![npm version](https://badge.fury.io/js/node-sql-parser.svg)](https://badge.fury.io/js/node-sql-parser) | ||
[![NPM downloads](http://img.shields.io/npm/dm/node-sql-parser.svg?style=flat-square)](http://www.npmtrends.com/node-sql-parser) | ||
[![Coverage Status](https://img.shields.io/coveralls/github/taozhi8833998/node-sql-parser/master.svg)](https://coveralls.io/github/taozhi8833998/node-sql-parser?branch=master) | ||
[![Dependencies](https://img.shields.io/david/taozhi8833998/node-sql-parser.svg)](https://img.shields.io/david/taozhi8833998/node-sql-parser) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/taozhi8833998/node-sql-parser/badge.svg?targetFile=package.json)](https://snyk.io/test/github/taozhi8833998/node-sql-parser?targetFile=package.json) | ||
[![](https://img.shields.io/gitter/room/taozhi8833998/node-sql-parser.svg)](https://gitter.im/node-sql-parser/community) | ||
[![issues](https://img.shields.io/github/issues/taozhi8833998/node-sql-parser.svg)](https://github.com/taozhi8833998/node-sql-parser/issues) | ||
[![TypeScript definitions on DefinitelyTyped](http://definitelytyped.org/badges/standard.svg)](http://definitelytyped.org) | ||
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/taozhi8833998/node-sql-parser/blob/master/LICENSE) | ||
**Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList, columnList and convert it back to SQL.** | ||
@@ -18,5 +23,6 @@ | ||
- support multiple sql statement seperate by semicolon | ||
- support select, delete, update and insert type | ||
- output the table and column list that the sql visited with the corresponding authority | ||
- support multiple sql statement seperate by semicolon | ||
- support select, delete, update and insert type | ||
- output the table and column list that the sql visited with the corresponding authority | ||
- support typescript | ||
@@ -27,4 +33,18 @@ ## :tada: Install | ||
npm install node-sql-parser --save | ||
or | ||
yarn add node-sql-parser | ||
``` | ||
**Install the following type module for typescript usage** | ||
```bash | ||
npm install @types/node-sql-parser --save-dev | ||
or | ||
yarn add @types/node-sql-parser --dev | ||
``` | ||
## :rocket: Usage | ||
@@ -37,3 +57,3 @@ | ||
const parser = new Parser(); | ||
const ast = parser.sqlToAst('SELECT * FROM t'); | ||
const ast = parser.astify('SELECT * FROM t'); | ||
@@ -43,6 +63,25 @@ console.log(ast); | ||
### Convert AST back to SQL | ||
```javascript | ||
const { Parser } = require('node-sql-parser'); | ||
const parser = new Parser() | ||
const ast = parser.astify('SELECT * FROM t'); | ||
const sql = parse.sqlify(ast); | ||
console.log(sql); // SELECT * FROM `t` | ||
``` | ||
### TableList, ColumnList, Ast | ||
```javascript | ||
const { Parser } = require('node-sql-parser'); | ||
const parser = new Parser() | ||
const { tableList, columnList, ast } = parser.parse('SELECT * FROM t'); | ||
``` | ||
### Get the SQL visited tables | ||
- get the table list that the sql visited | ||
- the format is **{type}::{dbName}::{tableName}** // type could be select, update, delete or insert | ||
- get the table list that the sql visited | ||
- the format is **{type}::{dbName}::{tableName}** // type could be select, update, delete or insert | ||
@@ -59,5 +98,5 @@ ```javascript | ||
- get the column list that the sql visited | ||
- the format is **{type}::{tableName}::{columnName}** // type could be select, update, delete or insert | ||
- for `select *`, `delete` and `insert into tableName values()` without specified columns, the `.*` column authority regex is required | ||
- get the column list that the sql visited | ||
- the format is **{type}::{tableName}::{columnName}** // type could be select, update, delete or insert | ||
- for `select *`, `delete` and `insert into tableName values()` without specified columns, the `.*` column authority regex is required | ||
@@ -74,4 +113,4 @@ ```javascript | ||
- check table authority | ||
- `whiteListCheck` function check on `table` mode by default | ||
- check table authority | ||
- `whiteListCheck` function check on `table` mode by default | ||
@@ -86,3 +125,3 @@ ```javascript | ||
- check column authority | ||
- check column authority | ||
@@ -97,13 +136,2 @@ ```javascript | ||
### Convert AST back to SQL | ||
```javascript | ||
const { Parser, util } = require('node-sql-parser'); | ||
const parser = new Parser() | ||
const ast = parser.sqlToAst('SELECT * FROM t'); | ||
const sql = util.astToSQL(ast); | ||
console.log(sql); // SELECT * FROM `t` | ||
``` | ||
## :kissing_heart: Acknowledgement | ||
@@ -110,0 +138,0 @@ |
Sorry, the diff of this file is too big to display
335655
12011
136