-
Notifications
You must be signed in to change notification settings - Fork 7
/
QueryCustomExpressionSpec.kt
84 lines (74 loc) · 3.42 KB
/
QueryCustomExpressionSpec.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package io.zeko.db.sql
import io.zeko.db.sql.dsl.*
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.test.assertEquals
class QueryCustomExpressionSpec : Spek({
fun debug(msg: Any) {
if (true) println(msg.toString())
}
describe("Query Builder Custom Expression test") {
context("Given a query extension forUpdate()") {
it("should match sql with Select...For Update") {
fun Query.forUpdate(): Query {
this.addExpressionAfter(CustomPart.LIMIT, QueryBlock("FOR UPDATE"))
return this
}
val sql = Query().fields("*").from("user")
.where("name" eq "Leng")
.limit(1).forUpdate()
.toSql()
debug(sql)
assertEquals("SELECT * FROM user WHERE name = ? LIMIT 1 OFFSET 0 FOR UPDATE", sql)
}
}
context("Given a query extension union()") {
it("should match sql with Select...Union Select...") {
fun Query.union(query: Query): Query {
this.addExpressionAfter(CustomPart.WHERE, QueryBlock("UNION (", query.toSql(), ")"))
return this
}
val sql = Query().fields("id", "name").from("user")
.where("name" eq "Leng")
.union(
Query().fields("id", "first_name").from("customer")
)
.order("first_name")
.toSql()
debug(sql)
assertEquals("SELECT id, name FROM user WHERE name = ? UNION ( SELECT id, first_name FROM customer ) ORDER BY first_name ASC", sql)
}
}
context("Given multiple query extension") {
it("should match sql with Select Distinct...Union Select...") {
fun Query.distinct(): Query {
this.addExpressionAfter(CustomPart.SELECT, QueryBlock("DISTINCT"))
return this
}
fun Query.union(query: Query): Query {
this.addExpressionAfter(CustomPart.WHERE, QueryBlock("UNION (", query.toSql(), ")"))
return this
}
fun Query.unionAll(query: Query): Query {
this.addExpressionAfter(CustomPart.WHERE, QueryBlock("UNION ALL (", query.toSql(), ")"))
return this
}
val sql = Query().fields("id", "last_name").from("user")
.where("name" eq "Leng")
.union(
Query().distinct().fields("id", "first_name").from("customer")
)
.unionAll(
Query().fields("id", "first_name").from("customer_blacklist").where("status" eq 1)
)
.order("user.id")
.toSql()
debug(sql)
assertEquals("SELECT id, last_name FROM user WHERE name = ? " +
"UNION ( SELECT DISTINCT id, first_name FROM customer ) " +
"UNION ALL ( SELECT id, first_name FROM customer_blacklist " +
"WHERE status = 1 ) ORDER BY user.id ASC", sql)
}
}
}
})