Skip to content

Commit ed327ad

Browse files
committed
Merge pull request dresende#41 from devotis/master
Add support for SELECT TOP construct for mssql dialect for limit
2 parents 5dfae4d + 7f723e1 commit ed327ad

File tree

7 files changed

+57
-17
lines changed

7 files changed

+57
-17
lines changed

lib/Dialects/mssql.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ exports.escapeVal = function (val, timeZone) {
6666
};
6767

6868
exports.defaultValuesStmt = "DEFAULT VALUES";
69+
70+
exports.limitAsTop = true;

lib/Remove.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ function RemoveQuery(Dialect, opts) {
2525
build: function () {
2626
var query = [], tmp;
2727

28-
query.push("DELETE FROM");
28+
// limit as: SELECT TOP n (MSSQL only)
29+
if (Dialect.limitAsTop && sql.hasOwnProperty("limit")) {
30+
query.push("DELETE TOP " + sql.limit + " FROM");
31+
} else {
32+
query.push("DELETE FROM");
33+
}
2934
query.push(Dialect.escapeId(sql.table));
3035

3136
query = query.concat(Where.build(Dialect, sql.where, opts));
@@ -46,15 +51,17 @@ function RemoveQuery(Dialect, opts) {
4651
}
4752
}
4853

49-
// limit
50-
if (sql.hasOwnProperty("limit")) {
51-
if (sql.hasOwnProperty("offset")) {
52-
query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset);
53-
} else {
54-
query.push("LIMIT " + sql.limit);
54+
// limit for all Dialects but MSSQL
55+
if (!Dialect.limitAsTop) {
56+
if (sql.hasOwnProperty("limit")) {
57+
if (sql.hasOwnProperty("offset")) {
58+
query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset);
59+
} else {
60+
query.push("LIMIT " + sql.limit);
61+
}
62+
} else if (sql.hasOwnProperty("offset")) {
63+
query.push("OFFSET " + sql.offset);
5564
}
56-
} else if (sql.hasOwnProperty("offset")) {
57-
query.push("OFFSET " + sql.offset);
5865
}
5966

6067
return query.join(" ");

lib/Select.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ function SelectQuery(Dialect, opts) {
210210

211211
query.push("SELECT");
212212

213+
// limit as: SELECT TOP n (MSSQL only)
214+
if (Dialect.limitAsTop && sql.hasOwnProperty("limit")) {
215+
query.push("TOP " + sql.limit);
216+
}
217+
213218
for (i = 0; i < sql.from.length; i++) {
214219
sql.from[i].a = "t" + (i + 1);
215220
}
@@ -399,15 +404,17 @@ function SelectQuery(Dialect, opts) {
399404
}
400405
}
401406

402-
// limit
403-
if (sql.hasOwnProperty("limit")) {
404-
if (sql.hasOwnProperty("offset")) {
405-
query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset);
406-
} else {
407-
query.push("LIMIT " + sql.limit);
407+
// limit for all Dialects but MSSQL
408+
if (!Dialect.limitAsTop) {
409+
if (sql.hasOwnProperty("limit")) {
410+
if (sql.hasOwnProperty("offset")) {
411+
query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset);
412+
} else {
413+
query.push("LIMIT " + sql.limit);
414+
}
415+
} else if (sql.hasOwnProperty("offset")) {
416+
query.push("OFFSET " + sql.offset);
408417
}
409-
} else if (sql.hasOwnProperty("offset")) {
410-
query.push("OFFSET " + sql.offset);
411418
}
412419

413420
return query.join(" ");

test/integration/test-dialect-mssql.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ assert.equal(
9898
dialect.defaultValuesStmt,
9999
"DEFAULT VALUES"
100100
);
101+
102+
//Assert that mssql is configured to use the SELECT TOP as a contruct for limit
103+
assert.equal(
104+
dialect.limitAsTop,
105+
true
106+
);

test/integration/test-dialect-mysql.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ assert.equal(
9898
dialect.defaultValuesStmt,
9999
"VALUES()"
100100
);
101+
102+
//For all dialects but mssql limitAsTop should be undefined or false
103+
assert.equal(
104+
dialect.limitAsTop || false,
105+
false
106+
);

test/integration/test-dialect-postgresql.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,9 @@ assert.equal(
103103
dialect.defaultValuesStmt,
104104
"DEFAULT VALUES"
105105
);
106+
107+
//For all dialects but mssql limitAsTop should be undefined or false
108+
assert.equal(
109+
dialect.limitAsTop || false,
110+
false
111+
);

test/integration/test-dialect-sqlite.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ assert.equal(
9898
dialect.defaultValuesStmt,
9999
"DEFAULT VALUES"
100100
);
101+
102+
//For all dialects but mssql limitAsTop should be undefined or false
103+
assert.equal(
104+
dialect.limitAsTop || false,
105+
false
106+
);

0 commit comments

Comments
 (0)