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

com.lightningkite.ktordb.Condition.ext.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.ktordb

import kotlin.reflect.KProperty1


fun Condition<*>.referencesField(field: KProperty1<*, *>): Boolean = when (this) {
    is Condition.OnField<*, *> -> this.key.name == field.name || this.condition.referencesField(field)
    is Condition.And -> conditions.any { it.referencesField(field) }
    is Condition.Or -> conditions.any { it.referencesField(field) }
    is Condition.Not -> condition.referencesField(field)
    is Condition.AllElements<*> -> condition.referencesField(field)
    is Condition.AnyElements<*> -> condition.referencesField(field)
    is Condition.OnKey<*> -> condition.referencesField(field)
    is Condition.IfNotNull<*> -> condition.referencesField(field)
    else -> false
}
fun Modification<*>.referencesField(field: KProperty1<*, *>): Boolean = when (this) {
    is Modification.Chain -> modifications.any { it.referencesField(field) }
    is Modification.IfNotNull -> modification.referencesField(field)
    is Modification.PerElement<*> -> condition.referencesField(field)
    is Modification.ModifyByKey<*> -> map.values.any {it.referencesField(field)}
    is Modification.OnField<*, *> -> this.key.name == field.name || modification.referencesField(field)
    else -> false
}

fun Modification<*>.referencesFieldRead(field: KProperty1<*, *>): Boolean = when (this) {
    is Modification.Chain -> modifications.any { it.referencesFieldRead(field) }
    is Modification.Remove<*> -> this.condition.referencesField(field)
    is Modification.PerElement<*> -> condition.referencesField(field)
    is Modification.ModifyByKey<*> -> map.values.any { it.referencesFieldRead(field) }
    is Modification.OnField<*, *> -> modification.referencesFieldRead(field)
    else -> false
}

fun  Condition.forField(field: KProperty1): Condition =
    forFieldOrNull(field) ?: Condition.Always()

fun  Condition.forFieldOrNull(field: KProperty1): Condition? {
    return when (this) {
        is Condition.And -> conditions.mapNotNull { it.forFieldOrNull(field) }.takeUnless { it.isEmpty() }
            ?.let { Condition.And(it) }
        is Condition.Or -> conditions.mapNotNull { it.forFieldOrNull(field) }.takeUnless { it.isEmpty() }
            ?.let { Condition.Or(it) }
        is Condition.Not -> condition.forFieldOrNull(field)?.let { Condition.Not(it) }
        is Condition.OnField<*, *> -> if (this.key == field) this.condition as Condition else null
        else -> null
    }
}

fun  Condition>.perElement(): Condition = when (this) {
    is Condition.AnyElements -> this.condition
    else -> Condition.Always()
}

fun  Condition.probablySingleResult(): Boolean = when (this) {
    is Condition.Always -> false
    is Condition.Never -> true
    is Condition.OnField<*, *> -> this.key.name == "_id" && when (val c = condition) {
        is Condition.Equal -> true
        is Condition.Inside -> c.values.size <= 1
        else -> false
    }
    is Condition.Or -> conditions.all { it.probablySingleResult() }
    is Condition.And -> conditions.any { it.probablySingleResult() }
    else -> false
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy