GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
SQL::Abstract::Pg(3) User Contributed Perl Documentation SQL::Abstract::Pg(3)
 

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.
  # "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.
  # "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".
  # "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.
  # "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.
  # "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']]);

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.
  # "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.
  # "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.
  # "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.
  # "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.
  # "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>.
2018-11-22 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.