Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.firefly.kotlin.ext.db.SqlConnectionExtension.kt Maven / Gradle / Ivy
package com.firefly.kotlin.ext.db
import com.firefly.db.RecordNotFound
import com.firefly.db.SQLConnection
import com.firefly.db.SQLResultSet
import com.firefly.kotlin.ext.log.KtLogger
import com.firefly.utils.function.Func1
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.future.await
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import java.util.concurrent.TimeUnit
/**
* @author Pengtao Qiu
*/
val sysLogger = KtLogger.getLogger("firefly-system")
// query for single column
suspend fun SQLConnection.asyncQueryForSingleColumn(sql: String, vararg params: Any): T =
this.queryForSingleColumn(sql, *params).await()
suspend fun SQLConnection.asyncNamedQueryForSingleColumn(sql: String, paramMap: Map): T =
this.namedQueryForSingleColumn(sql, paramMap).await()
suspend fun SQLConnection.asyncNamedQueryForSingleColumn(sql: String, paramObject: Any): T =
this.namedQueryForSingleColumn(sql, paramObject).await()
// query for object
suspend inline fun SQLConnection.asyncQueryForObject(sql: String, vararg params: Any): T =
this.queryForObject(sql, T::class.java, *params).await()
suspend inline fun SQLConnection.asyncNamedQueryForObject(sql: String, paramMap: Map): T =
this.namedQueryForObject(sql, T::class.java, paramMap).await()
suspend inline fun SQLConnection.asyncNamedQueryForObject(sql: String, paramObject: Any): T =
this.namedQueryForObject(sql, T::class.java, paramObject).await()
// query by id
suspend inline fun SQLConnection.asyncQueryById(id: Any): T = this.queryById(id, T::class.java).await()
// query for bean map
suspend inline fun SQLConnection.asyncQueryForBeanMap(sql: String, vararg params: Any): Map =
this.queryForBeanMap(sql, V::class.java, *params).await() ?: mapOf()
suspend inline fun SQLConnection.asyncNamedQueryForBeanMap(
sql: String,
paramMap: Map
): Map =
this.namedQueryForBeanMap(sql, V::class.java, paramMap).await() ?: mapOf()
suspend inline fun SQLConnection.asyncNamedQueryForBeanMap(sql: String, paramObject: Any): Map =
this.namedQueryForBeanMap(sql, V::class.java, paramObject).await() ?: mapOf()
// query for list
suspend inline fun SQLConnection.asyncQueryForList(sql: String, vararg params: Any): List =
this.queryForList(sql, T::class.java, *params).await() ?: listOf()
suspend inline fun SQLConnection.asyncNamedQueryForList(sql: String, paramMap: Map): List =
this.namedQueryForList(sql, T::class.java, paramMap).await() ?: listOf()
suspend inline fun SQLConnection.asyncNamedQueryForList(sql: String, paramObject: Any): List =
this.namedQueryForList(sql, T::class.java, paramObject).await() ?: listOf()
// query
suspend fun SQLConnection.asyncQuery(sql: String, rsh: (rs: SQLResultSet) -> T, vararg params: Any): T =
this.query(sql, Func1 { rsh.invoke(it) }, *params).await()
suspend fun SQLConnection.asyncNamedQuery(
sql: String,
rsh: (rs: SQLResultSet) -> T,
paramMap: Map
): T =
this.namedQuery(sql, { rsh.invoke(it) }, paramMap).await()
suspend fun SQLConnection.asyncNamedQuery(sql: String, rsh: (rs: SQLResultSet) -> T, paramObject: Any): T =
this.namedQuery(sql, { rsh.invoke(it) }, paramObject).await()
// update
suspend fun SQLConnection.asyncUpdate(sql: String, vararg params: Any): Int {
return this.update(sql, *params).await() ?: 0
}
suspend fun SQLConnection.asyncNamedUpdate(sql: String, paramMap: Map): Int =
this.namedUpdate(sql, paramMap).await() ?: 0
suspend fun SQLConnection.asyncNamedUpdate(sql: String, paramObject: Any): Int =
this.namedUpdate(sql, paramObject).await() ?: 0
// update mapped object
suspend fun SQLConnection.asyncUpdateObject(obj: T): Int {
return this.updateObject(obj).await() ?: 0
}
// insert
suspend fun SQLConnection.asyncInsert(sql: String, vararg params: Any): R = this.insert(sql, *params).await()
suspend fun SQLConnection.asyncNamedInsert(sql: String, paramMap: Map): R =
this.namedInsert(sql, paramMap).await()
suspend fun SQLConnection.asyncNamedInsert(sql: String, paramObject: Any): R =
this.namedInsert(sql, paramObject).await()
suspend fun SQLConnection.asyncInsertObject(obj: T): R = this.insertObject(obj).await()
suspend inline fun SQLConnection.asyncInsertObjectBatch(list: List, rsh: Func1): R =
this.insertObjectBatch(list, T::class.java, rsh).await()
suspend inline fun SQLConnection.asyncInsertObjectBatch(list: List): List =
this.insertObjectBatch(list, T::class.java).await() ?: listOf()
// delete
suspend inline fun SQLConnection.asyncDeleteById(id: Any): Int =
this.deleteById(id, T::class.java).await() ?: 0
suspend fun SQLConnection.execSQL(
time: Long = 10000L, unit: TimeUnit = TimeUnit.MILLISECONDS,
handler: suspend (conn: SQLConnection) -> T
): T {
beginTransaction().await()
return try {
val ret = withTimeout(unit.toMillis(time)) { handler.invoke(this@execSQL) }
commitAndEndTransaction().await()
ret
} catch (e: RecordNotFound) {
sysLogger.warn("execute SQL exception. record not found", e)
commitAndEndTransaction().await()
throw e
} catch (e: TimeoutCancellationException) {
sysLogger.error("execute SQL exception. timeout", e)
rollbackAndEndTransaction().await()
throw e
} catch (e: Exception) {
sysLogger.error("execute SQL exception", e)
rollbackAndEndTransaction().await()
throw e
}
}
suspend fun T.safeUse(block: suspend (T) -> R): R {
try {
return block(this)
} finally {
withContext(NonCancellable) {
commitAndClose().await()
}
}
}