Skip to content

Instantly share code, notes, and snippets.

@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")
}
}
@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 / 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 / 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: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 / gist:8124099
Last active January 1, 2016 09:19
A type-safe DSL for NoSQL using Kotlin language (experiment)
import kotlin.nosql.*
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>
}
@cheptsov
cheptsov / gist:8146503
Created December 27, 2013 12:42
Access Amazon DynamoDB using the type-safe Kotlin DSL for NoSQL databases. For more details: https://github.com/cheptsov/Exposed/tree/NoSQL
import kotlin.nosql.*
import kotlin.nosql.dynamodb.*
object Users : Table() {
val id = string("id", length = 10).id() // PKColumn<String, Users>
val name = string("name", length = 50) // Column<String, Users>
val requiredCityId = integer("required_city_id") // Column<Int, Users>
val optionalCityId = integer("optional_city_id").nullable() // Column<Int?, Users>
val all = id + name + requiredCityId + optionalCityId // Template4<Users, String, Int, Int?>

Kotlin NoSQL Roadmap

v0.1

  • Drop iterate methods without filtering
  • Support for Template1-10
  • Support for Query1-10
  • Support for other operators: gt, lt, ge, le, mb, ne, nm, nn, nl, mt
  • Support for boolean, date, timestamp, double, float, long, short, byte types
  • Support for add, delete, remove, etc operations
@cheptsov
cheptsov / gist:e99e5fa7343143008b2b
Created July 6, 2014 22:03
Kotlin NoSQL API for MongoDB has been rewritten to use RxJava (see https://github.com/cheptsov/kotlin-nosql for details):
// Get a document
Albums.find { details.artistId.equal(artistId) }.subscribe(onNext = { album ->
}, onError = {
})
// Get selected fields of a document
Albums.find { id.equal(albumId) }.projection { sku + details.title + pricing }.subscribe(onNext = {
val (sku, title, pricing) = it
}, onError = {
})
(dao.topics outerJoin dao.comments on { case (topic, comment) => comment.topicId === topic.id }).list
Results in
play.api.Application$$anon$1: Execution exception[[SlickException: Expected a collection type, found UnassignedType]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.7.jar:2.3.7]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.7.jar:2.3.7]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205) [play_2.11-2.3.7.jar:2.3.7]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202) [play_2.11-2.3.7.jar:2.3.7]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.5.jar:na]