SQL::Abstract::Pg - PostgreSQL
use SQL::Abstract::Pg; my $abstract = SQL::Abstract::Pg->new; say $abstract->select('some_table');
SQL::Abstract::Pg extends SQL::Abstract with a few PostgreSQL features used by Mojo::Pg.
In many places (as supported by SQL::Abstract) you can use the -json unary op to encode JSON from Perl data structures.
-json
# "update some_table set foo = '[1,2,3]' where bar = 23" $abstract->update('some_table', {foo => {-json => [1, 2, 3]}}, {bar => 23}); # "select * from some_table where foo = '[1,2,3]'" $abstract->select('some_table', '*', {foo => {'=' => {-json => [1, 2, 3]}}});
$abstract->insert($table, \@values || \%fieldvals, \%options);
The on_conflict option can be used to generate INSERT queries with ON CONFLICT clauses. So far, undef to pass DO NOTHING, array references to pass DO UPDATE with conflict targets and a SET expression, scalar references to pass literal SQL and array reference references to pass literal SQL with bind values are supported.
on_conflict
INSERT
ON CONFLICT
undef
DO NOTHING
DO UPDATE
SET
# "insert into t (a) values ('b') on conflict do nothing" $abstract->insert('t', {a => 'b'}, {on_conflict => undef}); # "insert into t (a) values ('b') on conflict do nothing" $abstract->insert('t', {a => 'b'}, {on_conflict => \'do nothing'});
This includes operations commonly referred to as upsert.
upsert
# "insert into t (a) values ('b') on conflict (a) do update set a = 'c'" $abstract->insert('t', {a => 'b'}, {on_conflict => [a => {a => 'c'}]}); # "insert into t (a, b) values ('c', 'd') # on conflict (a, b) do update set a = 'e'" $abstract->insert( 't', {a => 'c', b => 'd'}, {on_conflict => [['a', 'b'] => {a => 'e'}]}); # "insert into t (a) values ('b') on conflict (a) do update set a = 'c'" $abstract->insert( 't', {a => 'b'}, {on_conflict => \['(a) do update set a = ?', 'c']});
$abstract->select($source, $fields, $where, $order); $abstract->select($source, $fields, $where, \%options);
The $fields argument now also accepts array references containing array references with field names and aliases, as well as array references containing scalar references to pass literal SQL and array reference references to pass literal SQL with bind values.
$fields
# "select foo as bar from some_table" $abstract->select('some_table', [[foo => 'bar']]); # "select foo, bar as baz, yada from some_table" $abstract->select('some_table', ['foo', [bar => 'baz'], 'yada']); # "select extract(epoch from foo) as foo, bar from some_table" $abstract->select('some_table', [\'extract(epoch from foo) as foo', 'bar']); # "select 'test' as foo, bar from some_table" $abstract->select('some_table', [\['? as foo', 'test'], 'bar']);
The $source argument now also accepts array references containing not only table names, but also array references with tables to generate JOIN clauses for.
$source
JOIN
# "select * from foo join bar on (bar.foo_id = foo.id)" $abstract->select(['foo', ['bar', foo_id => 'id']]); # "select * from foo join bar on (foo.id = bar.foo_id)" $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]); # "select * from a join b on (b.a_id = a.id) join c on (c.a_id = a.id)" $abstract->select(['a', ['b', a_id => 'id'], ['c', a_id => 'id']]); # "select * from foo left join bar on (bar.foo_id = foo.id)" $abstract->select(['foo', [-left => 'bar', foo_id => 'id']]); # "select * from a left join b on (b.a_id = a.id and b.a_id2 = a.id2)" $abstract->select(['a', [-left => 'b', a_id => 'id', a_id2 => 'id2']]);
Alternatively to the $order argument accepted by SQL::Abstract you can now also pass a hash reference with various options. This includes order_by, which takes the same values as the $order argument.
$order
order_by
# "select * from some_table order by foo desc" $abstract->select('some_table', '*', undef, {order_by => {-desc => 'foo'}});
The limit and offset options can be used to generate SELECT queries with LIMIT and OFFSET clauses.
limit
offset
SELECT
LIMIT
OFFSET
# "select * from some_table limit 10" $abstract->select('some_table', '*', undef, {limit => 10}); # "select * from some_table offset 5" $abstract->select('some_table', '*', undef, {offset => 5}); # "select * from some_table limit 10 offset 5" $abstract->select('some_table', '*', undef, {limit => 10, offset => 5});
The group_by option can be used to generate SELECT queries with GROUP BY clauses. So far, array references to pass a list of fields and scalar references to pass literal SQL are supported.
group_by
GROUP BY
# "select * from some_table group by foo, bar" $abstract->select('some_table', '*', undef, {group_by => ['foo', 'bar']}); # "select * from some_table group by foo, bar" $abstract->select('some_table', '*', undef, {group_by => \'foo, bar'});
The having option can be used to generate SELECT queries with HAVING clauses, which takes the same values as the $where argument.
having
HAVING
$where
# "select * from t group by a having b = 'c'" $abstract->select('t', '*', undef, {group_by => ['a'], having => {b => 'c'}});
The for option can be used to generate SELECT queries with FOR clauses. So far, the scalar value update to pass UPDATE and scalar references to pass literal SQL are supported.
for
FOR
update
UPDATE
# "select * from some_table for update" $abstract->select('some_table', '*', undef, {for => 'update'}); # "select * from some_table for update skip locked" $abstract->select('some_table', '*', undef, {for => \'update skip locked'});
SQL::Abstract::Pg inherits all methods from SQL::Abstract.
Mojo::Pg, Mojolicious::Guides, https://mojolicious.org.
To install Mojo::Pg, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Pg
CPAN shell
perl -MCPAN -e shell install Mojo::Pg
For more information on module installation, please visit the detailed CPAN module installation guide.