SQL-Abstract-Pg-1.0/000755 000765 000024 00000000000 14014237147 014061 5ustar00sristaff000000 000000 SQL-Abstract-Pg-1.0/LICENSE000644 000765 000024 00000021413 14014235162 015062 0ustar00sristaff000000 000000 The Artistic License 2.0 Copyright (c) 2000-2006, The Perl Foundation. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble This license establishes the terms under which a given free software Package may be copied, modified, distributed, and/or redistributed. The intent is that the Copyright Holder maintains some artistic control over the development of that Package while still keeping the Package available as open source and free software. You are always permitted to make arrangements wholly outside of this license directly with the Copyright Holder of a given Package. If the terms of this license do not permit the full use that you propose to make of the Package, you should contact the Copyright Holder and seek a different licensing arrangement. Definitions "Copyright Holder" means the individual(s) or organization(s) named in the copyright notice for the entire Package. "Contributor" means any party that has contributed code or other material to the Package, in accordance with the Copyright Holder's procedures. "You" and "your" means any person who would like to copy, distribute, or modify the Package. "Package" means the collection of files distributed by the Copyright Holder, and derivatives of that collection and/or of those files. A given Package may consist of either the Standard Version, or a Modified Version. "Distribute" means providing a copy of the Package or making it accessible to anyone else, or in the case of a company or organization, to others outside of your company or organization. "Distributor Fee" means any fee that you charge for Distributing this Package or providing support for this Package to another party. It does not mean licensing fees. "Standard Version" refers to the Package if it has not been modified, or has been modified only in ways explicitly requested by the Copyright Holder. "Modified Version" means the Package, if it has been changed, and such changes were not explicitly requested by the Copyright Holder. "Original License" means this Artistic License as Distributed with the Standard Version of the Package, in its current version or as it may be modified by The Perl Foundation in the future. "Source" form means the source code, documentation source, and configuration files for the Package. "Compiled" form means the compiled bytecode, object code, binary, or any other form resulting from mechanical transformation or translation of the Source form. Permission for Use and Modification Without Distribution (1) You are permitted to use the Standard Version and create and use Modified Versions for any purpose without restriction, provided that you do not Distribute the Modified Version. Permissions for Redistribution of the Standard Version (2) You may Distribute verbatim copies of the Source form of the Standard Version of this Package in any medium without restriction, either gratis or for a Distributor Fee, provided that you duplicate all of the original copyright notices and associated disclaimers. At your discretion, such verbatim copies may or may not include a Compiled form of the Package. (3) You may apply any bug fixes, portability changes, and other modifications made available from the Copyright Holder. The resulting Package will still be considered the Standard Version, and as such will be subject to the Original License. Distribution of Modified Versions of the Package as Source (4) You may Distribute your Modified Version as Source (either gratis or for a Distributor Fee, and with or without a Compiled form of the Modified Version) provided that you clearly document how it differs from the Standard Version, including, but not limited to, documenting any non-standard features, executables, or modules, and provided that you do at least ONE of the following: (a) make the Modified Version available to the Copyright Holder of the Standard Version, under the Original License, so that the Copyright Holder may include your modifications in the Standard Version. (b) ensure that installation of your Modified Version does not prevent the user installing or running the Standard Version. In addition, the Modified Version must bear a name that is different from the name of the Standard Version. (c) allow anyone who receives a copy of the Modified Version to make the Source form of the Modified Version available to others under (i) the Original License or (ii) a license that permits the licensee to freely copy, modify and redistribute the Modified Version using the same licensing terms that apply to the copy that the licensee received, and requires that the Source form of the Modified Version, and of any works derived from it, be made freely available in that license fees are prohibited but Distributor Fees are allowed. Distribution of Compiled Forms of the Standard Version or Modified Versions without the Source (5) You may Distribute Compiled forms of the Standard Version without the Source, provided that you include complete instructions on how to get the Source of the Standard Version. Such instructions must be valid at the time of your distribution. If these instructions, at any time while you are carrying out such distribution, become invalid, you must provide new instructions on demand or cease further distribution. If you provide valid instructions or cease distribution within thirty days after you become aware that the instructions are invalid, then you do not forfeit any of your rights under this license. (6) You may Distribute a Modified Version in Compiled form without the Source, provided that you comply with Section 4 with respect to the Source of the Modified Version. Aggregating or Linking the Package (7) You may aggregate the Package (either the Standard Version or Modified Version) with other packages and Distribute the resulting aggregation provided that you do not charge a licensing fee for the Package. Distributor Fees are permitted, and licensing fees for other components in the aggregation are permitted. The terms of this license apply to the use and Distribution of the Standard or Modified Versions as included in the aggregation. (8) You are permitted to link Modified and Standard Versions with other works, to embed the Package in a larger work of your own, or to build stand-alone binary or bytecode versions of applications that include the Package, and Distribute the result without restriction, provided the result does not expose a direct interface to the Package. Items That are Not Considered Part of a Modified Version (9) Works (including, but not limited to, modules and scripts) that merely extend or make use of the Package, do not, by themselves, cause the Package to be a Modified Version. In addition, such works are not considered parts of the Package itself, and are not subject to the terms of this license. General Provisions (10) Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. (11) If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. (12) This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. (13) This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed. (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SQL-Abstract-Pg-1.0/Changes000644 000765 000024 00000000044 14014234301 015337 0ustar00sristaff000000 000000 1.0 2021-02-20 - First release. SQL-Abstract-Pg-1.0/MANIFEST000644 000765 000024 00000000476 14014237147 015221 0ustar00sristaff000000 000000 .perltidyrc Changes lib/SQL/Abstract/Pg.pm LICENSE Makefile.PL MANIFEST This list of files MANIFEST.SKIP README.md t/pod.t t/pod_coverage.t t/sql.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) SQL-Abstract-Pg-1.0/t/000755 000765 000024 00000000000 14014237147 014324 5ustar00sristaff000000 000000 SQL-Abstract-Pg-1.0/README.md000644 000765 000024 00000001357 14014237056 015345 0ustar00sristaff000000 000000 # SQL::Abstract::Pg [![](https://github.com/mojolicious/sql-abstract-pg/workflows/linux/badge.svg)](https://github.com/mojolicious/sql-abstract-pg/actions) [PostgreSQL](https://www.postgresql.org) features for [SQL::Abstract](https://metacpan.org/pod/SQL::Abstract). ```perl use SQL::Abstract::Pg; my $abstract = SQL::Abstract::Pg->new; say $abstract->select('some_table'); ``` ## Installation All you need is a one-liner, it takes less than a minute. $ curl -L https://cpanmin.us | perl - -M https://cpan.metacpan.org -n SQL::Abstract::Pg We recommend the use of a [Perlbrew](http://perlbrew.pl) environment. ## Want to know more? Take a look at our excellent [documentation](https://mojolicious.org/perldoc/SQL/Abstract/Pg)! SQL-Abstract-Pg-1.0/MANIFEST.SKIP000644 000765 000024 00000000132 14014236007 015745 0ustar00sristaff000000 000000 ^\.(?!perltidyrc) .*\.old$ \.tar\.gz$ ^Makefile$ ^MYMETA\. ^blib ^pm_to_blib \B\.DS_Store SQL-Abstract-Pg-1.0/META.yml000644 000765 000024 00000001671 14014237147 015337 0ustar00sristaff000000 000000 --- abstract: 'PostgreSQL features for SQL::Abstract' author: - 'Sebastian Riedel ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'ExtUtils::MakeMaker version 7.60, CPAN::Meta::Converter version 2.150010' license: artistic_2 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: SQL-Abstract-Pg no_index: directory: - t - inc - examples - t requires: SQL::Abstract: '2.0' perl: '5.016' resources: IRC: url: irc://chat.freenode.net/#mojo web: https://webchat.freenode.net/#mojo bugtracker: https://github.com/mojolicious/sql-abstract-pg/issues homepage: https://mojolicious.org license: http://www.opensource.org/licenses/artistic-license-2.0 repository: https://github.com/mojolicious/sql-abstract-pg.git version: '1.0' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' SQL-Abstract-Pg-1.0/lib/000755 000765 000024 00000000000 14014237147 014627 5ustar00sristaff000000 000000 SQL-Abstract-Pg-1.0/Makefile.PL000644 000765 000024 00000002217 14014235677 016043 0ustar00sristaff000000 000000 use 5.016; use strict; use warnings; use utf8; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'SQL::Abstract::Pg', VERSION_FROM => 'lib/SQL/Abstract/Pg.pm', ABSTRACT => 'PostgreSQL features for SQL::Abstract', AUTHOR => 'Sebastian Riedel ', LICENSE => 'artistic_2', META_MERGE => { dynamic_config => 0, 'meta-spec' => {version => 2}, no_index => {directory => ['examples', 't']}, prereqs => {runtime => {requires => {perl => '5.016'}}}, resources => { bugtracker => {web => 'https://github.com/mojolicious/sql-abstract-pg/issues'}, homepage => 'https://mojolicious.org', license => ['http://www.opensource.org/licenses/artistic-license-2.0'], repository => { type => 'git', url => 'https://github.com/mojolicious/sql-abstract-pg.git', web => 'https://github.com/mojolicious/sql-abstract-pg', }, x_IRC => {url => 'irc://chat.freenode.net/#mojo', web => 'https://webchat.freenode.net/#mojo'} }, }, PREREQ_PM => {'SQL::Abstract' => '2.0'}, test => {TESTS => 't/*.t t/*/*.t'} ); SQL-Abstract-Pg-1.0/.perltidyrc000644 000765 000024 00000001024 14014235167 016240 0ustar00sristaff000000 000000 -pbp # Start with Perl Best Practices -w # Show all warnings -iob # Ignore old breakpoints -l=120 # 120 characters per line -mbl=2 # No more than 2 blank lines -i=2 # Indentation is 2 columns -ci=2 # Continuation indentation is 2 columns -vt=0 # Less vertical tightness -pt=2 # High parenthesis tightness -bt=2 # High brace tightness -sbt=2 # High square bracket tightness -wn # Weld nested containers -isbc # Don't indent comments without leading space -nst # Don't output to STDOUT SQL-Abstract-Pg-1.0/META.json000644 000765 000024 00000003112 14014237147 015477 0ustar00sristaff000000 000000 { "abstract" : "PostgreSQL features for SQL::Abstract", "author" : [ "Sebastian Riedel " ], "dynamic_config" : 0, "generated_by" : "ExtUtils::MakeMaker version 7.60, CPAN::Meta::Converter version 2.150010", "license" : [ "artistic_2" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "SQL-Abstract-Pg", "no_index" : { "directory" : [ "t", "inc", "examples", "t" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "SQL::Abstract" : "2.0", "perl" : "5.016" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/mojolicious/sql-abstract-pg/issues" }, "homepage" : "https://mojolicious.org", "license" : [ "http://www.opensource.org/licenses/artistic-license-2.0" ], "repository" : { "type" : "git", "url" : "https://github.com/mojolicious/sql-abstract-pg.git", "web" : "https://github.com/mojolicious/sql-abstract-pg" }, "x_IRC" : { "url" : "irc://chat.freenode.net/#mojo", "web" : "https://webchat.freenode.net/#mojo" } }, "version" : "1.0", "x_serialization_backend" : "JSON::PP version 4.06" } SQL-Abstract-Pg-1.0/lib/SQL/000755 000765 000024 00000000000 14014237147 015266 5ustar00sristaff000000 000000 SQL-Abstract-Pg-1.0/lib/SQL/Abstract/000755 000765 000024 00000000000 14014237147 017031 5ustar00sristaff000000 000000 SQL-Abstract-Pg-1.0/lib/SQL/Abstract/Pg.pm000644 000765 000024 00000026265 14014235137 017745 0ustar00sristaff000000 000000 package SQL::Abstract::Pg; use strict; use warnings; use utf8; use parent 'SQL::Abstract'; our $VERSION = '1.0'; BEGIN { *puke = \&SQL::Abstract::puke } sub insert { my ($self, $table, $data, $options) = @_; local @{$options}{qw(returning _pg_returning)} = (1, 1) if exists $options->{on_conflict} && !$options->{returning}; return $self->SUPER::insert($table, $data, $options); } sub new { my $self = shift->SUPER::new(@_); # -json op push @{$self->{unary_ops}}, { regex => qr/^json$/, handler => sub { '?', {json => $_[2]} } }; return $self; } sub _insert_returning { my ($self, $options) = @_; delete $options->{returning} if $options->{_pg_returning}; # ON CONFLICT my $sql = ''; my @bind; if (exists $options->{on_conflict}) { my $conflict = $options->{on_conflict}; my ($conflict_sql, @conflict_bind); $self->_SWITCH_refkind( $conflict => { ARRAYREF => sub { my ($target, $set) = @$conflict; puke 'on_conflict value must be in the form [$target, \%set]' unless ref $set eq 'HASH'; $target = [$target] unless ref $target eq 'ARRAY'; $conflict_sql = '(' . join(', ', map { $self->_quote($_) } @$target) . ')'; $conflict_sql .= $self->_sqlcase(' do update set '); my ($set_sql, @set_bind) = $self->_update_set_values($set); $conflict_sql .= $set_sql; push @conflict_bind, @set_bind; }, ARRAYREFREF => sub { ($conflict_sql, @conflict_bind) = @$$conflict }, SCALARREF => sub { $conflict_sql = $$conflict }, UNDEF => sub { $conflict_sql = $self->_sqlcase('do nothing') } } ); $sql .= $self->_sqlcase(' on conflict ') . $conflict_sql; push @bind, @conflict_bind; } $sql .= $self->SUPER::_insert_returning($options) if $options->{returning}; return $sql, @bind; } sub _order_by { my ($self, $options) = @_; # Legacy return $self->SUPER::_order_by($options) if ref $options ne 'HASH' or grep {/^-(?:desc|asc)/i} keys %$options; # GROUP BY my $sql = ''; my @bind; if (defined(my $group = $options->{group_by})) { my $group_sql; $self->_SWITCH_refkind( $group => { ARRAYREF => sub { $group_sql = join ', ', map { $self->_quote($_) } @$group; }, SCALARREF => sub { $group_sql = $$group } } ); $sql .= $self->_sqlcase(' group by ') . $group_sql; } # HAVING if (defined(my $having = $options->{having})) { my ($having_sql, @having_bind) = $self->_recurse_where($having); $sql .= $self->_sqlcase(' having ') . $having_sql; push @bind, @having_bind; } # ORDER BY $sql .= $self->_order_by($options->{order_by}) if defined $options->{order_by}; # LIMIT if (defined $options->{limit}) { $sql .= $self->_sqlcase(' limit ') . '?'; push @bind, $options->{limit}; } # OFFSET if (defined $options->{offset}) { $sql .= $self->_sqlcase(' offset ') . '?'; push @bind, $options->{offset}; } # FOR if (defined(my $for = $options->{for})) { my $for_sql; $self->_SWITCH_refkind( $for => { SCALAR => sub { puke qq{for value "$for" is not allowed} unless $for eq 'update'; $for_sql = $self->_sqlcase('UPDATE'); }, SCALARREF => sub { $for_sql .= $$for } } ); $sql .= $self->_sqlcase(' for ') . $for_sql; } return $sql, @bind; } sub _select_fields { my ($self, $fields) = @_; return $fields unless ref $fields eq 'ARRAY'; my (@fields, @bind); for my $field (@$fields) { $self->_SWITCH_refkind( $field => { ARRAYREF => sub { puke 'field alias must be in the form [$name => $alias]' if @$field < 2; push @fields, $self->_quote($field->[0]) . $self->_sqlcase(' as ') . $self->_quote($field->[1]); }, ARRAYREFREF => sub { push @fields, shift @$$field; push @bind, @$$field; }, SCALARREF => sub { push @fields, $$field }, FALLBACK => sub { push @fields, $self->_quote($field) } } ); } return join(', ', @fields), @bind; } sub _table { my ($self, $table) = @_; return $self->SUPER::_table($table) unless ref $table eq 'ARRAY'; my (@table, @join); for my $t (@$table) { if (ref $t eq 'ARRAY') { push @join, $t } else { push @table, $t } } $table = $self->SUPER::_table(\@table); my $sep = $self->{name_sep} // ''; for my $join (@join) { puke 'join must be in the form [$table, $fk => $pk]' if @$join < 3; my ($type, $name, $fk, $pk, @morekeys) = @$join % 2 == 0 ? @$join : ('', @$join); $table .= $self->_sqlcase($type =~ /^-(.+)$/ ? " $1 join " : ' join ') . $self->_quote($name) . $self->_sqlcase(' on ') . '('; do { $table .= $self->_quote(index($fk, $sep) > 0 ? $fk : "$name.$fk") . ' = ' . $self->_quote(index($pk, $sep) > 0 ? $pk : "$table[0].$pk") . (@morekeys ? $self->_sqlcase(' and ') : ')'); } while ($fk, $pk, @morekeys) = @morekeys; } return $table; } 1; =encoding utf8 =head1 NAME SQL::Abstract::Pg - PostgreSQL features for SQL::Abstract =head1 SYNOPSIS use SQL::Abstract::Pg; my $abstract = SQL::Abstract::Pg->new; say $abstract->select('some_table'); =head1 DESCRIPTION L extends L with a few PostgreSQL features used by L. =head2 JSON In many places (as supported by L) you can use the C<-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]}}}); =head1 INSERT $abstract->insert($table, \@values || \%fieldvals, \%options); =head2 ON CONFLICT The C option can be used to generate C queries with C clauses. So far, C to pass C, array references to pass C with conflict targets and a C 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 C. # "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']}); =head1 SELECT $abstract->select($source, $fields, $where, $order); $abstract->select($source, $fields, $where, \%options); =head2 AS The C<$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']); =head2 JOIN The C<$source> argument now also accepts array references containing not only table names, but also array references with tables to generate C 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']]); # "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']]); =head2 ORDER BY Alternatively to the C<$order> argument accepted by L you can now also pass a hash reference with various options. This includes C, which takes the same values as the C<$order> argument. # "SELECT * FROM some_table ORDER BY foo DESC" $abstract->select('some_table', '*', undef, {order_by => {-desc => 'foo'}}); =head2 LIMIT/OFFSET The C and C options can be used to generate C queries with C 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'}); =head2 HAVING The C option can be used to generate C queries with C clauses. So far, the scalar value C to pass C 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'}); =head1 METHODS L inherits all methods from L. =head1 AUTHOR Sebastian Riedel, C. =head1 COPYRIGHT AND LICENSE Copyright (C) 2014-2021, Sebastian Riedel and others. This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. =head1 SEE ALSO L, L, L. =cut SQL-Abstract-Pg-1.0/t/pod.t000644 000765 000024 00000000411 14014235464 015267 0ustar00sristaff000000 000000 use strict; use warnings; use Test::More; plan skip_all => 'set TEST_POD to enable this test (developer only!)' unless $ENV{TEST_POD}; plan skip_all => 'Test::Pod 1.14+ required for this test!' unless eval 'use Test::Pod 1.14; 1'; all_pod_files_ok(); SQL-Abstract-Pg-1.0/t/sql.t000644 000765 000024 00000017322 14014235010 015300 0ustar00sristaff000000 000000 use strict; use warnings; use utf8; use Test::More; use SQL::Abstract::Pg; use SQL::Abstract::Test import => ['is_same_sql_bind']; sub is_query { my ($got, $want, $msg) = @_; my $got_sql = shift @$got; my $want_sql = shift @$want; local $Test::Builder::Level = $Test::Builder::Level + 1; is_same_sql_bind $got_sql, $got, $want_sql, $want, $msg; } my $abstract = SQL::Abstract::Pg->new(array_datatypes => 1, name_sep => '.', quote_char => '"'); subtest 'Basics' => sub { is_query [$abstract->insert('foo', {bar => 'baz'})], ['INSERT INTO "foo" ( "bar") VALUES ( ? )', 'baz'], 'right query'; is_query [$abstract->select('foo', '*')], ['SELECT * FROM "foo"'], 'right query'; is_query [$abstract->select(['foo', 'bar', 'baz'])], ['SELECT * FROM "foo", "bar", "baz"'], 'right query'; }; subtest 'ON CONFLICT' => sub { my @sql = $abstract->insert('foo', {bar => 'baz'}, {on_conflict => \'do nothing'}); is_query \@sql, ['INSERT INTO "foo" ( "bar") VALUES ( ? ) ON CONFLICT do nothing', 'baz'], 'right query'; @sql = $abstract->insert('foo', {bar => 'baz'}, {on_conflict => undef}); is_query \@sql, ['INSERT INTO "foo" ( "bar") VALUES ( ? ) ON CONFLICT DO NOTHING', 'baz'], 'right query'; @sql = $abstract->insert('foo', {bar => 'baz'}, {on_conflict => \'do nothing', returning => '*'}); my $result = ['INSERT INTO "foo" ( "bar") VALUES ( ? ) ON CONFLICT do nothing RETURNING *', 'baz']; is_query \@sql, $result, 'right query'; @sql = $abstract->insert('foo', {bar => 'baz'}, {on_conflict => \['(foo) do update set foo = ?', 'yada']}); $result = ['INSERT INTO "foo" ( "bar") VALUES ( ? )' . ' ON CONFLICT (foo) do update set foo = ?', 'baz', 'yada']; is_query \@sql, $result, 'right query'; @sql = $abstract->insert('foo', {bar => 'baz'}, {on_conflict => [foo => {foo => 'yada'}]}); $result = ['INSERT INTO "foo" ( "bar") VALUES ( ? )' . ' ON CONFLICT ("foo") DO UPDATE SET "foo" = ?', 'baz', 'yada']; is_query \@sql, $result, 'right query'; @sql = $abstract->insert('foo', {bar => 'baz'}, {on_conflict => [['foo', 'bar'] => {foo => 'yada'}]}); $result = ['INSERT INTO "foo" ( "bar") VALUES ( ? )' . ' ON CONFLICT ("foo", "bar") DO UPDATE SET "foo" = ?', 'baz', 'yada']; is_query \@sql, $result, 'right query'; }; subtest 'ON CONFLICT (unsupported value)' => sub { eval { $abstract->insert('foo', {bar => 'baz'}, {on_conflict => [[], []]}) }; like $@, qr/on_conflict value must be in the form \[\$target, \\\%set\]/, 'right error'; eval { $abstract->insert('foo', {bar => 'baz'}, {on_conflict => {}}) }; like $@, qr/HASHREF/, 'right error'; }; subtest 'ORDER BY' => sub { my @sql = $abstract->select('foo', '*', {bar => 'baz'}, {-desc => 'yada'}); is_query \@sql, ['SELECT * FROM "foo" WHERE ( "bar" = ? ) ORDER BY "yada" DESC', 'baz'], 'right query'; @sql = $abstract->select('foo', '*', {bar => 'baz'}, {order_by => {-desc => 'yada'}}); is_query \@sql, ['SELECT * FROM "foo" WHERE ( "bar" = ? ) ORDER BY "yada" DESC', 'baz'], 'right query'; }; subtest 'LIMIT/OFFSET' => sub { my @sql = $abstract->select('foo', '*', undef, {limit => 10, offset => 5}); is_query \@sql, ['SELECT * FROM "foo" LIMIT ? OFFSET ?', 10, 5], 'right query'; }; subtest 'GROUP BY' => sub { my @sql = $abstract->select('foo', '*', undef, {group_by => \'bar, baz'}); is_query \@sql, ['SELECT * FROM "foo" GROUP BY bar, baz'], 'right query'; @sql = $abstract->select('foo', '*', undef, {group_by => ['bar', 'baz']}); is_query \@sql, ['SELECT * FROM "foo" GROUP BY "bar", "baz"'], 'right query'; }; subtest 'HAVING' => sub { my @sql = $abstract->select('foo', '*', undef, {group_by => ['bar'], having => {baz => 'yada'}}); is_query \@sql, ['SELECT * FROM "foo" GROUP BY "bar" HAVING "baz" = ?', 'yada'], 'right query'; @sql = $abstract->select('foo', '*', {bar => {'>' => 'baz'}}, {group_by => ['bar'], having => {baz => {'<' => 'bar'}}}); my $result = ['SELECT * FROM "foo" WHERE ( "bar" > ? ) GROUP BY "bar" HAVING "baz" < ?', 'baz', 'bar']; is_query \@sql, $result, 'right query'; }; subtest 'GROUP BY (unsupported value)' => sub { eval { $abstract->select('foo', '*', undef, {group_by => {}}) }; like $@, qr/HASHREF/, 'right error'; }; subtest 'FOR' => sub { my @sql = $abstract->select('foo', '*', undef, {for => 'update'}); is_query \@sql, ['SELECT * FROM "foo" FOR UPDATE'], 'right query'; @sql = $abstract->select('foo', '*', undef, {for => \'update skip locked'}); is_query \@sql, ['SELECT * FROM "foo" FOR update skip locked'], 'right query'; }; subtest 'FOR (unsupported value)' => sub { eval { $abstract->select('foo', '*', undef, {for => 'update skip locked'}) }; like $@, qr/for value "update skip locked" is not allowed/, 'right error'; eval { $abstract->select('foo', '*', undef, {for => []}) }; like $@, qr/ARRAYREF/, 'right error'; }; subtest 'AS' => sub { my @sql = $abstract->select('foo', ['bar', [bar => 'baz'], 'yada']); is_query \@sql, ['SELECT "bar", "bar" AS "baz", "yada" FROM "foo"'], 'right query'; @sql = $abstract->select('foo', ['bar', \'extract(epoch from baz) as baz', 'yada']); is_query \@sql, ['SELECT "bar", extract(epoch from baz) as baz, "yada" FROM "foo"'], 'right query'; @sql = $abstract->select('foo', ['bar', \['? as baz', 'test'], 'yada']); is_query \@sql, ['SELECT "bar", ? as baz, "yada" FROM "foo"', 'test'], 'right query'; }; subtest 'AS (unsupported value)' => sub { eval { $abstract->select('foo', [[]]) }; like $@, qr/field alias must be in the form \[\$name => \$alias\]/, 'right error'; }; subtest 'JSON' => sub { my @sql = $abstract->update('foo', {bar => {-json => [1, 2, 3]}}); is_query \@sql, ['UPDATE "foo" SET "bar" = ?', {json => [1, 2, 3]}], 'right query'; @sql = $abstract->select('foo', '*', {bar => {'=' => {-json => [1, 2, 3]}}}); is_query \@sql, ['SELECT * FROM "foo" WHERE ( "bar" = ? )', {json => [1, 2, 3]}], 'right query'; }; subtest 'JOIN' => sub { my @sql = $abstract->select(['foo', ['bar', foo_id => 'id']]); is_query \@sql, ['SELECT * FROM "foo" JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'], 'right query'; @sql = $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]); is_query \@sql, ['SELECT * FROM "foo" JOIN "bar" ON ("foo"."id" = "bar"."foo_id")'], 'right query'; @sql = $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id', 'foo.id2' => 'bar.foo_id2']]); is_query \@sql, ['SELECT * FROM "foo" JOIN "bar" ON ("foo"."id" = "bar"."foo_id"' . ' AND "foo"."id2" = "bar"."foo_id2"' . ')'], 'right query'; @sql = $abstract->select(['foo', ['bar', foo_id => 'id'], ['baz', foo_id => 'id']]); my $result = [ 'SELECT * FROM "foo"' . ' JOIN "bar" ON ("bar"."foo_id" = "foo"."id")' . ' JOIN "baz" ON ("baz"."foo_id" = "foo"."id")' ]; is_query \@sql, $result, 'right query'; @sql = $abstract->select(['foo', [-left => 'bar', foo_id => 'id']]); is_query \@sql, ['SELECT * FROM "foo" LEFT JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'], 'right query'; @sql = $abstract->select(['foo', [-right => 'bar', foo_id => 'id']]); is_query \@sql, ['SELECT * FROM "foo" RIGHT JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'], 'right query'; @sql = $abstract->select(['foo', [-inner => 'bar', foo_id => 'id']]); is_query \@sql, ['SELECT * FROM "foo" INNER JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'], 'right query'; @sql = $abstract->select(['foo', [-left => 'bar', foo_id => 'id', foo_id2 => 'id2', foo_id3 => 'id3']]); is_query \@sql, [ 'SELECT * FROM "foo" LEFT JOIN "bar" ON ("bar"."foo_id" = "foo"."id"' . ' AND "bar"."foo_id2" = "foo"."id2"' . ' AND "bar"."foo_id3" = "foo"."id3"' . ')' ], 'right query'; }; subtest 'JOIN (unsupported value)' => sub { eval { $abstract->select(['foo', []]) }; like $@, qr/join must be in the form \[\$table, \$fk => \$pk\]/, 'right error'; }; done_testing(); SQL-Abstract-Pg-1.0/t/pod_coverage.t000644 000765 000024 00000000677 14014235551 017155 0ustar00sristaff000000 000000 use strict; use warnings; use Test::More; plan skip_all => 'set TEST_POD to enable this test (developer only!)' unless $ENV{TEST_POD}; plan skip_all => 'Test::Pod::Coverage 1.04+ required for this test!' unless eval 'use Test::Pod::Coverage 1.04; 1'; my $private = ['insert', 'new', 'puke', 'select']; my %RULES = ('SQL::Abstract::Pg' => {also_private => $private}); pod_coverage_ok($_, $RULES{$_} || {}) for all_modules(); done_testing();