blib/lib/Common.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 142 CC 15 SCAR 37.6
CC
15
Cov
0%
is_object_exist
72.0
get_dbh
56.0
new
6.0
CRAP 43.1
1 package Oracle::DML::Common;
2  
3 # Perl standard modules
4 use strict;
5 use warnings;
6 use Carp;
7 # use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
8 # warningsToBrowser(1);
9 # use CGI;
10 # use Getopt::Std;
11 use Debug::EchoMessage;
12 use DBI;
13  
14 our $VERSION = 0.21;
15  
16 require Exporter;
17 our @ISA = qw(Exporter);
18 our @EXPORT = qw();
19 our @EXPORT_OK = qw(get_dbh is_object_exist
20 );
21 our %EXPORT_TAGS = (
22 all =>[@EXPORT_OK],
23 db_conn=>[qw(get_dbh is_object_exist
24 )],
25 table =>[qw(
26 )],
27 );
28  
29 =head1 NAME
30  
31 Oracle::DML::Common - Common routines for Oracle DML
32  
33 =head1 SYNOPSIS
34  
35 use Oracle::DML::Common;
36  
37 my %cfg = ('conn_string'=>'usr/pwd@db', 'table_name'=>'my_ora_tab');
38 my $ot = Oracle::DML::Common->new;
39 # or combine the two together
40 my $ot = Oracle::DML::Common->new(%cfg);
41 my $sql= $ot->prepare(%cfg);
42 $ot->execute(); # actually create the audit table and trigger
43  
44  
45 =head1 DESCRIPTION
46  
47 This class contains methods to create audit tables and triggers for
48 Oracle tables.
49  
50 =cut
51  
52 =head3 new ()
53  
54 Input variables:
55  
56 %ha - any hash array containing initial parameters
57  
58 Variables used or routines called:
59  
60 None
61  
62 How to use:
63  
64 my $obj = new Oracle::DML::Common; # or
65 my $obj = Oracle::DML::Common->new;
66  
67 Return: new empty or initialized Oracle::DML::Common object.
68  
69 This method constructs a Perl object and capture any parameters if
70 specified.
71
72 =cut
73  
74 sub new {
75 my $caller = shift;
76 my $caller_is_obj = ref($caller);
77 my $class = $caller_is_obj || $caller;
78 my $self = bless {}, $class;
79 my %arg = @_; # convert rest of inputs into hash array
80 foreach my $k ( keys %arg ) {
81 if ($caller_is_obj) {
82 $self->{$k} = $caller->{$k};
83 } else {
84 $self->{$k} = $arg{$k};
85 }
86 }
87 return $self;
88 }
89  
90 =head1 METHODS
91  
92 The following are the common methods, routines, and functions used
93 by other classes.
94  
95 =head2 Connection Methods
96  
97 The I<:db_conn> tag includes sub-rountines for creating and
98 managing database connections.
99  
100 use Oracle::DML::Common qw(:db_conn);
101  
102 It includes the following sub-routines:
103  
104 =head3 get_dbh($con, $dtp)
105  
106 Input variables:
107  
108 $con - Connection string for
109 Oralce: usr/pwd@db (default)
110 CSV: /path/to/file
111 ODBC|SQL: usr/pwd@DSN[:approle/rolepwd]
112 $dtp - Database type: Oracle, CSV, etc
113  
114 Variables used or routines called:
115  
116 DBI
117 DBD::Oracle
118 Win32::ODBC
119  
120 How to use:
121  
122 $self->get_dbh('usr/pwd@dblk', 'Oracle');
123 $self->get_dbh('usr/pwd@dblk:approle/rpwd', 'SQL');
124  
125 Return: database handler
126  
127 If application role is provided, it will activate the application role
128 as well.
129  
130 =cut
131  
132 sub get_dbh {
133 my $self = shift;
134 my ($con, $dtp) = @_;
135 # Input variables:
136 # $con - connection string: usr/pwd@db
137 # $dtp - database type: Oracle, CSV
138 #
139 $dtp = 'Oracle' if !$dtp;
140 my (@conn, $dsn, $dbh,$msg);
141 my ($usr, $pwd, $sid) = ($con =~ /(\w+)\/(\w+)\@(\w+)/i);
142 my ($apusr, $appwd) = ($con =~ /:(\w+)\/(\w+)/i);
143 if ($dtp =~ /Oracle/i) {
144 @conn = ("DBI:Oracle:$sid", $usr, $pwd);
145 $dbh=DBI->connect(@conn) ||
146 die "Connection error : $DBI::errstr\n";
147 $dbh->{RaiseError} = 1;
148 } elsif ($dtp =~ /CSV/i) {
149 carp "WARN: CSV directory - $con does not exist.\n"
150 if (!-d $con);
151 @conn = ("DBI:CSV:f_dir=$con","","");
152 $dbh=DBI->connect(@conn) ||
153 die "Connection error : $DBI::errstr\n";
154  
155 } else { # ODBC or SQL
156 $dsn = "DSN=$sid;uid=$usr;pwd=$pwd;";
157 $dbh = new Win32::ODBC($dsn);
158 if (! $dbh) {
159 Win32::ODBC::DumpError();
160 $msg = "Could not open connection to DSN ($dsn) ";
161 $msg .= "because of [$!]";
162 die "$msg";
163 }
164 if ($apusr) {
165 $dbh->Sql("exec sp_setapprole $apusr, $appwd");
166 }
167 }
168 return $dbh;
169 }
170  
171 =head3 is_object_exist($dbh,$tn,$tp)
172  
173 Input variables:
174  
175 $dbh - database handler, required.
176 $tn - table/object name, required.
177 schema.table_name is allowed.
178  
179 Variables used or routines called:
180  
181 echoMSG - display messages.
182  
183 How to use:
184  
185 # whether table 'emp' exist
186 $yesno = $self->is_object_exist($dbh,'emp');
187  
188 Return: 0 - the object does not exist;
189 1 - the object exist;
190  
191 =cut
192  
193 sub is_object_exist {
194 my $self = shift;
195 my($dbh,$tn, $tp) = @_;
196 croak "ERR: could not find database handler.\n" if !$dbh;
197 croak "ERR: no table or object name is specified.\n" if !$tn;
198 # get owner name and table name
199 my ($sch, $tab, $stb) = ("","","");
200 if (index($tn, '.')>0) {
201 ($sch, $tab) = ($tn =~ /(\w+)\.([\w\$]+)/);
202 }
203 my($q,$r);
204 $tp = 'TABLE' if ! $tp;
205 $stb = 'user_objects';
206 $stb = 'all_objects' if $sch;
207 $q = "SELECT object_name from $stb ";
208 $q .= " WHERE object_type = '" . uc($tp) . "'";
209 if ($sch) {
210 $q .= " AND object_name = '" . uc($tab) . "'";
211 $q .= " AND owner = '" . uc($sch) . "'";
212 } else {
213 # $tn =~ s/\$/\\\$/g;
214 $q .= " AND object_name = '" . uc($tn) . "'";
215 }
216 $self->echoMSG($q, 5);
217 my $sth=$dbh->prepare($q) || die "Stmt error: $dbh->errstr";
218 $sth->execute() || die "Stmt error: $dbh->errstr";
219 my $n = $sth->rows;
220 my $arf = $sth->fetchall_arrayref;
221 $r = 0;
222 $r = 1 if ($#{$arf}>=0);
223 return $r;
224 }
225  
226 =head2 Table Methods
227  
228 The I<:table> tag includes sub-rountines for creating, checking and
229 manipulating tables.
230  
231 use Oracle::DML::Common qw(:table);
232  
233 It includes the following sub-routines:
234  
235 =cut
236  
237 1;
238  
239 =head1 HISTORY
240  
241 =over 4
242  
243 =item * Version 0.1
244  
245 This versionwas contained in Oracle::Trigger class.
246  
247 =item * Version 0.2
248  
249 04/29/2005 (htu) - extracted common routines from Oracle::Trigger class
250 and formed Oracle::DML::Common.
251  
252 =item * Version 0.21
253  
254 Remove get_table_definition method to I<Oracle::Schema> class.
255  
256 =cut
257  
258 =head1 SEE ALSO (some of docs that I check often)
259  
260 Data::Describe, Oracle::Loader, CGI::Getopt, File::Xcopy,
261 Oracle::Trigger,
262 perltoot(1), perlobj(1), perlbot(1), perlsub(1), perldata(1),
263 perlsub(1), perlmod(1), perlmodlib(1), perlref(1), perlreftut(1).
264  
265 =head1 AUTHOR
266  
267 Copyright (c) 2005 Hanming Tu. All rights reserved.
268  
269 This package is free software and is provided "as is" without express
270 or implied warranty. It may be used, redistributed and/or modified
271 under the terms of the Perl Artistic License (see
272 http://www.perl.com/perl/misc/Artistic.html)
273  
274 =cut
275  
276