InsertQueryBuilder

Defines the public API for building SQL INSERT queries.

// Enlist a new legionnaire, returning the generated ID
val data = legionnaire.toDataMap("id")
dataAccess.insertInto("legionnaires")
.values(data)
.valueExpression("enlisted_at", "NOW()")
.returning("id")
.toField<Int>(data)

Functions

Link copied to clipboard
abstract fun asStep(): StepBuilderMethods

Converts this builder to a StepBuilderMethods for lazy execution within a TransactionPlan.

Link copied to clipboard
abstract fun columns(vararg columns: String): InsertQueryBuilder

Explicitly defines the columns for the INSERT statement.

Link copied to clipboard
abstract fun copy(): InsertQueryBuilder

Creates and returns a deep copy of this builder. Useful for creating query variants from a shared base without modifying the original.

Link copied to clipboard
abstract fun execute(params: Map<String, Any?> = emptyMap()): DataResult<Int>

Executes the query and returns the number of rows that were updated

Link copied to clipboard
Link copied to clipboard
abstract fun fromSelect(query: String): InsertQueryBuilder

Defines a SELECT query as the data source for insertion.

Link copied to clipboard
abstract fun iterate(fetchSize: Int = 100): IterativeTerminalMethods

Switches the builder to iterative mode, optimal for large datasets.

Link copied to clipboard

Configures behavior in case of a key conflict (ON CONFLICT clause).

Link copied to clipboard

Configures additional options for this query.

Link copied to clipboard

Marks the WITH clause as recursive.

Link copied to clipboard
abstract fun returning(vararg columns: String): InsertQueryBuilder

Adds a RETURNING clause. Requires using .toList(), .toSingle(), etc. instead of .execute().

Link copied to clipboard
abstract fun <T> toColumn(targetType: KType, params: Map<String, Any?> = emptyMap()): DataResult<List<T>>

Fetches a list of values from the first column of all rows. Nullability of elements is determined by the KType: use toColumn<Int>() for non-null or toColumn<Int?>() for nullable elements.

Link copied to clipboard
inline fun <T> TerminalReturningMethods.toColumn(params: Map<String, Any?> = emptyMap()): DataResult<List<T>>
inline fun <T> TerminalReturningMethods.toColumn(vararg params: Pair<String, Any?>): DataResult<List<T>>
Link copied to clipboard
abstract fun <T> toField(targetType: KType, params: Map<String, Any?> = emptyMap()): DataResult<T>

Fetches a single value from the first column of the first row. Nullability is determined by the KType: use toField<Int>() for non-null or toField<Int?>() for nullable results.

Link copied to clipboard
inline fun <T> TerminalReturningMethods.toField(params: Map<String, Any?> = emptyMap()): DataResult<T>
inline fun <T> TerminalReturningMethods.toField(vararg params: Pair<String, Any?>): DataResult<T>
Link copied to clipboard
abstract fun <T> toFieldStrict(targetType: KType, params: Map<String, Any?> = emptyMap()): DataResult<T>

Fetches a single value from the first column of the first row. Always returns Failure if no rows are found, regardless of nullability. Nullability only controls whether a null value in the column is allowed: use toFieldStrict<Int>() to fail on null, or toFieldStrict<Int?>() to allow null values.

Link copied to clipboard
inline fun <T> TerminalReturningMethods.toFieldStrict(params: Map<String, Any?> = emptyMap()): DataResult<T>
Link copied to clipboard
abstract fun toList(params: Map<String, Any?> = emptyMap()): DataResult<List<Map<String, Any?>>>

Fetches a list of rows as List>.

Link copied to clipboard
Link copied to clipboard
abstract fun <T : Any> toListOf(params: Map<String, Any?> = emptyMap(), mapper: DataMapper<T>): DataResult<List<T>>

Maps results to a list of objects using the provided mapper. This bypasses reflection and is the most efficient way to map results to domain objects.

abstract fun <T : Any> toListOf(kType: KType, params: Map<String, Any?> = emptyMap()): DataResult<List<T>>

Maps results to a list of objects of the given type. Requires that column names/aliases in SQL (in snake_case convention) match property names in the target class (in camelCase convention) or have a @MapKey annotation with the stored column name.

Link copied to clipboard
inline fun <T : Any> TerminalReturningMethods.toListOf(params: Map<String, Any?> = emptyMap()): DataResult<List<T>>
inline fun <T : Any> TerminalReturningMethods.toListOf(vararg params: Pair<String, Any?>): DataResult<List<T>>
fun <T : Any> TerminalReturningMethods.toListOf(vararg params: Pair<String, Any?>, mapper: DataMapper<T>): DataResult<List<T>>
Link copied to clipboard
abstract fun toSingle(params: Map<String, Any?> = emptyMap()): DataResult<Map<String, Any?>?>

Fetches a single row as Map?. Returns Success(null) if no rows.

Link copied to clipboard
Link copied to clipboard
abstract fun <T> toSingleOf(kType: KType, params: Map<String, Any?> = emptyMap()): DataResult<T>

Maps the result to a single object of the given type. Works on the same mapping principle as toListOf. Nullability is determined by the KType: use toSingleOf<User>() for non-null or toSingleOf<User?>() for nullable results.

abstract fun <T> toSingleOf(kType: KType, params: Map<String, Any?> = emptyMap(), mapper: DataMapper<T & Any>): DataResult<T>

Maps the result to a single object using the provided mapper. Nullability is determined by the KType: use toSingleOf<User>(mapper) for non-null or toSingleOf<User?>(mapper) for nullable results. This bypasses reflection and is the most efficient way to map results to domain objects.

Link copied to clipboard
inline fun <T> TerminalReturningMethods.toSingleOf(params: Map<String, Any?> = emptyMap()): DataResult<T>
inline fun <T> TerminalReturningMethods.toSingleOf(vararg params: Pair<String, Any?>): DataResult<T>
inline fun <T> TerminalReturningMethods.toSingleOf(params: Map<String, Any?> = emptyMap(), mapper: DataMapper<T & Any>): DataResult<T>
inline fun <T> TerminalReturningMethods.toSingleOf(vararg params: Pair<String, Any?>, mapper: DataMapper<T & Any>): DataResult<T>
Link copied to clipboard
abstract fun toSingleStrict(params: Map<String, Any?> = emptyMap()): DataResult<Map<String, Any?>>

Fetches a single row as Map. Returns Failure if no rows.

Link copied to clipboard
Link copied to clipboard
abstract fun toSql(): String

Returns the SQL string without executing the query.

Link copied to clipboard
abstract fun value(column: String): InsertQueryBuilder

Defines a single value, automatically generating a placeholder.

Link copied to clipboard
abstract fun valueExpression(column: String, expression: String): InsertQueryBuilder

Defines a single value to insert as a SQL expression.

Link copied to clipboard
abstract fun values(values: List<String>): InsertQueryBuilder

Defines values to insert, automatically generating placeholders in @value format for each value in the list.

abstract fun values(data: Map<String, Any?>): InsertQueryBuilder

Defines values to insert, automatically generating placeholders. This is the preferred, high-level method for inserting data. Values from the map must be passed in the terminal method (e.g., .execute()).

Link copied to clipboard
abstract fun valuesExpressions(expressions: Map<String, String>): InsertQueryBuilder

Defines values to insert as SQL expressions or placeholders. This is a low-level method.

Link copied to clipboard
abstract fun with(name: String, query: String): InsertQueryBuilder

Adds a Common Table Expression (CTE) to the query.