| 1 | |||
| 2 | |||
| 3 | package OOPS::DBO; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | sub query_debug | ||
| 8 | { #debug | ||
| 9 | my ($dbo, $dprefix, $q, %debug_args) = @_; | ||
| 10 | |||
| 11 | my $debug_rx_target; | ||
| 12 | if ($OOPS::debug_q_regex_target eq 'data') { | ||
| 13 | require Data::Dumper; # debug; | ||
| 14 | $debug_rx_target = Data::Dumper::Dumper(%debug_args); | ||
| 15 | } elsif ($OOPS::debug_q_regex_target eq 'query') { | ||
| 16 | $debug_rx_target = $dbo->{queries}{$q}; | ||
| 17 | } elsif ($OOPS::debug_q_regex_target eq 'comment') { | ||
| 18 | $debug_rx_target = $dbo->{debug_q}{$q}; | ||
| 19 | } # debug | ||
| 20 | |||
| 21 | my $debug_match = $debug_rx_target =~ /$OOPS::debug_q_regex/; | ||
| 22 | |||
| 23 | print STDERR "Q$dprefix: $q\n" if ($OOPS::debug_queries & 2) | ||
| 24 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 25 | print STDERR "Q$dprefix: $dbo->{queries}{$q}" if ($OOPS::debug_queries & 4) | ||
| 26 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 27 | |||
| 28 | my @debug_args = exists($debug_args{execute}) | ||
| 29 | ? (defined($debug_args{execute}) | ||
| 30 | ? (ref($debug_args{execute}) | ||
| 31 | ? @{$debug_args{execute}} | ||
| 32 | : $debug_args{execute}) | ||
| 33 | : ()) # debug | ||
| 34 | : %debug_args; | ||
| 35 | |||
| 36 | print STDERR "A$dprefix: ".join(',', $q, @debug_args)."\n" | ||
| 37 | if ($OOPS::debug_queries & 8) | ||
| 38 | && (($OOPS::debug_queries & 1) || $debug_match); | ||
| 39 | } #debug | ||
| 40 | |||
| 41 | |||
| 42 | package OOPS::DBO::DBIdebug; | ||
| 43 | |||
| 44 | use strict; | ||
| 45 | use warnings; | ||
| 46 | use Carp qw(confess longmess); | ||
| 47 | use POSIX qw(EAGAIN ENOENT EEXIST O_RDWR); | ||
| 48 | use Fcntl qw(LOCK_SH LOCK_EX LOCK_NB LOCK_UN); | ||
| 49 | use Scalar::Util qw(weaken); | ||
| 50 | |||
| 51 | my $id = 1; | ||
| 52 | |||
| 53 | sub new | ||
| 54 | { | ||
| 55 | my ($pkg, $dbh) = @_; | ||
| 56 | |||
| 57 | if ($ENV{OOPS_DEBUGLOG}) { | ||
| 58 | open(DBOdebug, ">>$ENV{OOPS_DEBUGLOG}") || die "open >> $ENV{OOPS_DEBUGLOG}: $!"; | ||
| 59 | } else { | ||
| 60 | open(DBOdebug, ">&STDOUT") || die "can't dup STDOUT: $!"; | ||
| 61 | } | ||
| 62 | my $lastq = "idle"; | ||
| 63 | my $self = bless { dbh => $dbh, id => $id++, primary => 1 }; | ||
| 64 | $self->output("Connect"); | ||
| 65 | $self->{lastq} = \$lastq; | ||
| 66 | push(@OOPS::transaction_rollback, sub { | ||
| 67 | my $lq = ${$self->{lastq}}; | ||
| 68 | $lq = "<UNDEF>" unless defined $lq; | ||
| 69 | print STDERR "Last query before rollback: $lq\n" | ||
| 70 | unless $lq =~ /^(idle|disconnect)$/ | ||
| 71 | }) if $OOPS::transaction_tries; | ||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub subnew | ||
| 76 | { | ||
| 77 | my ($pkg, $sth, $parent) = @_; | ||
| 78 | my $self = bless { dbh => $sth, id => $parent->{id}, parent => $parent, lastq => $parent->{lastq} }; | ||
| 79 | push(@{$parent->{children}}, $self); | ||
| 80 | weaken($parent->{children}[$#{$parent->{children}}]); | ||
| 81 | weaken($self->{parent}); | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub AUTOLOAD | ||
| 86 | { | ||
| 87 | my $self = shift; | ||
| 88 | our $AUTOLOAD; | ||
| 89 | my $a = $AUTOLOAD; | ||
| 90 | $a =~ s/.*:://; | ||
| 91 | die "USING AUTOLOAD ON $self->{dbh} ->$a()\n"; | ||
| 92 | my $method = $self->{dbh}->can($a) || $self->{dbh}->can($AUTOLOAD) || confess "cannot find method $a for $self->[0]"; | ||
| 93 | my @r; | ||
| 94 | if (wantarray) { | ||
| 95 | @r = &$method($self->{dbh}, @_); | ||
| 96 | } else { | ||
| 97 | $r[0] = &$method($self->{dbh}, @_); | ||
| 98 | } | ||
| 99 | if (ref $r[0] && ref($r[0]) ne __PACKAGE__) { | ||
| 100 | $r[0] = __PACKAGE__->subnew($r[0], $self); | ||
| 101 | } | ||
| 102 | return @r if wantarray; | ||
| 103 | return $r[0]; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub prepare | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | my ($query) = @_; | ||
| 110 | my $sth; | ||
| 111 | confess if @_ > 2; | ||
| 112 | eval { | ||
| 113 | $sth = __PACKAGE__->subnew($self->{dbh}->prepare($query), $self); | ||
| 114 | }; | ||
| 115 | confess $@ if $@; | ||
| 116 | $sth->{query} = $query; | ||
| 117 | return $sth; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub prepare_cached | ||
| 121 | { | ||
| 122 | my $self = shift; | ||
| 123 | my ($query) = @_; | ||
| 124 | my $sth = __PACKAGE__->subnew($self->{dbh}->prepare_cached($query), $self); | ||
| 125 | $sth->{query} = $query; | ||
| 126 | return $sth; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub execute | ||
| 130 | { | ||
| 131 | my $self = shift; | ||
| 132 | my (@values) = scalar(@_) | ||
| 133 | ? @_ | ||
| 134 | : ($self->{values} | ||
| 135 | ? @{$self->{values}} | ||
| 136 | : ()); | ||
| 137 | my $query = $self->{query}; | ||
| 138 | die unless $query; | ||
| 139 | my $code = "Do"; | ||
| 140 | $self->{display} = preformat($self->{query}, @values); | ||
| 141 | ${$self->{lastq}} = $self->{display}; | ||
| 142 | if ($query =~ /\bSELECT\b/i) { | ||
| 143 | $code = "Query"; | ||
| 144 | } | ||
| 145 | $self->output("Pre-$code", $self->{display}); | ||
| 146 | die "PREVIOUSLY DISCONNECTED AT <<<<<<<$self->{parent}{disconnect}>>>>>>>>" if $self->{parent}{disconnect}; | ||
| 147 | my $r = $self->{dbh}->execute(@_); | ||
| 148 | my $dr = $r ? $r : "ERROR"; | ||
| 149 | $self->output($code, "[$dr] = ", $self->{display}); | ||
| 150 | ${$self->{lastq}} = "idle"; | ||
| 151 | return $r; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub selectrow_array | ||
| 155 | { | ||
| 156 | my $self = shift; | ||
| 157 | my ($query) = @_; | ||
| 158 | $self->{display} = preformat($query); | ||
| 159 | $self->output("Pre-Fetch", $self->{display}); | ||
| 160 | ${$self->{lastq}} = $self->{display}; | ||
| 161 | my (@r) = $self->{dbh}->selectrow_array($query); | ||
| 162 | $self->output("Fetch", resultsformat(@r). " = ".$self->{display}); | ||
| 163 | ${$self->{lastq}} = "idle"; | ||
| 164 | return @r; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub bind_param | ||
| 168 | { | ||
| 169 | my $self = shift; | ||
| 170 | my ($num, $value) = @_; | ||
| 171 | $self->{values}[$num-1] = $value; | ||
| 172 | $self->{dbh}->bind_param(@_); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub fetchrow_array | ||
| 176 | { | ||
| 177 | my $self = shift; | ||
| 178 | die unless $self->{display}; | ||
| 179 | $self->output("Pre-fetch", $self->{display}); | ||
| 180 | ${$self->{lastq}} = $self->{display}; | ||
| 181 | my (@r) = $self->{dbh}->fetchrow_array(); | ||
| 182 | $self->output("fetch", resultsformat(@r)." = $self->{display}"); | ||
| 183 | ${$self->{lastq}} = "idle"; | ||
| 184 | return @r; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub fetchall_arrayref | ||
| 188 | { | ||
| 189 | my $self = shift; | ||
| 190 | die unless $self->{display}; | ||
| 191 | $self->output("Pre-fetchall_arrayref", $self->{display}); | ||
| 192 | ${$self->{lastq}} = $self->{display}; | ||
| 193 | my $r = $self->{dbh}->fetchall_arrayref(); | ||
| 194 | my $o = ''; | ||
| 195 | for my $a (@$r) { | ||
| 196 | $o .= resultsformat(@$a); | ||
| 197 | } | ||
| 198 | $self->output("fetch", "$o = $self->{display}"); | ||
| 199 | ${$self->{lastq}} = "idle"; | ||
| 200 | return $r; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | sub do | ||
| 205 | { | ||
| 206 | my $self = shift; | ||
| 207 | my ($query, undef, @values) = @_; | ||
| 208 | $self->{display} = preformat($query, @values); | ||
| 209 | $self->output("Pre-do", $self->{display}); | ||
| 210 | ${$self->{lastq}} = $self->{display}; | ||
| 211 | my $r = $self->{dbh}->do(@_); | ||
| 212 | my $dr = $r ? $r : "ERROR"; | ||
| 213 | $self->output("do", "[$dr] = $self->{display}"); | ||
| 214 | ${$self->{lastq}} = "idle"; | ||
| 215 | return $r; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub preformat | ||
| 219 | { | ||
| 220 | my ($query, @values) = @_; | ||
| 221 | $query =~ s/\n/ /g; | ||
| 222 | $query =~ s/\s\s*/ /g; | ||
| 223 | $query =~ s/^\s+//; | ||
| 224 | $query =~ s/\?$/? /; | ||
| 225 | my @q = split(/\?/, $query); | ||
| 226 | my $display = shift(@q); | ||
| 227 | while (@q && @values) { | ||
| 228 | my $v = shift(@values); | ||
| 229 | if (defined $v) { | ||
| 230 | $display .= "'$v'"; | ||
| 231 | } else { | ||
| 232 | $display .= "NULL"; | ||
| 233 | } | ||
| 234 | $display .= shift(@q); | ||
| 235 | } | ||
| 236 | $display = "<UNDEF>" unless defined $display; | ||
| 237 | $display =~ s/\s+$//; | ||
| 238 | return $display; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub resultsformat | ||
| 242 | { | ||
| 243 | my (@r) = @_; | ||
| 244 | return "[". join(", ", map { defined $_ ? "'$_'" : 'undef' } @r)."]"; | ||
| 245 | #return "[]" unless @r; | ||
| 246 | #return ("['".join("', '", @r). "']"); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub output | ||
| 250 | { | ||
| 251 | my $self = shift; | ||
| 252 | my $code = shift; | ||
| 253 | my $stuff = join('', map { defined $_ ? $_ : '<UNDEF>' } @_); | ||
| 254 | $stuff =~ s/^\s+//; | ||
| 255 | if ($ENV{OOPS_DEBUGLOG}) { | ||
| 256 | flock(DBOdebug, LOCK_EX); | ||
| 257 | print DBOdebug "\t\t $$.$self->{id} $code $stuff\n"; | ||
| 258 | flock(DBOdebug, LOCK_UN); | ||
| 259 | } else { | ||
| 260 | print DBOdebug "\t\t $$.$self->{id} $code $stuff\n"; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | sub display | ||
| 265 | { | ||
| 266 | my $self = shift; | ||
| 267 | my $code = shift; | ||
| 268 | $self->output($code, preformat(@_)); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub DESTROY | ||
| 272 | { | ||
| 273 | my $self = shift; | ||
| 274 | unless (exists $self->{parent}) { | ||
| 275 | $self->output("DESTROY"); | ||
| 276 | for my $child (@{$self->{children}}) { | ||
| 277 | next unless $child; | ||
| 278 | next unless $child->{active}; | ||
| 279 | print STDERR "ACTIVE STATEMENT: $child->{output}\n"; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | %$self = (); | ||
| 283 | } | ||
| 284 | |||
| 285 | sub disconnect | ||
| 286 | { | ||
| 287 | my $self = shift; | ||
| 288 | return unless $self->{dbh}; | ||
| 289 | delete $self->{display}; | ||
| 290 | ${$self->{lastq}} = "disconnect"; | ||
| 291 | $self->{disconnect} = longmess(); | ||
| 292 | $self->output("Disconnect"); | ||
| 293 | $self->{dbh}->disconnect(@_); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub commit | ||
| 297 | { | ||
| 298 | my $self = shift; | ||
| 299 | $self->output("Pre-Commit"); | ||
| 300 | ${$self->{lastq}} = "commit"; | ||
| 301 | my $r = $self->{dbh}->commit(@_); | ||
| 302 | my $dr = $r ? $r : "ERROR"; | ||
| 303 | ${$self->{lastq}} = "idle"; | ||
| 304 | $self->output("Commit", "[$dr]"); | ||
| 305 | return $r; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub rollback | ||
| 309 | { | ||
| 310 | my $self = shift; | ||
| 311 | $self->output("Pre-Rollback"); | ||
| 312 | my $r = $self->{dbh}->rollback(@_); | ||
| 313 | my $dr = $r ? $r : "ERROR"; | ||
| 314 | $self->output("Rollback", "[$dr]"); | ||
| 315 | return $r; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub finish | ||
| 319 | { | ||
| 320 | my $self = shift; | ||
| 321 | $self->{dbh}->finish(@_); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub err | ||
| 325 | { | ||
| 326 | my $self = shift; | ||
| 327 | $self->{dbh}->err(@_); | ||
| 328 | } | ||
| 329 | |||
| 330 | sub errstr | ||
| 331 | { | ||
| 332 | my $self = shift; | ||
| 333 | $self->{dbh}->errstr(@_); | ||
| 334 | } | ||
| 335 | |||
| 336 | sub func | ||
| 337 | { | ||
| 338 | my $self = shift; | ||
| 339 | $self->{dbh}->func(@_); | ||
| 340 | } | ||
| 341 | |||
| 342 | sub begin_work | ||
| 343 | { | ||
| 344 | my $self = shift; | ||
| 345 | $self->output("Pre-BeginWork"); | ||
| 346 | my $r = $self->{dbh}->begin_work(@_); | ||
| 347 | my $dr = $r ? $r : "ERROR"; | ||
| 348 | $self->output("BeginWork", "[$dr]"); | ||
| 349 | return $r; | ||
| 350 | } | ||
| 351 | |||
| 352 | |||
| 353 | 1; | ||
| 354 |