1SQL::Abstract::Pg(3) User Contributed Perl Documentation SQL::Abstract::Pg(3)
2
3
4
6 SQL::Abstract::Pg - PostgreSQL
7
9 use SQL::Abstract::Pg;
10
11 my $abstract = SQL::Abstract::Pg->new;
12 say $abstract->select('some_table');
13
15 SQL::Abstract::Pg extends SQL::Abstract with a few PostgreSQL features
16 used by Mojo::Pg.
17
18 JSON
19 In many places (as supported by SQL::Abstract) you can use the "-json"
20 unary op to encode JSON from Perl data structures.
21
22 # "update some_table set foo = '[1,2,3]' where bar = 23"
23 $abstract->update('some_table', {foo => {-json => [1, 2, 3]}}, {bar => 23});
24
25 # "select * from some_table where foo = '[1,2,3]'"
26 $abstract->select('some_table', '*', {foo => {'=' => {-json => [1, 2, 3]}}});
27
29 $abstract->insert($table, \@values || \%fieldvals, \%options);
30
31 ON CONFLICT
32 The "on_conflict" option can be used to generate "INSERT" queries with
33 "ON CONFLICT" clauses. So far "undef" to pass "DO NOTHING", array
34 references to pass "DO UPDATE" with conflict targets and a "SET"
35 expression, scalar references to pass literal SQL and array reference
36 references to pass literal SQL with bind values are supported.
37
38 # "insert into t (a) values ('b') on conflict do nothing"
39 $abstract->insert('t', {a => 'b'}, {on_conflict => undef});
40
41 # "insert into t (a) values ('b') on conflict do nothing"
42 $abstract->insert('t', {a => 'b'}, {on_conflict => \'do nothing'});
43
44 This includes operations commonly referred to as "upsert".
45
46 # "insert into t (a) values ('b') on conflict (a) do update set a = 'c'"
47 $abstract->insert('t', {a => 'b'}, {on_conflict => [a => {a => 'c'}]});
48
49 # "insert into t (a, b) values ('c', 'd')
50 # on conflict (a, b) do update set a = 'e'"
51 $abstract->insert(
52 't', {a => 'c', b => 'd'}, {on_conflict => [['a', 'b'] => {a => 'e'}]});
53
54 # "insert into t (a) values ('b') on conflict (a) do update set a = 'c'"
55 $abstract->insert(
56 't', {a => 'b'}, {on_conflict => \['(a) do update set a = ?', 'c']});
57
59 $abstract->select($source, $fields, $where, $order);
60 $abstract->select($source, $fields, $where, \%options);
61
62 AS
63 The $fields argument now also accepts array references containing array
64 references with field names and aliases, as well as array references
65 containing scalar references to pass literal SQL and array reference
66 references to pass literal SQL with bind values.
67
68 # "select foo as bar from some_table"
69 $abstract->select('some_table', [[foo => 'bar']]);
70
71 # "select foo, bar as baz, yada from some_table"
72 $abstract->select('some_table', ['foo', [bar => 'baz'], 'yada']);
73
74 # "select extract(epoch from foo) as foo, bar from some_table"
75 $abstract->select('some_table', [\'extract(epoch from foo) as foo', 'bar']);
76
77 # "select 'test' as foo, bar from some_table"
78 $abstract->select('some_table', [\['? as foo', 'test'], 'bar']);
79
80 JOIN
81 The $source argument now also accepts array references containing not
82 only table names, but also array references with tables to generate
83 "JOIN" clauses for.
84
85 # "select * from foo join bar on (bar.foo_id = foo.id)"
86 $abstract->select(['foo', ['bar', foo_id => 'id']]);
87
88 # "select * from foo join bar on (foo.id = bar.foo_id)"
89 $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]);
90
91 # "select * from a join b on (b.a_id = a.id) join c on (c.a_id = a.id)"
92 $abstract->select(['a', ['b', a_id => 'id'], ['c', a_id => 'id']]);
93
94 # "select * from foo left join bar on (bar.foo_id = foo.id)"
95 $abstract->select(['foo', [-left => 'bar', foo_id => 'id']]);
96
97 ORDER BY
98 Alternatively to the $order argument accepted by SQL::Abstract you can
99 now also pass a hash reference with various options. This includes
100 "order_by", which takes the same values as the $order argument.
101
102 # "select * from some_table order by foo desc"
103 $abstract->select('some_table', '*', undef, {order_by => {-desc => 'foo'}});
104
105 LIMIT/OFFSET
106 The "limit" and "offset" options can be used to generate "SELECT"
107 queries with "LIMIT" and "OFFSET" clauses.
108
109 # "select * from some_table limit 10"
110 $abstract->select('some_table', '*', undef, {limit => 10});
111
112 # "select * from some_table offset 5"
113 $abstract->select('some_table', '*', undef, {offset => 5});
114
115 # "select * from some_table limit 10 offset 5"
116 $abstract->select('some_table', '*', undef, {limit => 10, offset => 5});
117
118 GROUP BY
119 The "group_by" option can be used to generate "SELECT" queries with
120 "GROUP BY" clauses. So far array references to pass a list of fields
121 and scalar references to pass literal SQL are supported.
122
123 # "select * from some_table group by foo, bar"
124 $abstract->select('some_table', '*', undef, {group_by => ['foo', 'bar']});
125
126 # "select * from some_table group by foo, bar"
127 $abstract->select('some_table', '*', undef, {group_by => \'foo, bar'});
128
129 HAVING
130 The "having" option can be used to generate "SELECT" queries with
131 "HAVING" clauses, which takes the same values as the $where argument.
132
133 # "select * from t group by a having b = 'c'"
134 $abstract->select('t', '*', undef, {group_by => ['a'], having => {b => 'c'}});
135
136 FOR
137 The "for" option can be used to generate "SELECT" queries with "FOR"
138 clauses. So far the scalar value "update" to pass "UPDATE" and scalar
139 references to pass literal SQL are supported.
140
141 # "select * from some_table for update"
142 $abstract->select('some_table', '*', undef, {for => 'update'});
143
144 # "select * from some_table for update skip locked"
145 $abstract->select('some_table', '*', undef, {for => \'update skip locked'});
146
148 SQL::Abstract::Pg inherits all methods from SQL::Abstract.
149
151 Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
152
153
154
155perl v5.28.0 2018-08-01 SQL::Abstract::Pg(3)