| 1 | package SQL::Maker::SelectSet; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use SQL::Maker::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ro => [qw/new_line operator/], | |||||||||||||||||||||||||||
| 10 | 1 | ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(union union_all intersect intersect_all except except_all); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Functions | ||||||||||||||||||||||||||||
| 15 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | 1 | for (qw/union union_all intersect intersect_all except except_all/) { | |||||||||||||||||||||||||||
| 17 | 6 | my $method = $_; | |||||||||||||||||||||||||||
| 18 | 6 | (my $operator = uc $_) =~ s/_/ /; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 6 | ▶ | *{__PACKAGE__ . '::' . $method} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 33 | my $stmt = SQL::Maker::SelectSet->new( | |||||||||||||||||||||||||||
| 23 | operator => $operator, | ||||||||||||||||||||||||||||
| 24 | new_line => $_[0]->new_line, | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | 30 | $stmt->add_statement($_) for @_; | |||||||||||||||||||||||||||
| 27 | 30 | return $stmt; | |||||||||||||||||||||||||||
| 28 | 6 | }; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # Methods | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 30 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 30 | my $class = shift; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | my %args = @_==1 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 30 | ▶ | Carp::croak("Missing mandatory parameter 'operator' for SQL::Maker::SelectSet->new") unless exists $args{operator}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 30 | my $set = bless { | |||||||||||||||||||||||||||
| 42 | new_line => qq{\n}, | ||||||||||||||||||||||||||||
| 43 | %args, | ||||||||||||||||||||||||||||
| 44 | }, $class; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 30 | return $set; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 60 | ▶ | sub add_statement { | ||||||||||||||||||||||||||
|
Subroutine
add_statement: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 60 | my ($self, $statement) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 60 | ▶ | unless ( Scalar::Util::blessed($statement) and $statement->can('as_sql') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($statement) and $statement->can("as_sql")
MC/DC0%0/2
builtin::blessed($statement) and $statement->can("as_sql")
builtin::blessed($statement)
$statement->can("as_sql")
Truth table33%1/3
A and B
A= builtin::blessed($statement)B= $statement->can("as_sql")
| |||||||||||||||||||||||||||||
| 53 | 0 | Carp::croak( "'$statement' doesn't have 'as_sql' method."); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 60 | push @{$self->{statements}}, $statement; | |||||||||||||||||||||||||||
| 56 | 60 | return $self; # method chain | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub as_sql_order_by { | ||||||||||||||||||||||||||
|
Subroutine
as_sql_order_by: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | my @attrs = @{$self->{order_by}}; | |||||||||||||||||||||||||||
| 63 | 3 | ▶ | return '' unless @attrs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | return 'ORDER BY ' | ||||||||||||||||||||||||||||
| 66 | . join(', ', map { | ||||||||||||||||||||||||||||
| 67 | 3 | my ($col, $type) = @$_; | |||||||||||||||||||||||||||
| 68 | 3 | ▶ | if (ref $col) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $$col | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 3 | ▶ | $type ? $self->_quote($col) . " $type" : $self->_quote($col) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } @attrs); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub _quote { | ||||||||||||||||||||||||||
|
Subroutine
_quote: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 77 | 3 | my ($self, $label) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | return $$label if ref $label eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | SQL::Maker::Util::quote_identifier($label, $self->{quote_char}, $self->{name_sep}) | ||||||||||||||||||||||||||||
| 81 | 3 | } | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 36 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 36 | my ($self) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 36 | my $new_line = $self->new_line; | |||||||||||||||||||||||||||
| 87 | 36 | my $operator = $self->operator; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | my $sql = join( | ||||||||||||||||||||||||||||
| 90 | $new_line . $operator . $new_line, | ||||||||||||||||||||||||||||
| 91 | 36 | map { $_->as_sql } @{ $self->{statements} } | |||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | 36 | ▶ | $sql .= ' ' . $self->as_sql_order_by() if $self->{order_by}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 36 | return $sql; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 36 | ▶ | sub bind { | ||||||||||||||||||||||||||
|
Subroutine
bind: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 36 | my ($self) = @_; | |||||||||||||||||||||||||||
| 99 | 36 | my @binds; | |||||||||||||||||||||||||||
| 100 | 36 | for my $select ( @{ $self->{statements} } ) { | |||||||||||||||||||||||||||
| 101 | 72 | push @binds, $select->bind; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 36 | return @binds; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | ▶ | sub add_order_by { | ||||||||||||||||||||||||||
|
Subroutine
add_order_by: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 3 | my ($self, $col, $type) = @_; | |||||||||||||||||||||||||||
| 108 | 3 | push @{$self->{order_by}}, [$col, $type]; | |||||||||||||||||||||||||||
| 109 | 3 | return $self; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||