Skip to content

Commit

Permalink
Merge pull request #43 from sockstack/add-place-holder-support
Browse files Browse the repository at this point in the history
`${value}` like place holder support
  • Loading branch information
albin3 authored Jul 30, 2021
2 parents 09b15e4 + 4200a15 commit f2524a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sqlParser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[#]\s.*\n /* skip sql comments */
\s+ /* skip whitespace */

[$][{](.*?)[}] return 'PLACE_HOLDER'
[`][a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]*[`] return 'IDENTIFIER'
[\w]+[\u4e00-\u9fa5]+[0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER'
[\u4e00-\u9fa5][0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER'
Expand Down Expand Up @@ -304,6 +305,7 @@ literal
| number { $$ = $1 }
| boolean { $$ = $1 }
| null { $$ = $1 }
| place_holder { $$ = $1 }
;
function_call
: IDENTIFIER '(' function_call_param_list ')' { $$ = {type: 'FunctionCall', name: $1, params: $3} }
Expand Down Expand Up @@ -587,3 +589,6 @@ table_factor
| '(' selectClause ')' aliasOpt { $$ = { type: 'TableFactor', value: { type: 'SubQuery', value: $2 }, alias: $4.alias, hasAs: $4.hasAs} }
| '(' table_references ')' { $$ = $2; $$.hasParentheses = true }
;
place_holder
: PLACE_HOLDER { $$ = { type: 'PlaceHolder', value: $1, param: $1.slice(2, -1)} }
;
17 changes: 17 additions & 0 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,20 @@ Sql.prototype.travelSelectParenthesized = function(ast) {
this.travel(ast.value);
this.appendKeyword(')');
};
Sql.prototype.travelPlaceHolder = function (ast) {
if (ast.left) {
this.travel(ast.left);
}

if (ast.operator) {
this.append(ast.operator);
}

if (ast.right) {
this.append(ast.right);
}

if (ast.value) {
this.travel(ast.value);
}
};
5 changes: 5 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ describe('select grammar support', function () {
it('bugfix table alias2', function () {
testParser('select a.* from a t1 join b t2 on t1.a = t2.a')
});
it('place holder support', function() {
testParser(
"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})"
)
});

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

0 comments on commit f2524a0

Please sign in to comment.