DataLoader/DB.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 319 CC 26 SCAR 60.8
CC
26
Cov
0%
DBInsertUpdate
600.0
sqldate
6.0
conv_sqldate
6.0
CRAP 437.4
1 package DBIx::XML::DataLoader::DB;
2  
3 use strict;
4 use warnings;
5  
6  
7  
8 ###########################################################################
9 ## Description of Subs
10 ###########################################################################
11 ##
12 ## DBConnect
13 ##
14 ## requires no arguments
15 ## returns a db connection handle
16 ##
17 ## DBprepareOut
18 ##
19 ## arguments for this sub are the table name a array ref to the columns,
20 ## and the variable holding the db connection
21 ## This sub then returns the statement handle for doing the select
22 ##
23 ##
24 ###########################################################################
25 ###########################################################################
26  
27 use DBI; # DataBase Interface Modlule
28  
29 #use DBD::Oracle; # Oracle Specific Driver for DBI
30  
31 ## I am just trying to load all the DBD drivers available
32 ## you might want to limit this in some way.
33 my @driver_names = DBI->available_drivers;
34 for my $drv (@driver_names){
35 eval "use DBD::$drv;"; # loading available driver
36 }
37
38  
39 ##################
40 sub new{
41 ########
42 my $class=shift;
43 my $self={};
44 my %args=@_;
45 my $dbmode=$args{dbmode}||"insertupdate";
46 my $dbprint=$args{dbprint}||undef;
47 $self->{dbprint}=$dbprint;
48 $self->{dbmode}=$dbmode;
49 bless ($self, $class);
50 #######
51 } # end sub new
52 ##################
53  
54 #####################
55 sub DBConnect{
56 ##############
57 ## requires no arguments
58 ## returns a db connection handle
59  
60 my $self =shift;
61 my $DBLOG = shift;
62 my $DBPWD= shift;
63 my $DATA_SOURCE=shift;
64  
65 #print $self, "+", $DBLOG, "-", $DBPWD, "[",$DATA_SOURCE,"]";
66 #if(!$DATA_SOURCE){die "did not get a datasource";}
67 # Connect to the database with auto-commit enabled
68 my $dbh = DBI->connect($DATA_SOURCE, $DBLOG, $DBPWD, {
69 PrintError => 0,
70 AutoCommit => 1,
71 }) || die "Could not Connect to Database! Oracle Error was: $DBI::errstr\n";
72  
73 return($dbh);
74 ############
75 } # end sub DBConnect;
76 ###############################
77  
78  
79  
80 ##############################
81 sub DBInsertUpdate{
82 ##################
83 my $self=shift;
84  
85 my %args=@_;
86 my $dbprint=$args{dbprint}||$self->{dbprint}||undef;
87 my $dbmode=$args{dbmode}||$self->{dbmode}||"insertupdate";
88  
89 my $dbhstuff=$args{dbconnections};
90 my @arrayoftableinfo=@{$args{datainfo}};
91 my $message;
92 my $error;
93 my $sqlload;
94 TABLE: for my $tabinfo (@arrayoftableinfo){
95 my $update;
96 my @upkeys;
97 my @insert_vals;
98 my @insert_cols;
99 my @update;
100 my @upkeystring;
101 my $results=$tabinfo->{results};
102 my @thecols=@{$tabinfo->{cols}};
103 my $table=$tabinfo->{table};
104  
105 @{$sqlload->{$table}->{cols}}=@thecols;
106 #### added to order update keys
107 ####
108 my @hashed_upkeys=@{$tabinfo->{keys}};
109 my $keyorder=1;
110 HASHED_KEYS:for (@hashed_upkeys){
111 for my $key (keys %{$_}){
112 #print $key, ":", $_->{$key},":",$keyorder,"\n";
113 if($key == $keyorder){
114 push @upkeys, $_->{$key};
115 $keyorder++;next HASHED_KEYS;
116 }
117 push @hashed_upkeys, $_;
118 } # end keylop
119 } # end loop HASHED_KEYS
120 ##################################################
121 my @results=@{$results};
122 my $dbh=$dbhstuff->{$tabinfo->{dbname}};
123 if($dbh !~ /DBI::/){next TABLE;}
124 for my $result (@results){
125 my $q_value=$result->{val};
126  
127 if(($result->{val} ne "SYSDATE")and($dbmode ne "sqlloader")){
128 #$q_value=qq[\'$result->{val}\'];}
129 $q_value=$dbh->quote($result->{val});}
130 push @insert_cols, $result->{col};
131 push @insert_vals, $q_value;
132 if($result->{key}){
133 my $upk=$result->{col}."=".$q_value;
134 push @upkeystring, $upk;
135 }
136 if((!$result->{key})and($result->{col} !~ /^CREATE/)){
137 my $up=$result->{col}."=".$q_value;
138 push @update, $up;}
139 } # end for @results;
140  
141 my $updateset=join ", ", @update;
142 my $upkeysstring=join " and ", @upkeystring;
143 my $inscols=join ", ", @insert_cols;
144 my $insertstuff=join ", ", @insert_vals;
145  
146 if(($dbprint eq "print")or($dbprint eq "dbandprint")){
147 $message.="\nHere is what would be placed in the table $table\n";
148 if($dbmode =~ /update/){
149 $message.= "UPDATE $table SET\n".$updateset. "\nWHERE\n". $upkeysstring."\n";
150 }
151 if($dbmode =~ /insert/){
152 $message.="\nINSERT\n(".$insertstuff.")\nVALUES\n(".$inscols.")\nINTO $table\n";
153 }
154 }
155  
156 if($dbprint ne "print"){
157 if($dbmode eq "insertupdate"){
158 $message.="\nAttempting to insert into table $table\n";
159  
160 my $local_error;
161 $dbh->do(qq{insert INTO $table ($inscols) VALUES($insertstuff)})
162 or $local_error=$DBI::errstr;
163 if(!$local_error){$message .= "\nInsert was successful for table $table\n";}
164 if($local_error){
165 $message.="\nInsert did not succeed. Now trying to update table $table instead\n";
166 if($local_error =~ /unique constraint/){
167 $dbh->do(qq{update $table set $updateset where $upkeysstring})
168 or my $uperror="db error when working with table $table:\n$DBI::errstr\n";
169 if(!$uperror){$message.="\nUpdate did succeed for Table $table";}
170 if($uperror){$error.=$uperror;}
171 }
172 if($local_error !~ /unique constraint/){
173 $error.="db error when working with table $table:\n$local_error\n";
174 }
175 }
176 }
177 if($dbmode eq "update"){
178 $message.="\nAttempting to make updates in table $table\n";
179 $dbh->do(qq{update $table set $updateset where $upkeysstring})
180 or
181 my $uperror="db error when working with table $table:\n$DBI::errstr\n";
182 if(!$uperror){$message.="\nUpdate did succeed for Table $table";}
183 if($uperror){$error.=$uperror;}
184  
185 }
186 if($dbmode eq "insert"){
187 $message.="\nAttempting to insert rows in table $table\n";
188 $dbh->do(qq{insert INTO $table ($inscols) VALUES($insertstuff)})
189 or
190 my $inerror="db error when working with table $table:\n$DBI::errstr\n";
191 if(!$inerror){$message.="\nInsert was successful for Table $table";}
192 if($inerror){$error.=$inerror;}
193  
194 }
195 if($dbmode eq "sqlloader"){
196 $message.="\nAdding to sqlload for table $table\n";
197 $sqlload->{$table}->{values}.=(join "::", @insert_vals)."::\n";
198 }
199 }
200 }
201 # end for $tabinfo
202  
203 return ($message, $error, $sqlload);
204 ######################
205 } # end DBInsertUpdate
206 #######################################
207  
208 ###############
209 sub sqldate{
210 ###########
211 ## this sub take 4 arguments passed as variables
212 ## $dbh a open db handle
213 ## $date which should be the actual date you want converted
214 ## $format the current format the date helf in $date is in (yy-mm-dd yyyy-mm-dd dd-mm-yy etc)
215 ## $table a valid table in your sql view
216 ## returned is a date that will conform to the SQL database format for dates
217 my $self=shift;
218 if((scalar @_) < 4){return;}
219 my $dbh=shift;
220 my $date=shift;
221 my $format=shift;
222 my $table=shift;
223 my $qdate=$dbh->quote($date);
224 my $qformat=$dbh->quote($format);
225 my $converted_date;
226 my $datekey=qq{TO_DATE($qdate, $qformat)};
227 my $datetest=$dbh->prepare(qq{select UNIQUE $datekey from $table})||warn "dbi error $DBI::errstr";
228 #my $datetest=$dbh->prepare(qq{select UNIQUE SYSDATE from rod_trans_request});
229 #if($datetest){
230 $datetest->execute()||warn "dbi error $DBI::errstr";;
231 while(my $hash_ref=$datetest->fetchrow_hashref()){
232 for (keys %{$hash_ref}){
233 $converted_date=$hash_ref->{$_};last;}
234 #}
235 }
236 return $converted_date;
237 ############
238 } # end sqldate
239 #################
240  
241  
242 ###############
243 sub conv_sqldate{
244 ###########
245 ## this sub take 4 arguments passed as variables
246 ## $dbh a open db handle
247 ## $date which should be the actual date you want converted
248 ## $format the current format the date helf in $date is in (yy-mm-dd yyyy-mm-dd dd-mm-yy etc)
249 ## $table a valid table in your sql view
250 ## returned is a date that will conform to the SQL database format for dates
251 my $self=shift;
252 if((scalar @_) < 4){return;}
253 my $dbh=shift;
254 my $date=shift;
255 my $format=shift;
256 my $table=shift;
257  
258 my $org_format="DD-MON-YY";
259 my $qorg_format=$dbh->quote($org_format);
260 my $qdate=$dbh->quote($date);
261 my $qformat=$dbh->quote($format);
262 my $converted_date;
263 my $datekey=qq{TO_DATE($qdate, $qorg_format)};
264 ## here we first alter the way time is outputted
265 my $datetest=$dbh->prepare(qq{alter session set nls_date_format=$qformat})||warn "dbi error $DBI::errstr";
266 $datetest->execute()||warn "dbi error $DBI::errstr";
267  
268 $datetest=$dbh->prepare(qq{select UNIQUE $datekey from $table})||warn "dbi error $DBI::errstr";
269 #my $datetest=$dbh->prepare(qq{select UNIQUE SYSDATE from rod_trans_request});
270 #if($datetest){
271 $datetest->execute()||warn "dbi error $DBI::errstr";;
272 while(my $hash_ref=$datetest->fetchrow_hashref()){
273 for (keys %{$hash_ref}){
274 $converted_date=$hash_ref->{$_};last;}
275 #}
276  
277 }
278  
279 $datetest=$dbh->prepare(qq{alter session set nls_date_format=$qorg_format})||warn "dbi error $DBI::errstr";
280 $datetest->execute()||warn "dbi error $DBI::errstr";
281  
282 return $converted_date;
283  
284 ##############
285 } # end conv_sqldate
286 #######################
287  
288 1;
289  
290  
291 __END__