First, we need to initialize a SqueryContext
with a standard JDBC DataSource
.
You will probably want to use a connection pool for performance (like HikariCP).
import ba.sake.squery.{*, given}
// import one of these if needed:
// import ba.sake.squery.postgres.{*, given}
// import ba.sake.squery.mysql.{*, given}
// import ba.sake.squery.mariadb.{*, given}
// import ba.sake.squery.oracle.{*, given}
// import ba.sake.squery.h2.{*, given}
val ds = com.zaxxer.hikari.HikariDataSource()
ds.setJdbcUrl(..)
ds.setUsername(..)
ds.setPassword(..)
val ctx = SqueryContext(ds)
Now we can run queries inside the context:
ctx.run {
// queries go here!
}
or if you want to run them transactionally:
ctx.runTransaction {
// queries go here!
}
ctx.run*
functions provide an implicit JDBC connection under the cover,
thanks to scala3's context functions! <3