| 1 | |||
| 2 | # | ||
| 3 | # base class for database connectors | ||
| 4 | # | ||
| 5 | |||
| 6 | BEGIN { | ||
| 7 | Filter::Util::Call::filter_add(\&OOPS::SelfFilter::filter) | ||
| 8 | unless $OOPS::SelfFilter::defeat; | ||
| 9 | } | ||
| 10 | |||
| 11 | package OOPS::DBO; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use Carp; | ||
| 16 | use Scalar::Util qw(weaken); | ||
| 17 | require OOPS::DBOdebug; | ||
| 18 | require Exporter; | ||
| 19 | |||
| 20 | our @ISA = qw(Exporter); | ||
| 21 | our @EXPORT = qw(dbiconnect dboconnect $pmatch); | ||
| 22 | |||
| 23 | our $backends = qr/(?:mysql|pg|sqlite|sqlite2)/i; | ||
| 24 | |||
| 25 | my %loaded; | ||
| 26 | my %deadlock_rx; | ||
| 27 | |||
| 28 | our $pmatch = qr/ | ||
| 29 | (?: | ||
| 30 | [^()] | ||
| 31 | | | ||
| 32 | \( | ||
| 33 | (?: | ||
| 34 | [^()] | ||
| 35 | | | ||
| 36 | \( | ||
| 37 | (?: | ||
| 38 | [^()] | ||
| 39 | | | ||
| 40 | \( | ||
| 41 | (?: | ||
| 42 | [^()] | ||
| 43 | | | ||
| 44 | \( | ||
| 45 | .*? | ||
| 46 | \) | ||
| 47 | )*? | ||
| 48 | \) | ||
| 49 | )*? | ||
| 50 | \) | ||
| 51 | )*? | ||
| 52 | \) | ||
| 53 | )*? | ||
| 54 | /xs; | ||
| 55 | |||
| 56 | |||
| 57 | sub new | ||
| 58 | { | ||
| 59 | my ($pkg, %args) = @_; | ||
| 60 | return bless \%args; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub initial_query_set | ||
| 64 | { | ||
| 65 | return ''; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub learn_queries | ||
| 69 | { | ||
| 70 | my ($dbo, $Q) = @_; | ||
| 71 | while ($Q =~ /\G\t\t([a-z]\w*):((?:\s+\d+)*)\s*(#.*)?\n/gc) { | ||
| 72 | my ($qn, $binary_list, $comment) = ($1, $2); | ||
| 73 | $dbo->{queries}{$qn} = ""; | ||
| 74 | while ($Q =~ /\G\t\t\t(.*)\n/gc) { | ||
| 75 | $dbo->{queries}{$qn} .= $1."\n"; | ||
| 76 | } | ||
| 77 | $dbo->{binary_q_list}{$qn} = $binary_list; | ||
| 78 | $dbo->{debug_q}{$qn} = $comment; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub dbiconnect | ||
| 83 | { | ||
| 84 | my ($pkg, %a) = @_; | ||
| 85 | my $args = \%a; | ||
| 86 | if (ref($pkg) && ! %a) { | ||
| 87 | $args = $pkg->{args} || $pkg; | ||
| 88 | } | ||
| 89 | my $database = $args->{dbi_dsn} || $args->{DBI_DSN} || $args->{database}; | ||
| 90 | my $user = $args->{user} || $args->{username} || $args->{USER} || $args->{USERNAME}; | ||
| 91 | my $password = $args->{pass} || $args->{password} || $args->{PASS} || $args->{PASSWORD}; | ||
| 92 | my $prefix = $args->{table_prefix} || $args->{TABLE_PREFIX} || $ENV{OOPS_PREFIX} || ''; | ||
| 93 | $user = $user || $ENV{OOPS_USER} || $ENV{DBI_USER}; | ||
| 94 | $password = $password || $ENV{OOPS_PASS} || $ENV{DBI_PASS}; | ||
| 95 | |||
| 96 | if (! defined($database)) { | ||
| 97 | if (defined($ENV{OOPS_DSN})) { | ||
| 98 | $database = $ENV{OOPS_DSN}; | ||
| 99 | } elsif (defined($ENV{DBI_DSN})) { | ||
| 100 | $database = $ENV{DBI_DSN} | ||
| 101 | } elsif (defined($ENV{OOPS_DRIVER})) { | ||
| 102 | $database = "dbi::$ENV{OOPS_DRIVER}"; | ||
| 103 | } elsif (defined($ENV{DBI_DRIVER})) { | ||
| 104 | $database = "dbi::$ENV{DBI_DRIVER}"; | ||
| 105 | } else { | ||
| 106 | die "no database specified"; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | die "only mysql, PostgreSQL & SQLite supported" | ||
| 110 | unless $database =~ /^dbi:($backends)\b/i; | ||
| 111 | die "no database specified" unless $database; | ||
| 112 | my $dbms = "\L$1"; | ||
| 113 | |||
| 114 | my $dbh; | ||
| 115 | unless ($args->{no_dbh}) { | ||
| 116 | my %a = ( | ||
| 117 | Taint => 0, | ||
| 118 | PrintError => 0, | ||
| 119 | AutoCommit => 0, | ||
| 120 | RaiseError => 1, | ||
| 121 | HandleError => sub { confess(shift) }, | ||
| 122 | ); | ||
| 123 | $dbh = DBI->connect($database, $user, $password, \%a) | ||
| 124 | or confess "connect to database: $DBI::errstr" unless $dbh; | ||
| 125 | $dbh->trace($OOPS::debug_dbi) if $OOPS::debug_dbi; | ||
| 126 | $dbh = OOPS::DBO::DBIdebug->new($dbh) | ||
| 127 | if $OOPS::debug_queries & 32; | ||
| 128 | } | ||
| 129 | |||
| 130 | require "OOPS/$dbms.pm"; | ||
| 131 | |||
| 132 | my $dboclass = "OOPS::$dbms"; | ||
| 133 | unless ($loaded{$dbms}++) { | ||
| 134 | my $f = $dboclass->can("deadlock_rx") || die; | ||
| 135 | my @dl_rx = $f->(); | ||
| 136 | @deadlock_rx{@dl_rx} = ($dbms) x @dl_rx; | ||
| 137 | my $rx = join('|', keys %deadlock_rx); | ||
| 138 | $OOPS::transfailrx = qr/$rx/; | ||
| 139 | } | ||
| 140 | |||
| 141 | my $tmode = $dboclass->can("tmode") || die; | ||
| 142 | &$tmode(undef, $dbh, $args->{readonly} || 0); | ||
| 143 | |||
| 144 | return $dbh unless wantarray; | ||
| 145 | |||
| 146 | my $new = $dboclass->can("new") || die; | ||
| 147 | my $dbo = &$new("OOPS::$dbms", | ||
| 148 | table_prefix => $prefix, | ||
| 149 | database => $database, | ||
| 150 | user => $user, | ||
| 151 | password => $password, | ||
| 152 | readonly => $args->{readonly}, | ||
| 153 | default_synchronous => $args->{default_syncronous}, | ||
| 154 | dbh => $dbh, | ||
| 155 | dbms => $dbms, | ||
| 156 | ); | ||
| 157 | bless $dbo, "OOPS::$dbms"; | ||
| 158 | |||
| 159 | $dbo->initialize() unless $args->{no_dbh}; | ||
| 160 | |||
| 161 | return ($dbh, $dbms, $prefix, $dbo); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub dboconnect | ||
| 165 | { | ||
| 166 | confess unless @_ % 2 == 1; | ||
| 167 | my ($pkg, %a) = @_; | ||
| 168 | my ($dbh, $dbms, $prefix, $dbo) = dbiconnect($pkg, %a); | ||
| 169 | $dbo->{do_disconnect} = 1; | ||
| 170 | return $dbo; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub DESTROY | ||
| 174 | { | ||
| 175 | my $dbo = shift; | ||
| 176 | $dbo->disconnect() if $dbo->{do_disconnect}; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub clean_query | ||
| 180 | { | ||
| 181 | my ($dbo, $query) = @_; | ||
| 182 | $query =~ s/^\s*#.*//mg; | ||
| 183 | $query =~ s/#.*debug.*//mg; | ||
| 184 | 1 while $query =~ s/DBO:\S+?\(($pmatch)\)/$1/gs; | ||
| 185 | $query =~ s/TP_/$dbo->{table_prefix}/g; | ||
| 186 | |||
| 187 | if ($query =~ /^\s*:$backends:\s*$/m) { | ||
| 188 | my ($before, %map) = split(/^\s*:($backends):\s*$/m, $query); | ||
| 189 | $query = $map{$dbo->{dbms}} || $before; | ||
| 190 | print "Query selected = $query\n" if $OOPS::debug_queries & 16; | ||
| 191 | } | ||
| 192 | $query =~ s/\s\s+/ /g; # mysql query log is easier to debug | ||
| 193 | |||
| 194 | return $query; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub query | ||
| 198 | { | ||
| 199 | my ($dbo, $q, %args) = @_; | ||
| 200 | |||
| 201 | my $query; | ||
| 202 | my $dbh; | ||
| 203 | my $sth; | ||
| 204 | |||
| 205 | $dbo->query_debug('', $q, %args); | ||
| 206 | |||
| 207 | if (($sth = $dbo->{cached_queries}{$q})) { | ||
| 208 | # great | ||
| 209 | if ($sth->{Active}) { | ||
| 210 | print "Query $q was still active\n" if $OOPS::debug_queries; | ||
| 211 | delete $dbo->{cached_queries}{$q}; | ||
| 212 | return query($dbo, $q, %args); | ||
| 213 | } | ||
| 214 | } elsif (($query = $dbo->{queries}{$q})) { | ||
| 215 | $query = $dbo->clean_query($query); | ||
| 216 | $dbh = $args{dbh} || $dbo->{dbh}; | ||
| 217 | $sth = $dbh->prepare($query) || confess "prepare $query: ".$dbh->errstr; | ||
| 218 | $dbo->{cached_queries}{$q} = $sth; | ||
| 219 | } else { | ||
| 220 | confess; | ||
| 221 | } | ||
| 222 | |||
| 223 | if (exists $args{execute}) { | ||
| 224 | my @a = defined($args{execute}) | ||
| 225 | ? (ref($args{execute}) | ||
| 226 | ? @{$args{execute}} | ||
| 227 | : $args{execute}) | ||
| 228 | : (); | ||
| 229 | $sth->execute(@a) || confess("could not execute '$query' with '@a':".$sth->errstr); | ||
| 230 | } | ||
| 231 | return $sth; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub adhoc_query | ||
| 235 | { | ||
| 236 | my ($dbo, $query, %args) = @_; | ||
| 237 | my $name = $query; | ||
| 238 | $name = $1 if $query =~ /^\s*#\s+DBO:name\s+(\S+)/; | ||
| 239 | $dbo->{queries}{$name} = $query; | ||
| 240 | $dbo->query($name, %args); | ||
| 241 | } | ||
| 242 | |||
| 243 | sub disconnect | ||
| 244 | { | ||
| 245 | my ($dbo) = @_; | ||
| 246 | return unless $dbo->{dbh}; | ||
| 247 | $dbo->{dbh}->disconnect(); | ||
| 248 | delete $dbo->{dbh}; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub commit | ||
| 252 | { | ||
| 253 | my $dbo = shift; | ||
| 254 | confess unless $dbo->{dbh}; | ||
| 255 | $dbo->{dbh}->commit(); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub errstr | ||
| 259 | { | ||
| 260 | my $dbo = shift; | ||
| 261 | return $DBI::errstr unless $dbo->{dbh}; | ||
| 262 | return $dbo->{dbh}->errstr; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub dbh | ||
| 266 | { | ||
| 267 | my $dbo = shift; | ||
| 268 | return $dbo->{dbh}; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub dbo | ||
| 272 | { | ||
| 273 | my $dbo = shift; | ||
| 274 | return $dbo; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub rollback | ||
| 278 | { | ||
| 279 | my $dbo = shift; | ||
| 280 | $dbo->{dbh}->rollback; | ||
| 281 | $dbo->{cached_queries} = {}; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub rebless | ||
| 285 | { | ||
| 286 | my $oops = shift; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub do_forcesave { 0; } | ||
| 290 | |||
| 291 | 1; |