| 1 | #!/usr/bin/perl | ||
| 2 | # -*- perl -*- | ||
| 3 | # | ||
| 4 | # | ||
| 5 | # DBD::Proxy - DBI Proxy driver | ||
| 6 | # | ||
| 7 | # | ||
| 8 | # Copyright (c) 1997,1998 Jochen Wiedmann | ||
| 9 | # | ||
| 10 | # The DBD::Proxy module is free software; you can redistribute it and/or | ||
| 11 | # modify it under the same terms as Perl itself. In particular permission | ||
| 12 | # is granted to Tim Bunce for distributing this as a part of the DBI. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # Author: Jochen Wiedmann | ||
| 16 | # Am Eisteich 9 | ||
| 17 | # 72555 Metzingen | ||
| 18 | # Germany | ||
| 19 | # | ||
| 20 | # Email: joe@ispsoft.de | ||
| 21 | # Phone: +49 7123 14881 | ||
| 22 | # | ||
| 23 | |||
| 24 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 4 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | require DBI; | ||
| 29 | DBI->require_version(1.0201); | ||
| 30 | |||
| 31 | 0 | ▶ | use RPC::PlClient 0.2000; # XXX change to 0.2017 once it's released |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 32 | |||
| 33 | { package DBD::Proxy::RPC::PlClient; | ||
| 34 | our @ISA = qw(RPC::PlClient); | ||
| 35 | sub Call { | ||
| 36 | my $self = shift; | ||
| 37 | if ($self->{debug}) { | ||
| 38 | my ($rpcmeth, $obj, $method, @args) = @_; | ||
| 39 | no warnings; # silence undefs | ||
| 40 | Carp::carp("Server $rpcmeth $method(@args)"); | ||
| 41 | } | ||
| 42 | return $self->SUPER::Call(@_); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | package DBD::Proxy; | ||
| 48 | |||
| 49 | our $VERSION = "0.2004"; | ||
| 50 | |||
| 51 | our $drh = undef; # holds driver handle once initialised | ||
| 52 | |||
| 53 | our %ATTR = ( # common to db & st, see also %ATTR in DBD::Proxy::db & ::st | ||
| 54 | 'Warn' => 'local', | ||
| 55 | 'Active' => 'local', | ||
| 56 | 'Kids' => 'local', | ||
| 57 | 'CachedKids' => 'local', | ||
| 58 | 'PrintError' => 'local', | ||
| 59 | 'RaiseError' => 'local', | ||
| 60 | 'HandleError' => 'local', | ||
| 61 | 'TraceLevel' => 'cached', | ||
| 62 | 'CompatMode' => 'local', | ||
| 63 | ); | ||
| 64 | |||
| 65 | sub driver ($$) { | ||
| 66 | if (!$drh) { | ||
| 67 | my($class, $attr) = @_; | ||
| 68 | |||
| 69 | $class .= "::dr"; | ||
| 70 | |||
| 71 | $drh = DBI::_new_drh($class, { | ||
| 72 | 'Name' => 'Proxy', | ||
| 73 | 'Version' => $VERSION, | ||
| 74 | 'Attribution' => 'DBD::Proxy by Jochen Wiedmann', | ||
| 75 | }); | ||
| 76 | $drh->STORE(CompatMode => 1); # disable DBI dispatcher attribute cache (for FETCH) | ||
| 77 | } | ||
| 78 | $drh; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub CLONE { | ||
| 82 | undef $drh; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub proxy_set_err { | ||
| 86 | my ($h,$errmsg) = @_; | ||
| 87 | my ($err, $state) = ($errmsg =~ s/ \[err=(.*?),state=(.*?)\]//) | ||
| 88 | ? ($1, $2) : (1, ' ' x 5); | ||
| 89 | return $h->set_err($err, $errmsg, $state); | ||
| 90 | } | ||
| 91 | |||
| 92 | package DBD::Proxy::dr; # ====== DRIVER ====== | ||
| 93 | |||
| 94 | our $imp_data_size = 0; | ||
| 95 | |||
| 96 | sub connect ($$;$$) { | ||
| 97 | my($drh, $dsn, $user, $auth, $attr)= @_; | ||
| 98 | my($dsnOrig) = $dsn; | ||
| 99 | |||
| 100 | my %attr = %$attr; | ||
| 101 | my ($var, $val); | ||
| 102 | while (length($dsn)) { | ||
| 103 | if ($dsn =~ /^dsn=(.*)/) { | ||
| 104 | $attr{'dsn'} = $1; | ||
| 105 | last; | ||
| 106 | } | ||
| 107 | if ($dsn =~ /^(.*?);(.*)/) { | ||
| 108 | $var = $1; | ||
| 109 | $dsn = $2; | ||
| 110 | } else { | ||
| 111 | $var = $dsn; | ||
| 112 | $dsn = ''; | ||
| 113 | } | ||
| 114 | if ($var =~ /^(.*?)=(.*)/) { | ||
| 115 | $var = $1; | ||
| 116 | $val = $2; | ||
| 117 | $attr{$var} = $val; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | my $err = ''; | ||
| 122 | if (!defined($attr{'hostname'})) { $err .= " Missing hostname."; } | ||
| 123 | if (!defined($attr{'port'})) { $err .= " Missing port."; } | ||
| 124 | if (!defined($attr{'dsn'})) { $err .= " Missing remote dsn."; } | ||
| 125 | |||
| 126 | # Create a cipher object, if requested | ||
| 127 | my $cipherRef = undef; | ||
| 128 | if ($attr{'cipher'}) { | ||
| 129 | $cipherRef = eval { $attr{'cipher'}->new(pack('H*', | ||
| 130 | $attr{'key'})) }; | ||
| 131 | if ($@) { $err .= " Cannot create cipher object: $@."; } | ||
| 132 | } | ||
| 133 | my $userCipherRef = undef; | ||
| 134 | if ($attr{'userkey'}) { | ||
| 135 | my $cipher = $attr{'usercipher'} || $attr{'cipher'}; | ||
| 136 | $userCipherRef = eval { $cipher->new(pack('H*', $attr{'userkey'})) }; | ||
| 137 | if ($@) { $err .= " Cannot create usercipher object: $@."; } | ||
| 138 | } | ||
| 139 | |||
| 140 | return DBD::Proxy::proxy_set_err($drh, $err) if $err; # Returns undef | ||
| 141 | |||
| 142 | my %client_opts = ( | ||
| 143 | 'peeraddr' => $attr{'hostname'}, | ||
| 144 | 'peerport' => $attr{'port'}, | ||
| 145 | 'socket_proto' => 'tcp', | ||
| 146 | 'application' => $attr{dsn}, | ||
| 147 | 'user' => $user || '', | ||
| 148 | 'password' => $auth || '', | ||
| 149 | 'version' => $DBD::Proxy::VERSION, | ||
| 150 | 'cipher' => $cipherRef, | ||
| 151 | 'debug' => $attr{debug} || 0, | ||
| 152 | 'timeout' => $attr{timeout} || undef, | ||
| 153 | 'logfile' => $attr{logfile} || undef | ||
| 154 | ); | ||
| 155 | # Options starting with 'proxy_rpc_' are forwarded to the RPC layer after | ||
| 156 | # stripping the prefix. | ||
| 157 | while (my($var,$val) = each %attr) { | ||
| 158 | if ($var =~ s/^proxy_rpc_//) { | ||
| 159 | $client_opts{$var} = $val; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | # Create an RPC::PlClient object. | ||
| 163 | my($client, $msg) = eval { DBD::Proxy::RPC::PlClient->new(%client_opts) }; | ||
| 164 | |||
| 165 | return DBD::Proxy::proxy_set_err($drh, "Cannot log in to DBI::ProxyServer: $@") | ||
| 166 | if $@; # Returns undef | ||
| 167 | return DBD::Proxy::proxy_set_err($drh, "Constructor didn't return a handle: $msg") | ||
| 168 | unless ($msg =~ /^((?:\w+|\:\:)+)=(\w+)/); # Returns undef | ||
| 169 | |||
| 170 | $msg = RPC::PlClient::Object->new($1, $client, $msg); | ||
| 171 | |||
| 172 | my $max_proto_ver; | ||
| 173 | my ($server_ver_str) = eval { $client->Call('Version') }; | ||
| 174 | if ( $@ ) { | ||
| 175 | # Server denies call, assume legacy protocol. | ||
| 176 | $max_proto_ver = 1; | ||
| 177 | } else { | ||
| 178 | # Parse proxy server version. | ||
| 179 | my ($server_ver_num) = $server_ver_str =~ /^DBI::ProxyServer\s+([\d\.]+)/; | ||
| 180 | $max_proto_ver = $server_ver_num >= 0.3 ? 2 : 1; | ||
| 181 | } | ||
| 182 | my $req_proto_ver; | ||
| 183 | if ( exists $attr{proxy_lazy_prepare} ) { | ||
| 184 | $req_proto_ver = ($attr{proxy_lazy_prepare} == 0) ? 2 : 1; | ||
| 185 | return DBD::Proxy::proxy_set_err($drh, | ||
| 186 | "DBI::ProxyServer does not support synchronous statement preparation.") | ||
| 187 | if $max_proto_ver < $req_proto_ver; | ||
| 188 | } | ||
| 189 | |||
| 190 | # Switch to user specific encryption mode, if desired | ||
| 191 | if ($userCipherRef) { | ||
| 192 | $client->{'cipher'} = $userCipherRef; | ||
| 193 | } | ||
| 194 | |||
| 195 | # create a 'blank' dbh | ||
| 196 | my $this = DBI::_new_dbh($drh, { | ||
| 197 | 'Name' => $dsnOrig, | ||
| 198 | 'proxy_dbh' => $msg, | ||
| 199 | 'proxy_client' => $client, | ||
| 200 | 'RowCacheSize' => $attr{'RowCacheSize'} || 20, | ||
| 201 | 'proxy_proto_ver' => $req_proto_ver || 1 | ||
| 202 | }); | ||
| 203 | |||
| 204 | foreach $var (keys %attr) { | ||
| 205 | if ($var =~ /proxy_/) { | ||
| 206 | $this->{$var} = $attr{$var}; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | $this->SUPER::STORE('Active' => 1); | ||
| 210 | |||
| 211 | $this; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | sub DESTROY { undef } | ||
| 216 | |||
| 217 | |||
| 218 | package DBD::Proxy::db; # ====== DATABASE ====== | ||
| 219 | |||
| 220 | our $imp_data_size = 0; | ||
| 221 | |||
| 222 | # XXX probably many more methods need to be added here | ||
| 223 | # in order to trigger our AUTOLOAD to redirect them to the server. | ||
| 224 | # (Unless the sub is declared it's bypassed by perl method lookup.) | ||
| 225 | # See notes in ToDo about method metadata | ||
| 226 | # The question is whether to add all the methods in %DBI::DBI_methods | ||
| 227 | # to the corresponding classes (::db, ::st etc) | ||
| 228 | # Also need to consider methods that, if proxied, would change the server state | ||
| 229 | # in a way that might not be visible on the client, ie begin_work -> AutoCommit. | ||
| 230 | |||
| 231 | sub commit; | ||
| 232 | sub rollback; | ||
| 233 | sub ping; | ||
| 234 | |||
| 235 | our $AUTOLOAD; | ||
| 236 | |||
| 237 | # inherited: STORE / FETCH against this class. | ||
| 238 | # local: STORE / FETCH against parent class. | ||
| 239 | # cached: STORE to remote and local objects, FETCH from local. | ||
| 240 | # remote: STORE / FETCH against remote object only (default). | ||
| 241 | # | ||
| 242 | # Note: Attribute names starting with 'proxy_' always treated as 'inherited'. | ||
| 243 | # | ||
| 244 | our %ATTR = ( # see also %ATTR in DBD::Proxy::st | ||
| 245 | %DBD::Proxy::ATTR, | ||
| 246 | RowCacheSize => 'inherited', | ||
| 247 | #AutoCommit => 'cached', | ||
| 248 | 'FetchHashKeyName' => 'cached', | ||
| 249 | Statement => 'local', | ||
| 250 | Driver => 'local', | ||
| 251 | dbi_connect_closure => 'local', | ||
| 252 | Username => 'local', | ||
| 253 | ); | ||
| 254 | |||
| 255 | sub AUTOLOAD { | ||
| 256 | my $method = $AUTOLOAD; | ||
| 257 | $method =~ s/(.*::(.*)):://; | ||
| 258 | my $class = $1; | ||
| 259 | my $type = $2; | ||
| 260 | #warn "AUTOLOAD of $method (class=$class, type=$type)"; | ||
| 261 | my %expand = ( | ||
| 262 | 'method' => $method, | ||
| 263 | 'class' => $class, | ||
| 264 | 'type' => $type, | ||
| 265 | 'call' => "$method(\@_)", | ||
| 266 | # XXX was trying to be smart but was tripping up over the DBI's own | ||
| 267 | # smartness. Disabled, but left here in case there are issues. | ||
| 268 | # 'call' => (UNIVERSAL::can("DBI::_::$type", $method)) ? "$method(\@_)" : "func(\@_, '$method')", | ||
| 269 | ); | ||
| 270 | |||
| 271 | my $method_code = q{ | ||
| 272 | package ~class~; | ||
| 273 | sub ~method~ { | ||
| 274 | my $h = shift; | ||
| 275 | local $@; | ||
| 276 | my @result = wantarray | ||
| 277 | ? eval { $h->{'proxy_~type~h'}->~call~ } | ||
| 278 | : eval { scalar $h->{'proxy_~type~h'}->~call~ }; | ||
| 279 | return DBD::Proxy::proxy_set_err($h, $@) if $@; | ||
| 280 | return wantarray ? @result : $result[0]; | ||
| 281 | } | ||
| 282 | }; | ||
| 283 | $method_code =~ s/\~(\w+)\~/$expand{$1}/eg; | ||
| 284 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 285 | my $err = do { local $@; eval $method_code.2; $@ }; | ||
| 286 | die $err if $err; | ||
| 287 | goto &$AUTOLOAD; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub DESTROY { | ||
| 291 | my $dbh = shift; | ||
| 292 | local $@ if $@; # protect $@ | ||
| 293 | $dbh->disconnect if $dbh->SUPER::FETCH('Active'); | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | sub connected { } # client-side not server-side, RT#75868 | ||
| 298 | |||
| 299 | sub disconnect ($) { | ||
| 300 | my ($dbh) = @_; | ||
| 301 | |||
| 302 | # Sadly the Proxy too-often disagrees with the backend database | ||
| 303 | # on the subject of 'Active'. In the short term, I'd like the | ||
| 304 | # Proxy to ease up and let me decide when it's proper to go over | ||
| 305 | # the wire. This ultimately applies to finish() as well. | ||
| 306 | #return unless $dbh->SUPER::FETCH('Active'); | ||
| 307 | |||
| 308 | # Drop database connection at remote end | ||
| 309 | my $rdbh = $dbh->{'proxy_dbh'}; | ||
| 310 | if ( $rdbh ) { | ||
| 311 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 312 | local $@; | ||
| 313 | eval { $rdbh->disconnect() } ; | ||
| 314 | DBD::Proxy::proxy_set_err($dbh, $@) if $@; | ||
| 315 | } | ||
| 316 | |||
| 317 | # Close TCP connect to remote | ||
| 318 | # XXX possibly best left till DESTROY? Add a config attribute to choose? | ||
| 319 | #$dbh->{proxy_client}->Disconnect(); # Disconnect method requires newer PlRPC module | ||
| 320 | $dbh->{proxy_client}->{socket} = undef; # hack | ||
| 321 | |||
| 322 | $dbh->SUPER::STORE('Active' => 0); | ||
| 323 | 1; | ||
| 324 | } | ||
| 325 | |||
| 326 | |||
| 327 | sub STORE ($$$) { | ||
| 328 | my($dbh, $attr, $val) = @_; | ||
| 329 | my $type = $ATTR{$attr} || 'remote'; | ||
| 330 | |||
| 331 | if ($attr eq 'TraceLevel') { | ||
| 332 | warn("TraceLevel $val"); | ||
| 333 | my $pc = $dbh->{proxy_client} || die; | ||
| 334 | $pc->{logfile} ||= 1; # XXX hack | ||
| 335 | $pc->{debug} = ($val && $val >= 4); | ||
| 336 | $pc->Debug("$pc debug enabled") if $pc->{debug}; | ||
| 337 | } | ||
| 338 | |||
| 339 | if ($attr =~ /^proxy_/ || $type eq 'inherited') { | ||
| 340 | $dbh->{$attr} = $val; | ||
| 341 | return 1; | ||
| 342 | } | ||
| 343 | |||
| 344 | if ($type eq 'remote' || $type eq 'cached') { | ||
| 345 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 346 | local $@; | ||
| 347 | my $result = eval { $dbh->{'proxy_dbh'}->STORE($attr => $val) }; | ||
| 348 | return DBD::Proxy::proxy_set_err($dbh, $@) if $@; # returns undef | ||
| 349 | $dbh->SUPER::STORE($attr => $val) if $type eq 'cached'; | ||
| 350 | return $result; | ||
| 351 | } | ||
| 352 | return $dbh->SUPER::STORE($attr => $val); | ||
| 353 | } | ||
| 354 | |||
| 355 | sub FETCH ($$) { | ||
| 356 | my($dbh, $attr) = @_; | ||
| 357 | # we only get here for cached attribute values if the handle is in CompatMode | ||
| 358 | # otherwise the DBI dispatcher handles the FETCH itself from the attribute cache. | ||
| 359 | my $type = $ATTR{$attr} || 'remote'; | ||
| 360 | |||
| 361 | if ($attr =~ /^proxy_/ || $type eq 'inherited' || $type eq 'cached') { | ||
| 362 | return $dbh->{$attr}; | ||
| 363 | } | ||
| 364 | |||
| 365 | return $dbh->SUPER::FETCH($attr) unless $type eq 'remote'; | ||
| 366 | |||
| 367 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 368 | local $@; | ||
| 369 | my $result = eval { $dbh->{'proxy_dbh'}->FETCH($attr) }; | ||
| 370 | return DBD::Proxy::proxy_set_err($dbh, $@) if $@; | ||
| 371 | return $result; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub prepare ($$;$) { | ||
| 375 | my($dbh, $stmt, $attr) = @_; | ||
| 376 | my $sth = DBI::_new_sth($dbh, { | ||
| 377 | 'Statement' => $stmt, | ||
| 378 | 'proxy_attr' => $attr, | ||
| 379 | 'proxy_cache_only' => 0, | ||
| 380 | 'proxy_params' => [], | ||
| 381 | } | ||
| 382 | ); | ||
| 383 | my $proto_ver = $dbh->{'proxy_proto_ver'}; | ||
| 384 | if ( $proto_ver > 1 ) { | ||
| 385 | $sth->{'proxy_attr_cache'} = {cache_filled => 0}; | ||
| 386 | my $rdbh = $dbh->{'proxy_dbh'}; | ||
| 387 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 388 | local $@; | ||
| 389 | my $rsth = eval { $rdbh->prepare($sth->{'Statement'}, $sth->{'proxy_attr'}, undef, $proto_ver) }; | ||
| 390 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 391 | return DBD::Proxy::proxy_set_err($sth, "Constructor didn't return a handle: $rsth") | ||
| 392 | unless ($rsth =~ /^((?:\w+|\:\:)+)=(\w+)/); | ||
| 393 | |||
| 394 | my $client = $dbh->{'proxy_client'}; | ||
| 395 | $rsth = RPC::PlClient::Object->new($1, $client, $rsth); | ||
| 396 | |||
| 397 | $sth->{'proxy_sth'} = $rsth; | ||
| 398 | # If statement is a positioned update we do not want any readahead. | ||
| 399 | $sth->{'RowCacheSize'} = 1 if $stmt =~ /\bfor\s+update\b/i; | ||
| 400 | # Since resources are used by prepared remote handle, mark us active. | ||
| 401 | $sth->SUPER::STORE(Active => 1); | ||
| 402 | } | ||
| 403 | $sth; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub quote { | ||
| 407 | my $dbh = shift; | ||
| 408 | my $proxy_quote = $dbh->{proxy_quote} || 'remote'; | ||
| 409 | |||
| 410 | return $dbh->SUPER::quote(@_) | ||
| 411 | if $proxy_quote eq 'local' && @_ == 1; | ||
| 412 | |||
| 413 | # For the common case of only a single argument | ||
| 414 | # (no $data_type) we could learn and cache the behaviour. | ||
| 415 | # Or we could probe the driver with a few test cases. | ||
| 416 | # Or we could add a way to ask the DBI::ProxyServer | ||
| 417 | # if $dbh->can('quote') == \&DBI::_::db::quote. | ||
| 418 | # Tim | ||
| 419 | # | ||
| 420 | # Sounds all *very* smart to me. I'd rather suggest to | ||
| 421 | # implement some of the typical quote possibilities | ||
| 422 | # and let the user set | ||
| 423 | # $dbh->{'proxy_quote'} = 'backslash_escaped'; | ||
| 424 | # for example. | ||
| 425 | # Jochen | ||
| 426 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 427 | local $@; | ||
| 428 | my $result = eval { $dbh->{'proxy_dbh'}->quote(@_) }; | ||
| 429 | return DBD::Proxy::proxy_set_err($dbh, $@) if $@; | ||
| 430 | return $result; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub table_info { | ||
| 434 | my $dbh = shift; | ||
| 435 | my $rdbh = $dbh->{'proxy_dbh'}; | ||
| 436 | #warn "table_info(@_)"; | ||
| 437 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 438 | local $@; | ||
| 439 | my($numFields, $names, $types, @rows) = eval { $rdbh->table_info(@_) }; | ||
| 440 | return DBD::Proxy::proxy_set_err($dbh, $@) if $@; | ||
| 441 | my ($sth, $inner) = DBI::_new_sth($dbh, { | ||
| 442 | 'Statement' => "SHOW TABLES", | ||
| 443 | 'proxy_params' => [], | ||
| 444 | 'proxy_data' => \@rows, | ||
| 445 | 'proxy_attr_cache' => { | ||
| 446 | 'NUM_OF_PARAMS' => 0, | ||
| 447 | 'NUM_OF_FIELDS' => $numFields, | ||
| 448 | 'NAME' => $names, | ||
| 449 | 'TYPE' => $types, | ||
| 450 | 'cache_filled' => 1 | ||
| 451 | }, | ||
| 452 | 'proxy_cache_only' => 1, | ||
| 453 | }); | ||
| 454 | $sth->SUPER::STORE('NUM_OF_FIELDS' => $numFields); | ||
| 455 | $inner->{NAME} = $names; | ||
| 456 | $inner->{TYPE} = $types; | ||
| 457 | $sth->SUPER::STORE('Active' => 1); # already execute()'d | ||
| 458 | $sth->{'proxy_rows'} = @rows; | ||
| 459 | return $sth; | ||
| 460 | } | ||
| 461 | |||
| 462 | sub tables { | ||
| 463 | my $dbh = shift; | ||
| 464 | #warn "tables(@_)"; | ||
| 465 | return $dbh->SUPER::tables(@_); | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | sub type_info_all { | ||
| 470 | my $dbh = shift; | ||
| 471 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 472 | local $@; | ||
| 473 | my $result = eval { $dbh->{'proxy_dbh'}->type_info_all(@_) }; | ||
| 474 | return DBD::Proxy::proxy_set_err($dbh, $@) if $@; | ||
| 475 | return $result; | ||
| 476 | } | ||
| 477 | |||
| 478 | |||
| 479 | package DBD::Proxy::st; # ====== STATEMENT ====== | ||
| 480 | |||
| 481 | our $imp_data_size = 0; | ||
| 482 | |||
| 483 | # inherited: STORE to current object. FETCH from current if exists, else call up | ||
| 484 | # to the (proxy) database object. | ||
| 485 | # local: STORE / FETCH against parent class. | ||
| 486 | # cache_only: STORE noop (read-only). FETCH from private_* if exists, else call | ||
| 487 | # remote and cache the result. | ||
| 488 | # remote: STORE / FETCH against remote object only (default). | ||
| 489 | # | ||
| 490 | # Note: Attribute names starting with 'proxy_' always treated as 'inherited'. | ||
| 491 | # | ||
| 492 | our %ATTR = ( # see also %ATTR in DBD::Proxy::db | ||
| 493 | %DBD::Proxy::ATTR, | ||
| 494 | 'Database' => 'local', | ||
| 495 | 'RowsInCache' => 'local', | ||
| 496 | 'RowCacheSize' => 'inherited', | ||
| 497 | 'NULLABLE' => 'cache_only', | ||
| 498 | 'NAME' => 'cache_only', | ||
| 499 | 'TYPE' => 'cache_only', | ||
| 500 | 'PRECISION' => 'cache_only', | ||
| 501 | 'SCALE' => 'cache_only', | ||
| 502 | 'NUM_OF_FIELDS' => 'cache_only', | ||
| 503 | 'NUM_OF_PARAMS' => 'cache_only' | ||
| 504 | ); | ||
| 505 | |||
| 506 | *AUTOLOAD = \&DBD::Proxy::db::AUTOLOAD; | ||
| 507 | |||
| 508 | sub execute ($@) { | ||
| 509 | my $sth = shift; | ||
| 510 | my $params = @_ ? \@_ : $sth->{'proxy_params'}; | ||
| 511 | |||
| 512 | # new execute, so delete any cached rows from previous execute | ||
| 513 | undef $sth->{'proxy_data'}; | ||
| 514 | undef $sth->{'proxy_rows'}; | ||
| 515 | |||
| 516 | my $rsth = $sth->{proxy_sth}; | ||
| 517 | my $dbh = $sth->FETCH('Database'); | ||
| 518 | my $proto_ver = $dbh->{proxy_proto_ver}; | ||
| 519 | |||
| 520 | my ($numRows, @outData); | ||
| 521 | |||
| 522 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 523 | local $@; | ||
| 524 | if ( $proto_ver > 1 ) { | ||
| 525 | ($numRows, @outData) = eval { $rsth->execute($params, $proto_ver) }; | ||
| 526 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 527 | |||
| 528 | # Attributes passed back only on the first execute() of a statement. | ||
| 529 | unless ($sth->{proxy_attr_cache}->{cache_filled}) { | ||
| 530 | my ($numFields, $numParams, $names, $types) = splice(@outData, 0, 4); | ||
| 531 | $sth->{'proxy_attr_cache'} = { | ||
| 532 | 'NUM_OF_FIELDS' => $numFields, | ||
| 533 | 'NUM_OF_PARAMS' => $numParams, | ||
| 534 | 'NAME' => $names, | ||
| 535 | 'cache_filled' => 1 | ||
| 536 | }; | ||
| 537 | $sth->SUPER::STORE('NUM_OF_FIELDS' => $numFields); | ||
| 538 | $sth->SUPER::STORE('NUM_OF_PARAMS' => $numParams); | ||
| 539 | } | ||
| 540 | |||
| 541 | } | ||
| 542 | else { | ||
| 543 | if ($rsth) { | ||
| 544 | ($numRows, @outData) = eval { $rsth->execute($params, $proto_ver) }; | ||
| 545 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 546 | |||
| 547 | } | ||
| 548 | else { | ||
| 549 | my $rdbh = $dbh->{'proxy_dbh'}; | ||
| 550 | |||
| 551 | # Legacy prepare is actually prepare + first execute on the server. | ||
| 552 | ($rsth, @outData) = | ||
| 553 | eval { $rdbh->prepare($sth->{'Statement'}, | ||
| 554 | $sth->{'proxy_attr'}, $params, $proto_ver) }; | ||
| 555 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 556 | return DBD::Proxy::proxy_set_err($sth, "Constructor didn't return a handle: $rsth") | ||
| 557 | unless ($rsth =~ /^((?:\w+|\:\:)+)=(\w+)/); | ||
| 558 | |||
| 559 | my $client = $dbh->{'proxy_client'}; | ||
| 560 | $rsth = RPC::PlClient::Object->new($1, $client, $rsth); | ||
| 561 | |||
| 562 | my ($numFields, $numParams, $names, $types) = splice(@outData, 0, 4); | ||
| 563 | $sth->{'proxy_sth'} = $rsth; | ||
| 564 | $sth->{'proxy_attr_cache'} = { | ||
| 565 | 'NUM_OF_FIELDS' => $numFields, | ||
| 566 | 'NUM_OF_PARAMS' => $numParams, | ||
| 567 | 'NAME' => $names | ||
| 568 | }; | ||
| 569 | $sth->SUPER::STORE('NUM_OF_FIELDS' => $numFields); | ||
| 570 | $sth->SUPER::STORE('NUM_OF_PARAMS' => $numParams); | ||
| 571 | $numRows = shift @outData; | ||
| 572 | } | ||
| 573 | } | ||
| 574 | # Always condition active flag. | ||
| 575 | $sth->SUPER::STORE('Active' => 1) if $sth->FETCH('NUM_OF_FIELDS'); # is SELECT | ||
| 576 | $sth->{'proxy_rows'} = $numRows; | ||
| 577 | # Any remaining items are output params. | ||
| 578 | if (@outData) { | ||
| 579 | foreach my $p (@$params) { | ||
| 580 | if (ref($p->[0])) { | ||
| 581 | my $ref = shift @outData; | ||
| 582 | ${$p->[0]} = $$ref; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | $sth->{'proxy_rows'} || '0E0'; | ||
| 588 | } | ||
| 589 | |||
| 590 | sub fetch ($) { | ||
| 591 | my $sth = shift; | ||
| 592 | |||
| 593 | my $data = $sth->{'proxy_data'}; | ||
| 594 | |||
| 595 | $sth->{'proxy_rows'} //= 0; | ||
| 596 | |||
| 597 | if(!$data || !@$data) { | ||
| 598 | return undef unless $sth->SUPER::FETCH('Active'); | ||
| 599 | |||
| 600 | my $rsth = $sth->{'proxy_sth'}; | ||
| 601 | if (!$rsth) { | ||
| 602 | die "Attempt to fetch row without execute"; | ||
| 603 | } | ||
| 604 | my $num_rows = $sth->FETCH('RowCacheSize') || 20; | ||
| 605 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 606 | local $@; | ||
| 607 | my @rows = eval { $rsth->fetch($num_rows) }; | ||
| 608 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 609 | unless (@rows == $num_rows) { | ||
| 610 | undef $sth->{'proxy_data'}; | ||
| 611 | # server side has already called finish | ||
| 612 | $sth->SUPER::STORE(Active => 0); | ||
| 613 | } | ||
| 614 | return undef unless @rows; | ||
| 615 | $sth->{'proxy_data'} = $data = [@rows]; | ||
| 616 | } | ||
| 617 | my $row = shift @$data; | ||
| 618 | |||
| 619 | $sth->SUPER::STORE(Active => 0) if ( $sth->{proxy_cache_only} and !@$data ); | ||
| 620 | $sth->{'proxy_rows'}++; | ||
| 621 | return $sth->_set_fbav($row); | ||
| 622 | } | ||
| 623 | *fetchrow_arrayref = \&fetch; | ||
| 624 | |||
| 625 | sub rows ($) { | ||
| 626 | my $rows = shift->{'proxy_rows'}; | ||
| 627 | return ($rows // -1); | ||
| 628 | } | ||
| 629 | |||
| 630 | sub finish ($) { | ||
| 631 | my($sth) = @_; | ||
| 632 | return 1 unless $sth->SUPER::FETCH('Active'); | ||
| 633 | my $rsth = $sth->{'proxy_sth'}; | ||
| 634 | $sth->SUPER::STORE('Active' => 0); | ||
| 635 | return 0 unless $rsth; # Something's out of sync | ||
| 636 | my $no_finish = exists($sth->{'proxy_no_finish'}) | ||
| 637 | ? $sth->{'proxy_no_finish'} | ||
| 638 | : $sth->FETCH('Database')->{'proxy_no_finish'}; | ||
| 639 | unless ($no_finish) { | ||
| 640 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 641 | local $@; | ||
| 642 | my $result = eval { $rsth->finish() }; | ||
| 643 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 644 | return $result; | ||
| 645 | } | ||
| 646 | 1; | ||
| 647 | } | ||
| 648 | |||
| 649 | sub STORE ($$$) { | ||
| 650 | my($sth, $attr, $val) = @_; | ||
| 651 | my $type = $ATTR{$attr} || 'remote'; | ||
| 652 | |||
| 653 | if ($attr =~ /^proxy_/ || $type eq 'inherited') { | ||
| 654 | $sth->{$attr} = $val; | ||
| 655 | return 1; | ||
| 656 | } | ||
| 657 | |||
| 658 | if ($type eq 'cache_only') { | ||
| 659 | return 0; | ||
| 660 | } | ||
| 661 | |||
| 662 | if ($type eq 'remote' || $type eq 'cached') { | ||
| 663 | my $rsth = $sth->{'proxy_sth'} or return undef; | ||
| 664 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 665 | local $@; | ||
| 666 | my $result = eval { $rsth->STORE($attr => $val) }; | ||
| 667 | return DBD::Proxy::proxy_set_err($sth, $@) if ($@); | ||
| 668 | return $result if $type eq 'remote'; # else fall through to cache locally | ||
| 669 | } | ||
| 670 | return $sth->SUPER::STORE($attr => $val); | ||
| 671 | } | ||
| 672 | |||
| 673 | sub FETCH ($$) { | ||
| 674 | my($sth, $attr) = @_; | ||
| 675 | |||
| 676 | if ($attr =~ /^proxy_/) { | ||
| 677 | return $sth->{$attr}; | ||
| 678 | } | ||
| 679 | |||
| 680 | my $type = $ATTR{$attr} || 'remote'; | ||
| 681 | if ($type eq 'inherited') { | ||
| 682 | if (exists($sth->{$attr})) { | ||
| 683 | return $sth->{$attr}; | ||
| 684 | } | ||
| 685 | return $sth->FETCH('Database')->{$attr}; | ||
| 686 | } | ||
| 687 | |||
| 688 | if ($type eq 'cache_only' && | ||
| 689 | exists($sth->{'proxy_attr_cache'}->{$attr})) { | ||
| 690 | return $sth->{'proxy_attr_cache'}->{$attr}; | ||
| 691 | } | ||
| 692 | |||
| 693 | if ($type ne 'local') { | ||
| 694 | my $rsth = $sth->{'proxy_sth'} or return undef; | ||
| 695 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 696 | local $@; | ||
| 697 | my $result = eval { $rsth->FETCH($attr) }; | ||
| 698 | return DBD::Proxy::proxy_set_err($sth, $@) if $@; | ||
| 699 | return $result; | ||
| 700 | } | ||
| 701 | elsif ($attr eq 'RowsInCache') { | ||
| 702 | my $data = $sth->{'proxy_data'}; | ||
| 703 | $data ? @$data : 0; | ||
| 704 | } | ||
| 705 | else { | ||
| 706 | $sth->SUPER::FETCH($attr); | ||
| 707 | } | ||
| 708 | } | ||
| 709 | |||
| 710 | sub bind_param ($$$@) { | ||
| 711 | my $sth = shift; my $param = shift; | ||
| 712 | $sth->{'proxy_params'}->[$param-1] = [@_]; | ||
| 713 | } | ||
| 714 | *bind_param_inout = \&bind_param; | ||
| 715 | |||
| 716 | sub DESTROY { | ||
| 717 | my $sth = shift; | ||
| 718 | $sth->finish if $sth->SUPER::FETCH('Active'); | ||
| 719 | } | ||
| 720 | |||
| 721 | |||
| 722 | 1; | ||
| 723 | |||
| 724 | |||
| 725 | __END__ | ||