Adrian
Posted on May 7, 2023
I just released the first version of SQL::Builder, a module to make SQL queries from data structures in Raku.
my $sql = SQL::Builder.new;
my $built = $sql.from('table').select('table.foo', 'other.bar').join('other', :using<id>).build;
say $built.sql;
# SELECT table.foo, other.bar FROM table JOIN other USING(id)
SQL::Builder: You're still basically writing SQL.
There's already other modules out there (Red, SQL::Abstract, among others), so what does this bring to the ecosystem?
Support for complex queries. This module can do nested joins and subselects, while enabling reusable logic.
SQL that's close to the metal. An ORM was not a good fit for this project, but generating queries from code is very helpful.
Better
WHERE
clause support. It can currently represent any condition in the form ofleft op right
, and while optimized for the typicalfoo = ?
query, it can safely handle functions and expressions.
This syntax is used forHAVING
andJOIN ... ON
as well, providing a lot of flexibility. This was easily the part requiring the most thought, and I plan to write more about the design choices here.It's faster, mostly due to returning rows instead of objects and having a simpler interface.
In short, this module is great for queries that are complex, but have some level of variability, like a query that has optional JOINs depending on user input.
What's coming next?
- support for
UPDATE/INSERT/DELETE
statements - better support for Arrays as values in WHERE clauses
- optional support for actually running the queries, either via
DBIish
orDB::Pg
(and related modules) - I'm sure there will be something else as I continue work on my other project that uses this :)
This module won't be for everyone, but I've found it very useful in another project. Any feedback is appreciated, please either leave a comment here, open an issue on GitHub, or find me in #raku
on irc.libera.chat
.
Posted on May 7, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.