BaseTable

abstract class BaseTable<E : Any>(tableName: String, alias: String? = null, catalog: String? = null, schema: String? = null, entityClass: KClass<E>? = null) : TypeReference<E> (source)

Base class of Ktorm's table objects, represents relational tables in the database.

This class provides the basic ability of table and column definition but doesn't support any binding mechanisms. If you need the binding support to Entity interfaces, use Table instead.

There is an abstract function doCreateEntity. Subclasses should implement this function, creating an entity object from the result set returned by a query, using the binding rules defined by themselves. Here, the type of the entity object could be an interface extending from Entity, or a data class, POJO, or any kind of classes.

Here is an example defining an entity as data class. The full documentation can be found at: https://www.ktorm.org/en/define-entities-as-any-kind-of-classes.html

data class Staff(
val id: Int,
val name: String,
val job: String,
val hireDate: LocalDate
)
object Staffs : BaseTable<Staff>("t_employee") {
val id = int("id").primaryKey()
val name = varchar("name")
val job = varchar("job")
val hireDate = date("hire_date")
override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean) = Staff(
id = row[id] ?: 0,
name = row[name].orEmpty(),
job = row[job].orEmpty(),
hireDate = row[hireDate] ?: LocalDate.now()
)
}

Since

2.5

Inheritors

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 fun aliased(alias: String): BaseTable<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

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