IO/OracleIO.pm untested

filter: Statementstmt 0.0% 0 / 262 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 64 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 557 CC 44 SCAR 41.7
CC
44
Cov
0%
qualify
182.0
column_attrs
90.0
_lengthof
56.0
CRAP 64.8
1 #
2 # $Id: OracleIO.pm,v 1.2 2002/07/20 02:37:37 rsandberg Exp $
3 #
4  
5 package DBIx::IO::OracleIO;
6  
7 use DBIx::IO;
8  
9 @ISA = qw(DBIx::IO);
10  
11 use strict;
12  
13 use DBD::Oracle qw(
14 ORA_CLOB
15 ORA_BLOB
16 ORA_LONG
17 );
18  
19 use DBIx::IO::OracleLib ();
20 use DBIx::IO::GenLib ();
21  
22 my %long_lob_types = (
23 $DBIx::IO::GenLib::LONG_TYPE => ORA_LONG,
24 $DBIx::IO::GenLib::BLOB_TYPE => ORA_BLOB,
25 $DBIx::IO::GenLib::CLOB_TYPE => ORA_CLOB,
26 );
27  
28 my %all_table_col_types;
29 my %all_table_col_defaults;
30 my %all_table_col_required;
31 my %all_table_col_lengths;
32 my %all_table_col_scale;
33 my %all_table_cols;
34 my %all_table_pk;
35 my %all_table_col_list;
36  
37 my %datetime_types =
38 (
39 $DBIx::IO::GenLib::DATETIME_TYPE => 1,
40 # qualify() will not treat a date type any differently than a datetime type
41 $DBIx::IO::GenLib::DATE_TYPE => 1,
42 DATE => 1,
43 );
44 my %date_types =
45 (
46 $DBIx::IO::GenLib::DATE_TYPE => 1,
47 DATE => 1,
48 );
49 my %interval_types =
50 (
51 INTERVAL => 1,
52 TIMESTAMP => 1,
53 );
54 my %numeric_types =
55 (
56 $DBIx::IO::GenLib::NUMERIC_TYPE => 1,
57 NUMBER => 1,
58 FLOAT => 1,
59 BINARY_FLOAT => 1,
60 BINARY_DOUBLE => 1,
61 );
62 my %char_types =
63 (
64 $DBIx::IO::GenLib::CHAR_TYPE => 1,
65 CHAR => 1,
66 NCHAR => 1,
67 VARCHAR2 => 1,
68 NVARCHAR2 => 1,
69 RAW => 1,
70 'LONG RAW' => 1,
71 );
72 my %rowid_types =
73 (
74 $DBIx::IO::GenLib::ROWID_TYPE => 1,
75 ROWID => 1,
76 UROWID => 1,
77 );
78 my %long_types =
79 (
80 $DBIx::IO::GenLib::LONG_TYPE => 1,
81 LONG => 1,
82 );
83 my %lob_types =
84 (
85 $DBIx::IO::GenLib::LOB_TYPE => 1,
86 CLOB => 1,
87 BLOB => 1,
88 NCLOB => 1,
89 );
90 my %ignore_types = (
91 BFILE => 1,
92 );
93  
94  
95 =head1 NAME
96  
97 DBIx::IO::OracleIO - DBIx::IO driver for Oracle
98  
99 =head1 DESCRIPTION
100  
101 See DBIx::IO.
102  
103 =head1 METHOD DETAILS
104  
105 =over 4
106  
107 See superclass DBIx::IO for more
108  
109  
110 =cut
111 sub qualify
112 {
113 my ($self,$val,$field,$date_format,$type) = @_;
114 ref($self) || (warn("\$self not an object"),return undef);
115 if (defined($type))
116 {
117 $type = uc($type);
118 }
119 else
120 {
121 $field = uc($field);
122 my $col_types = $self->column_types();
123 $type = $col_types->{$field};
124 }
125 defined($type) || ($self->_alert("Data type not defined"), return undef);
126  
127 if (is_long_type($type))
128 {
129 return '' unless length($val);
130 return $val;
131 }
132  
133 length($val) || return 'NULL';
134
135 if (exists($rowid_types{$type}))
136 {
137 return "'$val'";
138 }
139 elsif (exists($numeric_types{$type}))
140 {
141 return $val;
142 }
143 elsif (exists($datetime_types{$type}))
144 {
145 return $val if uc($val) eq 'SYSDATE';
146 return $val if $val =~ /^DATE\b/i;
147 $date_format ||= $DBIx::IO::OracleLib::NORMAL_DATETIME_FORMAT;
148 if ($date_format eq $DBIx::IO::GenLib::UNKNOWN_DATE_FORMAT)
149 {
150 my $parse_val = DBIx::IO::GenLib::normalize_date($val);
151 length($parse_val) || ($self->_alert("The date format of: $val could not be recognized"),return undef);
152 $date_format = $DBIx::IO::OracleLib::NORMAL_DATETIME_FORMAT;
153 $val = $parse_val;
154 }
155 return "TO_DATE('$val','$date_format')";
156 }
157 elsif (exists($interval_types{$type}))
158 {
159 # Hopefully the operator knows what they're doing in this case
160 return $val;
161 }
162 elsif (exists($char_types{$type}))
163 {
164 $val =~ s/\000//g;
165 length($val) || return 'NULL';
166 $val = $self->{dbh}->quote($val);
167 return undef if $self->{dbh}->err;
168 return $val;
169 }
170 $self->_alert("Unhandled data type: $type");
171 return undef;
172 }
173  
174 sub verify_datatype
175 {
176 my ($self,$val,$field,$type) = @_;
177 if (defined($type))
178 {
179 $type = uc($type);
180 }
181 else
182 {
183 $field = uc($field);
184 my $col_types = $self->column_types();
185 $type = $col_types->{$field};
186 }
187 defined($type) || ($self->_alert("Data type not defined"), return undef);
188  
189 if ($numeric_types{$type})
190 {
191 if ($self->{scale}{$field})
192 {
193 # This is a real number
194 return DBIx::IO::GenLib::isreal($val);
195 }
196 else
197 {
198 return -1 unless DBIx::IO::GenLib::isint($val);
199 }
200 }
201 return 1;
202 }
203  
204 sub is_long_type
205 {
206 my $type = shift;
207 return (exists($lob_types{$type}) || exists($long_types{$type}));
208 }
209  
210 sub is_lob_type
211 {
212 my ($self,$type) = @_;
213 return exists($lob_types{$type});
214 }
215  
216 sub is_ignore_type
217 {
218 my ($self,$type) = @_;
219 return (exists($lob_types{$type}) || exists($ignore_types{$type}));
220 }
221  
222 sub _assign_table_attrs
223 {
224 my $self = shift;
225 my $table_name = shift;
226 $table_name = uc($table_name);
227 my $rv;
228 unless ($rv = $self->SUPER::_assign_table_attrs($table_name,@_))
229 {
230 return $rv;
231 }
232  
233 my $table = $self->table_name();
234 my $owner;
235 ($table,$owner) = $self->_strip_owner($table);
236 $self->{sequence_name} = ($owner ? "${owner}." : "") . "SEQ_" . uc($table);
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->{scale} = $all_table_col_scale{$table};
249 $self->{defaults} = $all_table_col_defaults{$table};
250 $self->{required} = $all_table_col_required{$table};
251 $self->{lengths} = $all_table_col_lengths{$table};
252 $self->{pk} = $all_table_pk{$table};
253 $self->{select_cols} = $all_table_cols{$table};
254 $self->{col_list} = $all_table_col_list{$table};
255 # do not alter this hash ref!!!
256 return ($self->{column_types} = $all_table_col_types{$table});
257 }
258  
259 my $pksth = $self->make_cursor("SELECT cc.column_name FROM user_cons_columns cc,user_constraints c " .
260 "WHERE c.constraint_name = cc.constraint_name " .
261 "AND c.constraint_type = 'P' " .
262 "AND cc.table_name = '$table' " .
263 "AND c.status = 'ENABLED'") || return undef;
264 my $res = $pksth->fetchall_arrayref() || return undef;
265 return undef if $pksth->err();
266 my @pk = map($_->[0],@$res);
267  
268 my $sth = $self->make_cursor("SELECT utc.column_name,utc.data_type,ut.tablespace_name,utc.data_length, " .
269 "utc.data_precision,utc.data_scale,utc.nullable,utc.data_default " .
270 "FROM user_tab_columns utc, user_tables ut " .
271 "WHERE utc.table_name = '$table' " .
272 "AND ut.table_name(+) = utc.table_name") || return undef;
273 my ($col,$type,$null,$length,$prec,$scale,$default,$tablespace,%attrs,%defaults,%lengths,%required,%scale,$cols,@cols);
274 while (($col,$type,$tablespace,$length,$prec,$scale,$null,$default) = $sth->fetchrow_array)
275 {
276 $col = uc($col);
277 $type = uc($type);
278 $type =~ s/\W.*// unless $type eq 'LONG RAW';
279 $attrs{$col} = $type;
280 $attrs{$DBIx::IO::OracleLib::ROWID_COL_NAME} = $DBIx::IO::GenLib::ROWID_TYPE if $tablespace;
281 $default = $1 if $default =~ /\s*\'(.*)\'\s*/;
282 $defaults{$col} = $default;
283 $required{$col} = uc($null) eq 'N';
284 $length = _lengthof($type,$length,$prec,$scale);
285 $lengths{$col} = $length;
286 $scale{$col} = $scale;
287 push(@cols,$col);
288  
289 # Build a list of select columns, LOB types error out if selected via DBI so skip them
290 next if $lob_types{$type} || $ignore_types{$type} || $type eq 'INTERVAL';
291 $cols .= ($datetime_types{$type} ? "TO_CHAR($col,'$DBIx::IO::OracleLib::NORMAL_DATETIME_FORMAT') $col," :
292 "$col,");
293 }
294 return undef if $sth->err;
295 %attrs || ($self->_alert("table: $table doesn't seem to exist or have any columns"), return 0);
296 chop $cols;
297  
298 # do not alter these hash refs!!!
299 ##at subclasses should cache these
300 $self->{scale} = $all_table_col_scale{$table} = \%scale;
301 $self->{defaults} = $all_table_col_defaults{$table} = \%defaults;
302 $self->{required} = $all_table_col_required{$table} = \%required;
303 $self->{lengths} = $all_table_col_lengths{$table} = \%lengths;
304 $self->{pk} = $all_table_pk{$table} = \@pk;
305 $self->{select_cols} = $all_table_cols{$table} = $cols;
306 $self->{col_list} = $all_table_col_list{$table} = \@cols;
307 return ($self->{column_types} = $all_table_col_types{$table} = \%attrs);
308 }
309  
310 sub _lengthof
311 {
312 my ($type,$length,$prec,$scale) = @_;
313 return 50 if exists($interval_types{$type});
314 return 25 if $type eq 'BINARY_DOUBLE';
315 return 15 if $type eq 'BINARY_FLOAT';
316 return $length unless !$length || $type eq 'NUMBER' || $type eq 'FLOAT';
317 my $ll = abs($prec) + abs($scale);
318 if ($type eq 'NUMBER' && $prec)
319 {
320 # 1 for optional (-) and 1 for optional '.'
321 return abs($prec) + 1 + ($scale > 0);
322 }
323 elsif ($type eq 'NUMBER' || $type eq 'FLOAT')
324 {
325 return 126;
326 }
327 return 255;
328 }
329  
330 sub insert_hash
331 {
332 my ($self,$orig_insert,$date_format) = @_;
333 ref($self) || (warn("\$self not an object"),return undef);
334 ref($orig_insert) || ($self->_alert("\$insert_hash not a hash ref"),return undef);
335 my $insert = { %$orig_insert };
336 my $attrs = $self->column_types();
337 my $dbh = $self->{dbh};
338 my $table = $self->table_name();
339 my $pkname = $self->key_name();
340 my $pk;
341 if (exists($attrs->{$pkname}) && !exists($insert->{$pkname}))
342 {
343 $pk = $self->key_nextval();
344 defined($pk) || ($self->_alert("Can't generate key for $table"), return undef);
345 $insert->{$pkname} = $pk;
346 }
347 else
348 {
349 $pk = $insert->{$pkname};
350 }
351  
352 %$insert || return -1.1;
353 delete $insert->{ROWID};
354  
355 my ($fields,$values,$field,$qual_val,%bind);
356 foreach $field (keys %$insert)
357 {
358 $field = uc($field);
359 $fields .= "$field,";
360 $qual_val = $self->qualify($insert->{$field},$field,$date_format);
361 if (is_long_type($attrs->{$field}))
362 {
363 $bind{":$field"} = [ $field,$qual_val ];
364 $qual_val = ":$field";
365 }
366 unless (defined($qual_val))
367 {
368 $self->_alert("Unable to qualify insert value: qualify($insert->{$field},$field,$date_format)");
369 return undef;
370 }
371 $values .= "$qual_val,";
372 }
373 chop($fields);
374 chop($values);
375 my $sql = "INSERT INTO $table ($fields) VALUES ($values)";
376 my $sth = $dbh->prepare($sql) || ($self->_alert("Can't prepare $sql"), return undef);
377 my ($bind_field,$bind_val);
378 while (($bind_field,$bind_val) = each %bind)
379 {
380 my ($field,$val) = @$bind_val;
381 my $type = $long_lob_types{$attrs->{$field}};
382 $sth->bind_param($bind_field,$val,{ ora_type => $type, ora_field => $field }) || ($self->_alert("Error binding $bind_field"), return undef);
383 }
384 my $rv = $sth->execute();
385 unless ($rv)
386 {
387 if ($sth->err == 1)
388 {
389 return -1.4;
390 }
391 else
392 {
393 return undef;
394 }
395 }
396  
397 return (length($pk) ? $pk : -1.2);
398 }
399  
400  
401 =pod
402  
403 =item C<sequence_name>
404  
405 $sequence_name = $io->sequence_name([$sequence_name]);
406  
407 Get/set the name of the sequence that generates key values
408 for inserts. Defaults to the name of the table prepended with "SEQ_".
409  
410 =cut
411 sub sequence_name
412 {
413 my ($self,$sequence_name) = @_;
414 if (defined($sequence_name))
415 {
416 return $self->{sequence_name} = $sequence_name;
417 }
418 return $self->{sequence_name};
419 }
420  
421  
422 =pod
423  
424 =item C<key_nextval>
425  
426 $next_seq_val = $io->key_nextval([$seq_name]);
427  
428 Returns the next value in the Oracle sequence object named
429 $seq_name or the table name prepended with "SEQ_"
430 All sequence statement handles are cached per $dbh for performance
431 reasons. A new $sth will be prepared unless the object that calls
432 this method has previously called it with the same sequence request.
433  
434 Returns undef if error.
435  
436 =cut
437 sub key_nextval
438 {
439 my ($self,$seq) = @_;
440 ref($self) || (warn("\$self not an object"),return undef);
441 my $dbh = $self->{dbh};
442 $seq ||= $self->{sequence_name};
443 ##at DBI version requirement prepare_cached
444 my $crs = $dbh->prepare_cached("SELECT $seq.NEXTVAL FROM DUAL") || return undef;
445 $crs->execute() || return undef;
446 return (($crs->fetchrow_array)[0]);
447 }
448  
449 sub update_hash
450 {
451 my ($self,$update,$key,$date_format,$hint) = @_;
452 ref($self) || (warn("\$self not an object"),return undef);
453 ref($update) || ($self->_alert("\$update not a hash ref"), return undef);
454 %$update || return -1;
455 my $dbh = $self->dbh();
456 my $table = $self->table_name();
457 unless (ref($key))
458 {
459 $key = { $self->key_name() => $key };
460 }
461  
462 my $where = $self->_build_where_clause($key) || return undef;
463  
464 my $set_sql;
465 my $attrs = $self->column_types();
466 my %bind;
467 my ($col,$val);
468 while (($col,$val) = each %$update)
469 {
470 $col = uc($col);
471 ##at does insert implement it's optional $hint feature?
472 $val = $self->qualify($val,$col,$date_format);
473 if (is_long_type($attrs->{$col}))
474 {
475 $bind{":$col"} = [ $col,$val ];
476 $val = ":$col";
477 }
478 unless (defined($val))
479 {
480 $self->_alert("Unable to qualify insert value: qualify($val,$col,$date_format)");
481 return undef;
482 }
483 $set_sql .= "$col = $val,";
484 }
485 chop($set_sql);
486 my ($bind_field,$bind_val);
487 my $sql = "UPDATE $hint $table SET $set_sql $where";
488 my $sth = $dbh->prepare($sql) || ($self->_alert("Can't prepare $sql"), return undef);
489 while (($bind_field,$bind_val) = each %bind)
490 {
491 my ($field,$val) = @$bind_val;
492 my $type = $long_lob_types{$attrs->{$field}};
493 $sth->bind_param($bind_field,$val,{ ora_type => $type, ora_field => $field }) || ($self->_alert("Error binding $bind_field"), return undef);
494 }
495 return $sth->execute();
496 }
497  
498 ##at should normalize the data types, e.g. $io->{column_types}{$column} = $DBIx::IO::GenLib::NORMAL_DATETIME_TYPE
499  
500 =pod
501  
502 =item C<existing_table_names>
503  
504 $sorted_arrayref = DBIx::IO::OracleIO->existing_table_names([$dbh]);
505  
506 Return a sorted arrayref of table names found in the
507 data dictionary.
508  
509 Class or object method.
510 $dbh is required if called as a class method.
511  
512 Return undef if db error.
513  
514 =cut
515 sub existing_table_names
516 {
517 my ($caller,$dbh) = @_;
518 $dbh ||= $caller->dbh();
519 my $rv = $dbh->selectcol_arrayref('SELECT DISTINCT table_name FROM user_tab_columns ORDER BY table_name');
520 return undef if $dbh->err;
521 return $rv;
522 }
523  
524  
525 =pod
526  
527 =item C<is_datetime>
528  
529 $bool = $io->is_datetime($column_name);
530  
531 Determine if $column_name is of a datetime type.
532  
533 =cut
534 sub is_datetime
535 {
536 my ($self,$column_name) = @_;
537 my $types = $self->column_types();
538 return $datetime_types{$types->{$column_name}};
539 }
540  
541 =pod
542  
543 =item C<is_date>
544  
545 $bool = $io->is_date($column_name);
546  
547 Determine if $column_name is of a date type.
548  
549 =cut
550 sub is_date
551 {
552 my ($self,$column_name) = @_;
553 my $types = $self->column_types();
554 return $date_types{$types->{$column_name}};
555 }
556  
557 =pod
558  
559 =item C<is_char>
560  
561 $bool = $io->is_char($column_name);
562  
563 Determine if $column_name is of a character type.
564  
565 =cut
566 sub is_char
567 {
568 my ($self,$column_name) = @_;
569 my $types = $self->column_types();
570 return $char_types{$types->{$column_name}};
571 }
572  
573 =pod
574  
575 =item C<limit>
576  
577 $sql = $io->limit($sql,$limit);
578  
579 Modify the given $sql to return a limited set
580 of records.
581  
582 =cut
583 sub limit
584 {
585 my ($self,$sql,$limit,$where) = @_;
586 return "$sql $where ROWNUM < ($limit + 1)";
587 }
588  
589 =pod
590  
591 =item C<lc_func>
592  
593 $function = $io->lc_func($column);
594  
595 Apply the function
596 for modifying $column to lower case.
597  
598 =cut
599 sub lc_func
600 {
601 my ($self,$column) = @_;
602 return "LOWER($column)";
603 }
604  
605 =pod
606  
607 =back
608  
609 =cut
610  
611  
612 1;
613  
614 __END__