Skip to content
Permalink
Browse files

feat: wrap subQuery with parenthesis when it appears as table name (#…

  • Loading branch information
edvardchen authored and kibertoad committed Oct 28, 2019
1 parent 1f4d8f2 commit 0560959fa42fa960f163760653bd2f107c0de1b8
Showing with 70 additions and 3 deletions.
  1. +2 −2 lib/formatter.js
  2. +4 −1 lib/query/compiler.js
  3. +33 −0 test/integration/builder/selects.js
  4. +31 −0 test/unit/query/builder.js
@@ -171,8 +171,8 @@ class Formatter {

// Puts the appropriate wrapper around a value depending on the database
// engine, unless it's a knex.raw value, in which case it's left alone.
wrap(value) {
const raw = this.unwrapRaw(value);
wrap(value, isParameter) {
const raw = this.unwrapRaw(value, isParameter);
if (raw) return raw;
switch (typeof value) {
case 'function':
@@ -865,7 +865,10 @@ Object.defineProperty(QueryCompiler.prototype, 'tableName', {

if (tableName && schemaName) tableName = `${schemaName}.${tableName}`;

this._tableName = tableName ? this.formatter.wrap(tableName) : '';
this._tableName = tableName
? // Wrap subQuery with parenthesis, #3485
this.formatter.wrap(tableName, tableName instanceof QueryBuilder)
: '';
}
return this._tableName;
},
@@ -1380,5 +1380,38 @@ module.exports = function(knex) {
}
}
});

it('select from subquery', async function() {
const subquery = knex.from('accounts').whereBetween('id', [3, 5]);
return knex
.pluck('id')
.orderBy('id')
.from(subquery)
.then(
(rows) => {
expect(rows).to.deep.equal([3, 4, 5]);
expect(knex.client.driverName).to.oneOf(['sqlite3', 'oracledb']);
},
(e) => {
switch (knex.client.driverName) {
case 'mysql':
case 'mysql2':
expect(e.errno).to.equal(1248);
break;
case 'pg':
expect(e.message).to.contain('must have an alias');
break;

case 'mssql':
expect(e.message).to.contain(
"Incorrect syntax near the keyword 'order'"
);
break;
default:
throw e;
}
}
);
});
});
};
@@ -8179,6 +8179,37 @@ describe('QueryBuilder', () => {
);
});

it('should always wrap subquery with parenthesis', () => {
const subquery = qb().select(raw('?', ['inner raw select']), 'bar');
testsql(
qb()
.select(raw('?', ['outer raw select']))
.from(subquery),
{
mysql: {
sql: 'select ? from (select ?, `bar`)',
bindings: ['outer raw select', 'inner raw select'],
},
mssql: {
sql: 'select ? from (select ?, [bar])',
bindings: ['outer raw select', 'inner raw select'],
},
oracledb: {
sql: 'select ? from (select ?, "bar")',
bindings: ['outer raw select', 'inner raw select'],
},
pg: {
sql: 'select ? from (select ?, "bar")',
bindings: ['outer raw select', 'inner raw select'],
},
'pg-redshift': {
sql: 'select ? from (select ?, "bar")',
bindings: ['outer raw select', 'inner raw select'],
},
}
);
});

it('correctly orders parameters when selecting from subqueries, #704', () => {
const subquery = qb()
.select(raw('? as f', ['inner raw select']))

0 comments on commit 0560959

Please sign in to comment.
You can’t perform that action at this time.