This library is in alpha, and the API is subject to change
This is a zero-dependency Kotlin DSL for the SQL language, in particular the PostgreSQL dialect.
It was designed in parallel with the Kotgres ORM, but can be used completely stand-alone.
This is only a DSL, to run the queries produced by it you will need an ORM or a similar library (i.e. Kotgres).
The library is currently distributed through Jitpack, planning to release on Maven Central soon. To install, add this to your build.gradke.kts:
repositories {
<...>
maven { setUrl("https://jitpack.io") }
}
dependencies {
<...>
implementation("com.github.kotgres:kotgres-dsl:0.1.4")
}Or if you use Groovy (aka build.gradle):
repositories {
<...>
maven { url 'https://jitpack.io' }
}
dependencies {
<...>
implementation 'com.github.kotgres:kotgres-dsl:0.1.4'
}Paste this code into a Kotlin file:
import io.kotgres.dsl.select
import io.kotgres.dsl.operators.*
fun main() {
val sql = select("*")
.from("users")
.where("id" eq 1)
.orderBy("id")
.limit(100)
.toSql(true)
println(sql)
}And you will get this output:
SELECT *
FROM users
WHERE
id = 1
ORDER BY id ASC
LIMIT 100🎉 Congrats you have just written your first SQL query using Kotgres' DSL 🎉
Supports the following base Postgres statements:
- Select (entrypoint is the
selectfunction fromimport io.kotgres.dsl.select) - Insert (entrypoint is the
insertIntofunction fromimport io.kotgres.dsl.insertInto) - Update (entrypoint is the
updatefunction fromimport io.kotgres.dsl.update) - Delete (entrypoint is the
deleteFromfunction fromimport io.kotgres.dsl.deleteFrom)
Supports the most common operators in Postgres:
- = (via
eq) - != or <> (via
neq)
- > (via
greater) - >= (via
greaterEq) - < (via
less) - <= (via
lessEq)
- LIKE (via
like) - NOT LIKE (via
notLike) - ILIKE (via
ilike) - NOT ILIKE (via
notIlike)
- ~ (via
regexp) - !~ (via notRegexp)
- BETWEEN (via
between)
- IS NULL (via
isNull) - IS NOT NULL (via
isNotNull)
- IN (via
inList) - NOT IN (via
notInList)
- = ANY (via
eqAny) - != ANY (via
neqAny)
The DSL provides the flexibility to construct any statements even if non-standard or not yet in the library using Raw.
For example:
select("*")
.from("users")
.where("my_custom_operator(age)".raw)Just add .raw at the end of any string to construct a Raw statement.
import io.kotgres.dsl.select
import io.kotgres.dsl.operators.*
val sql = select("*")
.from("users")
.leftJoin("address").on("address.user_id = user.id")
.rightJoin("country").on("address.country_id = country.id")
.where("country.id" eq 1)
.orderBy("id")
.toSql(true)
println(sql)Will print:
SELECT *
FROM users
LEFT JOIN address ON ( address.user_id = user.id )
RIGHT JOIN country ON ( address.country_id = country.id )
WHERE
country.id = 1
ORDER BY id ASCimport io.kotgres.dsl.insertInto
import io.kotgres.dsl.ConflictSet
import io.kotgres.dsl.operators.*
val sql = insertInto("users")
.columns("name", "age")
.value(listOf("david", 18))
.onConflictColumn("name")
.doUpdate(ConflictSet("name", "duplicated"))
.where(isNull("age"))
.returning("*")
.toSql(true)
println(sql)Will print:
INSERT INTO users ( name, age )
VALUES ( 'david', 18 )
ON CONFLICT (name) DO UPDATE
SET name = 'duplicated'
WHERE
age IS NULL
RETURNING
*import io.kotgres.dsl.update
import io.kotgres.dsl.extensions.raw
import io.kotgres.dsl.operators.*
val sql = update("users")
.set("asd", "a")
.andSet("b", "c")
.where("a > b".raw)
.orWhere("hello")
.andWhere(isNull("b"))
.toSql(true)
println(sql)Will print:
UPDATE users
SET asd='a',
b='c'
WHERE
a > b
OR hello
AND b IS NULLimport io.kotgres.dsl.deleteFrom
import io.kotgres.dsl.operators.*
val sql = deleteFrom("users")
.where("age" greater 100)
.orWhere("was_deleted" eq false)
.toSql(true)
println(sql)Will print:
DELETE FROM users
WHERE
age > 100
OR was_deleted = false