ORM/Db/DBI/MySQL.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 196 CC 16 SCAR 32.4
CC
16
Cov
0%
table_struct
72.0
delete_object
30.0
qc
6.0
CRAP 25.6
1 #
2 # DESCRIPTION
3 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
4 # library that implements object-relational mapping. Its features are
5 # much similar to those of Java's Hibernate library, but interface is
6 # much different and easier to use.
7 #
8 # AUTHOR
9 # Alexey V. Akimov <akimov_alexey@sourceforge.net>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2005-2006 Alexey V. Akimov
13 #
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23 #
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #
28  
29 package ORM::Db::DBI::MySQL;
30  
31 $VERSION = 0.83;
32  
33 use base 'ORM::Db::DBI';
34  
35 ##
36 ## CONSTRUCTORS
37 ##
38  
39 sub new
40 {
41 my $class = shift;
42 my %arg = @_;
43 my $self;
44  
45 $arg{driver} = 'mysql';
46 $self = $class->SUPER::new( %arg );
47  
48 $self->{allow_nontransactional_tables} = $arg{allow_nontransactional_tables};
49  
50 return $self;
51 }
52  
53 ##
54 ## CLASS METHODS
55 ##
56  
57 sub qc
58 {
59 my $self = shift;
60 my $str = shift;
61  
62 if( defined $str )
63 {
64 $str =~ s/\'/\'\'/g;
65 $str =~ s/\\/\\\\/g;
66 $str =~ s/\n/\\n/g;
67 $str =~ s/\r/\\r/g;
68 $str = "'$str'";
69 }
70 else
71 {
72 $str = 'NULL';
73 }
74  
75 return $str;
76 }
77  
78 sub qi
79 {
80 my $self = shift;
81 my $str = shift;
82  
83 $str =~ s/\`/\`\`/g;
84 $str = "`$str`"; #"
85  
86 return $str;
87 }
88  
89 sub qt { $_[0]->qi( $_[1] ); }
90 sub qf { $_[0]->qi( $_[1] ); }
91  
92 ##
93 ## OBJECT METHODS
94 ##
95  
96 sub begin_transaction
97 {
98 my $self = shift;
99 my %arg = @_;
100  
101 $self->{ta} = 1;
102 $self->do( query=>"START TRANSACTION", error=>$arg{error} );
103 }
104  
105 sub commit_transaction
106 {
107 my $self = shift;
108 my %arg = @_;
109  
110 delete $self->{ta};
111 $self->do( query=>"COMMIT", error=>$arg{error} );
112 }
113  
114 sub rollback_transaction
115 {
116 my $self = shift;
117 my %arg = @_;
118  
119 delete $self->{ta};
120 unless( $self->{lost_connection} )
121 {
122 $self->do( query=>"ROLLBACK", error=>$arg{error} );
123 }
124 }
125  
126 sub update_object
127 {
128 my $self = shift;
129  
130 $self->update_object_part( all_tables=>1, @_ );
131 }
132  
133 ## Can be optimized to check consistency
134 ## right in DELETE statement using JOINS
135 ##
136 sub delete_object
137 {
138 my $self = shift;
139 my %arg = @_;
140 my $obj = $arg{object};
141 my $obj_class = ref $obj;
142 my $error = ORM::Error->new;
143 my $ta = $obj_class->new_transaction( error=>$error );
144 my $join = $obj_class->_db_tables_inner_join;
145 my $rows_affected;
146  
147 $self->check_object_referers
148 (
149 object => $obj,
150 error => $error,
151 check => $arg{emulate_foreign_keys},
152 );
153  
154 unless( $error->fatal )
155 {
156 $rows_affected = $self->do
157 (
158 error => $error,
159 query =>
160 (
161 "DELETE " . $obj_class->_db_tables_str
162 . " FROM " . $obj_class->_db_tables_str
163 . " WHERE "
164 . $self->qt( $obj_class->_db_table(0) ).'.id = '.$self->qc( $obj->id )
165 . ( $join ? " AND $join" : '' ),
166 ),
167 );
168  
169 if( $rows_affected != $obj_class->_db_tables_count )
170 {
171 $error->add_fatal
172 (
173 "Failed to delete ID#".$obj->id." from '"
174 . $obj_class->_db_tables_str
175 . "', $rows_affected rows affected"
176 );
177 }
178  
179 unless( $error->fatal )
180 {
181 $self->check_object_referers
182 (
183 object => $obj,
184 error => $error,
185 check => $arg{emulate_foreign_keys},
186 );
187 }
188 }
189  
190 $error->upto( $arg{error} );
191 }
192  
193 ## use: $id = $db->insertid()
194 ##
195 sub insertid
196 {
197 my $self = shift;
198 $self->_db_handler ? $self->_db_handler->{mysql_insertid} : undef;
199 }
200  
201 sub table_struct
202 {
203 my $self = shift;
204 my %arg = @_;
205 my $error = ORM::Error->new;
206 my %field;
207 my %defaults;
208 my $res;
209 my $data;
210  
211 ## Fetch table structure
212 $res = $self->select( error=>$error, query=>( 'SHOW COLUMNS FROM '.$self->qt($arg{table}) ) );
213 unless( $error->fatal )
214 {
215 while( $data = $res->next_row )
216 {
217 $defaults{$data->{Field}} = $data->{Default};
218 $field{$data->{Field}} = $arg{class}->_db_type_to_class( $data->{Field}, $data->{Type} );
219 }
220 }
221  
222 ## Check whether table's engine supports transactions
223 unless( $error->fatal )
224 {
225 $res = $self->select( error=>$error, query=>( 'SHOW TABLE STATUS LIKE '.$self->ql( $arg{table} ) ) );
226 $data = $res && $res->next_row;
227  
228 my $engine = $data->{Engine} || $data->{Type};
229  
230 if( !$data )
231 {
232 $error->add_fatal( 'Can\'t detect engine for "'.$arg{table}.'"' );
233 }
234 elsif( !$self->{allow_nontransactional_tables} && $engine ne 'InnoDB' && $engine ne 'BDB' )
235 {
236 $error->add_fatal
237 (
238 "Engine for table '$arg{table}' is '$engine', should be 'InnoDB' or 'BDB'."
239 );
240 }
241 }
242  
243 ## Fetch class references
244 if( scalar( %field ) )
245 {
246 $res = $self->select
247 (
248 error => $error,
249 query => 'SELECT * FROM '.$self->qt('_ORM_refs').' WHERE class='.$self->qc( $arg{class} ),
250 );
251 unless( $error->fatal )
252 {
253 while( $data = $res->next_row )
254 {
255 if( exists $field{$data->{prop}} )
256 {
257 $field{$data->{prop}} = $data->{ref_class};
258 }
259 }
260 }
261 }
262  
263 $error->upto( $arg{error} );
264 return \%field, \%defaults;
265 }
266  
267 sub _lost_connection
268 {
269 my $self = shift;
270 my $err = shift;
271  
272 defined $err && ( $err == 2006 || $err == 2013 );
273 }
274  
275  
276 ## use: $encrypted_password = $db->pwd( $password )
277 ##
278 sub pwd
279 {
280 my $self = shift;
281 my $pwd = shift;
282 my $st;
283  
284 $st = $self->_db_handler && $self->_db_handler->prepare( 'select password('.($self->qc($pwd)).')' );
285 if( $st )
286 {
287 $st->execute;
288 return ($st->fetchrow_arrayref)->[0];
289 }
290 else
291 {
292 return undef;
293 }
294 }
295  
296 ##
297 ## SQL FUNCTIONS
298 ##
299  
300 sub _func_concat { shift; ORM::Filter::Func->new( 'CONCAT', @_ ); }