Skip to content

Commit

Permalink
Fix mssql binding ordering for queries that combine a limit with sele…
Browse files Browse the repository at this point in the history
…ct raw or update (#2066)

* Fix binding ordering for queries that combine a limit with a select raw or update

* Use old school quotes so tests pass
  • Loading branch information
dirkmc authored and elhigu committed May 16, 2017
1 parent a7639c3 commit 6795170
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/dialects/mssql/query/compiler.js
Expand Up @@ -78,11 +78,11 @@ assign(QueryCompiler_MSSQL.prototype, {

// Compiles an `update` query, allowing for a return value.
update() {
const top = this.top();
const updates = this._prepUpdate(this.single.update);
const join = this.join();
const where = this.where();
const order = this.order();
const top = this.top();
const { returning } = this.single;
return {
sql: this.with() + `update ${top ? top + ' ' : ''}${this.tableName}` +
Expand Down Expand Up @@ -116,6 +116,7 @@ assign(QueryCompiler_MSSQL.prototype, {
columns() {
let distinct = false;
if (this.onlyUnions()) return ''
const top = this.top();
const columns = this.grouped.columns || []
let i = -1, sql = [];
if (columns) {
Expand All @@ -131,7 +132,7 @@ assign(QueryCompiler_MSSQL.prototype, {
}
}
if (sql.length === 0) sql = ['*'];
const top = this.top();

return `select ${distinct ? 'distinct ' : ''}` +
(top ? top + ' ' : '') +
sql.join(', ') + (this.tableName ? ` from ${this.tableName}` : '');
Expand Down
29 changes: 27 additions & 2 deletions test/unit/query/builder.js
Expand Up @@ -1590,6 +1590,31 @@ describe("QueryBuilder", function() {
});
});

it("limits and raw selects", function() {
testsql(qb().select(raw('name = ? as isJohn', ['john'])).from('users').limit(1), {
mysql: {
sql: 'select name = ? as isJohn from `users` limit ?',
bindings: ['john', 1]
},
oracle: {
sql: 'select * from (select name = ? as isJohn from "users") where rownum <= ?',
bindings: ['john', 1]
},
mssql: {
sql: 'select top (?) name = ? as isJohn from [users]',
bindings: [1, 'john']
},
oracledb: {
sql: 'select * from (select name = ? as isJohn from "users") where rownum <= ?',
bindings: ['john', 1]
},
postgres: {
sql: 'select name = ? as isJohn from "users" limit ?',
bindings: ['john', 1]
}
});
});

it("first", function() {
testsql(qb().first('*').from('users'), {
mysql: {
Expand Down Expand Up @@ -2758,7 +2783,7 @@ describe("QueryBuilder", function() {
},
mssql: {
sql: 'update top (?) [users] set [email] = ?, [name] = ? where [id] = ? order by [foo] desc;select @@rowcount',
bindings: ['foo', 'bar', 1, 5]
bindings: [5, 'foo', 'bar', 1]
},
postgres: {
sql: 'update "users" set "email" = ?, "name" = ? where "id" = ?',
Expand Down Expand Up @@ -2793,7 +2818,7 @@ describe("QueryBuilder", function() {
},
mssql: {
sql: 'update top (?) [users] set [email] = ?, [name] = ? where [users].[id] = ?;select @@rowcount',
bindings: ['foo', 'bar', 1, 1]
bindings: [1, 'foo', 'bar', 1]
},
postgres: {
sql: 'update "users" set "email" = ?, "name" = ? where "users"."id" = ?',
Expand Down

0 comments on commit 6795170

Please sign in to comment.