gotlin/examples/sql_expanded.gt
2026-08-28 00:02:32 +02:00

24 lines
921 B
Text

package main
@table("accounts")
data class AccountRow(@id var id: String, var customerId: String, var balance: Double)
@table("customers")
data class CustomerRow(@id var id: String, var name: String)
data class AccountSummary(var customerId: String, var customerName: String, var total: Double, var entries: Long)
fun summaries(): GotlinSQLQuery = sql.from<AccountRow>()
.alias("account")
.leftJoin<CustomerRow>("customer") { account, customer -> account.customerId == customer.id }
.select { account, customer -> AccountSummary(account.customerId, customer.name, sum(account.balance), count()) }
.groupBy { account, customer -> listOf(account.customerId, customer.name) }
.offset(20)
.build()
fun bulk(rows: List<AccountRow>): GotlinSQLQuery = sql.insertAll<AccountRow>(rows).build()
fun main() {
println(summaries().sql)
println(bulk(listOf(AccountRow("a-1", "c-1", 10.0))).sql)
}