| 1 | package DBIx::Oro::Driver::SQLite; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use DBIx::Oro; | ||
| 5 | our @ISA; | ||
| 6 | BEGIN { @ISA = 'DBIx::Oro' }; | ||
| 7 | |||
| 8 | use v5.10.1; | ||
| 9 | |||
| 10 | # Todo: use 'truncate' for table deletion | ||
| 11 | |||
| 12 | # Defaults to 500 for SQLITE_MAX_COMPOUND_SELECT | ||
| 13 | our $MAX_COMP_SELECT; | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | $MAX_COMP_SELECT = 500; | ||
| 17 | }; | ||
| 18 | |||
| 19 | use Carp qw/carp/; | ||
| 20 | |||
| 21 | # Find and create database file | ||
| 22 | use File::Path; | ||
| 23 | use File::Basename; | ||
| 24 | |||
| 25 | # Default arguments for snippet function | ||
| 26 | my @arguments = | ||
| 27 | qw/start end ellipsis column token/; | ||
| 28 | |||
| 29 | my $arguments = qr/^(?:start|end|ellipsis|column|token)$/; | ||
| 30 | |||
| 31 | my @default = ('<b>', '</b>', '<b>...</b>', -1, -15); | ||
| 32 | |||
| 33 | |||
| 34 | # Constructor | ||
| 35 | sub new { | ||
| 36 | my $class = shift; | ||
| 37 | my %param = @_; | ||
| 38 | $param{created} //= 0; | ||
| 39 | |||
| 40 | my $autocommit = delete $param{autocommit}; | ||
| 41 | |||
| 42 | # Bless object with hash | ||
| 43 | my $self = bless \%param, $class; | ||
| 44 | |||
| 45 | # Store filename | ||
| 46 | my $file = $self->{file} = $param{file} // ''; | ||
| 47 | |||
| 48 | # Temporary or memory file | ||
| 49 | if (!$file || $file eq ':memory:') { | ||
| 50 | $self->{created} = 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Create path for file - based on ORLite | ||
| 54 | elsif (!-e $file) { | ||
| 55 | |||
| 56 | my $dir = File::Basename::dirname($file); | ||
| 57 | unless (-d $dir) { | ||
| 58 | File::Path::mkpath( $dir, { verbose => 0 } ); | ||
| 59 | }; | ||
| 60 | |||
| 61 | # Touch the file | ||
| 62 | if (open(TOUCH,'>' . $file)) { | ||
| 63 | $self->{created} = 1; | ||
| 64 | close(TOUCH); | ||
| 65 | }; | ||
| 66 | }; | ||
| 67 | |||
| 68 | # Data source name | ||
| 69 | $self->{dsn} = 'dbi:SQLite:dbname=' . $self->{file}; | ||
| 70 | |||
| 71 | # Attach hash | ||
| 72 | $self->{attached} = {} unless exists $self->{attached}; | ||
| 73 | |||
| 74 | # Autocommit | ||
| 75 | ${$self->{autocommit}} = | ||
| 76 | ${$self->{_autocounter}} = 0; | ||
| 77 | |||
| 78 | # Set autocommit | ||
| 79 | $self->autocommit($autocommit) if $autocommit; | ||
| 80 | |||
| 81 | # Return object | ||
| 82 | $self; | ||
| 83 | }; | ||
| 84 | |||
| 85 | |||
| 86 | # Initialize database if newly created | ||
| 87 | sub _init { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | # Get callback | ||
| 91 | my $cb = delete $self->{init} if $self->{init} && | ||
| 92 | (ref $self->{init} || '') eq 'CODE'; | ||
| 93 | |||
| 94 | # Import SQL file | ||
| 95 | my $import = delete $self->{import}; | ||
| 96 | my $import_cb = delete $self->{import_cb}; | ||
| 97 | |||
| 98 | # Initialize database if newly created | ||
| 99 | if ($self->created && ($import || $cb)) { | ||
| 100 | |||
| 101 | # Start creation transaction | ||
| 102 | unless ( | ||
| 103 | $self->txn( | ||
| 104 | sub { | ||
| 105 | |||
| 106 | # Import SQL file | ||
| 107 | if ($import) { | ||
| 108 | $self->import_sql($import, $import_cb) or return -1; | ||
| 109 | }; | ||
| 110 | |||
| 111 | # Release callback | ||
| 112 | if ($cb) { | ||
| 113 | local $_ = $self; | ||
| 114 | return $cb->($self); | ||
| 115 | }; | ||
| 116 | return 1; | ||
| 117 | }) | ||
| 118 | ) { | ||
| 119 | |||
| 120 | # Unlink SQLite database | ||
| 121 | if (index($self->file, ':') != 0) { | ||
| 122 | unlink $self->file; | ||
| 123 | }; | ||
| 124 | |||
| 125 | # Not successful | ||
| 126 | $self = undef; | ||
| 127 | return; | ||
| 128 | }; | ||
| 129 | }; | ||
| 130 | |||
| 131 | return 1; | ||
| 132 | }; | ||
| 133 | |||
| 134 | |||
| 135 | # Connect to database | ||
| 136 | sub _connect { | ||
| 137 | my $self = shift; | ||
| 138 | my $dbh = $self->SUPER::_connect( | ||
| 139 | sqlite_unicode => defined $self->{unicode} ? $self->{unicode} : 1 | ||
| 140 | ); | ||
| 141 | |||
| 142 | # Turn foreign keys on as default | ||
| 143 | $dbh->do('PRAGMA foreign_keys = ON') unless $self->{foreign_keys}; | ||
| 144 | |||
| 145 | # Set busy timeout | ||
| 146 | $dbh->sqlite_busy_timeout( $self->{busy_timeout} || 300 ); | ||
| 147 | |||
| 148 | # Reattach possibly attached databases | ||
| 149 | while (my ($db_name, $file) = each %{$self->{attached}}) { | ||
| 150 | $self->prep_and_exec("ATTACH '$file' AS ?", [$db_name]); | ||
| 151 | }; | ||
| 152 | |||
| 153 | # Return database handle | ||
| 154 | $dbh; | ||
| 155 | }; | ||
| 156 | |||
| 157 | |||
| 158 | # File of database | ||
| 159 | sub file { $_[0]->{file} // '' }; | ||
| 160 | |||
| 161 | |||
| 162 | # Database driver | ||
| 163 | sub driver { 'SQLite' }; | ||
| 164 | |||
| 165 | |||
| 166 | # Database was just created | ||
| 167 | sub created { | ||
| 168 | my $self = shift; | ||
| 169 | |||
| 170 | # Creation state is 0 | ||
| 171 | return 0 unless $self->{created}; | ||
| 172 | |||
| 173 | # Check for thread id | ||
| 174 | if (defined $self->{tid} && $self->{tid} != threads->tid) { | ||
| 175 | return ($self->{created} = 0); | ||
| 176 | } | ||
| 177 | |||
| 178 | # Check for process id | ||
| 179 | elsif ($self->{pid} != $$) { | ||
| 180 | return ($self->{created} = 0); | ||
| 181 | }; | ||
| 182 | |||
| 183 | # Return creation state | ||
| 184 | return 1; | ||
| 185 | }; | ||
| 186 | |||
| 187 | |||
| 188 | # Explain query plan | ||
| 189 | sub explain { | ||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | # Prepare and execute explain query plan | ||
| 193 | my ($rv, $sth) = $self->prep_and_exec( | ||
| 194 | 'EXPLAIN QUERY PLAN ' . shift, @_ | ||
| 195 | ); | ||
| 196 | |||
| 197 | # Query was not succesfull | ||
| 198 | return unless $rv; | ||
| 199 | |||
| 200 | # Create string | ||
| 201 | my $string; | ||
| 202 | foreach ( @{ $sth->fetchall_arrayref([]) }) { | ||
| 203 | $string .= sprintf("%3d | %3d | %3d | %-60s\n", @$_); | ||
| 204 | }; | ||
| 205 | |||
| 206 | # Return query plan string | ||
| 207 | return $string; | ||
| 208 | }; | ||
| 209 | |||
| 210 | |||
| 211 | # Delete with SQLite feature | ||
| 212 | sub delete { | ||
| 213 | my $self = shift; | ||
| 214 | my $secure; | ||
| 215 | |||
| 216 | # Check if -secure parameter is set | ||
| 217 | if ($_[-1] && ref $_[-1] && ref $_[-1] eq 'HASH') { | ||
| 218 | $secure = delete $_[-1]->{-secure} || 0; | ||
| 219 | }; | ||
| 220 | |||
| 221 | # Delete | ||
| 222 | unless ($secure) { | ||
| 223 | |||
| 224 | my $rv = $self->SUPER::delete(@_); | ||
| 225 | |||
| 226 | # Decrement autocommit | ||
| 227 | $self->_decr_commit if $rv; | ||
| 228 | |||
| 229 | return $rv; | ||
| 230 | } | ||
| 231 | |||
| 232 | # Delete securely | ||
| 233 | else { | ||
| 234 | |||
| 235 | # Security value | ||
| 236 | my $sec_value; | ||
| 237 | |||
| 238 | # Retrieve secure delete pragma | ||
| 239 | my ($rv, $sth) = $self->prep_and_exec('PRAGMA secure_delete'); | ||
| 240 | $sec_value = $sth->fetchrow_array if $rv; | ||
| 241 | $sth->finish; | ||
| 242 | |||
| 243 | # Set secure_delete pragma | ||
| 244 | $self->do('PRAGMA secure_delete = ON') unless $sec_value; | ||
| 245 | |||
| 246 | # Delete | ||
| 247 | $rv = $self->SUPER::delete(@_); | ||
| 248 | |||
| 249 | # Decrement autocommit | ||
| 250 | $self->_decr_commit if $rv; | ||
| 251 | |||
| 252 | # Reset secure_delete pragma | ||
| 253 | $self->do('PRAGMA secure_delete = OFF') unless $sec_value; | ||
| 254 | |||
| 255 | # Return value | ||
| 256 | return $rv; | ||
| 257 | }; | ||
| 258 | }; | ||
| 259 | |||
| 260 | |||
| 261 | # Insert values to database | ||
| 262 | sub insert { | ||
| 263 | my $self = shift; | ||
| 264 | |||
| 265 | # Get table name | ||
| 266 | my $table = $self->_table_name(\@_) or return; | ||
| 267 | |||
| 268 | # No parameters | ||
| 269 | return unless $_[0]; | ||
| 270 | |||
| 271 | # Properties | ||
| 272 | my $prop = shift if ref $_[0] eq 'HASH' && ref $_[1]; | ||
| 273 | |||
| 274 | # Single insert | ||
| 275 | if (ref $_[0] eq 'HASH') { | ||
| 276 | |||
| 277 | # Param | ||
| 278 | my %param = %{ shift(@_) }; | ||
| 279 | |||
| 280 | # Create insert arrays | ||
| 281 | my (@keys, @values); | ||
| 282 | while (my ($key, $value) = each %param) { | ||
| 283 | # Insert pairs | ||
| 284 | next if !ref $key && $key !~ $DBIx::Oro::KEY_REGEX; | ||
| 285 | push @keys, $key; | ||
| 286 | push @values, $value; | ||
| 287 | }; | ||
| 288 | |||
| 289 | # Nothing to insert | ||
| 290 | return unless @keys; | ||
| 291 | |||
| 292 | # Create insert string | ||
| 293 | my $sql = 'INSERT '; | ||
| 294 | |||
| 295 | if ($prop && (my $oc = $prop->{-on_conflict})) { | ||
| 296 | if ($oc eq 'replace') { | ||
| 297 | $sql = 'REPLACE ' | ||
| 298 | } | ||
| 299 | elsif ($oc eq 'ignore') { | ||
| 300 | $sql .= 'IGNORE ' | ||
| 301 | }; | ||
| 302 | }; | ||
| 303 | |||
| 304 | $sql .= 'INTO ' . $table . | ||
| 305 | ' (' . join(', ', map { "`$_`" } @keys) . ') VALUES (' . DBIx::Oro::_q(\@values) . ')'; | ||
| 306 | |||
| 307 | # Prepare and execute | ||
| 308 | my $rv = $self->prep_and_exec( $sql, \@values ); | ||
| 309 | |||
| 310 | # Decrement autocommit | ||
| 311 | $self->_decr_commit if $rv; | ||
| 312 | |||
| 313 | return $rv; | ||
| 314 | } | ||
| 315 | |||
| 316 | # Multiple inserts | ||
| 317 | elsif (ref($_[0]) eq 'ARRAY') { | ||
| 318 | |||
| 319 | return unless $_[1]; | ||
| 320 | |||
| 321 | my @keys = @{ shift(@_) }; | ||
| 322 | |||
| 323 | # Default values | ||
| 324 | my @default = (); | ||
| 325 | |||
| 326 | # Check if keys are defaults | ||
| 327 | my $i = 0; | ||
| 328 | my @default_keys; | ||
| 329 | while ($keys[$i]) { | ||
| 330 | |||
| 331 | # No default - next | ||
| 332 | $i++, next unless ref $keys[$i]; | ||
| 333 | |||
| 334 | # Has default value | ||
| 335 | my ($key, $value) = @{ splice( @keys, $i, 1) }; | ||
| 336 | push(@default_keys, $key); | ||
| 337 | push(@default, $value); | ||
| 338 | }; | ||
| 339 | |||
| 340 | # Unshift default keys to front | ||
| 341 | unshift(@keys, @default_keys); | ||
| 342 | |||
| 343 | my $sql = 'INSERT INTO ' . $table . ' (' . join(', ', map { "`$_`" } @keys) . ') '; | ||
| 344 | my $union = 'SELECT ' . DBIx::Oro::_q(\@keys); | ||
| 345 | |||
| 346 | # Maximum bind variables | ||
| 347 | my $max = ($MAX_COMP_SELECT / @keys) - @keys; | ||
| 348 | |||
| 349 | if (scalar @_ <= $max) { | ||
| 350 | |||
| 351 | # Add data unions | ||
| 352 | $sql .= $union . ((' UNION ' . $union) x ( scalar(@_) - 1 )); | ||
| 353 | |||
| 354 | # Prepare and execute with prepended defaults | ||
| 355 | my @rv = $self->prep_and_exec( | ||
| 356 | $sql, | ||
| 357 | [ map { (@default, @$_); } @_ ] | ||
| 358 | ); | ||
| 359 | |||
| 360 | # Decrement autocommit | ||
| 361 | $self->_decr_commit if $rv[0]; | ||
| 362 | |||
| 363 | return @rv; | ||
| 364 | } | ||
| 365 | |||
| 366 | # More than SQLite MAX_COMP_SELECT insertions | ||
| 367 | else { | ||
| 368 | |||
| 369 | my ($rv, @v_array); | ||
| 370 | my @values = @_; | ||
| 371 | |||
| 372 | # Start transaction | ||
| 373 | $self->txn( | ||
| 374 | sub { | ||
| 375 | while (@v_array = splice(@values, 0, $max - 1)) { | ||
| 376 | |||
| 377 | # Delete undef values | ||
| 378 | @v_array = grep($_, @v_array) unless @_; | ||
| 379 | |||
| 380 | # Add data unions | ||
| 381 | my $sub_sql = $sql . $union . | ||
| 382 | ((' UNION ' . $union) x ( scalar(@v_array) - 1 )); | ||
| 383 | |||
| 384 | # Prepare and execute | ||
| 385 | my $rv_part = $self->prep_and_exec( | ||
| 386 | $sub_sql, | ||
| 387 | [ map { (@default, @$_); } @v_array ] | ||
| 388 | ); | ||
| 389 | |||
| 390 | # Rollback transaction | ||
| 391 | return -1 unless $rv_part; | ||
| 392 | $rv += $rv_part; | ||
| 393 | }; | ||
| 394 | |||
| 395 | }) or return; | ||
| 396 | |||
| 397 | # Decrement autocommit | ||
| 398 | $self->_decr_commit if $rv; | ||
| 399 | |||
| 400 | # Everything went fine | ||
| 401 | return $rv; | ||
| 402 | }; | ||
| 403 | }; | ||
| 404 | |||
| 405 | # Unknown query | ||
| 406 | return; | ||
| 407 | }; | ||
| 408 | |||
| 409 | |||
| 410 | # Update existing values in the database | ||
| 411 | sub update { | ||
| 412 | my $self = shift; | ||
| 413 | |||
| 414 | my $rv = $self->SUPER::update(@_); | ||
| 415 | |||
| 416 | # Decrement autocommit | ||
| 417 | $self->_decr_commit if $rv; | ||
| 418 | |||
| 419 | return $rv; | ||
| 420 | }; | ||
| 421 | |||
| 422 | |||
| 423 | sub merge { | ||
| 424 | my $self = shift; | ||
| 425 | |||
| 426 | my ($rv, $type) = $self->SUPER::merge(@_); | ||
| 427 | |||
| 428 | if ($rv && $type eq 'insert' && ${$self->{autocommit}}) { | ||
| 429 | ${$self->{_autocounter}}--; | ||
| 430 | }; | ||
| 431 | |||
| 432 | return wantarray ? ($rv, $type) : $rv; | ||
| 433 | }; | ||
| 434 | |||
| 435 | # Attach database | ||
| 436 | sub attach { | ||
| 437 | my ($self, $db_name, $file) = @_; | ||
| 438 | |||
| 439 | $file //= ''; | ||
| 440 | |||
| 441 | # Attach file, memory or temporary database | ||
| 442 | my $rv = scalar $self->prep_and_exec("ATTACH '$file' AS ?", [$db_name]); | ||
| 443 | |||
| 444 | $self->{attached}->{$db_name} = $file; | ||
| 445 | |||
| 446 | return $rv; | ||
| 447 | }; | ||
| 448 | |||
| 449 | |||
| 450 | # Detach database | ||
| 451 | sub detach { | ||
| 452 | my $self = shift; | ||
| 453 | return unless $_[0]; | ||
| 454 | |||
| 455 | # Detach all databases | ||
| 456 | foreach my $db_name (@_) { | ||
| 457 | delete $self->{attached}->{$db_name}; | ||
| 458 | return unless $self->prep_and_exec('DETACH ?', [$db_name]); | ||
| 459 | }; | ||
| 460 | |||
| 461 | return 1; | ||
| 462 | }; | ||
| 463 | |||
| 464 | |||
| 465 | # Wrapper for sqlite last_insert_row_id | ||
| 466 | sub last_insert_id { | ||
| 467 | shift->dbh->sqlite_last_insert_rowid; | ||
| 468 | }; | ||
| 469 | |||
| 470 | |||
| 471 | # Create matchinfo function | ||
| 472 | sub matchinfo { | ||
| 473 | my $self = shift; | ||
| 474 | |||
| 475 | # Use no multibyte characters | ||
| 476 | # use bytes; | ||
| 477 | |||
| 478 | # Format string | ||
| 479 | my $format = lc(shift) if $_[0] && $_[0] =~ /^[pcnalsx]+$/i; | ||
| 480 | |||
| 481 | # Return anonymous subroutine | ||
| 482 | return sub { | ||
| 483 | my $column; | ||
| 484 | if (@_) { | ||
| 485 | $column = shift || 'content'; | ||
| 486 | |||
| 487 | # Format string | ||
| 488 | $format = lc(shift) if $_[0] && $_[0] =~ /^[pcnalsx]+$/i; | ||
| 489 | }; | ||
| 490 | |||
| 491 | # Sort format for leading 'pc' if needed | ||
| 492 | if ($format) { | ||
| 493 | for ($format) { | ||
| 494 | |||
| 495 | # Sort alphabetically | ||
| 496 | $_ = join('', sort split('', $_)); | ||
| 497 | |||
| 498 | # Delete repeating characters | ||
| 499 | s/(.)\1+/$1/g; | ||
| 500 | |||
| 501 | # Prepend 'pc' if necessary | ||
| 502 | if (/[xals]/) { | ||
| 503 | tr/pc//d; # Delete 'pc' | ||
| 504 | $_ = 'pc' . $format; # Prepend 'pc' | ||
| 505 | }; | ||
| 506 | }; | ||
| 507 | } | ||
| 508 | |||
| 509 | # No format given | ||
| 510 | else { | ||
| 511 | $format = 'pcx'; | ||
| 512 | }; | ||
| 513 | |||
| 514 | # Return anonymous subroutine | ||
| 515 | return sub { | ||
| 516 | return | ||
| 517 | 'matchinfo(' . $column . ', "' . $format . '")', | ||
| 518 | \&_matchinfo_return, | ||
| 519 | $format; | ||
| 520 | }; | ||
| 521 | }; | ||
| 522 | }; | ||
| 523 | |||
| 524 | |||
| 525 | # Treat matchinfo return | ||
| 526 | sub _matchinfo_return { | ||
| 527 | my ($blob, $format) = @_; | ||
| 528 | |||
| 529 | # Get 32-bit blob chunks | ||
| 530 | my @matchinfo = unpack('l' . (length($blob) * 4), $blob); | ||
| 531 | |||
| 532 | # Parse format character | ||
| 533 | my %match; | ||
| 534 | foreach (split '', $format) { | ||
| 535 | |||
| 536 | # Characters: p, c, n | ||
| 537 | if ($_ eq 'p' or $_ eq 'c' or $_ eq 'n') { | ||
| 538 | $match{$_} = shift @matchinfo; | ||
| 539 | } | ||
| 540 | |||
| 541 | # Characters: a, l, s | ||
| 542 | elsif ($_ eq 'a' or $_ eq 'l' or $_ eq 's') { | ||
| 543 | $match{$_} = [ splice(@matchinfo, 0, $match{c}) ]; | ||
| 544 | } | ||
| 545 | |||
| 546 | # Characters: x | ||
| 547 | elsif ($_ eq 'x') { | ||
| 548 | my @match; | ||
| 549 | for (1 .. ($match{p} * $match{c})) { | ||
| 550 | push(@match, [ splice(@matchinfo, 0, 3) ]); | ||
| 551 | }; | ||
| 552 | |||
| 553 | $match{$_} = \@match; | ||
| 554 | } | ||
| 555 | |||
| 556 | # Unknown character | ||
| 557 | else { | ||
| 558 | shift @matchinfo; | ||
| 559 | }; | ||
| 560 | }; | ||
| 561 | return \%match; | ||
| 562 | }; | ||
| 563 | |||
| 564 | |||
| 565 | # Create offsets function | ||
| 566 | sub offsets { | ||
| 567 | my $self = shift; | ||
| 568 | my $field = shift; | ||
| 569 | |||
| 570 | # Use no multibyte characters | ||
| 571 | # use bytes; | ||
| 572 | |||
| 573 | # subroutine | ||
| 574 | return sub { | ||
| 575 | my $column = shift; | ||
| 576 | 'offsets(' . ($column // $field // 'content') . ')', | ||
| 577 | sub { | ||
| 578 | my $blob = shift; | ||
| 579 | my @offset; | ||
| 580 | my @array = split(/\s/, $blob); | ||
| 581 | while (@array) { | ||
| 582 | push(@offset, [ splice(@array, 0, 4) ]); | ||
| 583 | }; | ||
| 584 | return \@offset; | ||
| 585 | } | ||
| 586 | }; | ||
| 587 | }; | ||
| 588 | |||
| 589 | |||
| 590 | # Create snippet function | ||
| 591 | sub snippet { | ||
| 592 | my $self = shift; | ||
| 593 | |||
| 594 | # Snippet parameters | ||
| 595 | my @snippet; | ||
| 596 | |||
| 597 | # Parameters are given | ||
| 598 | if ($_[0]) { | ||
| 599 | my %snippet = (); | ||
| 600 | |||
| 601 | # Parameters are given as a hash | ||
| 602 | if ($_[0] =~ $arguments) { | ||
| 603 | %snippet = @_; | ||
| 604 | foreach (keys %snippet) { | ||
| 605 | carp "Unknown snippet parameter '$_'" unless $_ =~ $arguments; | ||
| 606 | }; | ||
| 607 | } | ||
| 608 | |||
| 609 | # Parameters are given as an array | ||
| 610 | else { | ||
| 611 | @snippet{@arguments} = @_; | ||
| 612 | }; | ||
| 613 | |||
| 614 | # Trim parameter array and fill gaps with defaults | ||
| 615 | my ($s, $i) = (0, 0); | ||
| 616 | foreach (reverse @arguments) { | ||
| 617 | $s = 1 if defined $snippet{$_}; | ||
| 618 | unshift(@snippet, $snippet{$_} // $default[$i]) if $s; | ||
| 619 | $i++; | ||
| 620 | }; | ||
| 621 | }; | ||
| 622 | |||
| 623 | # Return anonymous subroutine | ||
| 624 | my $sub = 'sub { | ||
| 625 | my $column = $_[0] ? shift : \'content\'; | ||
| 626 | my $str = "snippet(" . $column '; | ||
| 627 | |||
| 628 | if ($snippet[0]) { | ||
| 629 | $sub .= ' . ", ' . join(',', map { '\"' . $_ . '\"' } @snippet) . '"'; | ||
| 630 | }; | ||
| 631 | |||
| 632 | $sub .= " . \")\";\n};"; | ||
| 633 | |||
| 634 | return eval( $sub ); | ||
| 635 | }; | ||
| 636 | |||
| 637 | |||
| 638 | # New table object | ||
| 639 | sub table { | ||
| 640 | my $self = shift; | ||
| 641 | |||
| 642 | # Get object from superclass | ||
| 643 | my $table = $self->SUPER::table(@_); | ||
| 644 | |||
| 645 | # Add autocommit parameters | ||
| 646 | foreach (qw/autocommit _autocounter | ||
| 647 | foreign_keys/) { | ||
| 648 | $table->{$_} = $self->{$_}; | ||
| 649 | }; | ||
| 650 | |||
| 651 | # Return blessed object | ||
| 652 | return $table; | ||
| 653 | }; | ||
| 654 | |||
| 655 | |||
| 656 | # Set foreign_key pragma | ||
| 657 | sub foreign_keys { | ||
| 658 | my $self = shift; | ||
| 659 | |||
| 660 | # Get pragma | ||
| 661 | unless (defined $_[0]) { | ||
| 662 | return $self->{foreign_keys}; | ||
| 663 | } | ||
| 664 | |||
| 665 | # Turn foreign keys on | ||
| 666 | elsif ($_[0] && !$self->{foreign_keys}) { | ||
| 667 | $self->dbh->do('PRAGMA foreign_keys = ON'); | ||
| 668 | return ($self->{foreign_keys} = 1); | ||
| 669 | } | ||
| 670 | |||
| 671 | # Turn foreign keys off | ||
| 672 | elsif (!$_[0] && $self->{foreign_keys}) { | ||
| 673 | $self->dbh->do('PRAGMA foreign_keys = OFF'); | ||
| 674 | return ($self->{foreign_keys} = 0); | ||
| 675 | }; | ||
| 676 | |||
| 677 | return; | ||
| 678 | }; | ||
| 679 | |||
| 680 | |||
| 681 | # Set autocommit | ||
| 682 | sub autocommit { | ||
| 683 | my $self = shift; | ||
| 684 | |||
| 685 | # Get autocommit | ||
| 686 | unless (defined $_[0]) { | ||
| 687 | return ${$self->{autocommit}} || 0; | ||
| 688 | } | ||
| 689 | |||
| 690 | # Set autocommit | ||
| 691 | else { | ||
| 692 | my $num = shift; | ||
| 693 | my $dbh = $self->dbh; | ||
| 694 | |||
| 695 | # Is a number | ||
| 696 | if ($num && $num =~ m/^\d+/o) { | ||
| 697 | if ($num > 1) { | ||
| 698 | $dbh->{AutoCommit} = 0; | ||
| 699 | ${$self->{autocommit}} = | ||
| 700 | ${$self->{_autocounter}} = $num; | ||
| 701 | return 1; | ||
| 702 | } | ||
| 703 | |||
| 704 | else { | ||
| 705 | $dbh->{AutoCommit} = 1; | ||
| 706 | ${$self->{_autocounter}} = | ||
| 707 | ${$self->{autocommit}} = 0; | ||
| 708 | return 1; | ||
| 709 | }; | ||
| 710 | } | ||
| 711 | |||
| 712 | # Is null | ||
| 713 | elsif (!$num) { | ||
| 714 | ${$self->{autocommit}} = 0; | ||
| 715 | if (${$self->{_autocounter}}) { | ||
| 716 | ${$self->{_autocounter}} = 0; | ||
| 717 | $dbh->commit; | ||
| 718 | }; | ||
| 719 | $dbh->{AutoCommit} = 1 unless ${$self->{in_txn}}; | ||
| 720 | return 1; | ||
| 721 | } | ||
| 722 | |||
| 723 | # Failure | ||
| 724 | else { | ||
| 725 | return; | ||
| 726 | }; | ||
| 727 | }; | ||
| 728 | }; | ||
| 729 | |||
| 730 | |||
| 731 | # Decrement commit counter | ||
| 732 | sub _decr_commit { | ||
| 733 | my $self = shift; | ||
| 734 | |||
| 735 | # Autocounter is set | ||
| 736 | if (${$self->{_autocounter}}) { | ||
| 737 | my $auto = --${$self->{_autocounter}}; | ||
| 738 | |||
| 739 | # Commit is null | ||
| 740 | unless ($auto) { | ||
| 741 | |||
| 742 | $self->dbh->commit unless ${$self->{in_txn}}; | ||
| 743 | ${$self->{_autocounter}} = ${$self->{autocommit}}; | ||
| 744 | }; | ||
| 745 | }; | ||
| 746 | }; | ||
| 747 | |||
| 748 | |||
| 749 | 1; | ||
| 750 | |||
| 751 | |||
| 752 | __END__ |