Skip to content

Instantly share code, notes, and snippets.

@cheptsov
cheptsov / gist:6777390
Last active December 24, 2015 09:29
Using Expose library to write select statements with inner and outer joins
object Users : Table() {
val id = varchar("id", length = 10).id() // PKColumn<String, Users>
val name = varchar("name", length = 50) // Column<String, Users>
val requiredCityId = integer("required_city_id").references(Cities.id) // FKColumn<Int, Users>
val optionalCityId = integer("optional_city_id").references(Cities.id).optional() // FKOptionColumn<Int, Users>
val all = id + name + requiredCityId + optionalCityId // Template4<Users, String, Int, Int?> Select template
val values = id + name + requiredCityId + optionalCityId // Template4<Users, String, Int, Int?> Insert template
}
@cheptsov
cheptsov / exposed-inner-join
Last active December 20, 2015 16:49
Type-safe inner join via Exposed library
(Users.name + Users.cityId * Cities.name).filter { Users.id.equals("andrey") } forEach {
val (userName, cityName) = it
println("$userName lives in $cityName")
}
@cheptsov
cheptsov / gist:6061029
Created July 23, 2013 09:08
Type-safe database table declaration using Kotlin language and Expose library
object Users : Table() {
val id = varchar("id", ColumnType.PRIMARY_KEY, length = 10) // PKColumn<String>
val name = varchar("name", length = 50) // Column<String>
val cityId = integer("city_id", ColumnType.NULLABLE, references = Cities.id) // Column<Int?>
val all = id + name + cityId // Column3<String, String, Int?>
}
object Cities : Table() {
val id = integer("id", ColumnType.PRIMARY_KEY, autoIncrement = true) // PKColumn<Int>
@cheptsov
cheptsov / gist:6052798
Last active December 20, 2015 01:59
Support for type-safe wildcards in selects using Expose library
object Cities : Table() {
val id = id("id", autoIncrement = true)
val name = varchar("name", 50)
val all = id + name
}
select (Cities.all) forEach {
val (id, name) = it
println("$id: $name")
@cheptsov
cheptsov / Select with a function and group by statement
Last active December 19, 2015 18:29
Just added function and group by support to the Expose SQL library for Kotlin
select (Cities.name, сount(Users.id)) from Cities join Users groupBy Cities.name forEach {
val (cityName, userCount) = it
if (userCount > 0) {
println("$userCount user(s) live(s) in $cityName")
} else {
println("Nobody lives in $cityName")
}
}