SQL/Abstract/mysql.pm

filter: Statementstmt 96.4% 82 / 85 Branchbran 96.1% 50 / 52 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 87.5% 14 / 16 Podpod 100% 1 / 1 totaltotal 94.6% 159 / 168 CC 35 SCAR 20.9
CC
35
Cov
96%
_table
15.0
_order_by
9.0
insert
8.0
CRAP 8.1
« Mojo/mysql.pm ↑ Summary Mojo/mysql/Transaction.pm »
1 package SQL::Abstract::mysql;
2 18 ▶use Mojo::Base 'SQL::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶BEGIN { *puke = \&SQL::Abstract::puke }
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶sub insert {
Subroutine insert: called CC=8 SCAR=20.8
Pod documented
7 6 ▶ my ($self, $options) = (shift, $_[2] || {}); # ($self, $table, $data, $options)
Condition100%2/2
$_[2] || {}
l!l
51
MC/DC100%1/1
$_[2] || {}
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
8 6 my ($sql, @bind) = $self->SUPER::insert(@_);
9  
10 # options
11 6 ▶ if (exists $options->{on_conflict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"on_conflict"}) 5 1 6
12 5 ▶ my $on_conflict = $options->{on_conflict} // '';
Condition100%2/2
$options->{"on_conflict"} // ""
l!l
41
MC/DC100%1/1
$options->{"on_conflict"} // ""
$options->{"on_conflict"}
Truth table100%2/2
A
A= $options->{"on_conflict"}
Aresult
11
00
13 5 ▶ if (ref $on_conflict eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $on_conflict eq 'HASH') { } 1 4 5
elsif ($on_conflict eq 'ignore') { } 1 3 4
elsif ($on_conflict eq 'replace') { } 1 2 3
14 1 my ($s, @b) = $self->_update_set_values($on_conflict);
15 1 $sql .= $self->_sqlcase(' on duplicate key update ') . $s;
16 1 push @bind, @b;
17 }
18 elsif ($on_conflict eq 'ignore') {
19 1 $sql =~ s/^(\w+)/{$self->_sqlcase('insert ignore')}/e;
20 }
21 elsif ($on_conflict eq 'replace') {
22 1 $sql =~ s/^(\w+)/{$self->_sqlcase('replace')}/e;
23 }
24 else {
25 2 puke qq{on_conflict value "$on_conflict" is not allowed};
26 }
27 }
28  
29 4 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
30 }
31  
32 6 ▶sub _mysql_for {
Subroutine _mysql_for: called CC=1 SCAR=0.0
33 6 my ($self, $param) = @_;
34  
35 return $self->_SWITCH_refkind(
36 $param => {
37 4 ▶ SCALAR => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
38 4 ▶ return $self->_sqlcase('lock in share mode') if $param eq 'share';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "share" 2 2 4
39 2 ▶ return $self->_sqlcase('for update') if $param eq 'update';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "update" 1 1 2
40 1 puke qq{for value "$param" is not allowed};
41 },
42 1 ▶ SCALARREF => sub { $self->_sqlcase('for ') . $$param },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 }
44 6 );
45 }
46  
47 6 ▶sub _mysql_group_by {
Subroutine _mysql_group_by: called CC=1 SCAR=0.0
48 6 my ($self, $param) = @_;
49  
50 return $self->_SWITCH_refkind(
51 6 ▶ $param => {ARRAYREF => sub { join ', ', map $self->_quote($_), @$param }, SCALARREF => sub {$$param},});
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
52 }
53  
54 16 ▶sub _order_by {
Subroutine _order_by: called CC=9 SCAR=22.0
55 16 my ($self, $options) = @_;
56 16 my ($sql, @bind) = ('');
57  
58 # Legacy
59 16 ▶ return $self->SUPER::_order_by($options) if ref $options ne 'HASH' or grep {/^-(?:desc|asc)/i} keys %$options;
Branch: 1 branch
BranchTrueFalseTotal
if ref $options ne "HASH" or grep {/^-(?:desc|asc)/iu;} keys %$options 3 13 16
Condition100%3/3
ref $options ne "HASH" or grep {/^-(?:desc|asc)/iu;} keys %$options
l!l&&r!l&&!r
1213
MC/DC100%2/2
ref $options ne "HASH" or grep {/^-(?:desc|asc)/iu;} keys %$options
ref $options ne "HASH" grep {/^-(?:desc|asc)/iu;} keys %$options
Truth table100%3/3
A or B
A= ref $options ne "HASH"B= grep {/^-(?:desc|asc)/iu;} keys %$options
ABresult
1X1
011
000
60  
61 # GROUP BY
62 13 ▶ $sql .= $self->_sqlcase(' group by ') . $self->_mysql_group_by($options->{group_by}) if defined $options->{group_by};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"group_by"} 6 7 13
63  
64 # HAVING
65 12 ▶ if (defined($options->{having})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"having"}) 3 9 12
66 3 my ($s, @b) = $self->_recurse_where($options->{having});
67 3 $sql .= $self->_sqlcase(' having ') . $s;
68 3 push @bind, @b;
69 }
70  
71 # ORDER BY
72 12 ▶ $sql .= $self->_order_by($options->{order_by}) if defined $options->{order_by};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"order_by"} 2 10 12
73  
74 # LIMIT / OFFSET
75 12 for my $name (qw(limit offset)) {
76 24 ▶ next unless defined $options->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{$name} 20 4 24
77 4 $sql .= $self->_sqlcase(" $name ") . '?';
78 4 push @bind, $options->{$name};
79 }
80  
81 # FOR
82 12 ▶ $sql .= ' ' . $self->_mysql_for($options->{for}) if defined $options->{for};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"for"} 6 6 12
83  
84 10 return $sql, @bind;
85 }
86  
87 29 ▶sub _select_fields {
Subroutine _select_fields: called CC=3 SCAR=11.4
88 29 my ($self, $fields) = @_;
89  
90 29 ▶ return $fields unless ref $fields eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "ARRAY" 26 3 29
91  
92 3 my (@fields, @bind);
93 3 for my $field (@$fields) {
94 $self->_SWITCH_refkind(
95 $field => {
96 2 ▶ ARRAYREF => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
97 2 ▶ puke 'field alias must be in the form [$name => $alias]' if @$field < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$field < 2 0 2 2
98 2 push @fields, $self->_quote($field->[0]) . $self->_sqlcase(' as ') . $self->_quote($field->[1]);
99 },
100 0 ▶ ARRAYREFREF => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 push @fields, shift @$$field;
102 0 push @bind, @$$field;
103 },
104 0 ▶ SCALARREF => sub { push @fields, $$field },
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 3 ▶ FALLBACK => sub { push @fields, $self->_quote($field) }
Subroutine __ANON__: called CC=1 SCAR=0.0
106 }
107 5 );
108 }
109  
110 3 return join(', ', @fields), @bind;
111 }
112  
113 32 ▶sub _table {
Subroutine _table: called CC=15 SCAR=27.1
114 32 my ($self, $table) = @_;
115  
116 32 ▶ return $self->SUPER::_table($table) unless ref $table eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table eq "ARRAY" 17 15 32
117  
118 15 my (@tables, @joins);
119 15 for my $jt (@$table) {
120 33 ▶ if (ref $jt eq 'ARRAY') { push @joins, $jt }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $jt eq 'ARRAY') { } 14 19 33
121 19 else { push @tables, $jt }
122 }
123  
124 15 my $sql = $self->SUPER::_table(\@tables);
125 15 ▶ my $sep = $self->{name_sep} // '';
Condition50%1/2
$self->{"name_sep"} // ""
l!l
150
MC/DC0%0/1
$self->{"name_sep"} // ""
$self->{"name_sep"}
Truth table50%1/2
A
A= $self->{"name_sep"}
Aresult
11
00
126 15 for my $join (@joins) {
127  
128 14 my $type = '';
129 14 ▶ if ($join->[0] =~ /^-(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join->[0] =~ /^-(.+)/u) 6 8 14
130 6 $type = " $1";
131 6 shift @$join;
132 }
133  
134 14 my $name = shift @$join;
135 14 $sql .= $self->_sqlcase("$type join ") . $self->_quote($name);
136  
137 # NATURAL JOIN
138 14 ▶ if ($type eq ' natural') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq ' natural') { } 2 12 14
elsif (@$join == 1) { } 1 11 12
139 2 ▶ puke 'natural join must be in the form [-natural => $table]' if @$join;
Branch: 1 branch
BranchTrueFalseTotal
if @$join 1 1 2
140 }
141  
142 # JOIN USING
143 elsif (@$join == 1) {
144 1 $sql .= $self->_sqlcase(' using (') . $self->_quote($join->[0]) . ')';
145 }
146  
147 # others
148 else {
149 11 ▶ puke 'join must be in the form [$table, $fk => $pk]' if @$join < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$join < 2 1 10 11
150 10 ▶ puke 'join requires an even number of keys' if @$join % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$join % 2 1 9 10
151  
152 9 my @keys;
153 9 ▶ while (my ($fk, $pk) = splice @$join, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$join, 0, 2)) 12 9 21
154 12 ▶ push @keys,
Branch: 2 branches
BranchTrueFalseTotal
index($fk, $sep) > 0 ? : 3 9 12
index($pk, $sep) > 0 ? : 3 9 12
155 $self->_quote(index($fk, $sep) > 0 ? $fk : "$name.$fk") . ' = '
156 . $self->_quote(index($pk, $sep) > 0 ? $pk : "$tables[0].$pk");
157 }
158  
159 9 $sql .= $self->_sqlcase(' on ') . '(' . join($self->_sqlcase(' and '), @keys) . ')';
160 }
161  
162 }
163  
164 12 return $sql;
165 }
166  
167 18 1;
168  
169 =encoding utf8
170  
171 =head1 NAME
172  
173 SQL::Abstract::mysql - Generate SQL from Perl data structures for MySQL and MariaDB
174  
175 =head1 SYNOPSIS
176  
177 use SQL::Abstract::mysql;
178  
179 my $abstract = SQL::Abstract::mysql->new(quote_char => chr(96), name_sep => '.');
180 # The same as
181 use Mojo::mysql;
182 my $mysql = Mojo::mysql->new;
183 my $abstract = $mysql->abstract;
184  
185 say $abstract->insert('some_table', \%some_values, \%some_options);
186 say $abstract->select('some_table');
187  
188 =head1 DESCRIPTION
189  
190 L<SQL::Abstract::mysql> extends L<SQL::Abstract> with a few MySQL / MariaDB
191 features used by L<Mojo::mysql>. It was inspired by L<SQL::Abstract::Pg>.
192  
193 =head1 FUNCTIONS
194  
195 =head2 puke
196  
197 See L<SQL::Abstract/puke>.
198  
199 =head1 METHODS
200  
201 L<SQL::Abstract::mysql> inherits all methods from L<SQL::Abstract>.
202  
203 =head2 insert
204  
205 my ($stmt, @bind) = $abstract->insert($table, \@values || \%fieldvals, \%options);
206  
207 This method extends L<SQL::Abstract/insert> with the following functionality:
208  
209 =head3 ON CONFLICT
210  
211 The C<on_conflict> option can be used to generate C<INSERT IGNORE>, C<REPLACE> and
212 C<INSERT ... ON DUPLICATE KEY UPDATE> queries.
213 So far C<'ignore'> to pass C<INSERT IGNORE>, C<'replace'> to pass C<REPLACE> and
214 hash references to pass C<UPDATE> with conflict targets are supported.
215  
216 # "insert ignore into t (id, a) values (123, 'b')"
217 $abstract->insert('t', {id => 123, a => 'b'}, {on_conflict => 'ignore'});
218  
219 # "replace into t (id, a) values (123, 'b')"
220 $abstract->insert('t', {id => 123, a => 'b'}, {on_conflict => 'replace'});
221  
222 # "insert into t (id, a) values (123, 'b') on duplicate key update c='d'"
223 $abstract->insert('t', {id => 123, a => 'b'}, {on_conflict => {c => 'd'}});
224  
225 =head2 select
226  
227 my ($stmt, @bind) = $abstract->select($source, $fields, $where, $order);
228 my ($stmt, @bind) = $abstract->select($source, $fields, $where, \%options);
229  
230 This method extends L<SQL::Abstract/select> with the following functionality:
231  
232 =head3 AS
233  
234 The C<$fields> argument accepts array references containing array references
235 with field names and aliases, as well as array references containing scalar
236 references to pass literal SQL and array reference references to pass literal
237 SQL with bind values.
238  
239 # "select foo as bar from some_table"
240 $abstract->select('some_table', [[foo => 'bar']]);
241  
242 # "select foo, bar as baz, yada from some_table"
243 $abstract->select('some_table', ['foo', [bar => 'baz'], 'yada']);
244  
245 # "select extract(epoch from foo) as foo, bar from some_table"
246 $abstract->select('some_table', [\'extract(epoch from foo) as foo', 'bar']);
247  
248 # "select 'test' as foo, bar from some_table"
249 $abstract->select('some_table', [\['? as foo', 'test'], 'bar']);
250  
251 =head3 JOIN
252  
253 The C<$source> argument accepts array references containing not only table
254 names, but also array references with tables to generate C<JOIN> clauses for.
255  
256 # "select * from foo join bar on (bar.foo_id = foo.id)"
257 $abstract->select(['foo', ['bar', foo_id => 'id']]);
258  
259 # "select * from foo join bar on (foo.id = bar.foo_id)"
260 $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]);
261  
262 # -left, -right, -inner
263 # "select * from foo left join bar on (bar.foo_id = foo.id)"
264 $abstract->select(['foo', [-left => 'bar', foo_id => 'id']]);
265  
266 # -natural
267 # "select * from foo natural join bar"
268 $abstract->select(['foo', [-natural => 'bar']]);
269  
270 # join using
271 # "select * from foo join bar using (foo_id)"
272 $abstract->select(['foo', [bar => 'foo_id']]);
273  
274 # more than one table
275 # "select * from foo join bar on (bar.foo_id = foo.id) join baz on (baz.foo_id = foo.id)"
276 $abstract->select(['foo', ['bar', foo_id => 'id'], ['baz', foo_id => 'id']]);
277  
278 # more than one field
279 # "select * from foo left join bar on (bar.foo_id = foo.id and bar.foo_id2 = foo.id2)"
280 $abstract->select(['foo', [-left => 'bar', foo_id => 'id', foo_id2 => 'id2']]);
281  
282 =head2 where
283  
284 my ($stmt, @bind) = $abstract->where($where, \%options);
285  
286 This method extends L<SQL::Abstract/where> with the following functionality:
287  
288 =head3 FOR
289  
290 The C<for> option can be used to generate C<SELECT> queries with C<FOR UPDATE>
291 or C<LOCK IN SHARE MODE> clauses. So far the scalar values C<update> and
292 C<share> and scalar references to pass literal SQL are supported.
293  
294 # "select * from some_table for update"
295 $abstract->select('some_table', '*', undef, {for => 'update'});
296  
297 # "select * from some_table lock in share mode"
298 $abstract->select('some_table', '*', undef, {for => 'share'});
299  
300 # "select * from some_table for share"
301 $abstract->select('some_table', '*', undef, {for => \'share'});
302  
303 # "select * from some_table for update skip locked"
304 $abstract->select('some_table', '*', undef, {for => \'update skip locked'});
305  
306 =head3 GROUP BY
307  
308 The C<group_by> option can be used to generate C<SELECT> queries with C<GROUP
309 BY> clauses. So far array references to pass a list of fields and scalar
310 references to pass literal SQL are supported.
311  
312 # "select * from some_table group by foo, bar"
313 $abstract->select('some_table', '*', undef, {group_by => ['foo', 'bar']});
314  
315 # "select * from some_table group by foo, bar"
316 $abstract->select('some_table', '*', undef, {group_by => \'foo, bar'});
317  
318 =head3 HAVING
319  
320 The C<having> option can be used to generate C<SELECT> queries with C<HAVING>
321 clauses, which takes the same values as the C<$where> argument.
322  
323 # "select * from t group by a having b = 'c'"
324 $abstract->select('t', '*', undef, {group_by => ['a'], having => {b => 'c'}});
325  
326 =head3 ORDER BY
327  
328 In addition to the C<$order> argument accepted by L<SQL::Abstract> you can pass
329 a hash reference with various options. This includes C<order_by>, which takes
330 the same values as the C<$order> argument.
331  
332 # "select * from some_table order by foo desc"
333 $abstract->select('some_table', '*', undef, {order_by => {-desc => 'foo'}});
334  
335 =head3 LIMIT / OFFSET
336  
337 The C<limit> and C<offset> options can be used to generate C<SELECT> queries
338 with C<LIMIT> and C<OFFSET> clauses.
339  
340 # "select * from some_table limit 10"
341 $abstract->select('some_table', '*', undef, {limit => 10});
342  
343 # "select * from some_table offset 5"
344 $abstract->select('some_table', '*', undef, {offset => 5});
345  
346 # "select * from some_table limit 10 offset 5"
347 $abstract->select('some_table', '*', undef, {limit => 10, offset => 5});
348  
349 =head1 SEE ALSO
350  
351 L<Mojo::mysql>, L<SQL::Abstract::Pg>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
352  
353 =cut
« Mojo/mysql.pm ↑ Summary Mojo/mysql/Transaction.pm »