KQLite Table
Represents a database table definition in KQLite.
This abstract class serves as the foundation for creating type-safe table schemas. To define a table, create a singleton object that inherits from KQLiteTable. Inside the object, use the provided DSL functions (e.g., longColumn, stringColumn) to declare the columns and their constraints.
The core responsibilities of this class are:
Column Definition: Define columns using predefined functions like integerColumn, textColumn, etc.
Constraint Definition: Specify table-level constraints using the constraints property.
Index Definition: Create indexes on columns using the indexes property.
Example Usage:
object Products : KQLiteTable("products") {
val productId = longColumn("ProductId").primaryKey()
val name = textColumn("Name").notNull()
val price = doubleColumn("Price").notNull()
val discount = doubleColumn("Discount").notNull().default(0.0)
val inTime = dateTimeColumn("InTime").default(CURRENT_TIMESTAMP)
val propertiesJson = jsonObjectColumn("Properties")
override val constraints: (ConstraintBuilder.() -> Unit)? = {
// Ensure the price is always positive
check { price GT 0.0 }
// Ensure the discount is not greater than the price
constraint("chk_discount").check { discount LTE price }
}
override val indexes: (IndexBuilder.() -> Unit)? = {
createIndex("price_index").on(price)
}
}The class provides builders for defining table-level constraints and indexes, as well as a rich set of column types and modifiers to model a SQLite table accurately.
Author
MOHAMMAD AZIM ANSARI
Parameters
The name of the table in the database. It will be escaped for special characters.
If true, creates a TEMP table. Default is false.
If true, the CREATE TABLE statement will include IF NOT EXISTS. Default is true.
If true, enables STRICT mode for the table, enforcing more rigid data type validation.
See also
Inheritors
Properties
Functions
Creates an alias for a table using SQL AS.
Marks this INTEGER PRIMARY KEY column of type Long to AUTOINCREMENT.
Defines a nullable BLOB column in the table, intended to store a ByteArray.
Defines a nullable BOOLEAN column in the table, intended to store a Boolean.
Defines a nullable BLOB column in the table, intended to store a ByteArray.
Defines a nullable TINYINT column in the table, intended to store a Byte.
Defines a nullable CHAR(1) column in the table, intended to store a single Char.
Adds a CHECK constraint to this column.
Adds a COLLATE clause to this column, specifying the text comparison behavior.
Defines a nullable DATE column to store a date.
Defines a nullable DATETIME column to store a dateTime.
Sets a DEFAULT value for this column.
Sets a DEFAULT value for this column using com.kqlite.column.Literal.
Defines a nullable REAL column in the table, intended to store a Double.
Defines a nullable ENUM column to store an enum value by its name.
Defines a nullable FLOAT column in the table, intended to store a Float.
Defines this column as a GENERATED ALWAYS AS whose value is computed from other columns.
Retrieves a list of all columns defined in this table.
Defines a nullable INTEGER column in the table.
Defines a nullable JSON ARRAY column to store a JSON array.
Defines a nullable JSON column to store any valid JSON data.
Defines a nullable JSON OBJECT column to store a JSON object.
Defines a nullable DOUBLE column to store a Julian Day Number.
Defines a nullable INTEGER column in the table, intended to store a Long.
Specifies the ON CONFLICT resolution algorithm for a UNIQUE, PRIMARY KEY, or NOT NULL constraint.
Designates this column as the PRIMARY KEY for the table.
Defines a nullable REAL column in the table, intended to store a Double.
Adds a FOREIGN KEY constraint to this column, referencing another column.
Defines a nullable SMALLINT column in the table, intended to store a Short.
Defines a nullable TEXT column in the table, intended to store a String.
Defines a nullable TEXT column in the table, intended to store a String.
Defines a nullable TIME column to store a time.
Defines a nullable TINYINT UNSIGNED column in the table, intended to store an unsigned 8-bit integer (UByte).
Defines a nullable INT UNSIGNED column in the table, intended to store an unsigned 32-bit integer (UInt).
Defines a nullable INTEGER column in the table, intended to store an unsigned 64-bit integer (ULong).
Adds a UNIQUE constraint to this column.
Defines a nullable BIGINT column to store a Unix Epoch timestamp.
Defines a nullable SMALLINT UNSIGNED column in the table, intended to store an unsigned 16-bit integer (UShort).