Table

open class Table<E : Entity<E>>(tableName: String, alias: String? = null, catalog: String? = null, schema: String? = null, entityClass: KClass<E>? = null) : BaseTable<E> (source)

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

Link copied to clipboard
constructor(tableName: String, alias: String? = null, catalog: String? = null, schema: String? = null, entityClass: KClass<E>? = null)

Properties

Link copied to clipboard

The table's alias.

Link copied to clipboard

The table's catalog.

Link copied to clipboard

Return all columns of the table.

Link copied to clipboard

The entity class this table is bound to.

Link copied to clipboard

The primary key columns of this table.

Link copied to clipboard

The actual kotlin type argument of subclass without erased.

Link copied to clipboard

The actual type argument of subclass without erased.

Link copied to clipboard

The table's schema.

Link copied to clipboard

The table's name.

Functions

Link copied to clipboard
open override 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.

Link copied to clipboard

Convert this table to a TableExpression.

Link copied to clipboard
inline fun <C : Any> Column<C>.bindTo(selector: (E) -> C?): Column<C>

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

Link copied to clipboard

Define a column typed of BlobSqlType.

Link copied to clipboard

Define a column typed of BooleanSqlType.

Link copied to clipboard

Define a column typed of BytesSqlType.

Link copied to clipboard
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.

Link copied to clipboard

Define a column typed of LocalDateSqlType.

Link copied to clipboard

Define a column typed of LocalDateTimeSqlType.

Link copied to clipboard

Define a column typed of DecimalSqlType.

Link copied to clipboard

Define a column typed of DoubleSqlType.

Link copied to clipboard
inline fun <C : Enum<C>> BaseTable<*>.enum(name: String): Column<C>

Define a column typed of EnumSqlType.

Link copied to clipboard
operator override 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.

Link copied to clipboard

Define a column typed of FloatSqlType.

Link copied to clipboard
operator fun get(name: String): Column<*>

Obtain a column from this table by the name.

Link copied to clipboard
override fun hashCode(): Int

Return a hash code value for this table.

Link copied to clipboard
fun BaseTable<*>.int(name: String): Column<Int>

Define a column typed of IntSqlType.

Link copied to clipboard

Define a column typed of DateSqlType.

Link copied to clipboard

Define a column typed of TimeSqlType.

Link copied to clipboard

Define a column typed of TimestampSqlType.

Link copied to clipboard
fun BaseTable<*>.long(name: String): Column<Long>

Define a column typed of LongSqlType.

Link copied to clipboard

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

Link copied to clipboard
fun <C : Any> Column<C>.primaryKey(): Column<C>

Mark the registered column as a primary key.

Link copied to clipboard
inline 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.

Link copied to clipboard
fun <C : Any> registerColumn(name: String, sqlType: SqlType<C>): Column<C>

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

Link copied to clipboard

Define a column typed of ShortSqlType.

Link copied to clipboard

Define a column typed of TextSqlType.

Link copied to clipboard

Define a column typed of LocalTimeSqlType.

Link copied to clipboard

Define a column typed of InstantSqlType.

Link copied to clipboard
open override fun toString(): String

Return a string representation of this table.

Link copied to clipboard
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.

Link copied to clipboard
fun BaseTable<*>.uuid(name: String): Column<UUID>

Define a column typed of UuidSqlType.

Link copied to clipboard

Define a column typed of VarcharSqlType.

Link copied to clipboard
fun BaseTable<*>.year(name: String): Column<Year>

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

Link copied to clipboard

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