blib/lib/Schema.pm untested

filter: Statementstmt 0.0% 0 / 289 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 375 CC 22 SCAR 49.1
CC
22
Cov
0%
get_table_definition
240.0
new
72.0
BEGIN
2.0
CRAP 135.2
1 package Oracle::Schema;
2  
3 # Perl standard modules
4 use strict;
5 use warnings;
6 use Carp;
7 use DBI;
8 use Debug::EchoMessage;
9 use Oracle::DML::Common qw(:db_conn);
10  
11 require 5.003;
12 $Oracle::Schema::VERSION = 0.02;
13  
14 require Exporter;
15 our @ISA = qw(Exporter);
16 our @EXPORT = qw();
17 our @EXPORT_OK = qw( get_table_definition
18 );
19 our %EXPORT_TAGS = (
20 all => [@EXPORT_OK],
21 table => [qw(get_table_definition)],
22 );
23 our @IMPORT_OK = qw(
24 get_dbh is_object_exist
25 debug echoMSG disp_param
26 );
27  
28 =head1 NAME
29  
30 Oracle::Schema - Perl class for Oracle Schema Information and
31 Management
32  
33 =head1 SYNOPSIS
34  
35 use Oracle::Schema;
36  
37 my %cfg = ('conn_string'=>'usr/pwd@db');
38 my $os = Oracle::Schema->new;
39 # or combine the two together
40 my $os = Oracle::Schema->new('cs'=>'usr/pwd@db');
41 $os->display_objects;
42  
43  
44 =head1 DESCRIPTION
45  
46 This class includes methods to query (find, retrieve, and
47 compare) objects in an Oracle schema and to manage (create, drop,
48 update, merge, and move) Oracle objects.
49  
50 =cut
51  
52 =head2 new (cs=>'usr/pwd@db',tn=>'my_table')
53  
54 Input variables:
55  
56 $cs - Oracle connection string in usr/pwd@db
57 $tn - Oracle table name without schema
58  
59 Variables used or routines called:
60  
61 None
62  
63 How to use:
64  
65 my $obj = new Oracle::Schema; # or
66 my $obj = Oracle::Schema->new; # or
67 my $cs = 'usr/pwd@db';
68 my $tn = 'my_table';
69 my $obj = Oracle::Schema->new(cs=>$cs,tn=>$tn); # or
70 my $obj = Oracle::Schema->new('cs',$cs, 'tn',$tn);
71  
72 Return: new empty or initialized Oracle::Schema object.
73  
74 This method constructs a Perl object and capture any parameters if
75 specified. It creates and defaults the following variables:
76
77 $self->{conn_string} = ""; # or $self->{cs}
78 $self->{table_name} = ""; # or $self->{tn}
79  
80 =cut
81  
82 sub new {
83 my $caller = shift;
84 my $caller_is_obj = ref($caller);
85 my $class = $caller_is_obj || $caller;
86 my $self = bless {}, $class;
87 my %arg = @_; # convert rest of inputs into hash array
88 foreach my $k ( keys %arg ) {
89 if ($caller_is_obj) {
90 $self->{$k} = $caller->{$k};
91 } else {
92 $self->{$k} = $arg{$k};
93 }
94 }
95 my $vs = 'conn_string,table_name,cs,tn';
96 foreach my $k (split /,/, $vs) {
97 $self->{$k} = "" if ! exists $arg{$k};
98 $self->{$k} = $arg{$k} if exists $arg{$k};
99 }
100 my $cs1 = $self->{conn_string};
101 my $tn1 = $self->{table_name};
102 $self->{cs} = ($cs1)?$cs1:$self->{cs};
103 $self->{tn} = ($tn1)?$tn1:$self->{tn};
104 $self->{conn_string} = ($self->{cs})?$self->{cs}:$cs1;
105 $self->{table_name} = ($self->{tn})?$self->{tn}:$tn1;
106 return $self;
107 }
108  
109 =head1 METHODS
110  
111 The following are the common methods, routines, and functions
112 defined in this class.
113  
114 =head2 Exported Tag: All
115  
116 The I<:all> tag includes all the methods or sub-rountines
117 defined in this class.
118  
119 use Oracle::Schema qw(:all);
120  
121 It includes the following sub-routines:
122  
123 =head2 Table Methods
124  
125 The I<:table> tag includes sub-rountines for creating, checking and
126 manipulating tables.
127  
128 use Oracle::DML::Common qw(:table);
129  
130 It includes the following sub-routines:
131  
132 =head3 get_table_definition($dbh,$tn,$cns,$otp)
133  
134 Input variables:
135  
136 $dbh - database handler, required.
137 $tn - table/object name, required.
138 schema.table_name is allowed.
139 $cns - column names separated by comma.
140 Default is null, i.e., to get all the columns.
141 If specified, only get definition for those specified.
142 $otp - output array type:
143 AR|ARRAY - returns ($cns,$df1,$cmt)
144 AH1|ARRAY_HASH1 - returns ($cns,$df2,$cmt)
145 HH|HASH - returns ($cns,$df3,$cmt)
146 AH2|ARRAY_HASH2 - returns ($cns,$df4,$cmt)
147  
148 Variables used or routines called:
149  
150 echoMSG - display messages.
151  
152 How to use:
153  
154 ($cns,$df1,$cmt) = $self->getTableDef($dbh,$table_name,'','array');
155 ($cns,$df2,$cmt) = $self->getTableDef($dbh,$table_name,'','ah1');
156 ($cns,$df3,$cmt) = $self->getTableDef($dbh,$table_name,'','hash');
157 ($cns,$df4,$cmt) = $self->getTableDef($dbh,$table_name,'','ah2');
158  
159 Return:
160  
161 $cns - a list of column names separated by comma.
162 $df1 - column definiton array ref in [$seq][$cnn].
163 where $seq is column sequence number, $cnn is array
164 index number corresponding to column names:
165 0 - cname,
166 1 - coltype,
167 2 - width,
168 3 - scale,
169 4 - precision,
170 5 - nulls,
171 6 - colno,
172 7 - character_set_name.
173 $df2 - column definiton array ref in [$seq]{$itm}.
174 where $seq is column number (colno) and $itm are:
175 col - column name
176 seq - column sequence number
177 typ - column data type
178 wid - column width
179 max - max width
180 min - min width
181 dec - number of decimals
182 req - requirement: null or not null
183 dft - date format
184 dsp - description or comments
185 $df3 - {$cn}{$itm} when $otp = 'HASH'
186 where $cn is column name in lower case and
187 $itm are the same as the above
188 $df4 - [$seq]{$itm} when $otp = 'AH2'
189 where $seq is the column number, and $itm are:
190 cname - column name (col)
191 coltype - column data type (typ)
192 width - column width (wid)
193 scale - column scale (dec)
194 precision - column precision (wid for N)
195 nulls - null or not null (req)
196 colno - column sequence number (seq)
197 character_set_name - character set name
198 $cmt - {$cn}: contains comments for each column
199  
200 =cut
201  
202 sub get_table_definition {
203 my $self = shift;
204 my($dbh, $tn, $cns, $otp) = @_;
205 # Input variables:
206 # $dbh - database handler
207 # $tn - table name
208 # $cns - column names
209 #
210 # 0. check inputs
211 croak "ERR: could not find database handler.\n" if !$dbh;
212 croak "ERR: no table or object name is specified.\n" if !$tn;
213 $tn = uc($tn);
214 $self->echoMSG(" - reading table $tn definition...", 1);
215 $otp = 'ARRAY' if (! defined($otp));
216 $otp = uc $otp;
217 if ($cns) { $cns =~ s/,\s*/','/g; $cns = "'$cns'"; }
218 #
219 # 1. retrieve column definitions
220 my($q,$msg);
221 if (index($tn,'.')>0) { # it is in schema.table format
222 my ($sch,$tab) = ($tn =~ /([-\w]+)\.([-\w]+)/);
223 $q = " SELECT column_name,data_type,data_length,";
224 $q .= "data_scale,data_precision,\n ";
225 $q .= "nullable,column_id,character_set_name\n";
226 $msg = "$q";
227 $q .= " FROM dba_tab_columns\n";
228 $msg .= " FROM dba_tab_columns\n";
229 $q .= " WHERE owner = '$sch' AND table_name = '$tab'\n";
230 $msg .= " WHERE owner = '$sch' AND table_name = '$tab'\n";
231 } else {
232 $q = " SELECT cname,coltype,width,scale,precision,nulls,";
233 $q .= "colno,character_set_name\n";
234 $msg = "$q";
235 $q .= " FROM col\n WHERE tname = '$tn'";
236 $msg .= " FROM col\n WHERE tname = '$tn'\n";
237 }
238 if ($cns) {
239 $q .= " AND cname in (" . uc($cns) . ")\n";
240 $msg .= " AND cname in (" . uc($cns) . ")\n";
241 }
242 if (index($tn,'.')>0) { # it is in schema.table format
243 $q .= "\n ORDER BY table_name,column_id";
244 $msg .= " ORDER BY table_name, column_id\n";
245 } else {
246 $q .= "\n ORDER BY tname, colno";
247 $msg .= " ORDER BY tname, colno\n";
248 }
249 $self->echoMSG(" $msg", 2);
250 my $sth=$dbh->prepare($q) || croak "ERR: Stmt - $dbh->errstr";
251 $sth->execute() || croak "ERR: Stmt - $dbh->errstr";
252 my $arf = $sth->fetchall_arrayref; # = output $df1
253 #
254 # 2. construct column name list
255 my $r = ${$arf}[0][0];
256 for my $i (1..$#{$arf}) { $r .= ",${$arf}[$i][0]"; }
257 $msg = $r; $msg =~ s/,/, /g;
258 $self->echoMSG(" $msg", 5);
259 #
260 # 3. get column comments
261 $q = "SELECT column_name, comments\n FROM user_col_comments";
262 $q .= "\n WHERE table_name = '$tn'";
263 $msg = "SELECT column_name, comments\nFROM user_col_comments";
264 $msg .= "\nWHERE table_name = '$tn'<p>";
265 $self->echoMSG(" $msg", 5);
266 my $s2=$dbh->prepare($q) || croak "ERR: Stmt - $dbh->errstr";
267 $s2->execute() || croak "ERR: Stmt - $dbh->errstr";
268 my $brf = $s2->fetchall_arrayref;
269 my (%cmt, $j, $k, $cn);
270 for my $i (0..$#{$brf}) {
271 $j = lc(${$brf}[$i][0]); # column name
272 $cmt{$j} = ${$brf}[$i][1]; # comments
273 }
274 #
275 # 4. construct output $df2($def) and $df3($df2)
276 my $def = bless [], ref($self)||$self; # = output $df2
277 my $df2 = bless {}, ref($self)||$self; # = output $df3
278 for my $i (0..$#{$arf}) {
279 $j = ${$arf}[$i][6]-1; # column seq number
280 ${$def}[$j]{seq} = $j; # column seq number
281 $cn = lc(${$arf}[$i][0]); # column name
282 ${$def}[$j]{col} = uc($cn); # column name
283 ${$def}[$j]{typ} = ${$arf}[$i][1]; # column type
284 if (${$arf}[$i][4]) { # precision > 0
285 # it is NUMBER data type
286 ${$def}[$j]{wid} = ${$arf}[$i][4]; # column width
287 ${$def}[$j]{dec} = ${$arf}[$i][3]; # number decimal
288 } else { # CHAR or VARCHAR2
289 ${$def}[$j]{wid} = ${$arf}[$i][2]; # column width
290 ${$def}[$j]{dec} = "" # number decimal
291 }
292 ${$def}[$j]{max} = ${$def}[$j]{wid};
293  
294 if (${$def}[$j]{typ} =~ /date/i) { # typ is DATE
295 ${$def}[$j]{max} = 17; # set width to 17
296 ${$def}[$j]{wid} = 17; # set width to 17
297 ${$def}[$j]{dft} = 'YYYYMMDD.HH24MISS';
298 } else {
299 ${$def}[$j]{dft} = ''; # set date format to null
300 }
301 if (${$arf}[$i][5] =~ /^(not null|N)/i) {
302 ${$def}[$j]{req} = 'NOT NULL';
303 } else {
304 ${$def}[$j]{req} = '';
305 }
306 if (exists $cmt{$cn}) {
307 ${$def}[$j]{dsp} = $cmt{$cn};
308 } else {
309 ${$def}[$j]{dsp} = '';
310 }
311 ${$def}[$j]{min} = 0;
312 ${$df2}{$cn}{seq} = $j;
313 ${$df2}{$cn}{col} = ${$def}[$j]{col};
314 ${$df2}{$cn}{typ} = ${$def}[$j]{typ};
315 ${$df2}{$cn}{dft} = ${$def}[$j]{dft};
316 ${$df2}{$cn}{wid} = ${$def}[$j]{wid};
317 ${$df2}{$cn}{dec} = ${$def}[$j]{dec};
318 ${$df2}{$cn}{max} = ${$def}[$j]{max};
319 ${$df2}{$cn}{min} = ${$def}[$j]{min};
320 ${$df2}{$cn}{req} = ${$def}[$j]{req};
321 ${$df2}{$cn}{dsp} = ${$def}[$j]{dsp};
322 }
323 #
324 # 5. construct output array $df4
325 my $df4 = bless [],ref($self)||$self; # = output $df4
326 for my $i (0..$#{$arf}) {
327 $j = lc(${$arf}[$i][0]); # column name
328 push @$df4, {cname=>$j, coltype=>${$arf}[$i][1],
329 width=>${$arf}[$i][2], scale=>${$arf}[$i][3],
330 precision=>${$arf}[$i][4], nulls=>${$arf}[$i][5],
331 colno=>${$arf}[$i][6],
332 character_set_name=>${$arf}[$i][7]};
333 }
334 #
335 # 6. output based on output type
336 if ($otp =~ /^(AR|ARRAY)$/i) {
337 return ($r, $arf, \%cmt); # output ($cns,$df1,$cmt)
338 } elsif ($otp =~ /^(AH1|ARRAY_HASH1)$/i) {
339 return ($r, $def, \%cmt); # output ($cns,$df2,$cmt)
340 } elsif ($otp =~ /^(HH|HASH)$/i) {
341 return ($r, $df2, \%cmt); # output ($cns,$df3,$cmt)
342 } else {
343 return ($r, $df4, \%cmt); # output ($cns,$df4,$cmt);
344 }
345 }
346  
347  
348  
349 1;
350  
351 =head1 HISTORY
352  
353 =over 4
354  
355 =item * Version 0.01
356  
357 This version is to set the framework and move the get_table_definition
358 from Oracle:;DML::Common.
359  
360 =item * Version 0.02
361  
362 Added table tag for export.
363  
364 =cut
365  
366 =head1 SEE ALSO (some of docs that I check often)
367  
368 Data::Describe, Oracle::Loader, CGI::Getopt, File::Xcopy,
369 perltoot(1), perlobj(1), perlbot(1), perlsub(1), perldata(1),
370 perlsub(1), perlmod(1), perlmodlib(1), perlref(1), perlreftut(1).
371  
372 =head1 AUTHOR
373  
374 Copyright (c) 2005 Hanming Tu. All rights reserved.
375  
376 This package is free software and is provided "as is" without express
377 or implied warranty. It may be used, redistributed and/or modified
378 under the terms of the Perl Artistic License (see
379 http://www.perl.com/perl/misc/Artistic.html)
380  
381 =cut
382  
383