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

org.babyfish.jimmer.sql.kt.impl.KQueriesImpl.kt Maven / Gradle / Ivy

There is a newer version: 0.9.35
Show newest version
package org.babyfish.jimmer.sql.kt.impl

import org.babyfish.jimmer.kt.toImmutableProp
import org.babyfish.jimmer.meta.ImmutableProp
import org.babyfish.jimmer.meta.ImmutableType
import org.babyfish.jimmer.sql.association.Association
import org.babyfish.jimmer.sql.association.meta.AssociationType
import org.babyfish.jimmer.sql.ast.impl.query.FilterLevel
import org.babyfish.jimmer.sql.ast.impl.query.MutableRootQueryImpl
import org.babyfish.jimmer.sql.ast.table.AssociationTable
import org.babyfish.jimmer.sql.ast.table.Table
import org.babyfish.jimmer.sql.kt.KQueries
import org.babyfish.jimmer.sql.kt.ast.query.KConfigurableRootQuery
import org.babyfish.jimmer.sql.kt.ast.query.KMutableRootQuery
import org.babyfish.jimmer.sql.kt.ast.query.impl.KMutableRootQueryImpl
import org.babyfish.jimmer.sql.runtime.ExecutionPurpose
import org.babyfish.jimmer.sql.runtime.JSqlClientImplementor
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1

internal class KQueriesImpl(
    private val sqlClient: JSqlClientImplementor
) : KQueries {

    @Suppress("UNCHECKED_CAST")
    override fun  forEntity(
        entityType: KClass,
        block: KMutableRootQuery.() -> KConfigurableRootQuery
    ): KConfigurableRootQuery {
        val query = MutableRootQueryImpl>(
            sqlClient,
            ImmutableType.get(entityType.java),
            ExecutionPurpose.QUERY,
            FilterLevel.DEFAULT
        )
        return KMutableRootQueryImpl(
            query as MutableRootQueryImpl>
        ).block()
    }

    override fun  forReference(
        prop: KProperty1,
        block: KMutableRootQuery>.() -> KConfigurableRootQuery, R>
    ): KConfigurableRootQuery, R> {
        return forAssociation(prop.toImmutableProp(), block)
    }

    override fun  forList(
        prop: KProperty1>,
        block: KMutableRootQuery>.() -> KConfigurableRootQuery, R>
    ): KConfigurableRootQuery, R> {
        return forAssociation(prop.toImmutableProp(), block)
    }

    @Suppress("UNCHECKED_CAST")
    private fun  forAssociation(
        immutableProp: ImmutableProp,
        block: KMutableRootQuery>.() -> KConfigurableRootQuery, R>
    ): KConfigurableRootQuery, R> {
        val associationType = AssociationType.of(immutableProp)
        val query: MutableRootQueryImpl, T, Table>> =
            MutableRootQueryImpl(
                sqlClient,
                associationType,
                ExecutionPurpose.QUERY,
                FilterLevel.DEFAULT
            )
        return KMutableRootQueryImpl(
            query as MutableRootQueryImpl>>
        ).block()
    }
}