From 7f723e1e107c747cef98a90986021f4bcf69f904 Mon Sep 17 00:00:00 2001 From: Christiaan Westerbeek Date: Mon, 11 May 2015 10:40:39 +0200 Subject: [PATCH] Add support for SELECT TOP construct for mssql dialect for limit --- lib/Dialects/mssql.js | 2 ++ lib/Remove.js | 25 +++++++++++++-------- lib/Select.js | 23 ++++++++++++------- test/integration/test-dialect-mssql.js | 6 +++++ test/integration/test-dialect-mysql.js | 6 +++++ test/integration/test-dialect-postgresql.js | 6 +++++ test/integration/test-dialect-sqlite.js | 6 +++++ 7 files changed, 57 insertions(+), 17 deletions(-) diff --git a/lib/Dialects/mssql.js b/lib/Dialects/mssql.js index 69a3ada..e49f592 100644 --- a/lib/Dialects/mssql.js +++ b/lib/Dialects/mssql.js @@ -66,3 +66,5 @@ exports.escapeVal = function (val, timeZone) { }; exports.defaultValuesStmt = "DEFAULT VALUES"; + +exports.limitAsTop = true; diff --git a/lib/Remove.js b/lib/Remove.js index 6fe367b..cc4aa55 100644 --- a/lib/Remove.js +++ b/lib/Remove.js @@ -25,7 +25,12 @@ function RemoveQuery(Dialect, opts) { build: function () { var query = [], tmp; - query.push("DELETE FROM"); + // limit as: SELECT TOP n (MSSQL only) + if (Dialect.limitAsTop && sql.hasOwnProperty("limit")) { + query.push("DELETE TOP " + sql.limit + " FROM"); + } else { + query.push("DELETE FROM"); + } query.push(Dialect.escapeId(sql.table)); query = query.concat(Where.build(Dialect, sql.where, opts)); @@ -46,15 +51,17 @@ function RemoveQuery(Dialect, opts) { } } - // limit - if (sql.hasOwnProperty("limit")) { - if (sql.hasOwnProperty("offset")) { - query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset); - } else { - query.push("LIMIT " + sql.limit); + // limit for all Dialects but MSSQL + if (!Dialect.limitAsTop) { + if (sql.hasOwnProperty("limit")) { + if (sql.hasOwnProperty("offset")) { + query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset); + } else { + query.push("LIMIT " + sql.limit); + } + } else if (sql.hasOwnProperty("offset")) { + query.push("OFFSET " + sql.offset); } - } else if (sql.hasOwnProperty("offset")) { - query.push("OFFSET " + sql.offset); } return query.join(" "); diff --git a/lib/Select.js b/lib/Select.js index 205b9b7..a06ef03 100644 --- a/lib/Select.js +++ b/lib/Select.js @@ -210,6 +210,11 @@ function SelectQuery(Dialect, opts) { query.push("SELECT"); + // limit as: SELECT TOP n (MSSQL only) + if (Dialect.limitAsTop && sql.hasOwnProperty("limit")) { + query.push("TOP " + sql.limit); + } + for (i = 0; i < sql.from.length; i++) { sql.from[i].a = "t" + (i + 1); } @@ -399,15 +404,17 @@ function SelectQuery(Dialect, opts) { } } - // limit - if (sql.hasOwnProperty("limit")) { - if (sql.hasOwnProperty("offset")) { - query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset); - } else { - query.push("LIMIT " + sql.limit); + // limit for all Dialects but MSSQL + if (!Dialect.limitAsTop) { + if (sql.hasOwnProperty("limit")) { + if (sql.hasOwnProperty("offset")) { + query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset); + } else { + query.push("LIMIT " + sql.limit); + } + } else if (sql.hasOwnProperty("offset")) { + query.push("OFFSET " + sql.offset); } - } else if (sql.hasOwnProperty("offset")) { - query.push("OFFSET " + sql.offset); } return query.join(" "); diff --git a/test/integration/test-dialect-mssql.js b/test/integration/test-dialect-mssql.js index 1542b7e..b737bbb 100644 --- a/test/integration/test-dialect-mssql.js +++ b/test/integration/test-dialect-mssql.js @@ -98,3 +98,9 @@ assert.equal( dialect.defaultValuesStmt, "DEFAULT VALUES" ); + +//Assert that mssql is configured to use the SELECT TOP as a contruct for limit +assert.equal( + dialect.limitAsTop, + true +); \ No newline at end of file diff --git a/test/integration/test-dialect-mysql.js b/test/integration/test-dialect-mysql.js index f174c70..fa7b836 100644 --- a/test/integration/test-dialect-mysql.js +++ b/test/integration/test-dialect-mysql.js @@ -98,3 +98,9 @@ assert.equal( dialect.defaultValuesStmt, "VALUES()" ); + +//For all dialects but mssql limitAsTop should be undefined or false +assert.equal( + dialect.limitAsTop || false, + false +); \ No newline at end of file diff --git a/test/integration/test-dialect-postgresql.js b/test/integration/test-dialect-postgresql.js index 685b7f0..8fd1e30 100644 --- a/test/integration/test-dialect-postgresql.js +++ b/test/integration/test-dialect-postgresql.js @@ -103,3 +103,9 @@ assert.equal( dialect.defaultValuesStmt, "DEFAULT VALUES" ); + +//For all dialects but mssql limitAsTop should be undefined or false +assert.equal( + dialect.limitAsTop || false, + false +); \ No newline at end of file diff --git a/test/integration/test-dialect-sqlite.js b/test/integration/test-dialect-sqlite.js index d25e72e..5327706 100644 --- a/test/integration/test-dialect-sqlite.js +++ b/test/integration/test-dialect-sqlite.js @@ -98,3 +98,9 @@ assert.equal( dialect.defaultValuesStmt, "DEFAULT VALUES" ); + +//For all dialects but mssql limitAsTop should be undefined or false +assert.equal( + dialect.limitAsTop || false, + false +); \ No newline at end of file