Mojo::SQL(3)
πΈ NAME πΈ
okay bestie, welcome to the Mojo::SQL(3) page π this one comes from the User Contributed Perl Documentation crew, and itβs all about Mojo::SQL β safely generate and compose SQL statements. no cap, thatβs the whole main-character job. β¨
β¨ SYNOPSIS β¨
use Mojo::SQL qw(sql);
# {text => 'SELECT * FROM users WHERE name = $1', values => ['sebastian']}
my $query = sql('SELECT * FROM users WHERE name = ?', 'sebastian')->to_query;
π DESCRIPTION π
So Mojo::SQL safely generates and composes SQL statements π«Ά and the whole point is SQL injection prevention. Every ? in the input becomes a placeholder in the generated query, and the corresponding value gets bound to it. Partial statements can also be composed recursively to build more complex queries β little query Lego bricks, iykyk π§
Literal question marks can be escaped with ??.
use Mojo::SQL qw(sql);
my $role = 'admin';
my $partial = sql('AND role = ?', $role);
my $name = 'root';
# {text => 'SELECT * FROM users WHERE name = $1 AND role = $2', values => ['root', 'admin']}
my $query = sql('SELECT * FROM users WHERE name = ? ?', $name, $partial)->to_query;
You can make partial statements optional to dynamically generate WHERE clauses π«
my $optional = $foo ? sql('AND foo IS NOT NULL') : sql('');
my $query = sql('SELECT * FROM users WHERE name = ? ?', 'sebastian', $optional)->to_query;
If you need a little more control over the generated SQL query, you can also bypass safety features with sql_unsafe π but bestie, make sure to handle unsafe values yourself with appropriate escaping functions for your database. For PostgreSQL there are escape_literal and escape_identifier functions included with this module.
use Mojo::SQL qw(sql sql_unsafe escape_literal);
my $role = 'role = ' . escape_literal('power user');
my $partial = sql_unsafe 'AND ?', $role;
my $name = 'root';
# {text => "SELECT * FROM users WHERE name = \$1 AND role = 'power user'", values => ['root']}
my $query = sql('SELECT * FROM users WHERE name = ? ?', $name, $partial)->to_query;
For databases that do not support numbered placeholders like $1 and $2, you can set a custom character with the placeholder option π
# {text => 'SELECT * FROM users WHERE name = ?', values => ['root']}
my $query = sql('SELECT * FROM users WHERE name = ?', 'root')->to_query({placeholder => '?'});
π¦ FUNCTIONS π¦
Mojo::SQL implements the following functions, which can be imported individually π
π escape_identifier π
my $escaped = escape_identifier('some_table');
Escape an identifier β only the PostgreSQL format is currently supported π
π escape_literal π
my $escaped = escape_literal('some value');
Escape a literal β only the PostgreSQL format is currently supported β¨
π₯ sql π₯
my $stmt = sql('SELECT * FROM users WHERE name = ?', 'sebastian');
Create a new Mojo::SQL::Statement from an SQL string. Each ? in the string becomes a placeholder, and the corresponding value is bound to it. Mojo::SQL::Statement values are spliced in recursively, so partial statements can be composed to build more complex queries. Literal question marks can be escaped with ?? π«
π sql_unsafe π
my $stmt = sql_unsafe 'SELECT * FROM users WHERE name = ?', 'sebastian';
Create a new Mojo::SQL::Statement without safe placeholders. Each ? in the string is replaced literally by the corresponding value. Literal question marks can be escaped with ??. Use with care β οΈ
π COPYRIGHT AND LICENSE π
Copyright (C) 2026, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under the terms of the MIT license.
π SEE ALSO π
Mojo::SQL::Statement, Mojolicious, <https://mojolicious.org>.
thatβs the whole page, bestie π brought to you by perl v5.42.2, dated 2026-05-13, and yes β itβs Mojo::SQL(3) signing off πβ¨
π¦ where this page comes from
- alpine 3.24: perl-mojo-sql-doc 0.02-r0 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms