Skip to content

Commit 29c13ca

Browse files
committed
BAEL-2582 Building DSLs in Kotlin
* added basic SQL DSL * basic SQL DSL tests
1 parent 46bad01 commit 29c13ca

2 files changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.baeldung.kotlin.dsl
2+
3+
abstract class Condition {
4+
5+
fun and(initializer: Condition.() -> Unit) {
6+
addCondition(And().apply(initializer))
7+
}
8+
9+
fun or(initializer: Condition.() -> Unit) {
10+
addCondition(Or().apply(initializer))
11+
}
12+
13+
infix fun String.eq(value: Any?) {
14+
addCondition(Eq(this, value))
15+
}
16+
17+
protected abstract fun addCondition(condition: Condition)
18+
}
19+
20+
open class CompositeCondition(private val sqlOperator: String) : Condition() {
21+
private val conditions = mutableListOf<Condition>()
22+
23+
override fun addCondition(condition: Condition) {
24+
conditions += condition
25+
}
26+
27+
override fun toString(): String {
28+
return if (conditions.size == 1) {
29+
conditions.first().toString()
30+
} else {
31+
conditions.joinToString(prefix = "(", postfix = ")", separator = " $sqlOperator ") {
32+
"$it"
33+
}
34+
}
35+
}
36+
}
37+
38+
class And : CompositeCondition("and")
39+
40+
class Or : CompositeCondition("or")
41+
42+
class Eq(private val column: String, private val value: Any?) : Condition() {
43+
44+
init {
45+
if (value != null && value !is Number && value !is String) {
46+
throw IllegalArgumentException("Only <null>, numbers and strings values can be used in the 'where' clause")
47+
}
48+
}
49+
50+
override fun addCondition(condition: Condition) {
51+
throw IllegalStateException("Can't add a nested condition to the sql 'eq'")
52+
}
53+
54+
override fun toString(): String {
55+
return when (value) {
56+
null -> "$column is null"
57+
is String -> "$column = '$value'"
58+
else -> "$column = $value"
59+
}
60+
}
61+
}
62+
63+
class SqlSelectBuilder {
64+
65+
private val columns = mutableListOf<String>()
66+
private lateinit var table: String
67+
private var condition: Condition? = null
68+
69+
fun select(vararg columns: String) {
70+
if (columns.isEmpty()) {
71+
throw IllegalArgumentException("At least one column should be defined")
72+
}
73+
if (this.columns.isNotEmpty()) {
74+
throw IllegalStateException("Detected an attempt to re-define columns to fetch. Current columns list: "
75+
+ "${this.columns}, new columns list: $columns")
76+
}
77+
this.columns.addAll(columns)
78+
}
79+
80+
fun from(table: String) {
81+
this.table = table
82+
}
83+
84+
fun where(initializer: Condition.() -> Unit) {
85+
condition = And().apply(initializer)
86+
}
87+
88+
fun build(): String {
89+
if (!::table.isInitialized) {
90+
throw IllegalStateException("Failed to build an sql select - target table is undefined")
91+
}
92+
return toString()
93+
}
94+
95+
override fun toString(): String {
96+
val columnsToFetch =
97+
if (columns.isEmpty()) {
98+
"*"
99+
} else {
100+
columns.joinToString(", ")
101+
}
102+
val conditionString =
103+
if (condition == null) {
104+
""
105+
} else {
106+
" where $condition"
107+
}
108+
return "select $columnsToFetch from $table$conditionString"
109+
}
110+
}
111+
112+
fun query(initializer: SqlSelectBuilder.() -> Unit): SqlSelectBuilder {
113+
return SqlSelectBuilder().apply(initializer)
114+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.kotlin.dsl
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.Test
5+
import java.lang.Exception
6+
7+
class SqlDslTest {
8+
9+
@Test
10+
fun `when no columns are specified then star is used`() {
11+
doTest("select * from table1") {
12+
from ("table1")
13+
}
14+
}
15+
16+
@Test
17+
fun `when no condition is specified then correct query is built`() {
18+
doTest("select column1, column2 from table1") {
19+
select("column1", "column2")
20+
from ("table1")
21+
}
22+
}
23+
24+
@Test(expected = Exception::class)
25+
fun `when no table is specified then an exception is thrown`() {
26+
query {
27+
select("column1")
28+
}.build()
29+
}
30+
31+
@Test
32+
fun `when a list of conditions is specified then it's respected`() {
33+
doTest("select * from table1 where (column3 = 4 and column4 is null)") {
34+
from ("table1")
35+
where {
36+
"column3" eq 4
37+
"column4" eq null
38+
}
39+
}
40+
}
41+
42+
@Test
43+
fun `when 'or' conditions are specified then they are respected`() {
44+
doTest("select * from table1 where (column3 = 4 or column4 is null)") {
45+
from ("table1")
46+
where {
47+
or {
48+
"column3" eq 4
49+
"column4" eq null
50+
}
51+
}
52+
}
53+
}
54+
55+
@Test
56+
fun `when either 'and' or 'or' conditions are specified then they are respected`() {
57+
doTest("select * from table1 where ((column3 = 4 or column4 is null) and column5 = 42)") {
58+
from ("table1")
59+
where {
60+
and {
61+
or {
62+
"column3" eq 4
63+
"column4" eq null
64+
}
65+
"column5" eq 42
66+
}
67+
}
68+
}
69+
}
70+
71+
private fun doTest(expected: String, sql: SqlSelectBuilder.() -> Unit) {
72+
assertThat(query(sql).build()).isEqualTo(expected)
73+
}
74+
}

0 commit comments

Comments
 (0)