InsertQueryBuilder

Defines the public API for building SQL INSERT queries.

Functions

Link copied to clipboard
abstract fun asStep(): StepBuilderMethods

Converts this builder to a StepBuilder that enables lazy execution within a transaction. Returns a wrapper with terminal methods that create TransactionStep instead of executing the query.

Link copied to clipboard
abstract fun asStream(fetchSize: Int = 100): StreamingTerminalMethods

Switches the builder to streaming mode, optimal for large datasets. Requires using streaming-specific terminal methods like forEachRow(). REQUIRES ACTIVE TRANSACTION. This method must be called inside a DataAccess.transaction { ... } block. Otherwise, PostgreSQL will ignore fetchSize and load everything into RAM.

Link copied to clipboard
abstract fun async(scope: CoroutineScope, ioDispatcher: CoroutineDispatcher = Dispatchers.IO): AsyncTerminalMethods

Switches the builder to asynchronous mode. Requires providing a CoroutineScope in which operations will be launched.

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. Returns the concrete builder type (e.g., SelectQueryBuilder), maintaining API fluency.

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

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

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(vararg params: Pair<String, Any?>): DataResult<List<T>>
inline fun <T> TerminalReturningMethods.toColumn(params: Map<String, Any?> = emptyMap()): 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(vararg params: Pair<String, Any?>): DataResult<T>
inline fun <T> TerminalReturningMethods.toField(params: Map<String, Any?> = emptyMap()): 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> 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(vararg params: Pair<String, Any?>): DataResult<List<T>>

inline fun <T : Any> TerminalReturningMethods.toListOf(params: Map<String, Any?> = emptyMap()): DataResult<List<T>>

Convenient inline extension function for toListOf. Uses reified to automatically infer the target type.

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.

Link copied to clipboard
inline fun <T> TerminalReturningMethods.toSingleOf(vararg params: Pair<String, Any?>): DataResult<T>

inline fun <T> TerminalReturningMethods.toSingleOf(params: Map<String, Any?> = emptyMap()): DataResult<T>

Convenient inline extension function for toSingleOf. Uses reified to automatically infer the target type.

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 an 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.