Last active
December 24, 2015 09:29
-
-
Save cheptsov/6777390 to your computer and use it in GitHub Desktop.
Using Expose library to write select statements with inner and outer joins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
object Cities : Table() { | |
val id = integer("id").id().generated() // GeneratedPKColumn<Int, Cities> | |
val name = varchar("name", 50) // Column<String, Cities> | |
val all = id + name // Template2<Cities, Int, String> Select template | |
val values = name // Column<String, Cities> | |
} | |
(Users.name + Users.requiredCityId * Cities.name) forEach { | |
val (userName, cityName) = it // String, String | |
println("$userName's required city is $cityName") | |
} | |
// SQL: SELECT Users.name, Cities.name FROM Users INNER JOIN Cities ON Users.required_city_id = Cities.id | |
(Users.name + Users.optionalCityId * Cities.name) forEach { | |
val (userName, cityName) = it // String, String? | |
if (cityName != null) { | |
println("$userName's optional city is $cityName") | |
} else { | |
println("$userName has no optional city") | |
} | |
} | |
// SQL: SELECT Users.id, Users.name, Cities.id, Cities.name FROM Users LEFT JOIN Cities ON Users.optional_city_id = Cities.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment