enumColumn

protected fun <E : Enum<E>> enumColumn(name: String, entries: EnumEntries<E>): KQLiteColumn<E?>

Defines a nullable ENUM column to store an enum value by its name.

This function creates a ENUM column to store the string representation (the name) of an enum constant. KQLite automatically handles the conversion between the enum instance in your application and its corresponding string in the database.

To ensure data integrity, a CHECK constraint is automatically added to the column, restricting its value to one of the names provided in the entries collection. This prevents invalid strings from being inserted into the column.

Example:

enum class Status {
ACTIVE, INACTIVE, PENDING
}

object Tasks : KQLiteTable("tasks") {
val id = longColumn("id").primaryKey()
val title = stringColumn("title").notNull()

// A nullable column for the task status
val status = enumColumn("status", Status.entries)

// A non-null column with a default value
val priority = enumColumn("priority", Status.entries)
.notNull()
.default(Status.PENDING)
}

// Inserting data
Tasks.insert().bind {
it.title.bind("Finish documentation")
it.status.bind(Status.ACTIVE)
}.execute()

Author

MOHAMMAD AZIM ANSARI

Parameters

name

The name of the column in the database.

entries

The complete collection of enum entries, typically accessed via MyEnum.entries.

Type Parameters

E

The enum type this column will store.