| 1 | |||
| 2 | =head1 Name | ||
| 3 | |||
| 4 | QBit::Application::Model::DB - base class for DB. | ||
| 5 | |||
| 6 | =head1 Description | ||
| 7 | |||
| 8 | Base class for working with databases. | ||
| 9 | |||
| 10 | =head1 GitHub | ||
| 11 | |||
| 12 | https://github.com/QBitFramework/QBit-Application-Model-DB | ||
| 13 | |||
| 14 | =head1 Install | ||
| 15 | |||
| 16 | =over | ||
| 17 | |||
| 18 | =item * | ||
| 19 | |||
| 20 | cpanm QBit::Application::Model::DB | ||
| 21 | |||
| 22 | =item * | ||
| 23 | |||
| 24 | cpanm https://github.com/QBitFramework/QBit-Application-Model-DB.git | ||
| 25 | |||
| 26 | =back | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | package QBit::Application::Model::DB; | ||
| 31 | $QBit::Application::Model::DB::VERSION = '0.030'; | ||
| 32 | use qbit; | ||
| 33 | |||
| 34 | use base qw(QBit::Application::Model); | ||
| 35 | |||
| 36 | use Exception::DB; | ||
| 37 | use Exception::DB::DuplicateEntry; | ||
| 38 | |||
| 39 | use DBI; | ||
| 40 | |||
| 41 | =head1 Debug | ||
| 42 | |||
| 43 | $QBit::Application::Model::DB::DEBUG = TRUE; | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | our $DEBUG = FALSE; | ||
| 48 | |||
| 49 | =head1 Abstract methods | ||
| 50 | |||
| 51 | =over | ||
| 52 | |||
| 53 | =item * | ||
| 54 | |||
| 55 | B<query> | ||
| 56 | |||
| 57 | =item * | ||
| 58 | |||
| 59 | B<get_query_id> | ||
| 60 | |||
| 61 | =item * | ||
| 62 | |||
| 63 | B<filter> | ||
| 64 | |||
| 65 | =item * | ||
| 66 | |||
| 67 | B<_get_table_object> | ||
| 68 | |||
| 69 | =item * | ||
| 70 | |||
| 71 | B<_create_sql_db> | ||
| 72 | |||
| 73 | =item * | ||
| 74 | |||
| 75 | B<_connect> | ||
| 76 | |||
| 77 | =item * | ||
| 78 | |||
| 79 | B<_is_connection_error> | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | __PACKAGE__->abstract_methods(qw(query filter _get_table_object _create_sql_db _connect _is_connection_error)); | ||
| 86 | |||
| 87 | =head1 Package methods | ||
| 88 | |||
| 89 | =head2 meta | ||
| 90 | |||
| 91 | B<Arguments:> | ||
| 92 | |||
| 93 | =over | ||
| 94 | |||
| 95 | =item | ||
| 96 | |||
| 97 | B<%meta> - meta information about database | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | B<Example:> | ||
| 102 | |||
| 103 | package Test::DB; | ||
| 104 | |||
| 105 | use qbit; | ||
| 106 | |||
| 107 | use base qw(QBit::Application::Model::DB); | ||
| 108 | |||
| 109 | my $meta = { | ||
| 110 | tables => { | ||
| 111 | users => { | ||
| 112 | fields => [ | ||
| 113 | {name => 'id', type => 'INT', unsigned => 1, not_null => 1, autoincrement => 1,}, | ||
| 114 | {name => 'create_dt', type => 'DATETIME', not_null => 1,}, | ||
| 115 | {name => 'login', type => 'VARCHAR', length => 255, not_null => 1,}, | ||
| 116 | ], | ||
| 117 | primary_key => [qw(id)], | ||
| 118 | indexes => [{fields => [qw(login)], unique => 1},], | ||
| 119 | }, | ||
| 120 | |||
| 121 | fio => { | ||
| 122 | fields => [ | ||
| 123 | {name => 'user_id'}, | ||
| 124 | {name => 'name', type => 'VARCHAR', length => 255,}, | ||
| 125 | {name => 'midname', type => 'VARCHAR', length => 255,}, | ||
| 126 | {name => 'surname', type => 'VARCHAR', length => 255,}, | ||
| 127 | ], | ||
| 128 | foreign_keys => [[[qw(user_id)] => 'users' => [qw(id)]]] | ||
| 129 | }, | ||
| 130 | }, | ||
| 131 | }; | ||
| 132 | |||
| 133 | __PACKAGE__->meta($meta); | ||
| 134 | |||
| 135 | in Appplication.pm | ||
| 136 | |||
| 137 | use Test::DB accessor => 'db'; | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub meta { | ||
| 142 | my ($package, %meta) = @_; | ||
| 143 | |||
| 144 | throw gettext("First argument must be package name, QBit::Application::Model::DB descendant") | ||
| 145 | if !$package | ||
| 146 | || ref($package) | ||
| 147 | || !$package->isa('QBit::Application::Model::DB'); | ||
| 148 | |||
| 149 | my $pkg_stash = package_stash(ref($package) || $package); | ||
| 150 | $pkg_stash->{'__META__'} = \%meta; | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 get_all_meta | ||
| 154 | |||
| 155 | B<Arguments:> | ||
| 156 | |||
| 157 | =over | ||
| 158 | |||
| 159 | =item | ||
| 160 | |||
| 161 | B<$package> - package object or name (optional) | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | B<Return values:> | ||
| 166 | |||
| 167 | =over | ||
| 168 | |||
| 169 | =item | ||
| 170 | |||
| 171 | B<$meta> - meta information about database | ||
| 172 | |||
| 173 | =back | ||
| 174 | |||
| 175 | B<Example:> | ||
| 176 | |||
| 177 | my $meta = $app->db->get_all_meta('Test::DB'); | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub get_all_meta { | ||
| 182 | my ($self, $package) = @_; | ||
| 183 | |||
| 184 | $package = (ref($self) || $self) unless defined($package); | ||
| 185 | my $meta = {}; | ||
| 186 | |||
| 187 | foreach my $pkg (eval("\@${package}::ISA")) { | ||
| 188 | next unless $pkg->isa(__PACKAGE__); | ||
| 189 | $self->_add_meta($meta, $pkg->get_all_meta($pkg)); | ||
| 190 | } | ||
| 191 | |||
| 192 | $self->_add_meta($meta, package_stash($package)->{'__META__'} || {}); | ||
| 193 | |||
| 194 | return $meta; | ||
| 195 | } | ||
| 196 | |||
| 197 | =head2 init | ||
| 198 | |||
| 199 | B<No arguments.> | ||
| 200 | |||
| 201 | Method called from L</new> before return object. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub init { | ||
| 206 | my ($self) = @_; | ||
| 207 | |||
| 208 | $self->SUPER::init(); | ||
| 209 | |||
| 210 | unless (package_stash(ref($self))->{'__METHODS_CREATED__'}) { | ||
| 211 | my $meta = $self->get_all_meta(); | ||
| 212 | |||
| 213 | $self->make_tables($meta->{'tables'} // {}); | ||
| 214 | |||
| 215 | package_stash(ref($self))->{'__METHODS_CREATED__'} = TRUE; | ||
| 216 | } | ||
| 217 | |||
| 218 | $self->{'__SAVEPOINTS__'} = 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | =head2 make_tables | ||
| 222 | |||
| 223 | It create "accessors" for accessing tables. | ||
| 224 | |||
| 225 | B<Arguments:> | ||
| 226 | |||
| 227 | =over | ||
| 228 | |||
| 229 | =item | ||
| 230 | |||
| 231 | B<$tables> - reference of a hash (required). | ||
| 232 | |||
| 233 | { | ||
| 234 | table_name => <string (table name also)>, | ||
| 235 | table_name => <object (QBit::Application::Model::DB::Table)>, | ||
| 236 | table_name => <hash (definition of a table)>, | ||
| 237 | } | ||
| 238 | |||
| 239 | =back | ||
| 240 | |||
| 241 | B<Example:> | ||
| 242 | |||
| 243 | $app->db->make_tables({clients => 'users'}); | ||
| 244 | # or | ||
| 245 | $app->db->make_tables({clients => $app->db->users}); | ||
| 246 | # or | ||
| 247 | $app->db->make_tables({ | ||
| 248 | clients => { | ||
| 249 | fields => ..., | ||
| 250 | primary_key => ..., | ||
| 251 | indexes => ..., | ||
| 252 | foreign_keys => ..., | ||
| 253 | collate => ..., | ||
| 254 | engine => ..., | ||
| 255 | } | ||
| 256 | }); | ||
| 257 | |||
| 258 | # after | ||
| 259 | $app->db->clients->create(); | ||
| 260 | $app->db->clients->add_multi(...); | ||
| 261 | $app->db->drop(); | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub make_tables { | ||
| 266 | my ($self, $tables_meta) = @_; | ||
| 267 | |||
| 268 | my $class = ref($self); | ||
| 269 | my $pkg_stash = package_stash($class); | ||
| 270 | |||
| 271 | my ($meta, %tables); | ||
| 272 | foreach my $table_name (keys(%$tables_meta)) { | ||
| 273 | my $table_meta = $tables_meta->{$table_name}; | ||
| 274 | |||
| 275 | my %table = (); | ||
| 276 | if (ref($table_meta) eq 'HASH') { | ||
| 277 | %table = %$table_meta; | ||
| 278 | } else { | ||
| 279 | my $table_name_template = blessed($table_meta) ? $table_meta->name : $table_meta; | ||
| 280 | |||
| 281 | $meta //= $self->get_all_meta(); | ||
| 282 | |||
| 283 | throw gettext('Table "%s" not found', $table_name_template) | ||
| 284 | unless exists($meta->{'tables'}{$table_name_template}); | ||
| 285 | |||
| 286 | %table = %{$meta->{'tables'}{$table_name_template}}; | ||
| 287 | } | ||
| 288 | |||
| 289 | if ($pkg_stash->{'__METHODS_CREATED__'}) { | ||
| 290 | $pkg_stash->{'__META__'}{'tables'}{$table_name} = {%table}; | ||
| 291 | } | ||
| 292 | |||
| 293 | $table{'class'} = $self->_get_table_class(type => $table{'type'}); | ||
| 294 | $table{'fields'} = [$table{'class'}->default_fields(%table), @{$table{'fields'} || []}]; | ||
| 295 | $table{'indexes'} = [$table{'class'}->default_indexes(%table), @{$table{'indexes'} || []}]; | ||
| 296 | $table{'foreign_keys'} = [$table{'class'}->default_foreign_keys(%table), @{$table{'foreign_keys'} || []}]; | ||
| 297 | $table{'primary_key'} = $table{'class'}->default_primary_key(%table) | ||
| 298 | unless exists($table{'primary_key'}); | ||
| 299 | |||
| 300 | $tables{$table_name} = \%table; | ||
| 301 | } | ||
| 302 | |||
| 303 | $self->{'__TABLE_TREE_LEVEL__'}{$_} = $self->_table_tree_level(\%tables, $_, 0) foreach keys(%tables); | ||
| 304 | $self->{'__TABLES__'} //= {}; | ||
| 305 | |||
| 306 | my $preload_accessors = $self->app->get_option('preload_accessors'); | ||
| 307 | |||
| 308 | foreach my $table_name ($self->_sorted_tables(keys(%tables))) { | ||
| 309 | throw gettext('Cannot create table object, "%s" is reserved', $table_name) | ||
| 310 | if $self->can($table_name); | ||
| 311 | { | ||
| 312 | no strict 'refs'; | ||
| 313 | |||
| 314 | *{$class . '::' . $table_name} = sub { | ||
| 315 | $_[0]->{'__TABLES__'}{$table_name} //= do { | ||
| 316 | throw gettext('No package for table "%s"', $table_name) | ||
| 317 | unless defined($tables{$table_name}->{'class'}); | ||
| 318 | |||
| 319 | $tables{$table_name}->{'class'}->new( | ||
| 320 | %{$tables{$table_name}}, | ||
| 321 | name => $table_name, | ||
| 322 | db => $_[0], | ||
| 323 | ); | ||
| 324 | }; | ||
| 325 | }; | ||
| 326 | }; | ||
| 327 | |||
| 328 | $self->$table_name if $preload_accessors; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | =head2 set_dbh | ||
| 333 | |||
| 334 | B<Arguments:> | ||
| 335 | |||
| 336 | =over | ||
| 337 | |||
| 338 | =item | ||
| 339 | |||
| 340 | B<$dbh> - Database handle object (optional) | ||
| 341 | |||
| 342 | =back | ||
| 343 | |||
| 344 | B<Return values:> | ||
| 345 | |||
| 346 | =over | ||
| 347 | |||
| 348 | =item | ||
| 349 | |||
| 350 | B<$dbh> - Database handle object or undef | ||
| 351 | |||
| 352 | =back | ||
| 353 | |||
| 354 | B<Example:> | ||
| 355 | |||
| 356 | my $dbh = DBI->connect(...); | ||
| 357 | |||
| 358 | # set | ||
| 359 | $app->db->set_dbh($dbh); | ||
| 360 | |||
| 361 | # clear | ||
| 362 | $app->db->set_dbh(); | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | sub set_dbh { | ||
| 367 | my ($self, $dbh) = @_; | ||
| 368 | |||
| 369 | if (defined($dbh)) { | ||
| 370 | $self->{'__DBH__'}{$$} = $dbh; | ||
| 371 | } else { | ||
| 372 | delete($self->{'__DBH__'}{$$}); | ||
| 373 | } | ||
| 374 | |||
| 375 | return $dbh; | ||
| 376 | } | ||
| 377 | |||
| 378 | =head2 dbh | ||
| 379 | |||
| 380 | B<No arguments.> | ||
| 381 | |||
| 382 | returns a database handle object or undef | ||
| 383 | |||
| 384 | B<Example:> | ||
| 385 | |||
| 386 | my $dbh = $app->db->dbh; | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | sub dbh { | ||
| 391 | my ($self) = @_; | ||
| 392 | |||
| 393 | return $self->{'__DBH__'}{$$}; | ||
| 394 | } | ||
| 395 | |||
| 396 | =head2 quote | ||
| 397 | |||
| 398 | B<Arguments:> | ||
| 399 | |||
| 400 | =over | ||
| 401 | |||
| 402 | =item | ||
| 403 | |||
| 404 | B<$name> - string | ||
| 405 | |||
| 406 | =back | ||
| 407 | |||
| 408 | B<Return values:> | ||
| 409 | |||
| 410 | =over | ||
| 411 | |||
| 412 | =item | ||
| 413 | |||
| 414 | B<$quoted_name> - quoted string | ||
| 415 | |||
| 416 | =back | ||
| 417 | |||
| 418 | B<Example:> | ||
| 419 | |||
| 420 | my $quoted_name = $app->db->quote('users'); # 'users' | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | sub quote { | ||
| 425 | my ($self, $name) = @_; | ||
| 426 | |||
| 427 | my ($res) = $self->_sub_with_connected_dbh( | ||
| 428 | sub { | ||
| 429 | my ($self, $name) = @_; | ||
| 430 | return $self->dbh->quote($name); | ||
| 431 | }, | ||
| 432 | [$self, $name] | ||
| 433 | ); | ||
| 434 | |||
| 435 | return $res; | ||
| 436 | } | ||
| 437 | |||
| 438 | =head2 quote_identifier | ||
| 439 | |||
| 440 | B<Arguments:> | ||
| 441 | |||
| 442 | =over | ||
| 443 | |||
| 444 | =item | ||
| 445 | |||
| 446 | B<$name> - string | ||
| 447 | |||
| 448 | =back | ||
| 449 | |||
| 450 | B<Return values:> | ||
| 451 | |||
| 452 | =over | ||
| 453 | |||
| 454 | =item | ||
| 455 | |||
| 456 | B<$quoted_name> - quoted string | ||
| 457 | |||
| 458 | =back | ||
| 459 | |||
| 460 | B<Example:> | ||
| 461 | |||
| 462 | my $quoted_name = $app->db->quote_identifier('users'); # "users" | ||
| 463 | |||
| 464 | =cut | ||
| 465 | |||
| 466 | sub quote_identifier { | ||
| 467 | my ($self, $name) = @_; | ||
| 468 | |||
| 469 | my ($res) = $self->_sub_with_connected_dbh( | ||
| 470 | sub { | ||
| 471 | my ($self, $name) = @_; | ||
| 472 | return $self->dbh->quote_identifier($name); | ||
| 473 | }, | ||
| 474 | [$self, $name] | ||
| 475 | ); | ||
| 476 | |||
| 477 | return $res; | ||
| 478 | } | ||
| 479 | |||
| 480 | =head2 begin | ||
| 481 | |||
| 482 | B<No arguments.> | ||
| 483 | |||
| 484 | start a new transaction or create new savepoint | ||
| 485 | |||
| 486 | B<Example:> | ||
| 487 | |||
| 488 | $app->db->begin(); | ||
| 489 | |||
| 490 | =cut | ||
| 491 | |||
| 492 | sub begin { | ||
| 493 | my ($self) = @_; | ||
| 494 | |||
| 495 | $self->{'__SAVEPOINTS__'} == 0 | ||
| 496 | ? $self->_do('BEGIN') | ||
| 497 | : $self->_do('SAVEPOINT SP' . $self->{'__SAVEPOINTS__'}); | ||
| 498 | |||
| 499 | ++$self->{'__SAVEPOINTS__'}; | ||
| 500 | } | ||
| 501 | |||
| 502 | =head2 commit | ||
| 503 | |||
| 504 | B<No arguments.> | ||
| 505 | |||
| 506 | commits the current transaction or release savepoint | ||
| 507 | |||
| 508 | B<Example:> | ||
| 509 | |||
| 510 | $app->db->commit(); | ||
| 511 | |||
| 512 | =cut | ||
| 513 | |||
| 514 | sub commit { | ||
| 515 | my ($self) = @_; | ||
| 516 | |||
| 517 | --$self->{'__SAVEPOINTS__'} | ||
| 518 | ? $self->_do('RELEASE SAVEPOINT SP' . $self->{'__SAVEPOINTS__'}) | ||
| 519 | : $self->_do('COMMIT'); | ||
| 520 | } | ||
| 521 | |||
| 522 | =head2 rollback | ||
| 523 | |||
| 524 | B<No arguments.> | ||
| 525 | |||
| 526 | rolls back the current transaction or savepoint | ||
| 527 | |||
| 528 | B<Example:> | ||
| 529 | |||
| 530 | $app->db->rollback(); | ||
| 531 | |||
| 532 | =cut | ||
| 533 | |||
| 534 | sub rollback { | ||
| 535 | my ($self) = @_; | ||
| 536 | |||
| 537 | my $sql = | ||
| 538 | --$self->{'__SAVEPOINTS__'} | ||
| 539 | ? $self->_do('ROLLBACK TO SAVEPOINT SP' . $self->{'__SAVEPOINTS__'}) | ||
| 540 | : $self->_do('ROLLBACK'); | ||
| 541 | } | ||
| 542 | |||
| 543 | =head2 transaction | ||
| 544 | |||
| 545 | B<Arguments:> | ||
| 546 | |||
| 547 | =over | ||
| 548 | |||
| 549 | =item | ||
| 550 | |||
| 551 | B<$sub> - reference to sub | ||
| 552 | |||
| 553 | =back | ||
| 554 | |||
| 555 | B<Example:> | ||
| 556 | |||
| 557 | $app->db->transaction(sub { | ||
| 558 | # work with db | ||
| 559 | ... | ||
| 560 | }); | ||
| 561 | |||
| 562 | =cut | ||
| 563 | |||
| 564 | sub transaction { | ||
| 565 | my ($self, $sub) = @_; | ||
| 566 | |||
| 567 | $self->begin(); | ||
| 568 | try { | ||
| 569 | $sub->(); | ||
| 570 | } | ||
| 571 | catch { | ||
| 572 | my $ex = shift; | ||
| 573 | |||
| 574 | if ($ex->isa('Exception::DB')) { | ||
| 575 | $self->{'__SAVEPOINTS__'} = 0; | ||
| 576 | } else { | ||
| 577 | $self->rollback(); | ||
| 578 | } | ||
| 579 | |||
| 580 | throw $ex; | ||
| 581 | }; | ||
| 582 | |||
| 583 | $self->commit(); | ||
| 584 | } | ||
| 585 | |||
| 586 | sub _get_list_tables { | ||
| 587 | my ($self, @tables) = @_; | ||
| 588 | |||
| 589 | my $meta = $self->get_all_meta(); | ||
| 590 | |||
| 591 | my @result = (); | ||
| 592 | |||
| 593 | if (exists($meta->{'tables'})) { | ||
| 594 | my %table_names = map {$_ => TRUE} @tables; | ||
| 595 | |||
| 596 | push(@result, $self->_sorted_tables(grep {!@tables || $table_names{$_}} keys(%{$meta->{'tables'}}))); | ||
| 597 | } | ||
| 598 | |||
| 599 | return @result; | ||
| 600 | } | ||
| 601 | |||
| 602 | =head2 create_sql | ||
| 603 | |||
| 604 | B<Arguments:> | ||
| 605 | |||
| 606 | =over | ||
| 607 | |||
| 608 | =item | ||
| 609 | |||
| 610 | B<@tables> - table names (optional) | ||
| 611 | |||
| 612 | =back | ||
| 613 | |||
| 614 | B<Return values:> | ||
| 615 | |||
| 616 | =over | ||
| 617 | |||
| 618 | =item | ||
| 619 | |||
| 620 | B<$sql> - sql | ||
| 621 | |||
| 622 | =back | ||
| 623 | |||
| 624 | B<Example:> | ||
| 625 | |||
| 626 | my $sql = $app->db->create_sql(qw(users)); | ||
| 627 | |||
| 628 | =cut | ||
| 629 | |||
| 630 | sub create_sql { | ||
| 631 | my ($self, @tables) = @_; | ||
| 632 | |||
| 633 | $self->_connect(); | ||
| 634 | |||
| 635 | my $SQL = join("\n\n", map {$self->$_->create_sql()} $self->_get_list_tables(@tables)); | ||
| 636 | |||
| 637 | return "$SQL\n"; | ||
| 638 | } | ||
| 639 | |||
| 640 | =head2 init_db | ||
| 641 | |||
| 642 | B<Arguments:> | ||
| 643 | |||
| 644 | =over | ||
| 645 | |||
| 646 | =item | ||
| 647 | |||
| 648 | B<@tables> - table names (optional) | ||
| 649 | |||
| 650 | =back | ||
| 651 | |||
| 652 | B<Example:> | ||
| 653 | |||
| 654 | $app->db->init_db(qw(users)); | ||
| 655 | |||
| 656 | =cut | ||
| 657 | |||
| 658 | sub init_db { | ||
| 659 | my ($self, @tables) = @_; | ||
| 660 | |||
| 661 | $self->_connect(); | ||
| 662 | |||
| 663 | $self->_do($self->$_->create_sql()) foreach $self->_get_list_tables(@tables); | ||
| 664 | } | ||
| 665 | |||
| 666 | =head2 post_run | ||
| 667 | |||
| 668 | B<No arguments.> | ||
| 669 | |||
| 670 | Check that transaction closed | ||
| 671 | |||
| 672 | B<Example:> | ||
| 673 | |||
| 674 | $app->db->post_run(); | ||
| 675 | |||
| 676 | =cut | ||
| 677 | |||
| 678 | sub post_run { | ||
| 679 | my ($self) = @_; | ||
| 680 | |||
| 681 | if ($self->{'__SAVEPOINTS__'}) { | ||
| 682 | $self->{'__SAVEPOINTS__'} = 1; | ||
| 683 | $self->rollback(); | ||
| 684 | throw gettext("Unclosed transaction"); | ||
| 685 | } | ||
| 686 | } | ||
| 687 | |||
| 688 | sub _do { | ||
| 689 | my ($self, $sql, @params) = @_; | ||
| 690 | |||
| 691 | $self->timelog->start($self->_log_sql($sql, \@params)); | ||
| 692 | |||
| 693 | my ($res) = $self->_sub_with_connected_dbh( | ||
| 694 | sub { | ||
| 695 | my ($self, $sql, @params) = @_; | ||
| 696 | |||
| 697 | my $err_code; | ||
| 698 | return $self->dbh->do($sql, undef, @params) | ||
| 699 | || ($err_code = $self->dbh->err()) | ||
| 700 | && throw Exception::DB $self->dbh->errstr() . " ($err_code)\n" . $self->_log_sql($sql, \@params), | ||
| 701 | errorcode => $err_code; | ||
| 702 | }, | ||
| 703 | \@_ | ||
| 704 | ); | ||
| 705 | |||
| 706 | $self->timelog->finish(); | ||
| 707 | |||
| 708 | return $res; | ||
| 709 | } | ||
| 710 | |||
| 711 | sub _get_all { | ||
| 712 | my ($self, $sql, @params) = @_; | ||
| 713 | |||
| 714 | $self->timelog->start($self->_log_sql($sql, \@params)); | ||
| 715 | |||
| 716 | my ($data) = $self->_sub_with_connected_dbh( | ||
| 717 | sub { | ||
| 718 | my ($self, $sql, @params) = @_; | ||
| 719 | |||
| 720 | my $err_code; | ||
| 721 | $self->timelog->start(gettext('DBH prepare')); | ||
| 722 | my $sth = $self->dbh->prepare($sql) | ||
| 723 | || ($err_code = $self->dbh->err()) | ||
| 724 | && throw Exception::DB $self->dbh->errstr() . " ($err_code)\n" . $self->_log_sql($sql, \@params), | ||
| 725 | errorcode => $err_code; | ||
| 726 | |||
| 727 | $self->timelog->finish(); | ||
| 728 | |||
| 729 | $self->timelog->start(gettext('STH execute')); | ||
| 730 | $sth->execute(@params) | ||
| 731 | || ($err_code = $self->dbh->err()) | ||
| 732 | && throw Exception::DB $sth->errstr() . " ($err_code)\n" . $self->_log_sql($sql, \@params), | ||
| 733 | errorcode => $err_code; | ||
| 734 | $self->timelog->finish(); | ||
| 735 | |||
| 736 | $self->timelog->start(gettext('STH fetch_all')); | ||
| 737 | my $data = $sth->fetchall_arrayref({}) | ||
| 738 | || ($err_code = $self->dbh->err()) | ||
| 739 | && throw Exception::DB $sth->errstr() . " ($err_code)\n" . $self->_log_sql($sql, \@params), | ||
| 740 | errorcode => $err_code; | ||
| 741 | $self->timelog->finish(); | ||
| 742 | |||
| 743 | $self->timelog->start(gettext('STH finish')); | ||
| 744 | $sth->finish() | ||
| 745 | || ($err_code = $self->dbh->err()) | ||
| 746 | && throw Exception::DB $sth->errstr() . " ($err_code)\n" . $self->_log_sql($sql, \@params), | ||
| 747 | errorcode => $err_code; | ||
| 748 | $self->timelog->finish(); | ||
| 749 | |||
| 750 | return $data; | ||
| 751 | }, | ||
| 752 | \@_ | ||
| 753 | ); | ||
| 754 | |||
| 755 | $self->timelog->finish(); | ||
| 756 | |||
| 757 | return $data; | ||
| 758 | } | ||
| 759 | |||
| 760 | sub _log_sql { | ||
| 761 | my ($self, $sql, $params) = @_; | ||
| 762 | |||
| 763 | if (defined($params) && @$params) { | ||
| 764 | my $i = 0; | ||
| 765 | $sql =~ s/\?/$self->quote($params->[$i++])/eg; | ||
| 766 | } | ||
| 767 | |||
| 768 | l $sql if $DEBUG; | ||
| 769 | |||
| 770 | return $sql; | ||
| 771 | } | ||
| 772 | |||
| 773 | sub _add_meta { | ||
| 774 | my ($self, $res, $meta) = @_; | ||
| 775 | |||
| 776 | foreach my $obj_type (keys %{$meta}) { | ||
| 777 | foreach my $obj (keys %{$meta->{$obj_type}}) { | ||
| 778 | warn gettext('Object "%s" (%s) overrided', $obj, $obj_type) | ||
| 779 | if exists($res->{$obj_type}{$obj}); | ||
| 780 | $res->{$obj_type}{$obj} = $meta->{$obj_type}{$obj}; | ||
| 781 | } | ||
| 782 | } | ||
| 783 | } | ||
| 784 | |||
| 785 | sub _table_tree_level { | ||
| 786 | my ($self, $tables, $table_name, $level) = @_; | ||
| 787 | |||
| 788 | return $self->{'__TABLE_TREE_LEVEL__'}{$table_name} + $level | ||
| 789 | if exists($self->{'__TABLE_TREE_LEVEL__'}{$table_name}); | ||
| 790 | |||
| 791 | my @foreign_tables = (); | ||
| 792 | if (exists($tables->{$table_name})) { | ||
| 793 | @foreign_tables = | ||
| 794 | ((map {$_->[1]} @{$tables->{$table_name}{'foreign_keys'}}), @{$tables->{$table_name}{'inherits'} || []}); | ||
| 795 | } else { | ||
| 796 | @foreign_tables = ((map {$_->[1]} @{$self->$table_name->foreign_keys()}), @{$self->$table_name->inherits()}); | ||
| 797 | } | ||
| 798 | |||
| 799 | return @foreign_tables | ||
| 800 | ? array_max(map {$self->_table_tree_level($tables, $_, $level + 1)} @foreign_tables) | ||
| 801 | : $level; | ||
| 802 | } | ||
| 803 | |||
| 804 | sub _sorted_tables { | ||
| 805 | my ($self, @table_names) = @_; | ||
| 806 | |||
| 807 | return | ||
| 808 | sort {($self->{'__TABLE_TREE_LEVEL__'}{$a} || 0) <=> ($self->{'__TABLE_TREE_LEVEL__'}{$b} || 0) || $a cmp $b} | ||
| 809 | @table_names; | ||
| 810 | } | ||
| 811 | |||
| 812 | sub _sub_with_connected_dbh { | ||
| 813 | my ($self, $sub, $params, $try) = @_; | ||
| 814 | |||
| 815 | $try ||= 1; | ||
| 816 | my @res; | ||
| 817 | |||
| 818 | try { | ||
| 819 | $self->_connect() unless $self->{'__SAVEPOINTS__'}; | ||
| 820 | @res = $sub->(@{$params || []}); | ||
| 821 | } | ||
| 822 | catch { | ||
| 823 | my $exception = shift; | ||
| 824 | |||
| 825 | if ( | ||
| 826 | $try < 3 | ||
| 827 | && (!defined($self->dbh) | ||
| 828 | || $self->_is_connection_error($exception->{'errorcode'} || $self->dbh->err())) | ||
| 829 | ) | ||
| 830 | { | ||
| 831 | $self->set_dbh() if defined($self->dbh); | ||
| 832 | |||
| 833 | if ($self->{'__SAVEPOINTS__'}) { | ||
| 834 | throw $exception; | ||
| 835 | } else { | ||
| 836 | @res = $self->_sub_with_connected_dbh($sub, $params, $try + 1); | ||
| 837 | } | ||
| 838 | } else { | ||
| 839 | throw $exception; | ||
| 840 | } | ||
| 841 | }; | ||
| 842 | |||
| 843 | return @res; | ||
| 844 | } | ||
| 845 | |||
| 846 | TRUE; | ||
| 847 | |||
| 848 | =head1 Internal packages | ||
| 849 | |||
| 850 | =over | ||
| 851 | |||
| 852 | =item B<L<QBit::Application::Model::DB::Class>> - base class for DB modules; | ||
| 853 | |||
| 854 | =item B<L<QBit::Application::Model::DB::Field>> - base class for DB fields; | ||
| 855 | |||
| 856 | =item B<L<QBit::Application::Model::DB::Filter>> - base class for DB filters; | ||
| 857 | |||
| 858 | =item B<L<QBit::Application::Model::DB::Query>> - base class for DB queries; | ||
| 859 | |||
| 860 | =item B<L<QBit::Application::Model::DB::Table>> - base class for DB tables; | ||
| 861 | |||
| 862 | =item B<L<QBit::Application::Model::DB::VirtualTable>> - base class for DB virtual tables; | ||
| 863 | |||
| 864 | =back |