Table Operations

This chapter describes how to create, delete, empty, and synchronize database tables.

Make sure each KPojo has the table, column, primary key, index, and strategy annotations required by the table operation.

Table operations can be invoked through Kronos.dataSource.table after configuring the default data source, or through a concrete KronosDataSourceWrapper.table. Schema sync and CTAS have dedicated guides: 📚Schema Sync and 📚Create Table As Select.

Build DDL statements and preview SQL

TableOperation exposes build APIs for dry-run review. Use statement builders when you need a syntax object, and task builders when you need the rendered SQL and parameters for the active dialect.

statement

val table = Kronos.dataSource.table

val createStatement = table.buildCreateTableStatement(User())
val dropStatement = table.buildDropTableStatement("user", ifExists = true)
val truncateStatement = table.buildTruncateTableStatement("user", restartIdentity = false)

task

val dropTask = table.buildDropTableTask("user")
val truncateTask = table.buildTruncateTableTask("user", restartIdentity = false)

val (sql, params, atomicTasks) = truncateTask

println(sql)
println(params)
println(atomicTasks.map { it.operationType })

SQLite output

DELETE FROM "user"
{}
[TRUNCATE, TRUNCATE, TRUNCATE]

CTAS can also be built before execution.

ctas task

val paidOrders = Order()
    .select { [it.id, it.userId, it.status] }
    .where { it.status == 1 }

val task = table.buildCreateTableAsSelectTask(OrderArchive(), paidOrders)
val (ctasSql, ctasParams) = task

1. exists(tableName) whether the table exists

Check if the table exists by table name.


2. exists<T>(instance) whether the table exists


Check if the table exists by KPojo.

📚How does Kronos create KPojo from KType without reflective construction?


3. createTable(instance) Create a table


Create a table by KPojo.

📚How does Kronos create KPojo from KType without reflective construction?


4. createTable(target, query) Create table as select

Create a table from a KSelectable or UnionClause query by passing the target KPojo and the source query to wrapper.table.createTable(target, query).

CTAS uses the query output as the table shape. Use normal createTable(Target()) and then KSelectable.insert<Target>() when the table needs KPojo schema metadata such as indexes, comments, defaults, or primary key definitions.


5. truncateTable(vararg tableName, restartIdentity) Clear the table

Clear one or more tables by table name.


6. truncateTable(instance, restartIdentity) Clear the table


Clear the table by KPojo.

📚How does Kronos create KPojo from KType without reflective construction?


7. dropTable(vararg tableNames) Delete the table

Delete one or more tables by table name.


8. dropTable(instance) Delete the table


📚How does Kronos create KPojo from KType without reflective construction?


9. syncTable(instance) Synchronize table structure


📚How does Kronos create KPojo from KType without reflective construction?


When several table operations use the same object in sequence, keep that object in a local variable and pass it to exists(instance), createTable(instance), dropTable(instance), or syncTable(instance).