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

io.tcds.orm.statement.Statement.kt Maven / Gradle / Ivy

Go to download

Kotlin Simple ORM is a simple but powerful database orm for kotlin applications

There is a newer version: 0.3.14
Show newest version
package io.tcds.orm.statement

import io.tcds.orm.Condition
import io.tcds.orm.Param
import io.tcds.orm.column.DateTimeColumn
import io.tcds.orm.extension.*
import java.time.LocalDateTime

data class Statement(val conditions: MutableList>) {
    companion object {
        fun  deletedAt() = DateTimeColumn("deleted_at") { LocalDateTime.now() }
    }

    fun toStmt() = conditions.toStmt()
    fun toSql() = conditions.toSql()

    fun add(condition: Pair): Statement {
        when (conditions.isEmpty()) {
            true -> conditions.add(Pair(Operator.NONE, condition.second))
            else -> conditions.add(condition)
        }

        return this
    }

    fun params(): List> {
        val params = mutableListOf>()
        conditions.forEach { params.addAll(it.second.params()) }

        return params
    }

    fun  getSoftDeleteQueryParams(): List> {
        val deletedAt = deletedAt()
        val params = mutableListOf>(Param(deletedAt, LocalDateTime.now()))
        conditions.forEach { params.addAll(it.second.params()) }

        return params
    }

    fun  getSoftDeleteStatement(): Statement {
        val deletedAt = deletedAt()
        if (conditions.isEmpty()) return where(deletedAt.isNull())

        return where(StatementGroup(conditions.removeWhere())) and deletedAt.isNull()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy