English | 简体中文

api-docs / org.ktorm.schema / Table

Table

open class Table<E : Entity<E>> : BaseTable<E> (source code)

Base class of Ktorm’s table objects. This class extends from BaseTable, additionally providing a binding mechanism
with Entity interfaces based on functions such as bindTo, references.

Table implements the doCreateEntity function from the parent class. The function automatically creates an
entity object using the binding configuration specified by bindTo and references, reading columns’ values from
the result set and filling them into corresponding entity properties.

To use this class, we need to define our entities as interfaces extending from Entity. Here is an example. More
documents can be found at https://www.ktorm.org/en/entities-and-column-binding.html

interface Department : Entity<Department> {
val id: Int
var name: String
var location: String
}
object Departments : Table<Department>("t_department") {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val location = varchar("location").bindTo { it.location }
}

Constructors

NameSummary

<init>

Table(
    tableName: String,
    alias: String? = null,
    catalog: String? = null,
    schema: String? = null,
    entityClass: KClass<E>? = null)

Base class of Ktorm’s table objects. This class extends from BaseTable, additionally providing a binding mechanism
with Entity interfaces based on functions such as bindTo, references.

Inherited Properties

NameSummary

alias

val alias: String?

The table’s alias.

catalog

val catalog: String?

The table’s catalog.

columns

val columns: List<Column<*>>

Return all columns of the table.

entityClass

val entityClass: KClass<E>?

The entity class this table is bound to.

primaryKeys

val primaryKeys: List<Column<*>>

The primary key columns of this table.

schema

val schema: String?

The table’s schema.

tableName

val tableName: String

The table’s name.

Functions

NameSummary

aliased

open fun aliased(alias: String): Table<E>

Return a new-created table object with all properties (including the table name and columns and so on) being
copied from this table, but applying a new alias given by the parameter.

bindTo

fun <C : Any> Column<C>.bindTo(
    selector: (E) -> C?
): Column<C>

Bind the column to nested properties, eg. employee.manager.department.id.

doCreateEntity

fun doCreateEntity(
    row: QueryRowSet,
    withReferences: Boolean
): E

Create an entity object from the specific row of query results.

references

fun <C : Any, R : Entity<R>> Column<C>.references(
    referenceTable: Table<R>,
    selector: (E) -> R?
): Column<C>

Bind the column to a reference table, equivalent to a foreign key in relational databases.
Entity sequence APIs would automatically left-join all references (recursively) by default.

Inherited Functions

NameSummary

asExpression

fun asExpression(): TableExpression

Convert this table to a TableExpression.

copyDefinitionsFrom

fun copyDefinitionsFrom(src: BaseTable<*>): Unit

Copy column definitions from src to this table.

createEntity

fun createEntity(
    row: QueryRowSet,
    withReferences: Boolean = true
): E

Create an entity object from the specific row of query results. This function uses the binding configurations
of the table, filling column values into corresponding properties of the returned entity.

equals

fun equals(other: Any?): Boolean

Indicates whether some other object is “equal to” this table.
Two tables are equal only if they are the same instance.

get

operator fun get(name: String): Column<*>

Obtain a column from this table by the name.

hashCode

fun hashCode(): Int

Return a hash code value for this table.

primaryKey

fun <C : Any> Column<C>.primaryKey(): Column<C>

Mark the registered column as a primary key.

registerColumn

fun <C : Any> registerColumn(
    name: String,
    sqlType: SqlType<C>
): Column<C>

Register a column to this table with the given name and sqlType.

toString

open fun toString(): String

Return a string representation of this table.

transform

fun <C : Any, R : Any> Column<C>.transform(
    fromUnderlyingValue: (C) -> R,
    toUnderlyingValue: (R) -> C
): Column<R>

Transform the registered column’s SqlType to another. The transformed SqlType has the same typeCode and
typeName as the underlying one, and performs the specific transformations on column values.

Extension 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.

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.

blob

fun BaseTable<*>.blob(name: String): Column<ByteArray>

Define a column typed of BlobSqlType.

boolean

fun BaseTable<*>.boolean(name: String): Column<Boolean>

Define a column typed of BooleanSqlType.

bulkInsert

fun <T : BaseTable<*>> T.bulkInsert(
    block: BulkInsertStatementBuilder<T>.() -> Unit
): Int

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

bulkInsert

fun <T : BaseTable<*>> T.bulkInsert(
    block: BulkInsertStatementBuilder<T>.(T) -> Unit
): Int

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

bulkInsertOrUpdate

fun <T : BaseTable<*>> T.bulkInsertOrUpdate(
    block: BulkInsertOrUpdateStatementBuilder<T>.() -> Unit
): Int

Bulk insert records to the table, determining if there is a key conflict while inserting each of them,
and automatically performs updates if any conflict exists.

bulkInsertOrUpdate

fun <T : BaseTable<*>> T.bulkInsertOrUpdate(
    block: BulkInsertOrUpdateStatementBuilder<T>.(T) -> Unit
): Int

Bulk insert records to the table, determining if there is a key conflict while inserting each of them,
and automatically performs updates if any conflict exists.

bytes

fun BaseTable<*>.bytes(name: String): Column<ByteArray>

Define a column typed of BytesSqlType.

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.

cube

fun BaseTable<*>.cube(name: String): Column<Cube>

Define a column typed CubeSqlType.

date

fun BaseTable<*>.date(name: String): Column<LocalDate>

Define a column typed of LocalDateSqlType.

datetime

fun BaseTable<*>.datetime(
    name: String
): Column<LocalDateTime>

Define a column typed of LocalDateTimeSqlType.

datetimeoffset

fun BaseTable<*>.datetimeoffset(
    name: String
): Column<OffsetDateTime>

Define a column typed of DateTimeOffsetSqlType.

decimal

fun BaseTable<*>.decimal(name: String): Column<BigDecimal>

Define a column typed of DecimalSqlType.

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.

double

fun BaseTable<*>.double(name: String): Column<Double>

Define a column typed of DoubleSqlType.

earth

fun BaseTable<*>.earth(name: String): Column<Earth>

Define a column typed EarthSqlType.

enum

fun <C : Enum<C>> BaseTable<*>.enum(name: String): Column<C>

Define a column typed of EnumSqlType.

eq

infix fun <T : Any> T.eq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Equal operator, translated to = in SQL.

float

fun BaseTable<*>.float(name: String): Column<Float>

Define a column typed of FloatSqlType.

hstore

fun BaseTable<*>.hstore(name: String): Column<HStore>

Define a column typed HStoreSqlType.

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.

insertOrUpdate

fun <T : BaseTable<*>> T.insertOrUpdate(
    block: InsertOrUpdateStatementBuilder.(T) -> Unit
): Int

Insert a record to the table, determining if there is a key conflict while it’s being inserted, and automatically
performs an update if any conflict exists.

insertOrUpdate

fun <T : BaseTable<*>> T.insertOrUpdate(
    block: InsertOrUpdateStatementBuilder.(T) -> Unit
): Int

Insert a record to the table, determining if there is a key conflict while it’s being inserted, and automatically
performs an update if any conflict exists.

int

fun BaseTable<*>.int(name: String): Column<Int>

Define a column typed of IntSqlType.

jdbcDate

fun BaseTable<*>.jdbcDate(name: String): Column<Date>

Define a column typed of DateSqlType.

jdbcTime

fun BaseTable<*>.jdbcTime(name: String): Column<Time>

Define a column typed of TimeSqlType.

jdbcTimestamp

fun BaseTable<*>.jdbcTimestamp(
    name: String
): Column<Timestamp>

Define a column typed of TimestampSqlType.

joinReferencesAndSelect

fun BaseTable<*>.joinReferencesAndSelect(): Query

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

json

fun <C : Any> BaseTable<*>.json(
    name: String,
    mapper: ObjectMapper = sharedObjectMapper
): Column<C>

Define a column typed of JsonSqlType.

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.

long

fun BaseTable<*>.long(name: String): Column<Long>

Define a column typed of LongSqlType.

monthDay

fun BaseTable<*>.monthDay(name: String): Column<MonthDay>

Define a column typed of MonthDaySqlType, instances of MonthDay are saved as strings in format MM-dd.

naturalJoin

fun BaseTable<*>.naturalJoin(
    right: BaseTable<*>
): QuerySource

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

neq

infix fun <T : Any> T.neq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Not-equal operator, translated to <> in SQL.

notEq

infix fun <T : Any> T.notEq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Not-equal operator, translated to <> in SQL.

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.

short

fun BaseTable<*>.short(name: String): Column<Short>

Define a column typed of ShortSqlType.

text

fun BaseTable<*>.text(name: String): Column<String>

Define a column typed of TextSqlType.

textArray

fun BaseTable<*>.textArray(name: String): Column<TextArray>

Define a column typed TextArraySqlType.

time

fun BaseTable<*>.time(name: String): Column<LocalTime>

Define a column typed of LocalTimeSqlType.

timestamp

fun BaseTable<*>.timestamp(name: String): Column<Instant>

Define a column typed of InstantSqlType.

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.

uuid

fun BaseTable<*>.uuid(name: String): Column<UUID>

Define a column typed of UuidSqlType.

varchar

fun BaseTable<*>.varchar(name: String): Column<String>

Define a column typed of VarcharSqlType.

year

fun BaseTable<*>.year(name: String): Column<Year>

Define a column typed of YearSqlType, instances of Year are saved as integers.

yearMonth

fun BaseTable<*>.yearMonth(name: String): Column<YearMonth>

Define a column typed of YearMonthSqlType, instances of YearMonth are saved as strings in format yyyy-MM.