Skip to content

Commit 0098e28

Browse files
committed
feature: support restore semicolon.
1 parent a914ba6 commit 0098e28

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# js-sql-parser
22

3-
> parse sql (select grammar) in js.
3+
> parse / stringify sql (select grammar) in js.
44
55
[![Build Status][travis-image]][travis-url]
66
[![NPM Version][npm-image]][npm-url]
@@ -29,11 +29,17 @@ sql grammar follows https://dev.mysql.com/doc/refman/5.7/en/select.html
2929

3030
`npm install --save js-sql-parser`
3131

32-
```
32+
```js
3333
const parser = require('js-sql-parser');
3434
const ast = parser.parse('select * from dual');
3535

3636
console.log(JSON.stringify(ast, null, 2));
37+
38+
ast.value.selectItems.value[0].value = 'foo';
39+
ast.value.from.value[0].value.value.value = 'bar';
40+
41+
console.log(parser.stringify(ast));
42+
// SELECT foo FROM bar
3743
```
3844

3945
## script tag
@@ -42,7 +48,8 @@ console.log(JSON.stringify(ast, null, 2));
4248
<script src="./dist/parser/sqlParser.js"><script/>
4349

4450
var sqlParser = window.sqlParser;
45-
sqlParser.parse('select * from dual');
51+
var ast = sqlParser.parse('select * from dual');
52+
var sql = sqlParser.stringify(ast);
4653
```
4754

4855
## AMD support

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"babel-preset-es2015": "^6.22.0",
2626
"babel-register": "^6.22.0",
2727
"benchmark": "^2.1.3",
28+
"debug": "^3.1.0",
2829
"jison": "^0.4.17",
2930
"mocha": "^3.2.0"
3031
}

src/sqlParser.jison

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ LIMIT return 'LIMIT'
160160

161161
main
162162
: selectClause EOF { return {nodeType: 'Main', value: $1}; }
163-
| selectClause ';' EOF { return {nodeType: 'Main', value: $1}; }
163+
| selectClause ';' EOF { return {nodeType: 'Main', value: $1, hasSemicolon: true}; }
164164
;
165165

166166
selectClause

src/stringify.js

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Sql.prototype.append = function(word, noPrefix, noSuffix) {
5656
}
5757
Sql.prototype.travelMain = function(ast) {
5858
this.travelSelect(ast.value);
59+
if (ast.hasSemicolon) {
60+
this.append(';', true);
61+
}
5962
}
6063
Sql.prototype.travelSelect = function(ast) {
6164
this.appendKeyword('select', true);

test/main.test.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const debug = false;
3+
const debug = require('debug')('js-sql-parser');
44
const parser = require('../');
55

66
const testParser = function (sql) {
@@ -15,14 +15,8 @@ const testParser = function (sql) {
1515
throw 'err firstSql don\'t equals secondSql. ';
1616
}
1717

18-
if (debug) {
19-
console.log(
20-
JSON.stringify(secondAst, null, 2)
21-
);
22-
console.log(
23-
parser.stringify(secondAst)
24-
);
25-
}
18+
debug(JSON.stringify(secondAst, null, 2));
19+
debug(parser.stringify(secondAst));
2620

2721
return secondAst;
2822
}
@@ -138,5 +132,9 @@ AND (rd.rd_numberofrooms <= (select sum(rn.reservation_numberofrooms) as count_r
138132
it ('fix not equal.', function () {
139133
testParser('select a from b where a <> 1 limit 2, 3');
140134
});
135+
136+
it ('restore semicolon.', function () {
137+
testParser('select a from b limit 2;');
138+
});
141139
});
142140

0 commit comments

Comments
 (0)