blib/lib/SQL/Maker/SelectSet.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 56.2% 9 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod 66.6% 4 / 6 totaltotal 86.6% 104 / 120 SCAR 25.9
CC
13
Cov
87%
as_sql_order_by
4.3
add_statement
3.4
new
3.2
CRAP 13.3
« blib/lib/SQL/Maker/Plugin/InsertMulti.pm ↑ Summary blib/lib/SQL/Maker/Select/Oracle.pm »
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 use Class::Accessor::Lite (
9 1 ro => [qw/new_line operator/],
10 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our @EXPORT_OK = qw(union union_all intersect intersect_all except except_all);
13  
14 # Functions
15 BEGIN {
16 1 ▶ for (qw/union union_all intersect intersect_all except except_all/) {
Subroutine BEGIN: called CC=2 SCAR=6.9
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 {
22 33 ▶ my $stmt = SQL::Maker::SelectSet->new(
Subroutine __ANON__: called CC=2 SCAR=6.9
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 sub new {
37 30 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.8
Pod documented
38 30 ▶ my %args = @_==1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 30 30
39 30 ▶ Carp::croak("Missing mandatory parameter 'operator' for SQL::Maker::SelectSet->new") unless exists $args{operator};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"operator"} 0 30 30
40  
41 30 my $set = bless {
42 new_line => qq{\n},
43 %args,
44 }, $class;
45  
46 30 return $set;
47 }
48  
49 sub add_statement {
50 60 ▶ my ($self, $statement) = @_;
Subroutine add_statement: called CC=3 SCAR=12.3
Pod documented
51  
52 60 ▶ unless ( Scalar::Util::blessed($statement) and $statement->can('as_sql') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($statement) and $statement->can("as_sql")) 0 60 60
Condition33%1/3
builtin::blessed($statement) and $statement->can("as_sql")
!ll&&!rl&&r
0060
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")
ABresult
0X0
100
111
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 sub as_sql_order_by {
60 3 ▶ my ($self) = @_;
Subroutine as_sql_order_by: called CC=4 SCAR=14.6
Pod undocumented
61  
62 3 my @attrs = @{$self->{order_by}};
63 3 ▶ return '' unless @attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @attrs 0 3 3
64  
65 return 'ORDER BY '
66 . join(', ', map {
67 3 my ($col, $type) = @$_;
68 3 ▶ if (ref $col) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col) { } 0 3 3
69 0 $$col
70 } else {
71 3 ▶ $type ? $self->_quote($col) . " $type" : $self->_quote($col)
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 3 3
72 }
73 } @attrs);
74 }
75  
76 sub _quote {
77 3 ▶ my ($self, $label) = @_;
Subroutine _quote: called CC=2 SCAR=7.1
78  
79 3 ▶ return $$label if ref $label eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $label eq "SCALAR" 0 3 3
80 SQL::Maker::Util::quote_identifier($label, $self->{quote_char}, $self->{name_sep})
81 3 }
82  
83 sub as_sql {
84 36 ▶ my ($self) = @_;
Subroutine as_sql: called CC=2 SCAR=6.9
Pod documented
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
BranchTrueFalseTotal
if $self->{"order_by"} 3 33 36
94 36 return $sql;
95 }
96  
97 sub bind {
98 36 ▶ my ($self) = @_;
Subroutine bind: called CC=2 SCAR=6.9
Pod documented
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 sub add_order_by {
107 3 ▶ my ($self, $col, $type) = @_;
Subroutine add_order_by: called CC=1 SCAR=0.0
Pod undocumented
108 3 push @{$self->{order_by}}, [$col, $type];
109 3 return $self;
110 }
111  
112 1 1;
113 __END__
114  
115 =head1 NAME
116  
117 SQL::Maker::SelectSet - provides set functions
118  
119 =head1 SYNOPSIS
120  
121 use SQL::Maker::SelectSet qw(union_all except);
122 my $s1 = SQL::Maker::Select ->new()
123 ->add_select('foo')
124 ->add_from('t1');
125 my $s2 = SQL::Maker::Select ->new()
126 ->add_select('bar')
127 ->add_from('t2');
128 union_all( $s1, $s2 )->as_sql;
129 # =>
130 # SQL::Maker::SelectSet->new_set(
131 # operator => 'UNION ALL',
132 # new_line => $s1->new_line
133 # )->add_statement($s1)
134 # ->add_statement($s2)
135 # ->as_sql;
136 # => "SELECT foo FROM t1 UNION ALL SELECT bar FROM t2"
137 except( $s1, $s2 )->as_sql;
138 # => SQL::Maker::SelectSet->new_set( operator => 'EXCEPT', new_line => $s1->new_line )
139 # ->add_statement( $s1 )
140 # ->add_statement( $s2 )
141 # ->as_sql;
142 # => "SELECT foo FROM t1 EXCEPT SELECT bar FROM t2"
143  
144 =head1 DESCRIPTION
145  
146 This module provides some set functions which return a SQL::Maker::SelectSet object
147 inherited from L<SQL::Maker::Select>.
148  
149 =head1 FUNCTION
150  
151 =over 4
152  
153 =item C<< union($select :SQL::Maker::Select | $set :SQL::Maker::SelectSet) : SQL::Maker::SelectSet >>
154  
155 Tow statements are combined by C<UNION>.
156  
157 =item C<< union_all($select :SQL::Maker::Select | $set :SQL::Maker::SelectSet) : SQL::Maker::SelectSet >>
158  
159 Tow statements are combined by C<UNION ALL>.
160  
161 =item C<< intersect($select :SQL::Maker::Select | $set :SQL::Maker::SelectSet) : SQL::Maker::SelectSet >>
162  
163 Tow statements are combined by C<INTERSECT>.
164  
165 =item C<< intersect_all($select :SQL::Maker::Select | $set :SQL::Maker::SelectSet) : SQL::Maker::SelectSet >>
166  
167 Tow statements are combined by C<INTERSECT ALL>.
168  
169 =item C<< except($select :SQL::Maker::Select | $set :SQL::Maker::SelectSet) : SQL::Maker::SelectSet >>
170  
171 Tow statements are combined by C<EXCEPT>.
172  
173 =item C<< except($select :SQL::Maker::Select | $set :SQL::Maker::SelectSet) : SQL::Maker::SelectSet >>
174  
175 Tow statements are combined by C<EXCEPT ALL>.
176  
177 =back
178  
179 =head1 Class Method
180  
181 =over 4
182  
183 =item my $stmt = SQL::Maker::SelectSet->new( %args )
184  
185 $opretaor is a set operator (ex. C<UNION>).
186 $one and $another are SQL::Maker::Select object or SQL::Maker::SelectSet object.
187 It returns a SQL::Maker::SelectSet object.
188  
189 The parameters are:
190  
191 =over 4
192  
193 =item $new_line
194  
195 Default values is "\n".
196  
197 =item $operator : Str
198  
199 The operator. This parameter is required.
200  
201 =back
202  
203 =back
204  
205 =head1 Instance Methods
206  
207 =over 4
208  
209 =item C<< my $sql = $set->as_sql() : Str >>
210  
211 Returns a new select statement.
212  
213 =item C<< my @binds = $set->bind() : Array[Str] >>
214  
215 Returns bind variables.
216  
217 =item C<< $set->add_statement($stmt : $stmt->can('as_sql')) : SQL::Maker::SelectSet >>
218  
219 This method adds new statement object. C<< $stmt >> must provides 'as_sql' method.
220  
221 I<Return Value> is the $set itself.
222  
223 =back
224  
225 =head1 SEE ALSO
226  
227 L<SQL::Maker::Select>
228  
« blib/lib/SQL/Maker/Plugin/InsertMulti.pm ↑ Summary blib/lib/SQL/Maker/Select/Oracle.pm »