forked from ctripcorp/SQLlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.kt
329 lines (269 loc) · 11.5 KB
/
Database.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (C) 2022 Ctrip.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ctrip.sqllin.dsl
import com.ctrip.sqllin.driver.DatabaseConfiguration
import com.ctrip.sqllin.driver.DatabasePath
import com.ctrip.sqllin.driver.openDatabase
import com.ctrip.sqllin.dsl.sql.*
import com.ctrip.sqllin.dsl.sql.clause.*
import com.ctrip.sqllin.dsl.sql.operation.Delete
import com.ctrip.sqllin.dsl.sql.operation.Insert
import com.ctrip.sqllin.dsl.sql.operation.Update
import com.ctrip.sqllin.dsl.sql.operation.Select
import com.ctrip.sqllin.dsl.sql.statement.*
import com.ctrip.sqllin.dsl.sql.statement.DatabaseExecuteEngine
import com.ctrip.sqllin.dsl.sql.statement.TransactionStatementsGroup
import kotlinx.serialization.KSerializer
import kotlinx.serialization.modules.EmptySerializersModule
import kotlinx.serialization.serializer
import kotlin.jvm.Volatile
/**
* Database object
* @author yaqiao
*/
@Suppress("UNCHECKED_CAST")
public class Database(
configuration: DatabaseConfiguration,
) {
public constructor(
name: String,
path: DatabasePath,
version: Int,
) : this(
DatabaseConfiguration(
name = name,
path = path,
version = version,
)
)
private val databaseConnection = openDatabase(configuration)
/**
* Close the database connection.
*/
public fun close(): Unit = databaseConnection.close()
/**
* Start a scope with this database object that used for execute SQL.
*/
public operator fun <T> invoke(block: Database.() -> T): T {
val result = block()
executeAllStatement()
return result
}
/**
* Transaction.
*/
@Volatile
private var transactionStatementsGroup: TransactionStatementsGroup? = null
private inline val isInTransaction
get() = transactionStatementsGroup != null
public fun beginTransaction(): Boolean {
if (isInTransaction)
return false
transactionStatementsGroup = TransactionStatementsGroup(databaseConnection)
executeEngine.addStatement(transactionStatementsGroup!!)
return true
}
public fun endTransaction() {
transactionStatementsGroup = null
}
public inline fun <T> transaction(block: Database.() -> T): T {
beginTransaction()
try {
return block()
} finally {
endTransaction()
}
}
/**
* SQL execute.
*/
private val executeEngine = DatabaseExecuteEngine()
private fun addStatement(statement: SingleStatement) {
if (isInTransaction)
transactionStatementsGroup!!.addStatement(statement)
else
executeEngine.addStatement(statement)
}
private fun <T> addSelectStatement(statement: SelectStatement<T>) {
if (unionSelectStatementGroupStack.isNotEmpty)
(unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).addSelectStatement(statement)
else
addStatement(statement)
}
private fun executeAllStatement() = executeEngine.executeAllStatement()
/**
* Insert.
*/
public infix fun <T> Table<T>.INSERT(entities: Iterable<T>) {
val statement = Insert.insert(this, databaseConnection, entities)
addStatement(statement)
}
public infix fun <T> Table<T>.INSERT(entity: T): Unit =
INSERT(listOf(entity))
/**
* Update.
*/
public infix fun <T> Table<T>.UPDATE(clause: SetClause<T>): UpdateStatementWithoutWhereClause<T> =
transactionStatementsGroup?.let {
val statement = Update.update(this, databaseConnection, it, clause)
it addStatement statement
statement
} ?: Update.update(this, databaseConnection, executeEngine, clause).also {
executeEngine addStatement it
}
/**
* Delete.
*/
public infix fun Table<*>.DELETE(x: X) {
val statement = Delete.deleteAllEntity(this, databaseConnection)
addStatement(statement)
}
public infix fun <T> Table<T>.DELETE(clause: WhereClause<T>) {
val statement = Delete.delete(this, databaseConnection, clause)
addStatement(statement)
}
/**
* Select.
*/
/**
* Select with no any clause.
*/
public inline infix fun <reified T> Table<T>.SELECT(x: X): FinalSelectStatement<T> =
select(getKSerializer(), false)
public inline infix fun <reified T> Table<T>.SELECT_DISTINCT(x: X): FinalSelectStatement<T> =
select(getKSerializer(), true)
public fun <T> Table<T>.select(serializer: KSerializer<T>, isDistinct: Boolean): FinalSelectStatement<T> {
val container = getSelectStatementGroup()
val statement = Select.select(this, isDistinct, serializer, databaseConnection, container)
addSelectStatement(statement)
return statement
}
/**
* Receive the 'WHERE' clause.
*/
public inline infix fun <reified T> Table<T>.SELECT(clause: WhereClause<T>): WhereSelectStatement<T> =
select(getKSerializer(), clause, false)
public inline infix fun <reified T> Table<T>.SELECT_DISTINCT(clause: WhereClause<T>): WhereSelectStatement<T> =
select(getKSerializer(), clause, true)
public fun <T> Table<T>.select(serializer: KSerializer<T>, clause: WhereClause<T>, isDistinct: Boolean): WhereSelectStatement<T> {
val container = getSelectStatementGroup()
val statement = Select.select(this, clause, isDistinct, serializer, databaseConnection, container)
addSelectStatement(statement)
return statement
}
/**
* Receive the 'ORDER BY' clause.
*/
public inline infix fun <reified T> Table<T>.SELECT(clause: OrderByClause<T>): OrderBySelectStatement<T> =
select(getKSerializer(), clause, false)
public inline infix fun <reified T> Table<T>.SELECT_DISTINCT(clause: OrderByClause<T>): OrderBySelectStatement<T> =
select(getKSerializer(), clause, true)
public fun <T> Table<T>.select(serializer: KSerializer<T>, clause: OrderByClause<T>, isDistinct: Boolean): OrderBySelectStatement<T> {
val container = getSelectStatementGroup()
val statement = Select.select(this, clause, isDistinct, serializer, databaseConnection, container)
addSelectStatement(statement)
return statement
}
/**
* Receive the 'LIMIT' clause.
*/
public inline infix fun <reified T> Table<T>.SELECT(clause: LimitClause<T>): LimitSelectStatement<T> =
select(getKSerializer(), clause, false)
public inline infix fun <reified T> Table<T>.SELECT_DISTINCT(clause: LimitClause<T>): LimitSelectStatement<T> =
select(getKSerializer(), clause, true)
public fun <T> Table<T>.select(serializer: KSerializer<T>, clause: LimitClause<T>, isDistinct: Boolean): LimitSelectStatement<T> {
val container = getSelectStatementGroup()
val statement = Select.select(this, clause, isDistinct, serializer, databaseConnection, container)
addSelectStatement(statement)
return statement
}
/**
* Receive the 'GROUP BY' clause.
*/
public inline infix fun <reified T> Table<T>.SELECT(clause: GroupByClause<T>): GroupBySelectStatement<T> =
select(getKSerializer(), clause, false)
public inline infix fun <reified T> Table<T>.SELECT_DISTINCT(clause: GroupByClause<T>): GroupBySelectStatement<T> =
select(getKSerializer(), clause, true)
public fun <T> Table<T>.select(serializer: KSerializer<T>, clause: GroupByClause<T>, isDistinct: Boolean): GroupBySelectStatement<T> {
val container = getSelectStatementGroup()
val statement = Select.select(this, clause, isDistinct, serializer, databaseConnection, container)
addSelectStatement(statement)
return statement
}
public inline fun <reified T> getKSerializer(): KSerializer<T> = EmptySerializersModule().serializer()
/**
* The 'UNION' clause of Select.
*/
private val unionSelectStatementGroupStack by lazy { Stack<UnionSelectStatementGroup<*>>() }
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executeEngine
public inline fun <T> Table<T>.UNION(block: Table<T>.(Table<T>) -> Unit): FinalSelectStatement<T> {
beginUnion<T>()
var selectStatement: SelectStatement<T>? = null
try {
block(this)
selectStatement = createUnionSelectStatement(false)
return selectStatement
} finally {
endUnion(selectStatement)
}
}
public inline fun <T> Table<T>.UNION_ALL(block: Table<T>.(Table<T>) -> Unit): FinalSelectStatement<T> {
beginUnion<T>()
var selectStatement: SelectStatement<T>? = null
try {
block(this)
selectStatement = createUnionSelectStatement(true)
return selectStatement
} finally {
endUnion(selectStatement)
}
}
public fun <T> beginUnion() {
unionSelectStatementGroupStack.push(UnionSelectStatementGroup<T>())
}
public fun <T> createUnionSelectStatement(isUnionAll: Boolean): FinalSelectStatement<T> {
check(unionSelectStatementGroupStack.isNotEmpty) { "Please invoke the 'beginUnion' before you invoke this function!!!" }
return (unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).unionStatements(isUnionAll)
}
public fun <T> endUnion(selectStatement: SelectStatement<T>?) {
unionSelectStatementGroupStack.pop()
selectStatement?.let { addSelectStatement(it) }
}
/**
* Receive the 'JOIN' clause.
*/
public inline infix fun <T, reified R> Table<T>.SELECT(clause: JoinClause<R>): JoinStatementWithoutCondition<R> =
select(getKSerializer(), clause, false)
public inline infix fun <T, reified R> Table<T>.SELECT_DISTINCT(clause: JoinClause<R>): JoinStatementWithoutCondition<R> =
select(getKSerializer(), clause, true)
public fun <T, R> Table<T>.select(serializer: KSerializer<R>, clause: JoinClause<R>, isDistinct: Boolean): JoinStatementWithoutCondition<R> {
val container = getSelectStatementGroup()
return Select.select(this, clause, isDistinct, serializer, databaseConnection, container, ::addSelectStatement)
}
/**
* Receive the natural join clause(includes 'NATURAL LEFT OUTER JOIN' and 'NATURAL INNER JOIN').
*/
public inline infix fun <T, reified R> Table<T>.SELECT(clause: NaturalJoinClause<R>): JoinSelectStatement<R> =
select(getKSerializer(), clause, false)
public inline infix fun <T, reified R> Table<T>.SELECT_DISTINCT(clause: NaturalJoinClause<R>): JoinSelectStatement<R> =
select(getKSerializer(), clause, true)
public fun <T, R> Table<T>.select(serializer: KSerializer<R>, clause: NaturalJoinClause<R>, isDistinct: Boolean): JoinSelectStatement<R> {
val container = getSelectStatementGroup()
val statement = Select.select(this, clause, isDistinct, serializer, databaseConnection, container)
addSelectStatement(statement)
return statement
}
}