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+ }
0 commit comments