| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/Database.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::Database; | ||
| 7 | |||
| 8 | use GX::Exception; | ||
| 9 | |||
| 10 | use DBI (); | ||
| 11 | |||
| 12 | |||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 14 | # Constants | ||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | use constant DEBUG => $ENV{'GX_DATABASE_DEBUG_LEVEL'} // 0; | ||
| 18 | |||
| 19 | use constant DBH_STATE_KEY => 'private_GX_Database'; | ||
| 20 | |||
| 21 | use constant DBH_STATE_ATTRIBUTES => ( | ||
| 22 | qw( | ||
| 23 | AutoCommit | ||
| 24 | ChopBlanks | ||
| 25 | CompatMode | ||
| 26 | FetchHashKeyName | ||
| 27 | HandleError | ||
| 28 | InactiveDestroy | ||
| 29 | LongReadLen | ||
| 30 | LongTruncOk | ||
| 31 | PrintError | ||
| 32 | PrintWarn | ||
| 33 | Profile | ||
| 34 | RaiseError | ||
| 35 | ShowErrorStatement | ||
| 36 | Taint | ||
| 37 | TraceLevel | ||
| 38 | Warn | ||
| 39 | ), | ||
| 40 | ( $DBI::VERSION >= 1.31 ? ( qw( TaintIn TaintOut ) ) : () ), | ||
| 41 | ( $DBI::VERSION >= 1.614 ? ( qw( AutoInactiveDestroy ) ) : () ) | ||
| 42 | ); | ||
| 43 | |||
| 44 | |||
| 45 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 46 | # Class setup | ||
| 47 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 48 | |||
| 49 | use GX::Class; | ||
| 50 | |||
| 51 | extends 'GX::Component::Singleton'; | ||
| 52 | |||
| 53 | has 'connect_options' => ( | ||
| 54 | isa => 'Hash', | ||
| 55 | initialize => 1, | ||
| 56 | initializer => '_initialize_connect_options', | ||
| 57 | accessors => { | ||
| 58 | '_get_connect_options' => { type => 'get_reference' } | ||
| 59 | } | ||
| 60 | ); | ||
| 61 | |||
| 62 | has 'dsn' => ( | ||
| 63 | isa => 'Scalar', | ||
| 64 | initialize => 1, | ||
| 65 | accessors => { | ||
| 66 | '_get_dsn' => { type => 'get' }, | ||
| 67 | '_set_dsn' => { type => 'set' } | ||
| 68 | } | ||
| 69 | ); | ||
| 70 | |||
| 71 | has 'sql_builder' => ( | ||
| 72 | isa => 'Scalar', | ||
| 73 | initialize => 1, | ||
| 74 | initializer => '_initialize_sql_builder', | ||
| 75 | accessors => { | ||
| 76 | '_get_sql_builder' => { type => 'get' }, | ||
| 77 | '_set_sql_builder' => { type => 'set' } | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | |||
| 81 | build; | ||
| 82 | |||
| 83 | |||
| 84 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 85 | # Export mechanism | ||
| 86 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 87 | |||
| 88 | __PACKAGE__->_install_import_method; | ||
| 89 | |||
| 90 | |||
| 91 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 92 | # Database handle cache | ||
| 93 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 94 | |||
| 95 | { | ||
| 96 | |||
| 97 | my %CACHE; | ||
| 98 | |||
| 99 | sub _delete_cached_dbh { | ||
| 100 | return delete( ( $CACHE{$$} // return undef )->{ ref $_[0] } ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _get_cached_dbh { | ||
| 104 | return ( $CACHE{$$} // return undef )->{ ref $_[0] }; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _set_cached_dbh { | ||
| 108 | return $CACHE{$$}{ ref $_[0] } = $_[1]; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub CLONE { | ||
| 112 | %CACHE = (); | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | END { | ||
| 117 | |||
| 118 | local $@; | ||
| 119 | |||
| 120 | if ( $CACHE{$$} ) { | ||
| 121 | |||
| 122 | for my $class ( keys %{$CACHE{$$}} ) { | ||
| 123 | |||
| 124 | if ( my $dbh = delete $CACHE{$$}{$class} ) { | ||
| 125 | |||
| 126 | if ( DEBUG ) { | ||
| 127 | warn "[GX::Database] [$$] END: Forcing disconnect from database $class\n"; | ||
| 128 | } | ||
| 129 | |||
| 130 | if ( $dbh->{'Active'} && ! $dbh->{'AutoCommit'} ) { | ||
| 131 | |||
| 132 | if ( DEBUG ) { | ||
| 133 | warn "[GX::Database] [$$] END: Forcing rollback of database $class\n"; | ||
| 134 | } | ||
| 135 | |||
| 136 | eval { $dbh->rollback }; | ||
| 137 | |||
| 138 | if ( DEBUG && $@ ) { | ||
| 139 | warn "[GX::Database] [$$] END: Rollback of database $class failed: $@\n"; | ||
| 140 | } | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | eval { $dbh->disconnect }; | ||
| 145 | |||
| 146 | if ( DEBUG && $@ ) { | ||
| 147 | warn "[GX::Database] [$$] END: Disconnect from database $class failed: $@\n"; | ||
| 148 | } | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | delete $CACHE{$$}; | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | for my $pid ( keys %CACHE ) { | ||
| 159 | |||
| 160 | for my $class ( keys %{$CACHE{$pid}} ) { | ||
| 161 | |||
| 162 | if ( my $dbh = delete $CACHE{$pid}{$class} ) { | ||
| 163 | # Instruct DBI to treat the handle as not-Active on destruction | ||
| 164 | $dbh->{'InactiveDestroy'} = 1; | ||
| 165 | } | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | delete $CACHE{$pid}; | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 179 | # Public methods | ||
| 180 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 181 | |||
| 182 | sub connect { | ||
| 183 | |||
| 184 | my $self = shift->instance; | ||
| 185 | |||
| 186 | if ( DEBUG ) { | ||
| 187 | $self->_debug( "Connecting to database" ); | ||
| 188 | } | ||
| 189 | |||
| 190 | my $dbh; | ||
| 191 | |||
| 192 | if ( $dbh = $self->_get_cached_dbh ) { | ||
| 193 | |||
| 194 | if ( DEBUG ) { | ||
| 195 | $self->_debug( "Reusing cached connection" ); | ||
| 196 | } | ||
| 197 | |||
| 198 | if ( $self->_ping_dbh( $dbh ) ) { | ||
| 199 | |||
| 200 | if ( $self->_reset_dbh( $dbh ) ) { | ||
| 201 | return $dbh; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ( DEBUG ) { | ||
| 205 | $self->_debug( "Cannot reset cached connection" ); | ||
| 206 | } | ||
| 207 | |||
| 208 | } | ||
| 209 | else { | ||
| 210 | |||
| 211 | if ( DEBUG ) { | ||
| 212 | $self->_debug( "Cached connection is dead" ); | ||
| 213 | } | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | $self->_delete_cached_dbh; | ||
| 218 | |||
| 219 | } | ||
| 220 | |||
| 221 | if ( ! eval { $dbh = $self->_connect } ) { | ||
| 222 | GX::Exception->complain( | ||
| 223 | message => "Cannot connect to database $self", | ||
| 224 | subexception => ( $@ || $DBI::errstr ), | ||
| 225 | verbosity => 1 | ||
| 226 | ); | ||
| 227 | } | ||
| 228 | |||
| 229 | $self->_store_dbh_state( $dbh ); | ||
| 230 | |||
| 231 | $self->_set_cached_dbh( $dbh ); | ||
| 232 | |||
| 233 | return $dbh; | ||
| 234 | |||
| 235 | } | ||
| 236 | |||
| 237 | sub connect_options { | ||
| 238 | |||
| 239 | return %{$_[0]->instance->_get_connect_options}; | ||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | sub disconnect { | ||
| 244 | |||
| 245 | my $self = shift->instance; | ||
| 246 | |||
| 247 | if ( DEBUG ) { | ||
| 248 | $self->_debug( "Disconnecting from database" ); | ||
| 249 | } | ||
| 250 | |||
| 251 | my $dbh = $self->_delete_cached_dbh; | ||
| 252 | |||
| 253 | if ( ! $dbh ) { | ||
| 254 | |||
| 255 | if ( DEBUG ) { | ||
| 256 | $self->_debug( "Not connected" ); | ||
| 257 | } | ||
| 258 | |||
| 259 | return -1; | ||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | { | ||
| 264 | |||
| 265 | local $@; | ||
| 266 | |||
| 267 | if ( $dbh->{'Active'} && ! $dbh->{'AutoCommit'} ) { | ||
| 268 | |||
| 269 | if ( DEBUG ) { | ||
| 270 | $self->_debug( "Forcing rollback before disconnect" ); | ||
| 271 | } | ||
| 272 | |||
| 273 | eval { $dbh->rollback }; | ||
| 274 | |||
| 275 | if ( DEBUG && $@ ) { | ||
| 276 | $self->_debug( "Rollback failed: $@" ); | ||
| 277 | } | ||
| 278 | |||
| 279 | } | ||
| 280 | |||
| 281 | eval { $dbh->disconnect }; | ||
| 282 | |||
| 283 | if ( $@ ) { | ||
| 284 | |||
| 285 | if ( DEBUG ) { | ||
| 286 | $self->_debug( "Disconnect failed: $@" ); | ||
| 287 | } | ||
| 288 | |||
| 289 | return 0; | ||
| 290 | |||
| 291 | } | ||
| 292 | |||
| 293 | } | ||
| 294 | |||
| 295 | return 1; | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | sub dsn { | ||
| 300 | |||
| 301 | return $_[0]->instance->_get_dsn; | ||
| 302 | |||
| 303 | } | ||
| 304 | |||
| 305 | sub is_connected { | ||
| 306 | |||
| 307 | my $self = shift->instance; | ||
| 308 | |||
| 309 | my $dbh = $self->_get_cached_dbh; | ||
| 310 | |||
| 311 | return $dbh ? $self->_ping_dbh( $dbh ) : 0; | ||
| 312 | |||
| 313 | } | ||
| 314 | |||
| 315 | sub sql_builder { | ||
| 316 | |||
| 317 | return $_[0]->instance->_get_sql_builder; | ||
| 318 | |||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 323 | # Internal methods | ||
| 324 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 325 | |||
| 326 | sub DESTROY { | ||
| 327 | |||
| 328 | my $self = shift; | ||
| 329 | |||
| 330 | if ( DEBUG ) { | ||
| 331 | $self->_debug( "DESTROYing database instance" ); | ||
| 332 | } | ||
| 333 | |||
| 334 | $self->disconnect; | ||
| 335 | |||
| 336 | return; | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 342 | # Private methods | ||
| 343 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 344 | |||
| 345 | sub _connect { | ||
| 346 | |||
| 347 | # Abstract method | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | sub _dbi_driver { | ||
| 352 | |||
| 353 | # Abstract method | ||
| 354 | |||
| 355 | } | ||
| 356 | |||
| 357 | sub _dbi_error_handler { | ||
| 358 | |||
| 359 | my $error = shift; | ||
| 360 | my $dbh = shift; | ||
| 361 | |||
| 362 | my $statement = $dbh->{'Statement'}; | ||
| 363 | |||
| 364 | throw sprintf( | ||
| 365 | "DBI Error: \"%s\"%s", | ||
| 366 | $error, | ||
| 367 | ( $statement ? " (statement: \"$statement\")" : '' ) | ||
| 368 | ); | ||
| 369 | |||
| 370 | } | ||
| 371 | |||
| 372 | sub _initialize_connect_options { | ||
| 373 | |||
| 374 | my $self = shift; | ||
| 375 | |||
| 376 | return { | ||
| 377 | 'AutoCommit' => 1, | ||
| 378 | 'HandleError' => $self->can( '_dbi_error_handler' ), | ||
| 379 | 'PrintError' => 0, | ||
| 380 | 'RaiseError' => 1 | ||
| 381 | }; | ||
| 382 | |||
| 383 | } | ||
| 384 | |||
| 385 | sub _initialize_sql_builder { | ||
| 386 | |||
| 387 | require GX::SQL::Builder; | ||
| 388 | |||
| 389 | return GX::SQL::Builder->new; | ||
| 390 | |||
| 391 | } | ||
| 392 | |||
| 393 | sub _ping_dbh { | ||
| 394 | |||
| 395 | my $self = shift; | ||
| 396 | my $dbh = shift; | ||
| 397 | |||
| 398 | if ( DEBUG ) { | ||
| 399 | $self->_debug( "Pinging dbh" ); | ||
| 400 | } | ||
| 401 | |||
| 402 | return $dbh->ping; | ||
| 403 | |||
| 404 | } | ||
| 405 | |||
| 406 | sub _reset_dbh { | ||
| 407 | |||
| 408 | my $self = shift; | ||
| 409 | my $dbh = shift; | ||
| 410 | |||
| 411 | if ( DEBUG ) { | ||
| 412 | $self->_debug( "Resetting dbh" ); | ||
| 413 | } | ||
| 414 | |||
| 415 | if ( $dbh->{'Active'} && ! $dbh->{'AutoCommit'} ) { | ||
| 416 | |||
| 417 | if ( DEBUG ) { | ||
| 418 | $self->_debug( "Forcing rollback" ); | ||
| 419 | } | ||
| 420 | |||
| 421 | local $@; | ||
| 422 | |||
| 423 | eval { $dbh->rollback }; | ||
| 424 | |||
| 425 | if ( $@ ) { | ||
| 426 | |||
| 427 | if ( DEBUG ) { | ||
| 428 | $self->_debug( "Rollback failed: $@" ); | ||
| 429 | } | ||
| 430 | |||
| 431 | return 0; | ||
| 432 | |||
| 433 | } | ||
| 434 | |||
| 435 | } | ||
| 436 | |||
| 437 | my $state = $self->_retrieve_dbh_state( $dbh ); | ||
| 438 | |||
| 439 | if ( ! $state ) { | ||
| 440 | |||
| 441 | if ( DEBUG ) { | ||
| 442 | $self->_debug( "No stored dbh state" ); | ||
| 443 | } | ||
| 444 | |||
| 445 | return 0; | ||
| 446 | |||
| 447 | } | ||
| 448 | |||
| 449 | for my $attribute ( keys %$state ) { | ||
| 450 | $dbh->{$attribute} = $state->{$attribute}; | ||
| 451 | } | ||
| 452 | |||
| 453 | return 1; | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | sub _retrieve_dbh_state { | ||
| 458 | |||
| 459 | my $self = shift; | ||
| 460 | my $dbh = shift; | ||
| 461 | |||
| 462 | if ( DEBUG ) { | ||
| 463 | $self->_debug( "Retrieving dbh state" ); | ||
| 464 | } | ||
| 465 | |||
| 466 | return $dbh->{ DBH_STATE_KEY() }; | ||
| 467 | |||
| 468 | } | ||
| 469 | |||
| 470 | sub _setup_config { | ||
| 471 | |||
| 472 | my $self = shift; | ||
| 473 | my $args = shift; | ||
| 474 | |||
| 475 | if ( exists $args->{'connect_options'} ) { | ||
| 476 | |||
| 477 | my $connect_options = delete $args->{'connect_options'}; | ||
| 478 | |||
| 479 | if ( ref $connect_options ne 'HASH' ) { | ||
| 480 | throw "Invalid option (\"connect_options\" must be a hash reference)"; | ||
| 481 | } | ||
| 482 | |||
| 483 | %{$self->_get_connect_options} = ( %{$self->_get_connect_options}, %$connect_options ); | ||
| 484 | |||
| 485 | } | ||
| 486 | |||
| 487 | $self->SUPER::_setup_config( $args ); | ||
| 488 | |||
| 489 | return; | ||
| 490 | |||
| 491 | } | ||
| 492 | |||
| 493 | sub _start { | ||
| 494 | |||
| 495 | my $self = shift; | ||
| 496 | |||
| 497 | $self->SUPER::_start; | ||
| 498 | |||
| 499 | $self->disconnect; | ||
| 500 | |||
| 501 | return; | ||
| 502 | |||
| 503 | } | ||
| 504 | |||
| 505 | sub _store_dbh_state { | ||
| 506 | |||
| 507 | my $self = shift; | ||
| 508 | my $dbh = shift; | ||
| 509 | |||
| 510 | if ( DEBUG ) { | ||
| 511 | $self->_debug( "Storing dbh state" ); | ||
| 512 | } | ||
| 513 | |||
| 514 | if ( exists $dbh->{ DBH_STATE_KEY() } ) { | ||
| 515 | throw "Cannot store dbh state"; | ||
| 516 | } | ||
| 517 | |||
| 518 | my %state; | ||
| 519 | |||
| 520 | for my $attribute ( DBH_STATE_ATTRIBUTES ) { | ||
| 521 | $state{$attribute} = $dbh->{$attribute} if exists $dbh->{$attribute}; | ||
| 522 | } | ||
| 523 | |||
| 524 | $dbh->{ DBH_STATE_KEY() } = \%state; | ||
| 525 | |||
| 526 | return 1; | ||
| 527 | |||
| 528 | } | ||
| 529 | |||
| 530 | sub _validate_class_name { | ||
| 531 | |||
| 532 | return $_[1] =~ /^(?:[_a-zA-Z]\w*::)+?Database(?:::[_a-zA-Z]\w*)+$/; | ||
| 533 | |||
| 534 | } | ||
| 535 | |||
| 536 | |||
| 537 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 538 | # Aliases | ||
| 539 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 540 | |||
| 541 | *dbh = \&connect; | ||
| 542 | |||
| 543 | |||
| 544 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 545 | # Debugging | ||
| 546 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 547 | |||
| 548 | sub _debug { | ||
| 549 | |||
| 550 | my $invocant = shift; | ||
| 551 | my $message = shift; | ||
| 552 | |||
| 553 | warn "[$invocant] [$$] $message\n"; | ||
| 554 | |||
| 555 | return; | ||
| 556 | |||
| 557 | } | ||
| 558 | |||
| 559 | |||
| 560 | 1; | ||
| 561 | |||
| 562 | __END__ |