SQL::Builder - making the complex SQL query manageable in Raku

avuserow

Adrian

Posted on May 7, 2023

SQL::Builder - making the complex SQL query manageable in Raku

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)

Enter fullscreen mode Exit fullscreen mode

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?

  1. Support for complex queries. This module can do nested joins and subselects, while enabling reusable logic.

  2. 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.

  3. Better WHERE clause support. It can currently represent any condition in the form of left op right, and while optimized for the typical foo = ? query, it can safely handle functions and expressions.
    This syntax is used for HAVING and JOIN ... 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.

  4. 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 or DB::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.

💖 💪 🙅 🚩
avuserow
Adrian

Posted on May 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related