IO/mysqlIO.pm untested

filter: Statementstmt 0.0% 0 / 249 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 70 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 488 CC 46 SCAR 46.5
CC
46
Cov
0%
qualify
210.0
column_attrs
210.0
verify_datatype
56.0
CRAP 105.0
1 #
2 # $Id$
3 #
4  
5 package DBIx::IO::mysqlIO;
6  
7 use DBIx::IO;
8  
9 @ISA = qw(DBIx::IO);
10  
11 use strict;
12  
13 use DBIx::IO::mysqlLib ();
14 use DBIx::IO::GenLib ();
15  
16 my %all_table_col_types;
17 my %all_table_col_defaults;
18 my %all_table_col_required;
19 my %all_table_col_lengths;
20 my %all_table_cols;
21 my %all_table_pk;
22 my %all_table_col_list;
23 my %all_table_col_picklist;
24  
25 my %datetime_types =
26 (
27 $DBIx::IO::GenLib::DATETIME_TYPE => 1,
28 DATETIME => 1,
29 TIMESTAMP => 1,
30 );
31 my %date_types =
32 (
33 $DBIx::IO::GenLib::DATE_TYPE => 1,
34 DATE => 1,
35 );
36 my %time_types =
37 (
38 $DBIx::IO::GenLib::TIME_TYPE => 1,
39 TIME => 1,
40 );
41 my %year_types =
42 (
43 $DBIx::IO::GenLib::YEAR_TYPE => 1,
44 YEAR => 1,
45 );
46 my %numeric_types =
47 (
48 $DBIx::IO::GenLib::NUMERIC_TYPE => 1,
49 NUMERIC => 1,
50 TINYINT => 1,
51 SMALLINT => 1,
52 MEDIUMINT => 1,
53 INT => 1,
54 INTEGER => 1,
55 BIGINT => 1,
56 FLOAT => 1,
57 DOUBLE => 1,
58 # 'DOUBLE PRECISION' => 1, # now truncating type so equiv to DOUBLE
59 # REAL => 1, # Synonym for DOUBLE
60 DECIMAL => 1,
61 );
62 ##at should make longs, blobs default to 'TEXTAREA' in CGI::AutoForm
63 my %char_types =
64 (
65 $DBIx::IO::GenLib::CHAR_TYPE => 1,
66 CHAR => 1,
67 'NATIONAL CHAR' => 1,
68 VARCHAR => 1,
69 'NATIONAL VARCHAR' => 1,
70 TINYBLOB => 1,
71 TINYTEXT => 1,
72 BLOB => 1,
73 TEXT => 1,
74 MEDIUMBLOB => 1,
75 MEDIUMTEXT => 1,
76 LONGBLOB => 1,
77 LONGTEXT => 1,
78 BINARY => 1,
79 VARBINARY => 1,
80 ENUM =>1,
81 SET =>1,
82 );
83 my %set_types =
84 (
85 ENUM =>1,
86 SET =>1,
87 );
88 my %long_types =
89 (
90 );
91 my %lob_types =
92 (
93 );
94 my %ignore_types = (
95 );
96  
97  
98  
99 =head1 NAME
100  
101 DBIx::IO::mysqlIO - DBIx::IO driver for MySQL
102  
103 =head1 DESCRIPTION
104  
105 See DBIx::IO.
106  
107 =head1 METHOD DETAILS
108  
109 =over 4
110  
111 See superclass DBIx::IO for more
112  
113  
114 =cut
115 sub qualify
116 {
117 my ($self,$val,$field,$date_format,$type) = @_;
118 ref($self) || (warn("\$self not an object"),return undef);
119 if (defined($type))
120 {
121 $type = uc($type);
122 }
123 else
124 {
125 $field = uc($field);
126 my $col_types = $self->column_types();
127 $type = $col_types->{$field};
128 }
129 defined($type) || ($self->_alert("Data type not defined"), return undef);
130  
131 length($val) || return 'NULL';
132 return "''" if $val eq $DBIx::IO::GenLib::EMPTY_STRING;
133
134 my $trunc_type = $type;
135 $trunc_type =~ s/\s.*//;
136 if (exists($numeric_types{$trunc_type}))
137 {
138 return $val;
139 }
140 elsif (exists($datetime_types{$type}) || exists($date_types{$type}) || exists($time_types{$type}) || exists($year_types{$type}))
141 {
142 return 'NOW()' if uc($val) eq 'SYSDATE';
143 if ($date_format eq $DBIx::IO::GenLib::UNKNOWN_DATE_FORMAT)
144 {
145 my $parse_val = DBIx::IO::GenLib::normalize_date($val);
146 length($parse_val) || ($self->_alert("The date format of: $val could not be recognized"),return undef);
147 if (exists($datetime_types{$type}))
148 {
149 $val = $parse_val;
150 return "'$val'";
151 }
152 elsif (exists($date_types{$type}))
153 {
154 $val = substr($parse_val,0,8);
155 $date_format = $DBIx::IO::mysqlLib::NORMAL_DATE_FORMAT;
156 }
157 elsif (exists($time_types{$type}))
158 {
159 #$val = substr($parse_val,-6);
160 #$date_format = $DBIx::IO::mysqlLib::NORMAL_TIME_FORMAT;
161 return "'$val'";
162 }
163 elsif (exists($year_types{$type}))
164 {
165 #$val = substr($parse_val,0,4);
166 #$date_format = $DBIx::IO::mysqlLib::NORMAL_YEAR_FORMAT;
167 return "'$val'";
168 }
169 else
170 {
171 die("A horrible death");
172 }
173 return "STR_TO_DATE('$val','$date_format')";
174 }
175 elsif (length($date_format))
176 {
177 return "STR_TO_DATE('$val','$date_format')";
178 }
179 return "'$val'";
180 }
181 elsif (exists($char_types{$type}))
182 {
183 $val =~ s/\000//g;
184 $val = $self->{dbh}->quote($val);
185 return undef if $self->{dbh}->err;
186 return $val;
187 }
188 $self->_alert("Unhandled data type: $type");
189 return undef;
190 }
191  
192 sub verify_datatype
193 {
194 ##at should make it optional to submit a scalar reference for $val
195 my ($self,$val,$field,$type) = @_;
196 if (defined($type))
197 {
198 $type = uc($type);
199 }
200 else
201 {
202 $field = uc($field);
203 my $col_types = $self->column_types();
204 $type = $col_types->{$field};
205 }
206 defined($type) || ($self->_alert("Data type not defined"), return undef);
207  
208 my $trunc_type = $type;
209 $trunc_type =~ s/\s.*//;
210 if ($numeric_types{$trunc_type})
211 {
212 if ($trunc_type =~ /int/i)
213 {
214 return -1 unless DBIx::IO::GenLib::isint($val);
215 if ($type =~ /zerofill|unsigned/i)
216 {
217 return -2 unless $val > 0;
218 }
219 }
220 else
221 {
222 return DBIx::IO::GenLib::isreal($val);
223 }
224 }
225 return 1;
226 }
227  
228 sub _assign_table_attrs
229 {
230 my $self = shift;
231 my $table_name = shift;
232 my $rv;
233 unless ($rv = $self->SUPER::_assign_table_attrs($table_name,@_))
234 {
235 return $rv;
236 }
237  
238 return 1;
239 }
240  
241 sub column_attrs
242 {
243 my ($self,$table) = @_;
244 ref($self) || (warn("\$self not an object"),return undef);
245  
246 if (exists($all_table_col_types{$table}))
247 {
248 $self->{defaults} = $all_table_col_defaults{$table};
249 $self->{required} = $all_table_col_required{$table};
250 $self->{lengths} = $all_table_col_lengths{$table};
251 $self->{pk} = $all_table_pk{$table};
252 $self->{select_cols} = $all_table_cols{$table};
253 $self->{col_list} = $all_table_col_list{$table};
254 $self->{picklist} = $all_table_col_picklist{$table};
255 # do not alter this hash ref!!!
256 return ($self->{column_types} = $all_table_col_types{$table});
257 }
258  
259 my $sth = $self->make_cursor("DESCRIBE $table");
260 unless ($sth)
261 {
262 ($self->{dbh}->err() == 1146) || return undef;
263 return 0;
264 }
265 ##at should use the 'Extra' column of output with a value of auto_increment to validate the sequence number generated was indeed
266 ##at for the column it was ASSUMED to be.
267 my ($col,$type,$null,$key,$default,%attrs,%defaults,%lengths,%required,$cols,@pk,@cols,@picklist,%picklist);
268 while (($col,$type,$null,$key,$default) = $sth->fetchrow_array)
269 {
270 @picklist = ();
271 $col = uc($col);
272 $key = uc($key);
273 push(@pk,$col) if $key =~ /PRI/;
274 $type =~ s/\((.*)\)//; #strip length specs
275 my $specs = $1;
276 $type = uc($type);
277  
278 my ($length,$scale);
279  
280 if ($set_types{$type})
281 {
282 my $specs_orig = $specs;
283  
284 # Lose the empty strings and NULLs
285 my $ucsp = uc($specs);
286 my $idx = 0;
287 while (($idx = index($ucsp,",'',")) > -1)
288 {
289 substr($specs,$idx,3) = '';
290 substr($ucsp,$idx,3) = '';
291 }
292 while (($idx = index($ucsp,",NULL,")) > -1)
293 {
294 substr($specs,$idx,5) = '';
295 substr($ucsp,$idx,5) = '';
296 }
297 $specs =~ s/^(''|NULL),//i;
298 $specs =~ s/,(''|NULL)$//i;
299 $specs =~ s/''/\\'/g;
300 my @elems = eval("($specs)");
301 $self->_alert("Couldn't parse MySQL set list, setting picklist to empty [$@]: $specs_orig") if $@;
302  
303 $length = 1;
304 my $l = 0;
305 foreach my $elem (@elems)
306 {
307 $l = length($elem);
308 $length = $l if $l > $length;
309 push(@picklist, { ID => $elem, MASK => $elem });
310 }
311  
312 $length = 250 if $type eq 'SET';
313 }
314 ##at use "show index from u;" to get unique keys where there is no PK
315 else
316 {
317 ($length,$scale) = split(/[,]/,$specs);
318 }
319
320 $attrs{$col} = $type;
321 undef($default) if uc($default) eq 'NULL';
322 $defaults{$col} = $default;
323 $required{$col} = !$null || $null eq 'NO';
324 $length = _lengthof($type,$length);
325 $lengths{$col} = $length;
326 $picklist{$col} = [ @picklist ];
327 push(@cols,$col);
328 # Build a list of select columns
329 # by using syntax select COLUMN_NAME vs column_name, mysql will actually return COLUMN_NAME vs column_name a COLUMN_ALIAS could also be used
330 if ($datetime_types{$type})
331 {
332 $cols .= "DATE_FORMAT($table.$col,'$DBIx::IO::mysqlLib::NORMAL_DATETIME_FORMAT') $col,";
333 }
334 elsif ($date_types{$type})
335 {
336 $cols .= "DATE_FORMAT($table.$col,'$DBIx::IO::mysqlLib::NORMAL_DATE_FORMAT') $col,";
337 }
338 elsif ($time_types{$type})
339 {
340 #$cols .= "DATE_FORMAT($table.$col,'$DBIx::IO::mysqlLib::NORMAL_TIME_FORMAT') $col,";
341 $cols .= "$table.$col,";
342 }
343 elsif ($year_types{$type})
344 {
345 $cols .= "$table.$col,";
346 }
347 else
348 {
349 $cols .= "$table.$col,";
350 }
351 }
352 return undef if $sth->err;
353 %attrs || ($self->_alert("table: $table doesn't seem to exist or have any columns"), return 0);
354 chop $cols;
355 # Cache types for each table
356 # do not alter these hash refs!!!
357 ##at all subclasses should cache these
358 $self->{defaults} = $all_table_col_defaults{$table} = \%defaults;
359 $self->{required} = $all_table_col_required{$table} = \%required;
360 $self->{lengths} = $all_table_col_lengths{$table} = \%lengths;
361 $self->{pk} = $all_table_pk{$table} = \@pk;
362 $self->{select_cols} = $all_table_cols{$table} = $cols;
363 $self->{col_list} = $all_table_col_list{$table} = \@cols;
364 $self->{picklist} = $all_table_col_picklist{$table} = \%picklist;
365 return ($self->{column_types} = $all_table_col_types{$table} = \%attrs);
366 }
367  
368 sub _lengthof
369 {
370 my ($type,$length) = @_;
371 return $length if $length;
372 return 24 if $type =~ /float/i;
373 return 53 if $type =~ /double/i;
374 return 255;
375 }
376  
377 sub insert_hash
378 {
379 my ($self,$insert,$date_format) = @_;
380 ref($self) || (warn("\$self not an object"),return undef);
381 ref($insert) || ($self->_alert("\$insert_hash not a hash ref"),return undef);
382 my $dbh = $self->{dbh};
383 my $table = $self->table_name();
384  
385 %$insert || return -1.1;
386  
387 my ($fields,$values,$field,$qual_val);
388 foreach $field (keys %$insert)
389 {
390 $field = uc($field);
391 $fields .= "$field,";
392 $qual_val = $self->qualify($insert->{$field},$field,$date_format);
393 unless (defined($qual_val))
394 {
395 $self->_alert("Unable to qualify insert value: qualify($insert->{$field},$field,$date_format)");
396 return undef;
397 }
398 $values .= "$qual_val,";
399 }
400 chop($fields);
401 chop($values);
402 my $sql = "INSERT INTO $table ($fields) VALUES ($values)";
403 my $sth = $dbh->prepare($sql) || ($self->_alert("Can't prepare $sql"), return undef);
404 my $rv = $sth->execute();
405 unless ($rv)
406 {
407 if ($sth->err == 1062)
408 {
409 return -1.4;
410 }
411 else
412 {
413 return undef;
414 }
415 }
416  
417 my $pkname = $self->key_name();
418 my $pk;
419 if ($pkname && !exists($insert->{$pkname}))
420 {
421 $pk = $sth->{mysql_insertid};
422 }
423 elsif (exists($insert->{$pkname}))
424 {
425 $pk = $insert->{$pkname};
426 }
427 return (length($pk) ? $pk : -1.2);
428 }
429  
430 sub update_hash
431 {
432 my ($self,$update,$key,$date_format,$hint) = @_;
433 ref($self) || (warn("\$self not an object"),return undef);
434 ref($update) || ($self->_alert("\$update not a hash ref"), return undef);
435 %$update || return -1;
436 my $dbh = $self->dbh();
437 my $table = $self->table_name();
438 unless (ref($key))
439 {
440 $key = { $self->key_name() => $key };
441 }
442  
443 my $where = $self->_build_where_clause($key) || return undef;
444  
445 my $set_sql;
446 my ($col,$val);
447 while (($col,$val) = each %$update)
448 {
449 $col = uc($col);
450 ##at does insert implement it's optional $hint feature?
451 $val = $self->qualify($val,$col,$date_format);
452 unless (defined($val))
453 {
454 $self->_alert("Unable to qualify insert value: qualify($val,$col,$date_format)");
455 return undef;
456 }
457 $set_sql .= "$col = $val,";
458 }
459 chop($set_sql);
460 my $sql = "UPDATE $hint $table SET $set_sql $where";
461 my $sth = $dbh->prepare($sql) || ($self->_alert("Can't prepare $sql"), return undef);
462 return $sth->execute();
463 }
464  
465 =pod
466  
467 =item C<existing_table_names>
468  
469 $sorted_arrayref = DBIx::IO::mysqlIO->existing_table_names([$dbh]);
470  
471 Return a sorted arrayref of table names found in the
472 data dictionary.
473  
474 Class or object method.
475 $dbh is required if called as a class method.
476  
477 Return undef if db error.
478  
479 =cut
480 sub existing_table_names
481 {
482 my ($caller,$dbh) = @_;
483 $dbh ||= $caller->dbh();
484 my $rv = $dbh->selectcol_arrayref('SHOW TABLES');
485 return undef if $dbh->err;
486 return $rv;
487 }
488  
489 =pod
490  
491 =item C<is_datetime>
492  
493 $bool = $io->is_datetime($column_name);
494  
495 Determine if $column_name is of a datetime type.
496  
497 =cut
498 sub is_datetime
499 {
500 my ($self,$column_name) = @_;
501 my $types = $self->column_types();
502 return $datetime_types{$types->{$column_name}};
503 }
504  
505 =pod
506  
507 =item C<is_date>
508  
509 $bool = $io->is_date($column_name);
510  
511 Determine if $column_name is of a date type.
512  
513 =cut
514 sub is_date
515 {
516 my ($self,$column_name) = @_;
517 my $types = $self->column_types();
518 return $date_types{$types->{$column_name}};
519 }
520  
521 =pod
522  
523 =item C<is_char>
524  
525 $bool = $io->is_char($column_name);
526  
527 Determine if $column_name is of a character type.
528  
529 =cut
530 sub is_char
531 {
532 my ($self,$column_name) = @_;
533 my $types = $self->column_types();
534 return $char_types{$types->{$column_name}};
535 }
536  
537 sub is_long_type
538 {
539 my $type = shift;
540 return (exists($lob_types{$type}) || exists($long_types{$type}));
541 }
542  
543 sub is_lob_type
544 {
545 my ($self,$type) = @_;
546 return exists($lob_types{$type});
547 }
548  
549 sub is_ignore_type
550 {
551 my ($self,$type) = @_;
552 return (exists($lob_types{$type}) || exists($ignore_types{$type}));
553 }
554  
555  
556 =pod
557  
558 =item C<limit>
559  
560 $sql = $io->limit($sql,$limit);
561  
562 Modify the given $sql to return a limited set
563 of records.
564  
565 =cut
566 sub limit
567 {
568 my ($self,$sql,$limit) = @_;
569 return "$sql LIMIT $limit";
570 }
571  
572 =pod
573  
574 =item C<lc_func>
575  
576 $function = $io->lc_func($column);
577  
578 Apply the MySQL specific function
579 for modifying $column to lower case.
580  
581 =cut
582 sub lc_func
583 {
584 my ($self,$column) = @_;
585 return "LOWER($column)";
586 }
587  
588 =pod
589  
590 =back
591  
592 =cut
593  
594 1;
595  
596 __END__