Escribe para buscar…

DSL

Esta página todavía no se ha traducido — se muestra en su idioma original:English

Table

In Exposed, the Table class is the core abstraction for defining database tables. This class provides methods to define various column types, constraints, and other table-specific properties.

Table is located in the org.jetbrains.exposed.v1.core package of the exposed-core module.

The following example defines a table with an auto-incrementing integer id column and string name and email column:

kotlin
object Customer : Table("customers") {
    val id = integer("id").autoIncrement()
    val name = text("name")
    val email = text("email").uniqueIndex()
}

Insert

Exposed provides several functions to insert rows into a table:

Insert a single row

To create a new table row, use the .insert() function:

kotlin
suspendTransaction(database) {
    Customer.insert {
        it[name] = "Mary"
        it[email] = "mary@xtec.dev"
    }
}

The .insert() function returns a InsertStatement object that contains the inserted row’s primary key value.

kotlin
val id = Customer.insert {
    it[name] = "Susan"
    it[email] = "susan@xtec.dev"
} get Customer.id  // or }.get(Customer.id)

If the same row already exists in the table, it throws an exception.

Batch insert

.batchInsert() allows mapping a list of entities into table rows in a single SQL statement.

It is more efficient than using the insert query for each row as it initiates only one statement.

kotlin
suspendTransaction {
    val customers = listOf(
        Pair("John", "john@xtec.dev"),
        Pair("Anne", "anne@xtec.dev")
    )
    Customer.batchInsert(customers) { (name, email) ->
        this[Customer.name] = name
        this[Customer.email] = email
    }
}

Estás leyendo una vista previa.

Inicia sesión con Google para leer la página completa. Una cuenta de Google incluye 5 páginas gratuitas en total; el alumnado y el profesorado leen las páginas de su curso sin límite.

Iniciar sesión