Add JDBC database provider for JVM
This commit is contained in:
1 parent
ee392daa13
commit
64273ac60a
22 files changed
+1913
-459
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import lyng.io.db
|
||||
import lyng.io.db.jdbc
|
||||
|
||||
println("H2 JDBC demo: typed open, generic open, generated keys")
|
||||
|
||||
val db = openH2("mem:lyng_h2_demo;DB_CLOSE_DELAY=-1")
|
||||
|
||||
db.transaction { tx ->
|
||||
tx.execute("create table if not exists person(id bigint auto_increment primary key, name varchar(120) not null, active boolean not null)")
|
||||
tx.execute("delete from person")
|
||||
|
||||
val firstInsert = tx.execute(
|
||||
"insert into person(name, active) values(?, ?)",
|
||||
"Ada",
|
||||
true
|
||||
)
|
||||
val firstId = firstInsert.getGeneratedKeys().toList()[0][0]
|
||||
assertEquals(1, firstId)
|
||||
|
||||
tx.execute(
|
||||
"insert into person(name, active) values(?, ?)",
|
||||
"Linus",
|
||||
false
|
||||
)
|
||||
|
||||
val rows = tx.select("select id, name, active from person order by id").toList()
|
||||
assertEquals(2, rows.size)
|
||||
println("#" + rows[0]["id"] + " " + rows[0]["name"] + " active=" + rows[0]["active"])
|
||||
println("#" + rows[1]["id"] + " " + rows[1]["name"] + " active=" + rows[1]["active"])
|
||||
}
|
||||
|
||||
val genericDb = openDatabase(
|
||||
"jdbc:h2:mem:lyng_h2_generic;DB_CLOSE_DELAY=-1",
|
||||
Map()
|
||||
)
|
||||
|
||||
val answer = genericDb.transaction { tx ->
|
||||
tx.select("select 42 as answer").toList()[0]["answer"]
|
||||
}
|
||||
|
||||
assertEquals(42, answer)
|
||||
println("Generic JDBC openDatabase(...) also works: answer=$answer")
|
||||
println("OK")
|
||||
@@ -0,0 +1,71 @@
|
||||
import lyng.io.db.jdbc
|
||||
|
||||
/*
|
||||
PostgreSQL JDBC demo.
|
||||
|
||||
Usage:
|
||||
lyng examples/postgres_basic.lyng [jdbc-url] [user] [password]
|
||||
|
||||
Typical local URL:
|
||||
jdbc:postgresql://127.0.0.1/postgres
|
||||
*/
|
||||
|
||||
fun cliArgs(): List<String> {
|
||||
val result: List<String> = []
|
||||
for (raw in ARGV as List) {
|
||||
result.add(raw as String)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
val argv = cliArgs()
|
||||
val URL = if (argv.size > 0) argv[0] else "jdbc:postgresql://127.0.0.1/postgres"
|
||||
val USER = if (argv.size > 1) argv[1] else ""
|
||||
val PASSWORD = if (argv.size > 2) argv[2] else ""
|
||||
|
||||
println("PostgreSQL JDBC demo: typed open, generated keys, nested transaction")
|
||||
|
||||
val db = openPostgres(URL, USER, PASSWORD)
|
||||
|
||||
db.transaction { tx ->
|
||||
tx.execute("create table if not exists lyng_pg_demo(id bigserial primary key, title text not null, done boolean not null)")
|
||||
tx.execute("delete from lyng_pg_demo")
|
||||
|
||||
val firstInsert = tx.execute(
|
||||
"insert into lyng_pg_demo(title, done) values(?, ?)",
|
||||
"Verify PostgreSQL JDBC support",
|
||||
false
|
||||
)
|
||||
val firstId = firstInsert.getGeneratedKeys().toList()[0][0]
|
||||
println("First generated id=" + firstId)
|
||||
|
||||
tx.execute(
|
||||
"insert into lyng_pg_demo(title, done) values(?, ?)",
|
||||
"Review documentation",
|
||||
true
|
||||
)
|
||||
|
||||
try {
|
||||
tx.transaction { inner ->
|
||||
inner.execute(
|
||||
"insert into lyng_pg_demo(title, done) values(?, ?)",
|
||||
"This row is rolled back",
|
||||
false
|
||||
)
|
||||
throw IllegalStateException("rollback nested")
|
||||
}
|
||||
} catch (_: IllegalStateException) {
|
||||
println("Nested transaction rolled back as expected")
|
||||
}
|
||||
|
||||
val rows = tx.select("select id, title, done from lyng_pg_demo order by id").toList()
|
||||
for (row in rows) {
|
||||
println("#" + row["id"] + " " + row["title"] + " done=" + row["done"])
|
||||
}
|
||||
|
||||
val count = tx.select("select count(*) as count from lyng_pg_demo").toList()[0]["count"]
|
||||
assertEquals(2, count)
|
||||
println("Visible rows after nested rollback: " + count)
|
||||
}
|
||||
|
||||
println("OK")
|
||||
Reference in new issue
Block a user