Back to Sqldelight

Query Arguments

docs/common/query_arguments.md

2.3.21.3 KB
Original Source

Type Inference

SQLDelight will infer the correct type and nullability of runtime parameters, including custom column types.

sql
selectByNumber:
SELECT *
FROM hockeyPlayer
WHERE player_number = ?;
kotlin
val selectNumber10 = playerQueries.selectByNumber(player_number = 10)
println(selectNumber10.executeAsOne())
// Prints "Corey Perry"

Named Arguments

Named parameters or indexed parameters can be used.

sql
firstOrLastName:
SELECT *
FROM hockeyPlayer
WHERE full_name LIKE ('% ' || :name)
OR full_name LIKE (:name || ' %');
kotlin
playerQueries.firstOrLastName(name = "Ryan")

Variable Arguments

Sets of values can also be passed as an argument.

sql
selectByNames:
SELECT *
FROM hockeyPlayer
WHERE full_name IN ?;
kotlin
playerQueries.selectByNames(listOf("Alec", "Jake", "Matt"))

Inserts

INSERT VALUES arguments can be bound to with the table's data class.

sql
insertPlayer:
INSERT INTO hockeyPlayer
VALUES ?;
kotlin
val rickardRakell = HockeyPlayer(
  full_name = "Rickard Rakell",
  number = 67
)
playerQueries.insertPlayer(rickardRakell)

Input Sanitization

SQLDelight uses query placeholders to pass arguments into queries. The actual sanitization of argument inputs is done by the underlying driver implementation on each respective platform and dialect.