Skip to content

Commit 37eb3aa

Browse files
committed
db name using `
1 parent ba6bb32 commit 37eb3aa

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

lib/sql.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function tablesToSQL(tables) {
172172
if (baseTable.type === 'dual') return 'DUAL'
173173
let str = baseTable.table ? identifierToSql(baseTable.table) : exprToSQL(baseTable.expr)
174174

175-
if (baseTable.db && baseTable.db !== null) str = `${baseTable.db}.${str}`
175+
if (baseTable.db && baseTable.db !== null) str = `${identifierToSql(baseTable.db)}.${str}`
176176
if (baseTable.as !== null) str = `${str} AS ${identifierToSql(baseTable.as)}`
177177

178178
clauses.push(str)
@@ -183,7 +183,7 @@ function tablesToSQL(tables) {
183183
str = (joinExpr.join && joinExpr.join !== null) ? ` ${joinExpr.join} ` : str = ', '
184184

185185
if (joinExpr.table) {
186-
if (joinExpr.db !== null) str = `${str}${joinExpr.db}.`
186+
if (joinExpr.db !== null) str = `${str}${identifierToSql(joinExpr.db)}.`
187187
str = `${str}${identifierToSql(joinExpr.table)}`
188188
} else {
189189
str = `${str}${exprToSQL(joinExpr.expr)}`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"scripts": {

test/ast.spec.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ describe('AST', () => {
1717
expect(getParsedSql('SELECT SQL_CALC_FOUND_ROWS SQL_BUFFER_RESULT col1 FROM t'))
1818
.to.equal('SELECT SQL_CALC_FOUND_ROWS SQL_BUFFER_RESULT `col1` FROM `t`');
1919
});
20+
it('should support MySQL query options', () => {
21+
expect(getParsedSql(`SELECT xx.dd, Max(IF(stat_key = 'yys', stat_us, 0)) AS 'yys_users' FROM waf.t_cpkg WHERE stat_ty = 'waf_ty' GROUP BY dd;`))
22+
.to.equal("SELECT `xx`.`dd`, MAX(IF(`stat_key` = 'yys', `stat_us`, 0)) AS `yys_users` FROM `waf`.`t_cpkg` WHERE `stat_ty` = 'waf_ty' GROUP BY `dd`");
23+
});
2024

2125
it('should support select *from ast to sql', () => {
2226
expect(getParsedSql('SELECT *FROM abc'))
@@ -245,7 +249,7 @@ describe('AST', () => {
245249
it('should support joins with tables from other databases', () => {
246250
sql = 'SELECT col1 FROM t JOIN otherdb.awesome_table at ON t.id = at.tid';
247251
expect(getParsedSql(sql))
248-
.to.equal('SELECT `col1` FROM `t` INNER JOIN otherdb.`awesome_table` AS `at` ON `t`.`id` = `at`.`tid`');
252+
.to.equal('SELECT `col1` FROM `t` INNER JOIN `otherdb`.`awesome_table` AS `at` ON `t`.`id` = `at`.`tid`');
249253
});
250254

251255
it('should support aliases in joins', () => {
@@ -273,11 +277,11 @@ describe('AST', () => {
273277
['<', '<=', '=', '!=', '>=', '>'].forEach((operator) => {
274278
it(`should support simple "${operator}" comparison`, () => {
275279
sql = `SELECT a fRom db.t wHERE "type" ${operator} 3`;
276-
expect(getParsedSql(sql)).to.equal(`SELECT \`a\` FROM db.\`t\` WHERE 'type' ${operator} 3`);
280+
expect(getParsedSql(sql)).to.equal(`SELECT \`a\` FROM \`db\`.\`t\` WHERE 'type' ${operator} 3`);
277281
});
278282
it(`should support simple "${operator}" comparison`, () => {
279283
sql = `SELECT a fRom db.t wHERE id ${operator} 3`;
280-
expect(getParsedSql(sql)).to.equal(`SELECT \`a\` FROM db.\`t\` WHERE \`id\` ${operator} 3`);
284+
expect(getParsedSql(sql)).to.equal(`SELECT \`a\` FROM \`db\`.\`t\` WHERE \`id\` ${operator} 3`);
281285
});
282286
});
283287

@@ -622,11 +626,11 @@ describe('AST', () => {
622626
['<', '<=', '=', '!=', '>=', '>'].forEach((operator) => {
623627
it(`should support simple "${operator}" comparison`, () => {
624628
sql = `DELETE a fRom db.t wHERE "type" ${operator} 3`;
625-
expect(getParsedSql(sql)).to.equal(`DELETE \`a\` FROM db.\`t\` WHERE 'type' ${operator} 3`);
629+
expect(getParsedSql(sql)).to.equal(`DELETE \`a\` FROM \`db\`.\`t\` WHERE 'type' ${operator} 3`);
626630
});
627631
it(`should support simple "${operator}" comparison`, () => {
628632
sql = `DELETE a fRom db.t wHERE id ${operator} 3`;
629-
expect(getParsedSql(sql)).to.equal(`DELETE \`a\` FROM db.\`t\` WHERE \`id\` ${operator} 3`);
633+
expect(getParsedSql(sql)).to.equal(`DELETE \`a\` FROM \`db\`.\`t\` WHERE \`id\` ${operator} 3`);
630634
});
631635
});
632636

0 commit comments

Comments
 (0)