MySQL/SQLMaker.pm

filter: Statementstmt 81.1% 73 / 90 Branchbran 65.0% 26 / 40 Conditioncond 27.7% 5 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 83.3% 10 / 12 Podpod 100% 4 / 4 totaltotal 68.0% 119 / 175 CC 27 SCAR 19.9
CC
27
Cov
70%
insert
20.0
_lock_select
9.5
_extract_target_name
6.9
CRAP 7.3
« MySQL/Diff/Column.pm ↑ Summary MySQL/Introspect.pm »
1 package DBIO::MySQL::SQLMaker;
2 # ABSTRACT: MySQL-specific SQL generation for DBIO
3  
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base qw( DBIO::SQLMaker );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Text::Balanced ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 0 ▶sub apply_limit {
Subroutine apply_limit: not called CC=2 SCAR=17.9
Pod documented
14 0 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
15 0 $sql .= $self->_parse_rs_attrs( $rs_attrs ) . " LIMIT ";
16 0 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 0 0 0
17 0 $sql .= '?, ';
18 0 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
19 }
20 0 $sql .= '?';
21 0 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
22  
23 0 return $sql;
24 }
25  
26 #
27 # MySQL does not understand the standard INSERT INTO $table DEFAULT VALUES
28 # Adjust SQL here instead
29 #
30 0 ▶sub insert {
Subroutine insert: not called CC=4 SCAR=30.0
Pod documented
31 0 my $self = shift;
32  
33 0 ▶ if (! $_[1] or (ref $_[1] eq 'HASH' and !keys %{$_[1]} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[1] or ref $_[1] eq "HASH" and not keys %{$_[1];}) 0 0 0
Condition0%0/6
not $_[1] or ref $_[1] eq "HASH" and not keys %{$_[1];}
l!l&&r!l&&!r
000
ref $_[1] eq "HASH" and not keys %{$_[1];}
!ll&&!rl&&r
000
MC/DC0%0/3
not $_[1] or ref $_[1] eq "HASH" and not keys %{$_[1];}
not $_[1] ref $_[1] eq "HASH" not keys %{$_[1];}
Truth table0%0/4
not(A) or B and not(C)
A= not $_[1]B= ref $_[1] eq "HASH"C= not keys %{$_[1];}
ABCresult
1XX1
0111
00X0
0100
34 0 my $table = $self->_quote($_[0]);
35 0 return "INSERT INTO ${table} () VALUES ()"
36 }
37  
38 0 return $self->next::method (@_);
39 }
40  
41  
42 # Allow STRAIGHT_JOIN's
43 9 ▶sub _generate_join_clause {
Subroutine _generate_join_clause: called CC=3 SCAR=11.0
44 9 my ($self, $join_type) = @_;
45  
46 9 ▶ if( $join_type && $join_type =~ /^STRAIGHT\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join_type and $join_type =~ /^STRAIGHT\z/i) 2 7 9
Condition66%2/3
$join_type and $join_type =~ /^STRAIGHT\z/i
!ll&&!rl&&r
702
MC/DC50%1/2
$join_type and $join_type =~ /^STRAIGHT\z/i
$join_type $join_type =~ /^STRAIGHT\z/i
Truth table66%2/3
A and B
A= $join_typeB= $join_type =~ /^STRAIGHT\z/i
ABresult
0X0
100
111
47 2 return ' STRAIGHT_JOIN '
48 }
49  
50 7 return $self->next::method($join_type);
51 }
52  
53 5 ▶sub _wrap_self_referencing_subquery {
Subroutine _wrap_self_referencing_subquery: called CC=6 SCAR=18.0
54 5 my ($self, $sql, $target_name) = @_;
55  
56 5 ▶ return $sql unless $target_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_name 0 5 5
57  
58 5 my $re = qr/ (?<!DELETE) [\s\)] (?: FROM | JOIN ) \s (?: \` \Q$target_name\E \` | \Q$target_name\E ) [\s\(] /xi;
59  
60 5 ▶ return $sql unless $sql =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /$re/ 0 5 5
61  
62 5 my $new_sql;
63 5 while (1) {
64  
65 11 my ($prefix, $parenthesized);
66  
67 11 ($parenthesized, $sql, $prefix) = do {
68 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
69 11 local $@;
70 11 Text::Balanced::extract_bracketed( $sql, '()', qr/[^\(]*/ );
71 };
72  
73 # this is how an error is indicated, in addition to crapping in $@
74 11 ▶ last unless $parenthesized;
Branch: 1 branch
BranchTrueFalseTotal
unless $parenthesized 5 6 11
75  
76 6 ▶ if ($parenthesized =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parenthesized =~ /$re/) 6 0 6
77 # is this a select subquery?
78 6 ▶ if ( $parenthesized =~ /^ \( \s* SELECT \s+ /xi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parenthesized =~ /^ \( \s* SELECT \s+ /xi) { } 5 1 6
79 5 $parenthesized = "( SELECT * FROM $parenthesized `_forced_double_subquery` )";
80 }
81 # then drill down until we find it (if at all)
82 else {
83 1 $parenthesized =~ s/^ \( (.+) \) $/$1/x;
84 1 $parenthesized = join ' ', '(', $self->_wrap_self_referencing_subquery( $parenthesized, $target_name ), ')';
85 }
86 }
87  
88 6 $new_sql .= $prefix . $parenthesized;
89 }
90  
91 5 return $new_sql . $sql;
92 }
93  
94 2 ▶sub update {
Subroutine update: called CC=2 SCAR=7.0
Pod documented
95 2 my $self = shift;
96 2 my ($target, $source, $attributes) = @_;
97  
98 2 my ($sql, @bind) = $self->next::method(@_);
99  
100 # Extract target table name for self-referencing detection
101 2 my $target_name = $self->_extract_target_name($target);
102  
103 2 ▶ $sql = $self->_wrap_self_referencing_subquery($sql, $target_name) if $target_name;
Branch: 1 branch
BranchTrueFalseTotal
if $target_name 2 0 2
104  
105 2 return ($sql, @bind);
106 }
107  
108 2 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
109 2 my $self = shift;
110 2 my ($target, $source, $attributes) = @_;
111  
112 2 my ($sql, @bind) = $self->next::method(@_);
113  
114 # Extract target table name for self-referencing detection
115 2 my $target_name = $self->_extract_target_name($target);
116  
117 2 ▶ $sql = $self->_wrap_self_referencing_subquery($sql, $target_name) if $target_name;
Branch: 1 branch
BranchTrueFalseTotal
if $target_name 2 0 2
118  
119 2 return ($sql, @bind);
120 }
121  
122 4 ▶sub _extract_target_name {
Subroutine _extract_target_name: called CC=6 SCAR=19.3
123 4 my ($self, $target) = @_;
124  
125 4 ▶ return unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 4 4
126  
127 4 ▶ if (ref $target eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target eq "SCALAR") 2 2 4
128 2 ▶ if ($$target =~ /^ (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($$target =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x) 2 0 2
129 \` ( [^`]+ ) \` #`
130 | ( [\w\-]+ )
131 ) $/x
132 ) {
133 2 ▶ return (defined $1) ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 2 2
134 }
135 0 return; # complex scalar ref, can't deal
136 }
137  
138 # For HASH refs (simple result source) or plain strings, return as-is
139 2 ▶ return ref $target ? undef : $target;
Branch: 1 branch
BranchTrueFalseTotal
ref $target ? : 0 2 2
140 }
141  
142  
143 #
144 # Support for MySQL lock clause syntax according to specification
145 # including updates introduced in MySQL 8.0.1)
146 # FOR UPDATE | FOR SHARE [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED]
147 #
148  
149 3 my $lock_types = {
150 update => 'FOR UPDATE',
151 share => 'FOR SHARE',
152 };
153  
154 3 my $lock_modifiers = {
155 nowait => 'NOWAIT',
156 skip_locked => 'SKIP LOCKED'
157 };
158  
159 12 ▶sub _lock_select {
Subroutine _lock_select: called CC=9 SCAR=22.6
160 12 my ($self, $type) = @_;
161  
162 # Handle hash-based configuration to support new featureset
163 12 ▶ if (ref $type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq "HASH") 10 2 12
164 10 my $lock_type = $type->{type};
165 10 my $tables = $type->{of};
166 10 my $modifier = $type->{modifier};
167  
168 10 ▶ my $lock_clause = $lock_types->{$lock_type}
Condition33%1/3
$lock_types->{$lock_type} || $self->throw_exception("Unknown SELECT .. FOR type '${lock_type}' requested")
l!l&&r!l&&!r
1000
MC/DC0%0/2
$lock_types->{$lock_type} || $self->throw_exception("Unknown SELECT .. FOR type '${lock_type}' requested")
$lock_types->{$lock_type} $self->throw_exception("Unknown SELECT .. FOR type '${lock_type}' requested")
Truth table33%1/3
A || B
A= $lock_types->{$lock_type}B= $self->throw_exception("Unknown SELECT .. FOR type '${lock_type}' requested")
ABresult
1X1
011
000
169 || $self->throw_exception("Unknown SELECT .. FOR type '$lock_type' requested");
170  
171 # Add OF clause if tables are specified
172 10 ▶ if ($tables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tables) 4 6 10
173 4 ▶ my @table_list = ref $tables eq 'ARRAY' ? @$tables : ($tables);
Branch: 1 branch
BranchTrueFalseTotal
ref $tables eq 'ARRAY' ? : 2 2 4
174 4 ▶ if (@table_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@table_list) 4 0 4
175 my $quoted_tables = join(', ',
176 4 map { $self->_quote($_) } @table_list
177 );
178 4 $lock_clause .= " OF $quoted_tables";
179 }
180 }
181  
182 # Add modifier if specified
183 10 ▶ if ($modifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifier) 6 4 10
184 6 ▶ my $mod_sql = $lock_modifiers->{$modifier}
Condition33%1/3
$lock_modifiers->{$modifier} || $self->throw_exception("Unknown lock modifier '${modifier}' requested")
l!l&&r!l&&!r
600
MC/DC0%0/2
$lock_modifiers->{$modifier} || $self->throw_exception("Unknown lock modifier '${modifier}' requested")
$lock_modifiers->{$modifier} $self->throw_exception("Unknown lock modifier '${modifier}' requested")
Truth table33%1/3
A || B
A= $lock_modifiers->{$modifier}B= $self->throw_exception("Unknown lock modifier '${modifier}' requested")
ABresult
1X1
011
000
185 || $self->throw_exception("Unknown lock modifier '$modifier' requested");
186 6 $lock_clause .= " $mod_sql";
187 }
188  
189 10 return " $lock_clause";
190 }
191  
192 # Handle simple string types (for backward compatibility)
193 2 ▶ my $sql = $lock_types->{$type}
Condition33%1/3
$lock_types->{$type} || $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
l!l&&r!l&&!r
200
MC/DC0%0/2
$lock_types->{$type} || $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
$lock_types->{$type} $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
Truth table33%1/3
A || B
A= $lock_types->{$type}B= $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
ABresult
1X1
011
000
194 || $self->throw_exception("Unknown SELECT .. FOR type '$type' requested");
195  
196 2 return " $sql";
197 }
198  
199  
200 3 1;
201  
202 __END__
203  
204 =pod
205  
206 =encoding UTF-8
207  
208 =head1 NAME
209  
210 DBIO::MySQL::SQLMaker - MySQL-specific SQL generation for DBIO
211  
212 =head1 VERSION
213  
214 version 0.900001
215  
216 =head1 SYNOPSIS
217  
218 # Used automatically by DBIO::MySQL::Storage — no manual setup needed.
219  
220 =head1 DESCRIPTION
221  
222 MySQL-specific SQL generation layer for L<DBIO>. Extends L<DBIO::SQLMaker>
223 with the following MySQL adaptations:
224  
225 =over 4
226  
227 =item *
228  
229 C<INSERT> with no columns emits C<INSERT INTO t () VALUES ()> instead of
230 the standard C<INSERT INTO t DEFAULT VALUES>, which MySQL does not support.
231  
232 =item *
233  
234 Supports C<STRAIGHT_JOIN> as a join type hint via the C<join_type> result
235 source attribute.
236  
237 =item *
238  
239 C<UPDATE> and C<DELETE> statements that reference the modification target
240 table in a subquery are automatically wrapped in a double subquery to work
241 around MySQL's restriction against self-referencing in DML.
242  
243 =item *
244  
245 Implements MySQL's C<SELECT ... FOR UPDATE / FOR SHARE> locking syntax,
246 including the C<OF tbl_name>, C<NOWAIT>, and C<SKIP LOCKED> clauses
247 introduced in MySQL 8.0.1.
248  
249 =back
250  
251 =head1 METHODS
252  
253 =head2 apply_limit
254  
255 Uses MySQL's C<LIMIT [offset,] rows> syntax instead of the standard
256 C<LIMIT rows OFFSET offset>.
257  
258 =head2 insert
259  
260 Overrides the standard C<INSERT> to emit C<INSERT INTO t () VALUES ()> for
261 rows with no column values, satisfying MySQL's syntax requirements.
262  
263 =head2 update
264  
265 =head2 delete
266  
267 Overrides the base C<update> and C<delete> methods. When the modification
268 target table is referenced in a subquery within the same statement (a pattern
269 MySQL rejects), the affected subquery is automatically re-wrapped in an
270 additional C<SELECT * FROM (...) `_forced_double_subquery`> layer to satisfy
271 MySQL's parser.
272  
273 =head2 _lock_select
274  
275 Generates the locking clause appended to C<SELECT> statements. Accepts
276 either a plain string (C<'update'>, C<'share'>) or a hashref
277 for fine-grained control:
278  
279 $rs->search({}, { for => { type => 'update', of => ['tbl'], modifier => 'nowait' } });
280  
281 Valid C<type> values: C<update>, C<share>.
282 Valid C<modifier> values: C<nowait>, C<skip_locked>.
283 The C<of> key takes a table name or arrayref of table names.
284  
285 =seealso
286  
287 =over 4
288  
289 =item * L<DBIO::MySQL::Storage> - Storage class that uses this SQL maker
290  
291 =item * L<DBIO::MySQL> - Schema component entry point
292  
293 =back
294  
295 =head1 AUTHOR
296  
297 DBIO & DBIx::Class Authors
298  
299 =head1 COPYRIGHT AND LICENSE
300  
301 Copyright (C) 2026 DBIO Authors
302 Portions Copyright (C) 2005-2025 DBIx::Class Authors
303 Based on DBIx::Class, heavily modified.
304  
305 This is free software; you can redistribute it and/or modify it under
306 the same terms as the Perl 5 programming language system itself.
307  
308 =cut
« MySQL/Diff/Column.pm ↑ Summary MySQL/Introspect.pm »