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 86.6% 13 / 15 Podpod 100% 1 / 1 totaltotal 94.6% 158 / 167 CC 35 SCAR 21.1
CC
35
Cov
96%
_table
15.0
_order_by
9.0
insert
8.0
CRAP 8.2
« 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 sub insert {
7 6 ▶ my ($self, $options) = (shift, $_[2] || {}); # ($self, $table, $data, $options)
Subroutine insert: called CC=8 SCAR=20.8
Pod documented
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 sub _mysql_for {
33 6 ▶ my ($self, $param) = @_;
Subroutine _mysql_for: called CC=1 SCAR=0.0
34  
35 return $self->_SWITCH_refkind(
36 $param => {
37 SCALAR => sub {
38 4 ▶ return $self->_sqlcase('lock in share mode') if $param eq 'share';
Subroutine __ANON__: called CC=3 SCAR=11.0
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 sub _mysql_group_by {
48 6 ▶ my ($self, $param) = @_;
Subroutine _mysql_group_by: called CC=1 SCAR=0.0
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
52 }
53  
54 sub _order_by {
55 16 ▶ my ($self, $options) = @_;
Subroutine _order_by: called CC=9 SCAR=22.0
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 sub _select_fields {
88 29 ▶ my ($self, $fields) = @_;
Subroutine _select_fields: called CC=3 SCAR=11.4
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 ARRAYREF => sub {
97 2 ▶ puke 'field alias must be in the form [$name => $alias]' if @$field < 2;
Subroutine __ANON__: called CC=2 SCAR=7.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 ARRAYREFREF => sub {
101 0 ▶ push @fields, shift @$$field;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 sub _table {
114 32 ▶ my ($self, $table) = @_;
Subroutine _table: called CC=15 SCAR=27.1
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 METHODS
194  
195 L<SQL::Abstract::mysql> inherits all methods from L<SQL::Abstract>.
196  
197 =head2 insert
198  
199 my ($stmt, @bind) = $abstract->insert($table, \@values || \%fieldvals, \%options);
200  
201 This method extends L<SQL::Abstract/insert> with the following functionality:
202  
203 =head3 ON CONFLICT
204  
205 The C<on_conflict> option can be used to generate C<INSERT IGNORE>, C<REPLACE> and
206 C<INSERT ... ON DUPLICATE KEY UPDATE> queries.
207 So far C<'ignore'> to pass C<INSERT IGNORE>, C<'replace'> to pass C<REPLACE> and
208 hash references to pass C<UPDATE> with conflict targets are supported.
209  
210 # "insert ignore into t (id, a) values (123, 'b')"
211 $abstract->insert('t', {id => 123, a => 'b'}, {on_conflict => 'ignore'});
212  
213 # "replace into t (id, a) values (123, 'b')"
214 $abstract->insert('t', {id => 123, a => 'b'}, {on_conflict => 'replace'});
215  
216 # "insert into t (id, a) values (123, 'b') on duplicate key update c='d'"
217 $abstract->insert('t', {id => 123, a => 'b'}, {on_conflict => {c => 'd'}});
218  
219 =head2 select
220  
221 my ($stmt, @bind) = $abstract->select($source, $fields, $where, $order);
222 my ($stmt, @bind) = $abstract->select($source, $fields, $where, \%options);
223  
224 This method extends L<SQL::Abstract/select> with the following functionality:
225  
226 =head3 AS
227  
228 The C<$fields> argument accepts array references containing array references
229 with field names and aliases, as well as array references containing scalar
230 references to pass literal SQL and array reference references to pass literal
231 SQL with bind values.
232  
233 # "select foo as bar from some_table"
234 $abstract->select('some_table', [[foo => 'bar']]);
235  
236 # "select foo, bar as baz, yada from some_table"
237 $abstract->select('some_table', ['foo', [bar => 'baz'], 'yada']);
238  
239 # "select extract(epoch from foo) as foo, bar from some_table"
240 $abstract->select('some_table', [\'extract(epoch from foo) as foo', 'bar']);
241  
242 # "select 'test' as foo, bar from some_table"
243 $abstract->select('some_table', [\['? as foo', 'test'], 'bar']);
244  
245 =head3 JOIN
246  
247 The C<$source> argument accepts array references containing not only table
248 names, but also array references with tables to generate C<JOIN> clauses for.
249  
250 # "select * from foo join bar on (bar.foo_id = foo.id)"
251 $abstract->select(['foo', ['bar', foo_id => 'id']]);
252  
253 # "select * from foo join bar on (foo.id = bar.foo_id)"
254 $abstract->select(['foo', ['bar', 'foo.id' => 'bar.foo_id']]);
255  
256 # -left, -right, -inner
257 # "select * from foo left join bar on (bar.foo_id = foo.id)"
258 $abstract->select(['foo', [-left => 'bar', foo_id => 'id']]);
259  
260 # -natural
261 # "select * from foo natural join bar"
262 $abstract->select(['foo', [-natural => 'bar']]);
263  
264 # join using
265 # "select * from foo join bar using (foo_id)"
266 $abstract->select(['foo', [bar => 'foo_id']]);
267  
268 # more than one table
269 # "select * from foo join bar on (bar.foo_id = foo.id) join baz on (baz.foo_id = foo.id)"
270 $abstract->select(['foo', ['bar', foo_id => 'id'], ['baz', foo_id => 'id']]);
271  
272 # more than one field
273 # "select * from foo left join bar on (bar.foo_id = foo.id and bar.foo_id2 = foo.id2)"
274 $abstract->select(['foo', [-left => 'bar', foo_id => 'id', foo_id2 => 'id2']]);
275  
276 =head2 where
277  
278 my ($stmt, @bind) = $abstract->where($where, \%options);
279  
280 This method extends L<SQL::Abstract/where> with the following functionality:
281  
282 =head3 FOR
283  
284 The C<for> option can be used to generate C<SELECT> queries with C<FOR UPDATE>
285 or C<LOCK IN SHARE MODE> clauses. So far the scalar values C<update> and
286 C<share> and scalar references to pass literal SQL are supported.
287  
288 # "select * from some_table for update"
289 $abstract->select('some_table', '*', undef, {for => 'update'});
290  
291 # "select * from some_table lock in share mode"
292 $abstract->select('some_table', '*', undef, {for => 'share'});
293  
294 # "select * from some_table for share"
295 $abstract->select('some_table', '*', undef, {for => \'share'});
296  
297 # "select * from some_table for update skip locked"
298 $abstract->select('some_table', '*', undef, {for => \'update skip locked'});
299  
300 =head3 GROUP BY
301  
302 The C<group_by> option can be used to generate C<SELECT> queries with C<GROUP
303 BY> clauses. So far array references to pass a list of fields and scalar
304 references to pass literal SQL are supported.
305  
306 # "select * from some_table group by foo, bar"
307 $abstract->select('some_table', '*', undef, {group_by => ['foo', 'bar']});
308  
309 # "select * from some_table group by foo, bar"
310 $abstract->select('some_table', '*', undef, {group_by => \'foo, bar'});
311  
312 =head3 HAVING
313  
314 The C<having> option can be used to generate C<SELECT> queries with C<HAVING>
315 clauses, which takes the same values as the C<$where> argument.
316  
317 # "select * from t group by a having b = 'c'"
318 $abstract->select('t', '*', undef, {group_by => ['a'], having => {b => 'c'}});
319  
320 =head3 ORDER BY
321  
322 In addition to the C<$order> argument accepted by L<SQL::Abstract> you can pass
323 a hash reference with various options. This includes C<order_by>, which takes
324 the same values as the C<$order> argument.
325  
326 # "select * from some_table order by foo desc"
327 $abstract->select('some_table', '*', undef, {order_by => {-desc => 'foo'}});
328  
329 =head3 LIMIT / OFFSET
330  
331 The C<limit> and C<offset> options can be used to generate C<SELECT> queries
332 with C<LIMIT> and C<OFFSET> clauses.
333  
334 # "select * from some_table limit 10"
335 $abstract->select('some_table', '*', undef, {limit => 10});
336  
337 # "select * from some_table offset 5"
338 $abstract->select('some_table', '*', undef, {offset => 5});
339  
340 # "select * from some_table limit 10 offset 5"
341 $abstract->select('some_table', '*', undef, {limit => 10, offset => 5});
342  
343 =head1 SEE ALSO
344  
345 L<Mojo::mysql>, L<SQL::Abstract::Pg>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
346  
347 =cut
« Mojo/mysql.pm ↑ Summary Mojo/mysql/Transaction.pm »