blib/lib/DBIx/Class/Helper/ResultSet/MySQLHacks.pm

filter: Statementstmt 91.2% 52 / 57 Branchbran 50.0% 3 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 83.3% 5 / 6 Podpod 100% 3 / 3 totaltotal 85.3% 64 / 75 CC 5 SCAR 10.3
CC
5
Cov
86%
dbh_execute
6.0
multi_table_update
3.0
multi_table_delete
2.0
CRAP 2.8
↑ Summary
1 package DBIx::Class::Helper::ResultSet::MySQLHacks;
2  
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use base 'DBIx::Class::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Useful MySQL-specific operations for DBIx::Class
8 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our $VERSION = 'v1.0.0'; # VERSION
10  
11 #pod =head1 SYNOPSIS
12 #pod
13 #pod # Your base resultset
14 #pod package MySchema::ResultSet;
15 #pod
16 #pod use strict;
17 #pod use warnings;
18 #pod
19 #pod use parent 'DBIx::Class::ResultSet';
20 #pod
21 #pod __PACKAGE__->load_components('Helper::ResultSet::MySQLHacks');
22 #pod
23 #pod # In other resultset classes
24 #pod package MySchema::ResultSet::Bar;
25 #pod
26 #pod use strict;
27 #pod use warnings;
28 #pod
29 #pod use parent 'MySchema::ResultSet';
30 #pod
31 #pod # In code using the resultset
32 #pod $rs->multi_table_delete(qw< rel1 rel2 >);
33 #pod $rs->multi_table_update(\%values);
34 #pod
35 #pod =head1 DESCRIPTION
36 #pod
37 #pod This MySQL-specific ResultSet helper contains a series of hacks for various SQL
38 #pod operations that only work for MySQL. These hacks are exactly that, so it's possible that
39 #pod the SQL manipulation isn't as clean as it should be.
40 #pod
41 #pod =head1 METHODS
42 #pod
43 #pod =head2 multi_table_delete
44 #pod
45 #pod my $underlying_storage_rv = $rs->multi_table_delete; # deletes rows from the current table
46 #pod my $underlying_storage_rv = $rs->multi_table_delete(qw< rel1 rel2 >);
47 #pod
48 #pod Runs a delete using the multiple table syntax, which supports join operations. This is
49 #pod useful in cases with a joined ResultSet that require rows to be deleted, and using
50 #pod L<DBIx::Class::ResultSet/delete_all> would be too slow.
51 #pod
52 #pod Without arguments, it will delete rows from the current table, ie: L<DBIx::Class::ResultSet/current_source_alias>.
53 #pod Otherwise, it can take a list of B<relationships> to delete from. These must be existing
54 #pod relationship aliases tied to the joins, not table names.
55 #pod
56 #pod This method works by taking a count ResultSet, removing the C<< SELECT COUNT(*) >>
57 #pod portion, and splicing in the C<< DELETE @aliases >> part.
58 #pod
59 #pod The return value is a pass through of what the underlying storage backend returned, and
60 #pod may vary. See L<DBI/execute> for the most common case.
61 #pod
62 #pod B<NOTE:> This method will not delete from views, per MySQL limitations.
63 #pod
64 #pod =cut
65  
66 8 ▶sub multi_table_delete {
Subroutine multi_table_delete: called CC=2 SCAR=6.9
Pod documented
67 8 my ($self, @rel_aliases) = @_;
68 8 ▶ @rel_aliases = ( $self->current_source_alias ) unless @rel_aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless @rel_aliases 4 4 8
69  
70 8 my $sql_maker = $self->result_source->storage->sql_maker;
71  
72 my $alias_str = join ', ', map {
73 8 $sql_maker->_from_chunk_to_sql($_)
74 } @rel_aliases;
75  
76 8 my ($sql, $bind);
77 8 ($sql, @$bind) = @${ $self->count_rs->as_query };
78  
79 # Remove (useless) outside parentheses
80 8 $sql =~ s/^\(\s*(.+)\s*\)$/$1/s;
81  
82 # Convert "SELECT COUNT(*) FROM" to "DELETE @aliases"
83 8 $sql =~ s/^SELECT COUNT[()*\s]+(?= FROM)/DELETE $alias_str/;
84  
85 8 my $rv = $self->dbh_execute($sql, $bind);
86 8 return $rv;
87 }
88  
89 #pod =head2 multi_table_update
90 #pod
91 #pod my $underlying_storage_rv = $rs->multi_table_update(\%values);
92 #pod
93 #pod Runs a update using the multiple table syntax, which supports join operations. This is
94 #pod useful in cases with a joined ResultSet that require rows to be updated, and using
95 #pod L<DBIx::Class::ResultSet/update_all> would be too slow.
96 #pod
97 #pod A values hashref is required. It's highly recommended that the keys are named as
98 #pod C<alias.column> pairs, since multiple tables are involved.
99 #pod
100 #pod This method works by acquiring the C<FROM>, C<SET>, and C<WHERE> clauses separately and
101 #pod merging them back into a proper multi-table C<UPDATE> query.
102 #pod
103 #pod The return value is a pass through of what the underlying storage backend returned, and
104 #pod may vary. See L<DBI/execute> for the most common case.
105 #pod
106 #pod =cut
107  
108 6 ▶sub multi_table_update {
Subroutine multi_table_update: called CC=3 SCAR=11.0
Pod documented
109 6 my ($self, $values) = @_;
110  
111 6 ▶ $self->throw_exception('Values for multi_table_update must be a hash')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "HASH" 0 6 6
112 unless ref $values eq 'HASH';
113  
114 6 my $rsrc = $self->result_source;
115 6 my $storage = $rsrc->storage;
116 6 my $sql_maker = $storage->sql_maker;
117  
118 ### NOTE: Much of this is based on deep-analysis of DBIx::Class::Storage::DBI, especially
119 ### $result->update / $result->single and how that eventually ends up to their respective
120 ### $storage->_execute calls.
121  
122 ### XXX: This FROM/WHERE piece might be replaced with a less private-heavy count_rs->query
123 ### hack, similar to multi_table_delete. However, the SET is going to be going
124 ### *in-between* the FROM/WHERE piece, so binds and SQL insertion might make things more
125 ### difficult. If this code breaks hard, we might have to revert to that model.
126  
127 # Collect attrs for various calls
128 6 my $resolved_attrs = { %{$self->_resolved_attrs} };
129  
130 # Use the resolved SELECT args here, since prune_unused_joins may be turned on, and
131 # we don't want that overtrimming the FROM/WHERE lists.
132 6 ▶ my $select_args = delete $resolved_attrs->{select} // ['*'];
Condition50%1/2
delete $resolved_attrs->{"select"} // ["*"]
l!l
60
MC/DC0%0/1
delete $resolved_attrs->{"select"} // ["*"]
delete $resolved_attrs->{"select"}
Truth table50%1/2
A
A= delete $resolved_attrs->{"select"}
Aresult
11
00
133 6 push @$select_args, keys %$values;
134  
135 # Need a more complex set than just $rsrc->columns_info, since relationship
136 # aliases are probably being used.
137 6 my $colinfo = $storage->_resolve_column_info($resolved_attrs->{from});
138  
139 # Get the SQL/binds for the SET part
140 6 my ($set_sql, $set_bind);
141 6 ($set_sql, @$set_bind) = $sql_maker->update('DUAL', $values); # no WHERE
142 6 $set_sql =~ s/^UPDATE `?DUAL`? //; # no UPDATE header
143  
144 6 $set_bind = $storage->_resolve_bindattrs( $rsrc, $set_bind, $colinfo );
145  
146 # Get the SQL/binds for the FROM part
147 my $from_attrs = (
148 6 $storage->_select_args( $resolved_attrs->{from}, $select_args, {}, $resolved_attrs )
149 )[4]; # just get the $attrs hash back again
150  
151 6 my ($from_sql, $from_bind);
152 6 ($from_sql, @$from_bind) = $sql_maker->select($from_attrs->{from}); # no WHERE, just * for the column list
153 6 $from_sql =~ s/^\(\s*(.+)\s*\)$/$1/s; # remove (useless) outside parentheses
154 6 $from_sql =~ s/^SELECT \* FROM //;
155  
156 6 $from_bind = $storage->_resolve_bindattrs( $from_attrs->{from}, $from_bind );
157  
158 # Get the SQL/binds for the WHERE part
159 my $where_attrs = (
160 6 $storage->_select_args( $resolved_attrs->{from}, $select_args, $resolved_attrs->{where}, $resolved_attrs )
161 )[4]; # just get the $attrs hash back again
162  
163 6 my ($where_sql, $where_bind);
164 6 ($where_sql, @$where_bind) = $sql_maker->where($where_attrs->{where}); # just the WHERE clause
165 6 $where_sql =~ s/^ //;
166  
167 6 $where_bind = $storage->_resolve_bindattrs( $from_attrs->{from}, $where_bind );
168  
169 # Mash them together!
170 6 my $update_sql = join ' ', 'UPDATE', $from_sql, $set_sql, $where_sql;
171 6 $update_sql =~ s/^\s+|\s+$//g;
172  
173 6 my $update_bind = [ @$from_bind, @$set_bind, @$where_bind ];
174  
175 6 my $rv = $self->dbh_execute($update_sql, $update_bind);
176 6 return $rv;
177 }
178  
179 #pod =head2 dbh_execute
180 #pod
181 #pod my $rv = $rs->dbh_execute($sql, $bind);
182 #pod my ($rv, $sth, @bind) = $rs->dbh_execute($sql, $bind);
183 #pod
184 #pod Sends any SQL statement to the C<$dbh> via L<DBIx::Class::Storage::DBI/dbh_do> while
185 #pod running the usual query loggers and re-connection protections that come with DBIC.
186 #pod
187 #pod This runs code similar to L<DBIx::Class::Storage::DBI>'s C<_execute> method, except that
188 #pod it takes SQL and binds as input. Like C<_dbh_execute> and C<_execute>, it returns
189 #pod different outputs, depending on the context.
190 #pod
191 #pod =cut
192  
193 0 ▶sub dbh_execute {
Subroutine dbh_execute: not called CC=2 SCAR=17.9
Pod documented
194 0 my ($self, $sql, $bind) = @_;
195  
196 0 my $rsrc = $self->result_source;
197 0 my $storage = $rsrc->storage;
198  
199 0 ▶ $storage->_populate_dbh unless $storage->_dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->_dbh 0 0 0
200  
201 0 return $storage->dbh_do( _dbh_execute => # retry over disconnects
202 $sql,
203 $bind,
204 $storage->_dbi_attrs_for_bind($rsrc, $bind),
205 );
206 }
207  
208 2 1;
209  
210 __END__
↑ Summary