Skip to content

Commit dec1f54

Browse files
authored
Merge pull request #59 from JavaScriptor/feat-placeholder-support
release: v1.5.0
2 parents f2524a0 + 203c5de commit dec1f54

File tree

8 files changed

+35
-19
lines changed

8 files changed

+35
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ package-lock.json
44
*.log
55
*.swp
66
dist/parser/sqlParser.js
7+
.vscode/

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
1.3.0 fix tableFactor alias bug. AST changed in tableFactor. #34
1111
1.4.0 fix bug `using ' & " for column alias?` #40 #44
1212
1.4.1 hogfix "support quoted alias: multiple alias and orderby support"
13+
1.5.0 support feature placeholder.

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
publish: test
22
@npm publish
33

4-
test:
4+
test:
55
@npm test
66

7+
test-with-log:
8+
@DEBUG=js-sql-parser npm test
9+
710
.PHONY: publish test

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ sql grammar follows https://dev.mysql.com/doc/refman/5.7/en/select.html
1010

1111
## news
1212

13+
- Support feature `PlaceHolder like ${param}` since v1.5.0 [#43](https://github.com/JavaScriptor/js-sql-parser/pull/43)
1314
- Fix bug `using ' & " for column alias?` since v1.4.1 [#40](https://github.com/JavaScriptor/js-sql-parser/issues/40), [#44](https://github.com/JavaScriptor/js-sql-parser/issues/44)
1415
- Fix bug tableFactor alias since v1.3.0 [#34](https://github.com/JavaScriptor/js-sql-parser/issues/34)
1516
- Add support for "`" quoted alias since v1.2.2. [#33](https://github.com/JavaScriptor/js-sql-parser/issues/33)
@@ -35,6 +36,18 @@ console.log(parser.stringify(ast));
3536
// SELECT foo FROM bar
3637
```
3738

39+
```js
40+
// placeholder test
41+
const parser = require('js-sql-parser');
42+
const ast = parser.parse('select ${a} as a');
43+
44+
ast['value']['selectItems']['value'][0]['value'] = "'value'";
45+
console.log(parser.stringify(ast));
46+
// SELECT 'value' AS a
47+
```
48+
49+
Note: PlaceHolder is an `literal` value but not an `identifier`. Table_name / column_name / function_name are `identifier` thus should NOT be placed with placeholder.
50+
3851
## script tag
3952

4053
```js
@@ -60,10 +73,6 @@ var sql = sqlParser.stringify(ast);
6073
- intervalexpr: Date INTERVAL keyword. // to support
6174
- into outfile: INTO OUTFILE keyword. // to support
6275

63-
## TODO
64-
65-
- ${value} like value place holder support.
66-
6776
## Build
6877

6978
- Run `npm run build` to build the distributable.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-sql-parser",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"description": "",
55
"main": "./dist/parser/sqlParser.js",
66
"scripts": {

src/sqlParser.jison

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[#]\s.*\n /* skip sql comments */
1212
\s+ /* skip whitespace */
1313

14-
[$][{](.*?)[}] return 'PLACE_HOLDER'
14+
[$][{](.+?)[}] return 'PLACE_HOLDER'
1515
[`][a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]*[`] return 'IDENTIFIER'
1616
[\w]+[\u4e00-\u9fa5]+[0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER'
1717
[\u4e00-\u9fa5][0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER'

src/stringify.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -552,18 +552,6 @@ Sql.prototype.travelSelectParenthesized = function(ast) {
552552
this.appendKeyword(')');
553553
};
554554
Sql.prototype.travelPlaceHolder = function (ast) {
555-
if (ast.left) {
556-
this.travel(ast.left);
557-
}
558-
559-
if (ast.operator) {
560-
this.append(ast.operator);
561-
}
562-
563-
if (ast.right) {
564-
this.append(ast.right);
565-
}
566-
567555
if (ast.value) {
568556
this.travel(ast.value);
569557
}

test/main.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const debug = require('debug')('js-sql-parser');
44
const parser = require('../');
5+
const assert = require('assert');
56

67
const testParser = function (sql) {
78
let firstAst = parser.parse(sql);
@@ -416,6 +417,19 @@ describe('select grammar support', function () {
416417
"select sum(quota_value) value, busi_col2 as sh, ${a} as a, YEAR(now()) from der_quota_summary where table_ename = 'gshmyyszje_derivedidx' and cd = (select id from t1 where a = ${t1})"
417418
)
418419
});
420+
it('place holder support2', function() {
421+
testParser(
422+
"select sum(quota_value) b, busi_col2 as sh, '${a}' as a, YEAR(now()) from der_quota_summary where table_ename = 'gshmyyszje_derivedidx' and cd = (select id from t1 where a = '${t1}')"
423+
)
424+
});
425+
it('place holder support3', function() {
426+
let firstAst = parser.parse('select ${a} as a');
427+
firstAst['value']['selectItems']['value'][0]['value'] = "'value'";
428+
let firstSql = parser.stringify(firstAst);
429+
debug(JSON.stringify(firstAst, null, 2));
430+
assert.equal(firstSql.trim().toUpperCase(), "select 'value' as a".toUpperCase());
431+
testParser(firstSql);
432+
});
419433

420434
it('support quoted alias: multiple alias and orderby support', function () {
421435
testParser('select a as `A A`, b as `B B` from z');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy