Introduction to sqala
sqala is an SQL query library based on Scala 3, named after the combination of Scala and SQL.
Using sqala, you can achieve:
Building queries in an object-oriented manner:
scalacase class User(id: Int, name: String) val q = query: from[User] .filter(u => u.id == 1) .map(u => u.name) val i = insert(User(1, "Dave"))
Managing projections with named tuples, eliminating the need to predefine structures for projection results or use
Map[String, Any]
, and accessing returned fields using.
:scalaval q = query: from[User].map(u => (id = u.id)) val result = db.fetch(q) for r <- result do println(r.id)
Utilizing Scala3's
inline
capability to generate high-performance deserialization code, which is 3-10 times faster than reflection-based Java mainstream query libraries.Show SQL statement at compile time:
Capture wrong queries at compiletime, and return sematic compilation warning:
Supporting multiple dialects including MySQL, PostgreSQL, and Oracle, with the same query expression generating different SQL by passing different dialect parameters.
Supporting Oracle's recursive query feature
CONNECT BY
, but generating standard SQL that is compatible across databases:scalacase class Department(id: Int, managerId: Int, name: String) val q = query: from[Department] .connectBy(d => prior(d.id) == d.managerId) .startWith(d => d.managerId == 0) .map(d => (id = d.id, managerId = d.managerId, name = d.name))
Beyond CRUD, sqala also supports advanced features like multidimensional grouping, subquery predicates,
LATERAL
subqueries, providing strong support for data analysis scenarios.The
dynamic
module provides a DSL and SQL parser for dynamically constructing complex queries, supporting applications like dynamic report building.No additional dependencies beyond Scala and Java official libraries.
Precautions
Since sqala is not built on the Scala3 LTS version, caution is advised when using it in production environments until the next LTS version of Scala3 is released.
Ensure the Scala version is
3.7.0
or higher.It is recommended to use the official Scala metals plugin with editors like VSCode, Vim, etc. Jetbrains IDEA currently cannot provide writing hints or correctly display the data types returned by queries.