Fey/SQL/Insert.pm

filter: Statementstmt 100% 91 / 91 Branchbran 100% 14 / 14 Conditioncond 75.0% 9 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 96.6% 144 / 149 SCAR 27.7
CC
16
Cov
97%
values
7.0
into
6.0
values_clause
5.1
CRAP 16.0
« Fey/SQL/Update.pm ↑ Summary Fey/FK.pm »
1 package Fey::SQL::Insert;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.43';
8  
9 use Fey::Types
10 26 qw( ArrayRef HashRef CanQuote IntoElement NonNullableInsertValue
11 26 ▶ NullableInsertValue );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 26 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 26 ▶use MooseX::Params::Validate 0.21 qw( validated_hash pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 26 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 26 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 26 with 'Fey::Role::SQL::HasBindParams';
21  
22 26 has '_into' => (
23 is => 'rw',
24 isa => ArrayRef,
25 init_arg => undef,
26 );
27  
28 26 has '_values_spec' => (
29 is => 'rw',
30 isa => HashRef,
31 init_arg => undef,
32 );
33  
34 has '_values' => (
35 traits => ['Array'],
36 is => 'bare',
37 isa => ArrayRef [HashRef],
38 27 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 26 handles => {
40 _add_values => 'push',
41 _values => 'elements',
42 },
43 init_arg => undef,
44 );
45  
46 26 with 'Fey::Role::SQL::Cloneable';
47  
48 26 ▶sub insert { return $_[0] }
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
49  
50 sub into {
51 27 ▶ my $self = shift;
Subroutine into: called CC=6 SCAR=17.9
Pod documented
52  
53 27 ▶ my $count = @_ ? scalar @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 26 1 27
54 27 my @into = pos_validated_list(
55 \@_,
56 ( ( { isa => IntoElement } ) x $count ),
57 MX_PARAMS_VALIDATE_NO_CACHE => 1,
58 );
59  
60 26 my @cols;
61 26 for my $elt (@into) {
62 43 ▶ push @cols, $elt->isa('Fey::Table')
Branch: 1 branch
BranchTrueFalseTotal
$elt->isa('Fey::Table') ? : 1 42 43
63 ? $elt->columns
64 : $elt;
65 }
66  
67 26 $self->_set_into( \@cols );
68  
69 26 my %spec;
70 26 for my $col ( @{ $self->_into() } ) {
71 46 ▶ $spec{ $col->name() }
Branch: 1 branch
BranchTrueFalseTotal
$col->is_nullable ? : 18 28 46
72 = $col->is_nullable()
73 ? { isa => NullableInsertValue }
74 : { isa => NonNullableInsertValue };
75 }
76  
77 26 $self->_set_values_spec( \%spec );
78  
79 26 return $self;
80 }
81  
82 sub values {
83 26 ▶ my $self = shift;
Subroutine values: called CC=7 SCAR=19.5
Pod documented
84  
85 my %vals = validated_hash(
86 \@_,
87 26 %{ $self->_values_spec() },
88 MX_PARAMS_VALIDATE_NO_CACHE => 1
89 );
90  
91 23 for my $col_name (
92 37 grep { exists $vals{$_} }
93 37 map { $_->name() } @{ $self->_into() }
94 ) {
95 37 my $val = $vals{$col_name};
96  
97 37 ▶ $val .= ''
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and overload::Overloaded($val) 4 33 37
Condition100%3/3
builtin::blessed($val) and overload::Overloaded($val)
!ll&&!rl&&r
22114
MC/DC100%2/2
builtin::blessed($val) and overload::Overloaded($val)
builtin::blessed($val) overload::Overloaded($val)
Truth table100%3/3
A and B
A= builtin::blessed($val)B= overload::Overloaded($val)
ABresult
0X0
100
111
98 if blessed $val && overload::Overloaded($val);
99  
100 37 ▶ if ( !blessed $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($val)) 26 11 37
101 26 ▶ if ( defined $val && $self->auto_placeholders() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $self->auto_placeholders) { } 16 10 26
Condition100%3/3
defined $val and $self->auto_placeholders
!ll&&!rl&&r
2816
MC/DC100%2/2
defined $val and $self->auto_placeholders
defined $val $self->auto_placeholders
Truth table100%3/3
A and B
A= defined $valB= $self->auto_placeholders
ABresult
0X0
100
111
102 16 $self->_add_bind_param($val);
103  
104 16 $val = Fey::Placeholder->new();
105 }
106 else {
107 10 $val = Fey::Literal->new_from_scalar($val);
108 }
109 }
110  
111 37 $vals{$col_name} = $val;
112 }
113  
114 23 $self->_add_values( \%vals );
115  
116 23 return $self;
117 }
118  
119 sub sql {
120 7 ▶ my $self = shift;
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
121 7 my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } );
122  
123 return (
124 7 join ' ',
125 $self->insert_clause($dbh),
126 $self->columns_clause($dbh),
127 $self->values_clause($dbh),
128 );
129 }
130  
131 sub insert_clause {
132 10 ▶ return ( 'INSERT INTO '
Subroutine insert_clause: called CC=1 SCAR=0.0
Pod documented
133 . $_[1]->quote_identifier( $_[0]->_into()->[0]->table()->name() )
134 );
135 }
136  
137 sub columns_clause {
138 return (
139 '('
140 . (
141 join ', ',
142 12 ▶ map { $_[1]->quote_identifier( $_->name() ) } @{ $_[0]->_into() }
Subroutine columns_clause: called CC=1 SCAR=0.0
Pod documented
143 )
144 . ')'
145 );
146 }
147  
148 sub values_clause {
149 20 ▶ my $self = shift;
Subroutine values_clause: called CC=5 SCAR=16.2
Pod documented
150 20 my $dbh = shift;
151  
152 20 my @cols = @{ $self->_into() };
153  
154 20 my @v;
155 20 for my $vals ( $self->_values() ) {
156 22 my $v = '(';
157  
158 $v .= (
159 join ', ',
160 map {
161 22 my $val = $vals->{ $_->name() };
162 35 my $sql = $val->sql($dbh);
163 35 ▶ blessed $val
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($val) && $val->can('does') && $val->does('Fey::Role::SQL::ReturnsData') ? : 1 34 35
Condition50%3/6
builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData")
!ll&&!rl&&r
0341
builtin::blessed($val) and $val->can("does")
!ll&&!rl&&r
0035
MC/DC33%1/3
builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData")
builtin::blessed($val) $val->can("does") $val->does("Fey::Role::SQL::ReturnsData")
Truth table50%2/4
A and B and C
A= builtin::blessed($val)B= $val->can("does")C= $val->does("Fey::Role::SQL::ReturnsData")
ABCresult
0XX0
10X0
1100
1111
164 && $val->can('does')
165 && $val->does('Fey::Role::SQL::ReturnsData')
166 ? "($sql)"
167 : $sql
168 } @cols
169 );
170  
171 22 $v .= ')';
172  
173 22 push @v, $v;
174 }
175  
176 20 return 'VALUES ' . join ',', @v;
177 }
178  
179 26 __PACKAGE__->meta()->make_immutable();
180  
181 26 1;
182  
183 # ABSTRACT: Represents a INSERT query
184  
185 __END__
186  
187 =pod
188  
189 =head1 NAME
190  
191 Fey::SQL::Insert - Represents a INSERT query
192  
193 =head1 VERSION
194  
195 version 0.43
196  
197 =head1 SYNOPSIS
198  
199 my $sql = Fey::SQL->new_insert();
200  
201 # INSERT INTO Part
202 # (part_id, name, quantity)
203 # VALUES
204 # (?, ?, ?)
205 $sql->insert()->into($Part);
206  
207 my $ph = Fey::Placeholder->new();
208  
209 $sql->values( part_id => $ph,
210 name => $ph,
211 quantity => $ph,
212 );
213  
214 print $sql->sql($dbh);
215  
216 =head1 DESCRIPTION
217  
218 This class represents a C<INSERT> query.
219  
220 =head1 METHODS
221  
222 This class provides the following methods:
223  
224 =head2 Constructor
225  
226 To construct an object of this class, call C<< $query->insert() >> on
227 a C<Fey::SQL> object.
228  
229 =head2 $insert->insert()
230  
231 This method is basically a no-op that exists to so that L<Fey::SQL>
232 has something to call after it constructs an object in this class.
233  
234 =head2 $insert->into()
235  
236 This method specifies the C<INTO> clause of the query. It expects a
237 list of L<Fey::Column> and/or L<Fey::Table> objects, but not aliases.
238  
239 If you pass a table object, then the C<INTO> will include all of that
240 table's columns, in the order returned by the C<< $table->columns() >>
241 method.
242  
243 Most RDBMS implementations only allow for a single table here, but
244 some (like MySQL) do allow for multi-table inserts.
245  
246 =head2 $insert->values(...)
247  
248 This method takes a hash where the keys are column names, and values are the
249 value to be inserted for that column. Each value can be of the following:
250  
251 =over 4
252  
253 =item * a plain scalar, including undef
254  
255 This will be passed to C<< Fey::Literal->new_from_scalar() >>.
256  
257 =item * C<Fey::Literal> object
258  
259 =item * C<Fey::Placeholder> object
260  
261 =back
262  
263 You can call this method multiple times in order to do a multi-row
264 insert.
265  
266 =head2 $insert->sql()
267  
268 Returns the full SQL statement which this object represents. A DBI
269 handle must be passed so that identifiers can be properly quoted.
270  
271 =head2 $insert->bind_params()
272  
273 See the L<Fey::SQL section on Bind Parameters|Fey::SQL/Bind
274 Parameters> for more details.
275  
276 =head2 $insert->insert_clause()
277  
278 Returns the C<INSERT INTO> clause portion of the SQL statement as a
279 string (just the tables).
280  
281 =head2 $insert->columns_clause()
282  
283 Returns the portion of the SQL statement containing the columns for
284 which values are being inserted as a string.
285  
286 =head2 $insert->values_clause()
287  
288 Returns the C<VALUES> clause portion of the SQL statement as a string.
289  
290 =head1 ROLES
291  
292 =over 4
293  
294 =item * L<Fey::Role::SQL::HasBindParams>
295  
296 =item * L<Fey::Role::SQL::Cloneable>
297  
298 =back
299  
300 =head1 BUGS
301  
302 See L<Fey> for details on how to report bugs.
303  
304 =head1 AUTHOR
305  
306 Dave Rolsky <autarch@urth.org>
307  
308 =head1 COPYRIGHT AND LICENSE
309  
310 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
311  
312 This is free software, licensed under:
313  
314 The Artistic License 2.0 (GPL Compatible)
315  
316 =cut
« Fey/SQL/Update.pm ↑ Summary Fey/FK.pm »