Skip to content

Instantly share code, notes, and snippets.

@oshai
oshai / table.kt
Last active December 5, 2017 20:44
definition of schema
// Table definition
object Cities : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
}
// Entity definition
data class City(
val id: Int,
val name: String
)
@oshai
oshai / upsert.kt
Last active December 6, 2017 08:24
The update or insert definition
fun execute(cities: List<City>) {
transaction {
Cities.batchInsertOnDuplicateKeyUpdate( cities, listOf(id, name) ) { batch, city ->
batch[Cities.id] = city.id
batch[Cities.name] = city.name
}
}
}
// The below code is just a copy-paste that should actually be in the lib
@oshai
oshai / ExposedLogging.kt
Created December 5, 2017 20:50
exposed logging
// Usage
transaction {
logger.addLogger(KotlinLoggingSqlLogger)
...
}
// Declaration
object KotlinLoggingSqlLogger : SqlLogger {
private val logger = KotlinLogging.logger { }
override
@oshai
oshai / dsl.kt
Created February 19, 2018 19:00
object StarWarsFilms : IntIdTable() {
val sequelId: Column<Int> = integer("sequel_id").uniqueIndex()
val name: Column<String> = varchar("name", 50)
val director: Column<String> = varchar("director", 50)
}
data class StarWarsFilm(val sequelId: Int,
val name: String,
val director: String)
fun main(args: Array<String>) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
// insert new city. SQL: INSERT INTO Cities (name) VALUES ('St. Petersburg')
val stPeteId = Cities.insert {
it[name] = "St. Petersburg"
} get Cities.id
// 'select *' SQL: SELECT Cities.id, Cities.name FROM Cities
// Table definition
object Cities : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
val create_date = datetime(name = "create_date")
}
// Entity definition
data class City(
val id: Int,
val name: String
@oshai
oshai / scala-to-kotlin.kts
Last active February 9, 2023 10:45
Stupid and simple convert a Scala file (already renamed to .kt extension) to Kotlin
#!/usr/bin/env kscript
import java.io.File
// usage - one argument a .kt file (Scala file that was only renamed)
// or a directory
try {
main(args)
} catch (e: Exception) {
e.printStackTrace()
import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.Connection
import com.github.jasync.sql.db.QueryResult
import com.github.jasync.sql.db.RowData
import com.github.jasync.sql.db.mysql.MySQLConnection
import com.github.jasync.sql.db.mysql.util.URLParser
import com.github.jasync.sql.db.util.head
import com.github.jasync.sql.db.util.map
import java.util.concurrent.CompletableFuture
@oshai
oshai / loadclass.kt
Created October 23, 2018 11:42
load a class from source code string
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.config.Services
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.io.PrintWriter
import java.net.URLClassLoader
@Test
fun `"Simple query with 1 nanosec timeout"`() {
withConfigurablePool(shortTimeoutConfiguration()) { pool ->
{
val connection = pool.take().get(10, TimeUnit.SECONDS)
assertThat(connection.isTimeout()).isEqualTo(false)
assertThat(connection.isConnected()).isEqualTo(true)
val queryResultFuture = connection.sendQuery("select sleep(1)")
verifyException(TimeoutException::class.java) {
queryResultFuture.get(10, TimeUnit.SECONDS)