Skip to content

Commit 9530c47

Browse files
committed
Enable .take without .drop for MS SQL
Use `SELECT TOP(?) ...` for MS SQL when there's no offset.
1 parent a24e84e commit 9530c47

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

scalasql/src/dialects/MsSqlDialect.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ object MsSqlDialect extends MsSqlDialect {
168168
groupBy0
169169
)
170170
with Select[Q, R] {
171-
override def take(n: Int): scalasql.query.Select[Q,R] = throw new Exception(".take must follow .sortBy")
171+
override def take(n: Int): scalasql.query.Select[Q,R] = {
172+
selectWithExprPrefix(true, _ => sql"TOP($n)")
173+
}
172174

173175
override def drop(n: Int): scalasql.query.Select[Q,R] = throw new Exception(".drop must follow .sortBy")
174176
}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package scalasql.dialects
22

33
import scalasql._
4+
import sourcecode.Text
45
import utest._
56
import utils.MsSqlSuite
67

78
trait MsSqlDialectTests extends MsSqlSuite {
89
def description = "Operations specific to working with Microsoft SQL Databases"
910

10-
def tests = Tests {}
11+
def tests = Tests {
12+
13+
test("top") - checker(
14+
query = Buyer.select.take(0),
15+
sql = """
16+
SELECT TOP(?) buyer0.id AS id, buyer0.name AS name, buyer0.date_of_birth AS date_of_birth
17+
FROM buyer buyer0
18+
""",
19+
value = Seq[Buyer[Sc]](),
20+
docs = """
21+
For ScalaSql's Microsoft SQL dialect provides, the `.take(n)` operator translates
22+
into a SQL `TOP(n)` clause
23+
"""
24+
)
25+
}
1126
}

0 commit comments

Comments
 (0)