/
jdbc-sqlite.kt
366 lines (287 loc) · 13.5 KB
/
jdbc-sqlite.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
@file:Suppress("KDocMissingDocumentation") // fixme add later
package net.aquadc.properties.sql
import net.aquadc.properties.MutableProperty
import net.aquadc.properties.Property
import net.aquadc.properties.internal.ManagedProperty
import net.aquadc.properties.internal.Manager
import net.aquadc.properties.internal.Unset
import net.aquadc.properties.propertyOf
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Statement
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.concurrent.getOrSet
// TODO: support dialects
// TODO: evicting stale objects
class JdbcSqliteSession(private val connection: Connection) : Session {
init {
connection.autoCommit = false
}
private val lock = ReentrantReadWriteLock()
private val recordManager = RecordManager()
// transactional things, guarded by write-lock
private var transaction: JdbcTransaction? = null
private val insertStatements = HashMap<Pair<Table<*, *>, List<Col<*, *>>>, PreparedStatement>()
private val updateStatements = HashMap<Pair<Table<*, *>, Col<*, *>>, PreparedStatement>()
private fun <REC : Record<REC, *>> insertStatementWLocked(table: Table<REC, *>, cols: Array<Col<REC, *>>): PreparedStatement =
insertStatements.getOrPut(Pair(table, cols.asList())) {
connection.prepareStatement(insertQuery(table, cols), Statement.RETURN_GENERATED_KEYS)
}
private fun <REC : Record<REC, *>> updateStatementWLocked(table: Table<REC, *>, col: Col<REC, *>): PreparedStatement =
updateStatements.getOrPut(Pair(table, col)) {
connection.prepareStatement(updateQuery(table, col))
}
override fun beginTransaction(): Transaction {
val wLock = lock.writeLock()
check(!wLock.isHeldByCurrentThread) { "Thread ${Thread.currentThread()} is already in transaction" }
wLock.lock()
val tr = JdbcTransaction()
transaction = tr
return tr
}
private fun onTransactionEnd(successful: Boolean) {
val transaction = transaction ?: throw AssertionError()
try {
if (successful) connection.commit() else connection.rollback()
this.transaction = null
transaction.updated?.forEach { (col, pkToVal) ->
pkToVal.forEach { (pk, value) ->
@Suppress("UPPER_BOUND_VIOLATED")
fieldOfInternal<Any?, Any?, Any?>(col as Col<Any?, Any?>, pk).commit(value)
}
}
transaction.inserted?.forEach { (table, pk) ->
// todo
}
} finally {
lock.writeLock().unlock()
}
}
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") // using finalization guard
private inner class JdbcTransaction : java.lang.Object(), Transaction {
private val createdAt = Exception()
private var thread: Thread? = Thread.currentThread()
private var isSuccessful = false
override val session: Session
get() = this@JdbcSqliteSession
// table : primary keys
internal var inserted: HashMap<Table<*, *>, ArrayList<Any>>? = null
// column : primary key : value
internal var updated: HashMap<Col<*, *>, HashMap<Any, Any?>>? = null
override fun <REC : Record<REC, ID>, ID : IdBound> insert(table: Table<REC, ID>, vararg contentValues: ColValue<REC, *>): ID {
checkOpenAndThread()
val size = contentValues.size
val vals = arrayOfNulls<Any>(size)
val cols = arrayOfNulls<Col<REC, *>>(size)
scatter(contentValues, colsToFill = cols, valsToFill = vals)
cols as Array<Col<REC, *>> // non-nulls, I PROVE IT
val statement = insertStatementWLocked(table, cols)
vals.forEachIndexed { idx, x -> statement.setObject(idx + 1, x) }
check(statement.executeUpdate() == 1)
val keys = statement.generatedKeys
val id: ID = keys.fetchSingle()
inserted ?: HashMap<Table<*, *>, ArrayList<Any>>().also { inserted = it }
.getOrPut(table, ::ArrayList)
.add(id)
// writes all insertion fields as updates
val updated = updated ?: HashMap<Col<*, *>, HashMap<Any, Any?>>().also { updated = it }
contentValues.forEach {
updated.getOrPut(it.col, ::HashMap)[it.col] = it.value
}
return id
}
override fun <REC : Record<REC, ID>, ID : IdBound, T> update(table: Table<REC, ID>, id: ID, column: Col<REC, T>, value: T) {
checkOpenAndThread()
val statement = updateStatementWLocked(table, column)
statement.setObject(1, value)
statement.setObject(2, id)
check(statement.executeUpdate() == 1)
(updated ?: HashMap<Col<*, *>, HashMap<Any, Any?>>().also { updated = it })
.getOrPut(column, ::HashMap)[id] = value
}
override fun setSuccessful() {
isSuccessful = true
}
override fun close() {
checkOpenAndThread()
onTransactionEnd(isSuccessful)
thread = null
}
private fun checkOpenAndThread() {
check(thread == Thread.currentThread()) {
if (thread == null) "this transaction was already closed" else "called from wrong thread"
}
}
override fun finalize() {
check(thread == null) {
throw IllegalStateException("unclosed transaction being finalized, originally created at", createdAt)
}
}
}
override fun <REC : Record<REC, ID>, ID : IdBound> find(table: Table<REC, ID>, id: ID): REC? {
val localId = localId(table, id)
val records = recordManager.entities.getOrPut(table, ::ConcurrentHashMap) as ConcurrentHashMap<Long, REC>
return records.getOrPut(localId) { table.create(this, id) } // TODO: check whether exists
}
private val selectStatements = ThreadLocal<HashMap<String, PreparedStatement>>()
private val selections = Vector<MutableProperty<out Selection<*, *>>>() // coding like in 1995, yay!
override fun <REC : Record<REC, ID>, ID : IdBound> select(
table: Table<REC, ID>, condition: WhereCondition<out REC>
): Property<List<REC>> { // TODO DiffProperty
val primaryKeys = cachedSelectStmt(selectStatements, table.idCol, table, condition)
.fetchAll<ID>()
.toTypedArray<Any>() as Array<ID>
return Selection(this, table, condition, primaryKeys)
.let(::propertyOf)
.also { selections.add(it) }
}
private val countStatements = ThreadLocal<HashMap<String, PreparedStatement>>()
private val counts = Vector<MutableProperty<Long>>() // coding like in 1995, yay!
override fun <REC : Record<REC, ID>, ID : IdBound> count(table: Table<REC, ID>, condition: WhereCondition<out REC>): Property<Long> {
return cachedSelectStmt(countStatements, null, table, condition)
.fetchSingle<Number>()
.let { propertyOf(it.toLong()) }
.also { counts.add(it) }
}
private fun <ID : IdBound, REC : Record<REC, ID>> cachedSelectStmt(
statements: ThreadLocal<HashMap<String, PreparedStatement>>,
column: Col<REC, *>?,
table: Table<REC, ID>, condition: WhereCondition<out REC>
): ResultSet {
val query = selectQuery(column, table, condition)
return statements
.getOrSet(::HashMap)
.getOrPut(query) { connection.prepareStatement(query) }
.also { it.bind(ArrayList<Any>().also(condition::appendValuesTo)) }
.executeQuery()
}
override fun <REC : Record<REC, ID>, ID : IdBound, T> fieldOf(col: Col<REC, T>, id: ID): MutableProperty<T> =
fieldOfInternal(col, id) // just erase type
fun <REC : Record<REC, ID>, ID : IdBound, T> fieldOfInternal(
col: Col<REC, T>, id: ID
): ManagedProperty<T, Col<REC, T>> {
val tableCols =
recordManager.records.getOrPut(col, ::ConcurrentHashMap) as ConcurrentHashMap<Long, ManagedProperty<T, Col<REC, T>>>
val localId = localId(col.table as Table<REC, ID>, id)
return tableCols.getOrPut(localId) {
ManagedProperty(recordManager as Manager<Col<REC, T>, T>, col, localId)
}
}
private fun <ID : IdBound> localId(table: Table<*, ID>, id: ID): Long = when (id) {
is Int -> id.toLong()
is Long -> id
else -> TODO("${id.javaClass} keys support")
}
private fun <ID : IdBound> dbId(table: Table<*, ID>, localId: Long): ID =
localId as ID
private inner class RecordManager : Manager<Col<*, *>, Any?> {
/*
* table: records
* recordId: record
*/
internal val entities = ConcurrentHashMap<Table<*, *>, ConcurrentHashMap<Long, Any>>()
/*
* col: records
* record: fields
*/
internal val records = ConcurrentHashMap<Col<*, *>, ConcurrentHashMap<Long, ManagedProperty<*, *>>>()
private val statements = ThreadLocal<HashMap<String, PreparedStatement>>()
@Suppress("UPPER_BOUND_VIOLATED")
private val reusableCond = ThreadLocal<WhereCondition.ColCond<Any, Any>>()
override fun getDirty(token: Col<*, *>, id: Long): Any? {
val transaction = transaction ?: return Unset
val thisCol = transaction.updated?.get(token) ?: return Unset
val primaryKey = dbId(token.table, id)
return if (thisCol.containsKey(primaryKey)) thisCol[primaryKey] else Unset
}
@Suppress("UPPER_BOUND_VIOLATED")
override fun getClean(token: Col<*, *>, id: Long): Any? {
val condition = reusableCond.getOrSet {
WhereCondition.ColCond<Any, Any>(token as Col<Any, Any>, " = ?", Unset)
}
condition.col = token.table.idCol as Col<Any, Any>
condition.valueOrValues = id
return cachedSelectStmt<Any, Any>(statements, token as Col<Any, *>, token.table as Table<Any, Any>, condition)
.fetchSingle()
}
@Suppress("UPPER_BOUND_VIOLATED")
override fun set(token: Col<*, *>, id: Long, expected: Any?, update: Any?): Boolean {
val transaction = transaction ?: throw IllegalStateException("This can be performed only within a transaction")
getDirty(token, id).let {
if (it === Unset) {
if (getClean(token, id) === update) return true
} else {
if (it === update) return true
}
}
transaction.update<Any?, Any?, Any?>(token.table as Table<Any?, Any?>, dbId<Any?>(token.table, id), token as Col<Any?, Any?>, update) // TODO: check expected
return true
}
}
}
private fun <REC : Record<REC, *>> scatter(
contentValues: Array<out ColValue<REC, *>>,
colsToFill: Array<Col<REC, *>?>, valsToFill: Array<Any?>) {
contentValues.forEachIndexed { i, pair ->
colsToFill[i] = pair.col
valsToFill[i] = pair.value
}
}
private fun <REC : Record<REC, *>> insertQuery(table: Table<REC, *>, cols: Array<Col<REC, *>>): String =
StringBuilder("INSERT INTO ").appendName(table.name)
.append(" (").appendNames(cols).append(") VALUES (").appendPlaceholders(cols.size).append(");")
.toString()
private fun <REC : Record<REC, *>> updateQuery(table: Table<REC, *>, col: Col<REC, *>): String =
StringBuilder("UPDATE ").appendName(table.name)
.append(" SET ").appendName(col.name).append(" = ? WHERE ").appendName(table.idCol.name).append(" = ?;")
.toString()
private fun <REC : Record<REC, *>> selectQuery(column: Col<REC, *>?, table: Table<REC, *>, condition: WhereCondition<out REC>): String {
val sb = StringBuilder("SELECT ")
.let { if (column == null) it.append("COUNT(*)") else it.appendName(column.name) }
.append(" FROM ").appendName(table.name)
.append(" WHERE ")
val afterWhere = sb.length
condition.appendTo(sb)
if (sb.length == afterWhere) sb.append('1') // no condition: SELECT "whatever" FROM "somewhere" WHERE 1
return sb.toString()
}
private fun StringBuilder.appendName(name: String) =
append('"').append(name.replace("\"", "\"\"")).append('"')
private fun <REC : Record<REC, *>> StringBuilder.appendNames(cols: Array<Col<REC, *>>): StringBuilder {
if (cols.isEmpty()) return this
cols.forEach { col ->
appendName(col.name).append(", ")
}
setLength(length - 2) // trim comma
return this
}
private fun StringBuilder.appendPlaceholders(count: Int): StringBuilder {
if (count == 0) return this
repeat(count) { append("?, ") }
setLength(length - 2) // trim comma
return this
}
private fun PreparedStatement.bind(params: ArrayList<Any>) {
for (i in params.indices)
setObject(i + 1, params[i])
}
private fun <T> ResultSet.fetchAll(): List<T> {
val values = ArrayList<Any>()
while (next())
values.add(getObject(1))
close()
return values as List<T>
}
fun <T> ResultSet.fetchSingle(): T {
try {
check(next())
return getObject(1) as T
} finally {
close()
}
}