All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.kotlin.ext.db.SqlConnectionExtension.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
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.experimental.TimeoutCancellationException
import kotlinx.coroutines.experimental.future.await
import kotlinx.coroutines.experimental.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 {
    val isNew = beginTransaction().await()
    return try {
        val ret = withTimeout(unit.toMillis(time)) { handler.invoke(this@execSQL) }
        if (isNew) {
            commitAndEndTransaction().await()
        }
        ret
    } catch (e: RecordNotFound) {
        sysLogger.warn("execute SQL exception. record not found", e)
        if (isNew) {
            commitAndEndTransaction().await()
        }
        throw e
    } catch (e: TimeoutCancellationException) {
        sysLogger.error("execute SQL exception. timeout", e)
        (if (isNew) rollbackAndEndTransaction() else rollback()).await()
        throw e
    } catch (e: Exception) {
        sysLogger.error("execute SQL exception", e)
        (if (isNew) rollbackAndEndTransaction() else rollback()).await()
        throw e
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy