English | 简体中文

api-docs / org.ktorm.global

Package org.ktorm.global

Provide a more concise DSL syntax based on a global database instance.

Functions

NameSummary

addEntity

fun <E : Entity<E>> Table<E>.addEntity(entity: E): Int

Insert the given entity into this sequence and return the affected record number.

all

fun <E : Any, T : BaseTable<E>> T.all(
    predicate: (T) -> ColumnDeclaring<Boolean>
): Boolean

Check if all the records in the table matches the given predicate.

any

fun <E : Any, T : BaseTable<E>> T.any(): Boolean

Check if there is any record in the table.

fun <E : Any, T : BaseTable<E>> T.any(
    predicate: (T) -> ColumnDeclaring<Boolean>
): Boolean

Check if there is any record in the table matches the given predicate.

asSequence

fun <E : Any, T : BaseTable<E>> T.asSequence(
    withReferences: Boolean = true
): EntitySequence<E, T>

Create an EntitySequence from this table.

averageBy

fun <E : Any, T : BaseTable<E>> T.averageBy(
    selector: (T) -> ColumnDeclaring<out Number>
): Double?

Return the average value of the given column or expression of all the records, null if there are no records.

batchInsert

fun <T : BaseTable<*>> T.batchInsert(
    block: BatchInsertStatementBuilder<T>.() -> Unit
): IntArray

Construct insert expressions in the given closure, then batch execute them and return the effected
row counts for each expression.

batchUpdate

fun <T : BaseTable<*>> T.batchUpdate(
    block: BatchUpdateStatementBuilder<T>.() -> Unit
): IntArray

Construct update expressions in the given closure, then batch execute them and return the effected
row counts for each expression.

count

fun <E : Any, T : BaseTable<E>> T.count(): Int

Return the records count in the table.

fun <E : Any, T : BaseTable<E>> T.count(
    predicate: (T) -> ColumnDeclaring<Boolean>
): Int

Return the records count in the table that matches the given predicate.

crossJoin

fun BaseTable<*>.crossJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to cross join in SQL.

delete

fun <T : BaseTable<*>> T.delete(
    predicate: (T) -> ColumnDeclaring<Boolean>
): Int

Delete the records in the table that matches the given predicate.

deleteAll

fun BaseTable<*>.deleteAll(): Int

Delete all the records in the table.

findAll

fun <E : Any> BaseTable<E>.findAll(): List<E>

Obtain all the entity objects from this table, auto left joining all the reference tables.

findList

fun <E : Any, T : BaseTable<E>> T.findList(
    predicate: (T) -> ColumnDeclaring<Boolean>
): List<E>

Obtain a list of entity objects matching the given predicate, auto left joining all the reference tables.

findOne

fun <E : Any, T : BaseTable<E>> T.findOne(
    predicate: (T) -> ColumnDeclaring<Boolean>
): E?

Obtain an entity object matching the given predicate, auto left joining all the reference tables.

innerJoin

fun BaseTable<*>.innerJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to inner join in SQL.

insert

fun <T : BaseTable<*>> T.insert(
    block: AssignmentsBuilder.(T) -> Unit
): Int

Construct an insert expression in the given closure, then execute it and return the effected row count.

insertAndGenerateKey

fun <T : BaseTable<*>> T.insertAndGenerateKey(
    block: AssignmentsBuilder.(T) -> Unit
): Any

Construct an insert expression in the given closure, then execute it and return the auto-generated key.

invoke

operator fun <T> Database.invoke(func: Database.() -> T): T

Execute the callback function using the current database instance.

isEmpty

fun <E : Any, T : BaseTable<E>> T.isEmpty(): Boolean

Return true if the table has no records.

isNotEmpty

fun <E : Any, T : BaseTable<E>> T.isNotEmpty(): Boolean

Return true if the table has at lease one record.

joinReferencesAndSelect

fun BaseTable<*>.joinReferencesAndSelect(): Query

Return a new-created Query object, left joining all the reference tables, and selecting all columns of them.

leftJoin

fun BaseTable<*>.leftJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to left join in SQL.

maxBy

fun <E : Any, T : BaseTable<E>, C : Comparable<C>> T.maxBy(
    selector: (T) -> ColumnDeclaring<C>
): C?

Return the max value of the given column or expression of all the records, null if there are no records in the table.

minBy

fun <E : Any, T : BaseTable<E>, C : Comparable<C>> T.minBy(
    selector: (T) -> ColumnDeclaring<C>
): C?

Return the min value of the given column or expression of all the records, null if there are no records in the table.

none

fun <E : Any, T : BaseTable<E>> T.none(): Boolean

Return true if there is no records in the table.

fun <E : Any, T : BaseTable<E>> T.none(
    predicate: (T) -> ColumnDeclaring<Boolean>
): Boolean

Return true if there is no records in the table that matches the given predicate.

rightJoin

fun BaseTable<*>.rightJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to right join in SQL.

select

fun BaseTable<*>.select(
    columns: Collection<ColumnDeclaring<*>>
): Query

fun BaseTable<*>.select(
    vararg columns: ColumnDeclaring<*>
): Query

Create a query object, selecting the specific columns or expressions from this table.

selectDistinct

fun BaseTable<*>.selectDistinct(
    columns: Collection<ColumnDeclaring<*>>
): Query

fun BaseTable<*>.selectDistinct(
    vararg columns: ColumnDeclaring<*>
): Query

Create a query object, selecting the specific columns or expressions from this table distinctly.

sumBy

fun <E : Any, T : BaseTable<E>, C : Number> T.sumBy(
    selector: (T) -> ColumnDeclaring<C>
): C?

Return the sum of the given column or expression of all the records, null if there are no records in the table.

update

fun <T : BaseTable<*>> T.update(
    block: UpdateStatementBuilder.(T) -> Unit
): Int

Construct an update expression in the given closure, then execute it and return the effected row count.

updateEntity

fun <E : Entity<E>> Table<E>.updateEntity(entity: E): Int

Update properties of the given entity to the database and return the affected record number.

useConnection

fun <T> useConnection(func: (Connection) -> T): T

Obtain a connection from Database.Companion.global and invoke the callback function with it.

useTransaction

fun <T> useTransaction(
    isolation: TransactionIsolation? = null,
    func: (Transaction) -> T
): T

Execute the specific callback function in a transaction of Database.Companion.global and returns its result if the
execution succeeds, otherwise, if the execution fails, the transaction will be rollback.

Companion Object Properties

NameSummary

global

val Database.Companion.global: Database

The global database instance, Ktorm uses this property to obtain a database when any SQL is executed.

Companion Object Functions

NameSummary

connectGlobally

fun Database.Companion.connectGlobally(
    dialect: SqlDialect = detectDialectImplementation(),
    logger: Logger = detectLoggerImplementation(),
    alwaysQuoteIdentifiers: Boolean = false,
    generateSqlInUpperCase: Boolean? = null,
    connector: () -> Connection
): Database

Connect to a database by a specific connector function and save the returned database instance
to Database.Companion.global.

fun Database.Companion.connectGlobally(
    dataSource: DataSource,
    dialect: SqlDialect = detectDialectImplementation(),
    logger: Logger = detectLoggerImplementation(),
    alwaysQuoteIdentifiers: Boolean = false,
    generateSqlInUpperCase: Boolean? = null
): Database

Connect to a database using a DataSource and save the returned database instance to Database.Companion.global.

fun Database.Companion.connectGlobally(
    url: String,
    driver: String? = null,
    user: String? = null,
    password: String? = null,
    dialect: SqlDialect = detectDialectImplementation(),
    logger: Logger = detectLoggerImplementation(),
    alwaysQuoteIdentifiers: Boolean = false,
    generateSqlInUpperCase: Boolean? = null
): Database

Connect to a database using the specific connection arguments and save the returned database instance
to Database.Companion.global.

connectWithSpringSupportGlobally

fun Database.Companion.connectWithSpringSupportGlobally(
    dataSource: DataSource,
    dialect: SqlDialect = detectDialectImplementation(),
    logger: Logger = detectLoggerImplementation(),
    alwaysQuoteIdentifiers: Boolean = false,
    generateSqlInUpperCase: Boolean? = null
): Database

Connect to a database using a DataSource with the Spring support enabled and save the returned database
instance to Database.Companion.global.