Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Oct 28, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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']))
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.