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

com.sxtanna.db.ext.extensions.kt Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
package com.sxtanna.db.ext

import com.sxtanna.db.struct.SqlType.Attribute
import com.sxtanna.db.struct.SqlType.Attribute.*
import com.sxtanna.db.struct.statement.*
import java.sql.ResultSet
import kotlin.internal.OnlyInputTypes
import kotlin.reflect.KProperty1
import kotlin.reflect.full.findAnnotation

// I am not documenting this internal crap, NEXT!

internal fun Boolean.value(input : String?) = if (this) input ?: "" else ""

internal fun  Boolean.value(input : T?) = if (this) input else null


//region Property attribute retrievable
internal fun KProperty1<*, *>.isNotNull() : Attribute<*>? {
    return returnType.isMarkedNullable.not().value(SqlNotNull)
}

internal fun KProperty1<*, *>.isPrimary() : Attribute<*>? {
    return findAnnotation()?.let { SqlPrimary }
}

internal fun KProperty1<*, *>.isUnsigned() : Attribute<*>? {
    return findAnnotation()?.let { SqlUnsigned }
}
//endregion


//region ResultSet whileNext implementations
/**
 * Execute this block of code for every result in the set
 */
inline fun ResultSet.whileNext(block : ResultSet.() -> Unit) {
    while (this.next()) this.block()
}

/**
 * Map each result in the set to an object and return a list of them
 */
inline fun  ResultSet.mapWhileNext(mapper : ResultSet.() -> O) : List {
    val output = mutableListOf()
    this.whileNext { output.add(this.mapper()) }

    return output
}
//endregion


//region Kuery `Value`
/**
 * Create a Column|Value association with strict typing
 */
infix fun  KProperty1.value(value : R) = Value(this, value)

/**
 * Column|Value association
 */
data class Value internal constructor(val prop : KProperty1, val value : R)
//endregion


//region Select `ForEach` implementations
/**
 * Performs a given action on each [R1]
 */
fun  Select1<*, List>.forEach(block : (R1) -> Unit) {
    val (r1) = this
    r1.forEach(block)
}

/**
 * Performs a given action on each [R1] and [R2]
 */
fun  Select2<*, List, List>.forEach(block : (R1, R2) -> Unit) {
    val (r1, r2) = this
    r1.forEachIndexed { index, it -> block(it, r2[index]) }
}

/**
 * Performs a given action on each [R1], [R2], and [R3]
 */
fun  Select3<*, List, List, List>.forEach(block : (R1, R2, R3) -> Unit) {
    val (r1, r2, r3) = this
    r1.forEachIndexed { index, it -> block(it, r2[index], r3[index]) }
}

/**
 * Performs a given action on each [R1], [R2], [R3], and [R4]
 */
fun  Select4<*, List, List, List, List>.forEach(block : (R1, R2, R3, R4) -> Unit) {
    val (r1, r2, r3, r4) = this
    r1.forEachIndexed { index, it -> block(it, r2[index], r3[index], r4[index]) }
}

/**
 * Performs a given action on each [R1], [R2], [R3], [R4], and [R5]
 */
fun  Select5<*, List, List, List, List, List>.forEach(block : (R1, R2, R3, R4, R5) -> Unit) {
    val (r1, r2, r3, r4, r5) = this
    r1.forEachIndexed { index, it -> block(it, r2[index], r3[index], r4[index], r5[index]) }
}
//endregion




© 2015 - 2024 Weber Informatics LLC | Privacy Policy