| 1 | package DBIx::Skinny; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.0742'; | ||
| 6 | |||
| 7 | use DBI; | ||
| 8 | use DBIx::Skinny::Iterator; | ||
| 9 | use DBIx::Skinny::DBD; | ||
| 10 | use DBIx::Skinny::Row; | ||
| 11 | use DBIx::Skinny::Util; | ||
| 12 | use DBIx::TransactionManager 1.07; | ||
| 13 | use Carp (); | ||
| 14 | use Storable (); | ||
| 15 | |||
| 16 | sub import { | ||
| 17 | my ($class, %opt) = @_; | ||
| 18 | |||
| 19 | return if $class ne 'DBIx::Skinny'; | ||
| 20 | |||
| 21 | my $caller = caller; | ||
| 22 | my $connect_info = $opt{connect_info}; | ||
| 23 | if (! $connect_info ) { | ||
| 24 | if ( $connect_info = $opt{setup} ) { | ||
| 25 | Carp::carp( "use DBIx::Skinny setup => { ... } has been deprecated. Please use connect_info instead" ); | ||
| 26 | } else { | ||
| 27 | $connect_info = {}; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | my $profiler = $opt{profiler}; | ||
| 32 | if (! $profiler ) { | ||
| 33 | if ( $profiler = $connect_info->{profiler} ) { | ||
| 34 | Carp::carp( "use DBIx::Skinny connect_info => { profiler => ... } has been deprecated. Please use use DBIx::Skinny profiler => ... instead" ); | ||
| 35 | } elsif ($ENV{SKINNY_TRACE}) { | ||
| 36 | require DBIx::Skinny::Profiler::Trace; | ||
| 37 | $profiler = DBIx::Skinny::Profiler::Trace->new; | ||
| 38 | } elsif ($ENV{SKINNY_PROFILE}) { | ||
| 39 | require DBIx::Skinny::Profiler; | ||
| 40 | $profiler = DBIx::Skinny::Profiler->new; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | my $schema = $opt{schema} || "$caller\::Schema"; | ||
| 45 | |||
| 46 | my $driver_name = _guess_driver_name($connect_info); | ||
| 47 | my $_attributes = +{ | ||
| 48 | check_schema => defined $connect_info->{check_schema} ? $connect_info->{check_schema} : 1, | ||
| 49 | dsn => $connect_info->{dsn}, | ||
| 50 | username => $connect_info->{username}, | ||
| 51 | password => $connect_info->{password}, | ||
| 52 | connect_options => $connect_info->{connect_options}, | ||
| 53 | on_connect_do => $connect_info->{on_connect_do}, | ||
| 54 | dbh => $connect_info->{dbh}||undef, | ||
| 55 | driver_name => $driver_name, | ||
| 56 | schema => $schema, | ||
| 57 | profiler => $profiler, | ||
| 58 | klass => $caller, | ||
| 59 | _common_row_class => '', | ||
| 60 | suppress_row_objects => 0, | ||
| 61 | last_pid => $$, | ||
| 62 | }; | ||
| 63 | |||
| 64 | { | ||
| 65 | no strict 'refs'; | ||
| 66 | push @{"${caller}::ISA"}, $class; | ||
| 67 | *{"$caller\::_attributes"} = sub { ref $_[0] ? $_[0] : $_attributes }; | ||
| 68 | *{"$caller\::attribute"} = sub { Carp::carp("attribute has been deprecated."); $_[0]->_attributes }; | ||
| 69 | } | ||
| 70 | $caller->_setup_dbd; | ||
| 71 | |||
| 72 | DBIx::Skinny::Util::load_class($schema); | ||
| 73 | |||
| 74 | strict->import; | ||
| 75 | warnings->import; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my ($class, $connection_info) = @_; | ||
| 80 | my $attr = $class->_attributes; | ||
| 81 | |||
| 82 | my $new_attr; | ||
| 83 | for my $key (qw/check_schema dsn username password connect_options driver_name schema profiler klass _common_row_class suppress_row_objects/) { | ||
| 84 | $new_attr->{$key} = $attr->{$key}; | ||
| 85 | } | ||
| 86 | |||
| 87 | my $self = bless $new_attr, $class; | ||
| 88 | $new_attr = $self->_attributes; | ||
| 89 | $new_attr->{last_pid} = $$; | ||
| 90 | |||
| 91 | # restore. | ||
| 92 | for my $key (qw/dbd profiler dbh connect_options on_connect_do/) { | ||
| 93 | $new_attr->{$key} = $attr->{$key}; | ||
| 94 | } | ||
| 95 | |||
| 96 | if ($connection_info) { | ||
| 97 | if ( $connection_info->{on_connect_do} ) { | ||
| 98 | $new_attr->{on_connect_do} = $connection_info->{on_connect_do}; | ||
| 99 | } | ||
| 100 | |||
| 101 | $self->connect_info($connection_info); | ||
| 102 | if ($connection_info->{dbh}) { | ||
| 103 | $self->set_dbh($connection_info->{dbh}); | ||
| 104 | } else { | ||
| 105 | $self->reconnect; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | my $schema_checked = 0; | ||
| 113 | sub schema { | ||
| 114 | my $attribute = $_[0]->_attributes; | ||
| 115 | my $schema = $attribute->{schema}; | ||
| 116 | if ( $attribute->{check_schema} && !$schema_checked ) { | ||
| 117 | { | ||
| 118 | no strict 'refs'; ## no critic.. | ||
| 119 | unless ( defined *{"@{[ $schema ]}::schema_info"} ) { | ||
| 120 | die "Cannot use schema $schema ( is it really loaded? )"; | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | $schema_checked=1; | ||
| 124 | } | ||
| 125 | return $schema; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub profiler { | ||
| 129 | my ($class, $sql, $bind) = @_; | ||
| 130 | my $attr = $class->_attributes; | ||
| 131 | if ($attr->{profiler} && $sql) { | ||
| 132 | $attr->{profiler}->record_query($sql, $bind); | ||
| 133 | } | ||
| 134 | return $attr->{profiler}; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub suppress_row_objects { | ||
| 138 | my ($class, $mode) = @_; | ||
| 139 | return $class->_attributes->{suppress_row_objects} unless defined $mode; | ||
| 140 | $class->_attributes->{suppress_row_objects} = $mode; | ||
| 141 | } | ||
| 142 | |||
| 143 | #-------------------------------------------------------------------------------- | ||
| 144 | # for transaction | ||
| 145 | |||
| 146 | sub txn_manager { | ||
| 147 | my $class = shift; | ||
| 148 | |||
| 149 | my $dbh = $class->dbh; | ||
| 150 | $class->_attributes->{txn_manager} ||= DBIx::TransactionManager->new($dbh); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub in_transaction_check { | ||
| 154 | my $class = shift; | ||
| 155 | |||
| 156 | return unless $class->_attributes->{txn_manager}; | ||
| 157 | |||
| 158 | return $class->_attributes->{txn_manager}->in_transaction; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub txn_scope { | ||
| 162 | my @caller = caller(); | ||
| 163 | $_[0]->txn_manager->txn_scope(caller => \@caller); | ||
| 164 | } | ||
| 165 | sub txn_begin { $_[0]->txn_manager->txn_begin } | ||
| 166 | sub txn_rollback { $_[0]->txn_manager->txn_rollback } | ||
| 167 | sub txn_commit { $_[0]->txn_manager->txn_commit } | ||
| 168 | sub txn_end { $_[0]->txn_manager->txn_end } | ||
| 169 | |||
| 170 | #-------------------------------------------------------------------------------- | ||
| 171 | # db handling | ||
| 172 | sub connect_info { | ||
| 173 | my ($class, $connect_info) = @_; | ||
| 174 | |||
| 175 | my $attr = $class->_attributes; | ||
| 176 | |||
| 177 | if ($connect_info) { | ||
| 178 | $attr->{dsn} = $connect_info->{dsn}; | ||
| 179 | $attr->{username} = $connect_info->{username}; | ||
| 180 | $attr->{password} = $connect_info->{password}; | ||
| 181 | $attr->{connect_options} = $connect_info->{connect_options}; | ||
| 182 | |||
| 183 | $class->_setup_dbd($connect_info); | ||
| 184 | return; | ||
| 185 | } else { | ||
| 186 | return +{ | ||
| 187 | dsn => $attr->{dsn}, | ||
| 188 | username => $attr->{username}, | ||
| 189 | password => $attr->{password}, | ||
| 190 | connect_options => $attr->{connect_options}, | ||
| 191 | }; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | sub connect { | ||
| 196 | my $class = shift; | ||
| 197 | |||
| 198 | $class->connect_info(@_) if scalar @_ >= 1; | ||
| 199 | |||
| 200 | my $attr = $class->_attributes; | ||
| 201 | my $do_connected=0; | ||
| 202 | if ( !$attr->{dbh} ) { | ||
| 203 | $do_connected=1; | ||
| 204 | } | ||
| 205 | $attr->{dbh} ||= DBI->connect( | ||
| 206 | $attr->{dsn}, | ||
| 207 | $attr->{username}, | ||
| 208 | $attr->{password}, | ||
| 209 | { RaiseError => 1, PrintError => 0, AutoCommit => 1, %{ $attr->{connect_options} || {} } } | ||
| 210 | ) or Carp::croak("Connection error: " . $DBI::errstr); | ||
| 211 | |||
| 212 | if ( $do_connected && $attr->{on_connect_do} ) { | ||
| 213 | $class->do_on_connect; | ||
| 214 | } | ||
| 215 | |||
| 216 | $attr->{dbh}; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub reconnect { | ||
| 220 | my $class = shift; | ||
| 221 | |||
| 222 | if ( my $info = $class->in_transaction_check ) { | ||
| 223 | my $caller = $info->{caller}; | ||
| 224 | my $pid = $info->{pid}; | ||
| 225 | Carp::confess("Detected transaction during a reconnect operation (last known transaction at $caller->[1] line $caller->[2], pid $pid). Refusing to proceed at"); | ||
| 226 | } | ||
| 227 | |||
| 228 | $class->disconnect(); | ||
| 229 | $class->connect(@_); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub do_on_connect { | ||
| 233 | my $class = shift; | ||
| 234 | |||
| 235 | my $on_connect_do = $class->_attributes->{on_connect_do}; | ||
| 236 | if (not ref($on_connect_do)) { | ||
| 237 | $class->do($on_connect_do); | ||
| 238 | } elsif (ref($on_connect_do) eq 'CODE') { | ||
| 239 | $on_connect_do->($class); | ||
| 240 | } elsif (ref($on_connect_do) eq 'ARRAY') { | ||
| 241 | $class->do($_) for @$on_connect_do; | ||
| 242 | } else { | ||
| 243 | Carp::croak('Invalid on_connect_do: '.ref($on_connect_do)); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | sub disconnect { | ||
| 248 | my $class = shift; | ||
| 249 | delete $class->_attributes->{txn_manager}; | ||
| 250 | if (my $dbh = delete $class->_attributes->{dbh}) { | ||
| 251 | $dbh->disconnect; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | sub set_dbh { | ||
| 256 | my ($class, $dbh) = @_; | ||
| 257 | $class->_attributes->{dbh} = $dbh; | ||
| 258 | $class->_setup_dbd({dbh => $dbh}); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _setup_dbd { | ||
| 262 | my ($class, $args) = @_; | ||
| 263 | my $driver_name = $args ? _guess_driver_name($args) : $class->_attributes->{driver_name}; | ||
| 264 | $class->_attributes->{driver_name} = $driver_name; | ||
| 265 | $class->_attributes->{dbd} = $driver_name ? DBIx::Skinny::DBD->new($driver_name) : undef; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _guess_driver_name { | ||
| 269 | my $args = shift; | ||
| 270 | if ($args->{dbh}) { | ||
| 271 | return $args->{dbh}->{Driver}->{Name}; | ||
| 272 | } elsif ($args->{dsn}) { | ||
| 273 | my (undef, $driver_name,) = DBI->parse_dsn($args->{dsn}) or Carp::croak "can't parse DSN: @{[ $args->{dsn} ]}"; | ||
| 274 | return $driver_name | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | sub dbd { | ||
| 279 | $_[0]->_attributes->{dbd} or do { | ||
| 280 | require Data::Dumper; | ||
| 281 | Carp::croak("Attribute 'dbd' is not defined. Either we failed to connect, or the connection has gone away. Current attribute dump: @{[ Data::Dumper::Dumper($_[0]->_attributes) ]}"); | ||
| 282 | }; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub _verify_pid { | ||
| 286 | my $class = shift; | ||
| 287 | |||
| 288 | my $attr = $class->_attributes; | ||
| 289 | if ( $attr->{last_pid} != $$ and my $dbh = $attr->{dbh}) { | ||
| 290 | $attr->{last_pid} = $$; | ||
| 291 | $dbh->{InactiveDestroy} = 1; | ||
| 292 | if ( my $info = $class->in_transaction_check ) { | ||
| 293 | my $pid = $info->{pid}; | ||
| 294 | my $caller = $info->{caller}; | ||
| 295 | Carp::confess("Detected transaction while processing forked child (last known transaction at $caller->[1] line $caller->[2], pid $pid). Refusing to proceed at"); | ||
| 296 | } | ||
| 297 | $attr->{txn_manager} = undef; | ||
| 298 | $attr->{dbh} = undef; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | sub DESTROY { | ||
| 303 | my $self = shift; | ||
| 304 | $self->_verify_pid; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub dbh { | ||
| 308 | my $class = shift; | ||
| 309 | |||
| 310 | $class->_verify_pid; | ||
| 311 | |||
| 312 | my $dbh = $class->connect; | ||
| 313 | unless ($dbh->FETCH('Active') && $dbh->ping) { | ||
| 314 | $class->reconnect; | ||
| 315 | } | ||
| 316 | $class->_attributes->{dbh}; | ||
| 317 | } | ||
| 318 | |||
| 319 | #-------------------------------------------------------------------------------- | ||
| 320 | # schema trigger call | ||
| 321 | sub call_schema_trigger { | ||
| 322 | my ($class, $trigger, $schema, $table, $args) = @_; | ||
| 323 | $schema->call_trigger($class, $table, $trigger, $args); | ||
| 324 | } | ||
| 325 | |||
| 326 | #-------------------------------------------------------------------------------- | ||
| 327 | sub do { | ||
| 328 | my ($class, $sql, $attr, @bind_vars) = @_; | ||
| 329 | $class->profiler($sql, @bind_vars ? \@bind_vars : undef); | ||
| 330 | my $ret; | ||
| 331 | eval { $ret = $class->dbh->do($sql, $attr, @bind_vars) }; | ||
| 332 | if ($@) { | ||
| 333 | $class->_stack_trace('', $sql, @bind_vars ? \@bind_vars : '', $@); | ||
| 334 | } | ||
| 335 | $ret; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub count { | ||
| 339 | my ($class, $table, $column, $where) = @_; | ||
| 340 | |||
| 341 | $column ||= '*'; | ||
| 342 | my $rs = $class->resultset( | ||
| 343 | { | ||
| 344 | from => [$table], | ||
| 345 | } | ||
| 346 | ); | ||
| 347 | |||
| 348 | $rs->add_select("COUNT($column)" => 'cnt'); | ||
| 349 | if ($where) { | ||
| 350 | $class->_add_where($rs, $where); | ||
| 351 | } | ||
| 352 | |||
| 353 | $rs->retrieve->first->cnt; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub resultset { | ||
| 357 | my ($class, $args) = @_; | ||
| 358 | $args->{skinny} = $class; | ||
| 359 | $class->dbd->query_builder_class->new($args); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub search { | ||
| 363 | my ($class, $table, $where, $opt) = @_; | ||
| 364 | |||
| 365 | $class->search_rs($table, $where, $opt)->retrieve; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub search_rs { | ||
| 369 | my ($class, $table, $where, $opt) = @_; | ||
| 370 | |||
| 371 | my $cols = $opt->{select} || do { | ||
| 372 | my $column_info = $class->schema->schema_info->{$table}; | ||
| 373 | unless ( $column_info ) { | ||
| 374 | Carp::croak("schema_info does not exist for table '$table'"); | ||
| 375 | } | ||
| 376 | $column_info->{columns}; | ||
| 377 | }; | ||
| 378 | |||
| 379 | my $rs = $class->resultset( | ||
| 380 | { | ||
| 381 | select => $cols, | ||
| 382 | from => [$table], | ||
| 383 | } | ||
| 384 | ); | ||
| 385 | |||
| 386 | if ( $where ) { | ||
| 387 | $class->_add_where($rs, $where); | ||
| 388 | } | ||
| 389 | |||
| 390 | $rs->limit( $opt->{limit} ) if $opt->{limit}; | ||
| 391 | $rs->offset( $opt->{offset} ) if $opt->{offset}; | ||
| 392 | |||
| 393 | if (my $terms = $opt->{order_by}) { | ||
| 394 | $terms = [$terms] unless ref($terms) eq 'ARRAY'; | ||
| 395 | my @orders; | ||
| 396 | for my $term (@{$terms}) { | ||
| 397 | my ($col, $case); | ||
| 398 | if (ref($term) eq 'HASH') { | ||
| 399 | ($col, $case) = each %$term; | ||
| 400 | } else { | ||
| 401 | $col = $term; | ||
| 402 | $case = 'ASC'; | ||
| 403 | } | ||
| 404 | push @orders, { column => $col, desc => $case }; | ||
| 405 | } | ||
| 406 | $rs->order(\@orders); | ||
| 407 | } | ||
| 408 | |||
| 409 | if (my $terms = $opt->{having}) { | ||
| 410 | for my $col (keys %$terms) { | ||
| 411 | $rs->add_having($col => $terms->{$col}); | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | $rs->for_update(1) if $opt->{for_update}; | ||
| 416 | |||
| 417 | return $rs; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub single { | ||
| 421 | my ($class, $table, $where, $opt) = @_; | ||
| 422 | $opt->{limit} = 1; | ||
| 423 | $class->search_rs($table, $where, $opt)->retrieve->first; | ||
| 424 | } | ||
| 425 | |||
| 426 | sub search_named { | ||
| 427 | my ($class, $sql, $args, $opts, $opt_table_info) = @_; | ||
| 428 | |||
| 429 | $sql = sprintf($sql, @{$opts||[]}); | ||
| 430 | my %named_bind = %{$args}; | ||
| 431 | my @bind; | ||
| 432 | $sql =~ s{:([A-Za-z_][A-Za-z0-9_]*)}{ | ||
| 433 | Carp::croak("$1 does not exists in hash") if !exists $named_bind{$1}; | ||
| 434 | if ( ref $named_bind{$1} && ref $named_bind{$1} eq "ARRAY" ) { | ||
| 435 | push @bind, @{ $named_bind{$1} }; | ||
| 436 | my $tmp = join ',', map { '?' } @{ $named_bind{$1} }; | ||
| 437 | "( $tmp )"; | ||
| 438 | } else { | ||
| 439 | push @bind, $named_bind{$1}; | ||
| 440 | '?' | ||
| 441 | } | ||
| 442 | }ge; | ||
| 443 | |||
| 444 | $class->search_by_sql($sql, \@bind, $opt_table_info); | ||
| 445 | } | ||
| 446 | |||
| 447 | sub search_by_sql { | ||
| 448 | my ($class, $sql, $bind, $opt_table_info) = @_; | ||
| 449 | |||
| 450 | my $sth = $class->_execute($sql, $bind); | ||
| 451 | return $class->_get_sth_iterator($sql, $sth, $opt_table_info); | ||
| 452 | } | ||
| 453 | |||
| 454 | sub find_or_new { | ||
| 455 | my ($class, $table, $args) = @_; | ||
| 456 | $class->single($table, $args) or do { | ||
| 457 | $class->hash_to_row($table, $args); | ||
| 458 | }; | ||
| 459 | } | ||
| 460 | |||
| 461 | sub hash_to_row { | ||
| 462 | my ($class, $table, $hash) = @_; | ||
| 463 | |||
| 464 | my $row_class = $class->_get_row_class($table, $table); | ||
| 465 | my $row = $row_class->new( | ||
| 466 | { | ||
| 467 | sql => undef, | ||
| 468 | row_data => $hash, | ||
| 469 | skinny => $class, | ||
| 470 | opt_table_info => $table, | ||
| 471 | } | ||
| 472 | ); | ||
| 473 | $row->setup; | ||
| 474 | $row; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub _get_sth_iterator { | ||
| 478 | my ($class, $sql, $sth, $opt_table_info) = @_; | ||
| 479 | |||
| 480 | return DBIx::Skinny::Iterator->new( | ||
| 481 | skinny => $class, | ||
| 482 | sth => $sth, | ||
| 483 | sql => $sql, | ||
| 484 | row_class => $class->_get_row_class($sql, $opt_table_info), | ||
| 485 | opt_table_info => $opt_table_info, | ||
| 486 | suppress_objects => $class->suppress_row_objects, | ||
| 487 | ); | ||
| 488 | } | ||
| 489 | |||
| 490 | sub data2itr { | ||
| 491 | my ($class, $table, $data) = @_; | ||
| 492 | |||
| 493 | return DBIx::Skinny::Iterator->new( | ||
| 494 | skinny => $class, | ||
| 495 | data => $data, | ||
| 496 | row_class => $class->_get_row_class($table, $table), | ||
| 497 | opt_table_info => $table, | ||
| 498 | suppress_objects => $class->suppress_row_objects, | ||
| 499 | ); | ||
| 500 | } | ||
| 501 | |||
| 502 | sub _guess_table_name { | ||
| 503 | my ($class, $sql) = @_; | ||
| 504 | |||
| 505 | if ($sql =~ /\sfrom\s+["`]?([\w]+)["`]?\s*/si) { | ||
| 506 | return $1; | ||
| 507 | } | ||
| 508 | return; | ||
| 509 | } | ||
| 510 | |||
| 511 | sub _get_row_class { | ||
| 512 | my ($class, $sql, $table) = @_; | ||
| 513 | |||
| 514 | $table ||= $class->_guess_table_name($sql)||''; | ||
| 515 | if ($table && $class->schema->schema_info->{$table}) { | ||
| 516 | return $class->schema->schema_info->{$table}->{row_class}; | ||
| 517 | } else { | ||
| 518 | return $class->_attributes->{_common_row_class} ||= do { | ||
| 519 | my $row_class = join '::', $class->_attributes->{klass}, 'Row'; | ||
| 520 | DBIx::Skinny::Util::load_class($row_class) or do { | ||
| 521 | no strict 'refs'; @{"$row_class\::ISA"} = ('DBIx::Skinny::Row'); | ||
| 522 | }; | ||
| 523 | $row_class; | ||
| 524 | }; | ||
| 525 | } | ||
| 526 | } | ||
| 527 | |||
| 528 | sub _quote { | ||
| 529 | my ($label, $quote, $name_sep) = @_; | ||
| 530 | |||
| 531 | return $label if $label eq '*'; | ||
| 532 | return $quote . $label . $quote if !defined $name_sep; | ||
| 533 | return join $name_sep, map { $quote . $_ . $quote } split /\Q$name_sep\E/, $label; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub bind_params { | ||
| 537 | my($class, $table, $columns, $sth) = @_; | ||
| 538 | |||
| 539 | my $schema = $class->schema; | ||
| 540 | my $dbd = $class->dbd; | ||
| 541 | my $i = 1; | ||
| 542 | for my $column (@{ $columns }) { | ||
| 543 | my($col, $val) = @{ $column }; | ||
| 544 | my $type = $schema->column_type($table, $col); | ||
| 545 | my $attr = $type ? $dbd->bind_param_attributes($type) : undef; | ||
| 546 | |||
| 547 | my $ref = ref $val; | ||
| 548 | if ($ref eq 'ARRAY') { | ||
| 549 | $sth->bind_param($i++, $_, $attr) for @$val; | ||
| 550 | } elsif (not $ref) { | ||
| 551 | $sth->bind_param($i++, $val, $attr); | ||
| 552 | } else { | ||
| 553 | die "you can't set bind value, arrayref or scalar. you set $ref ref value."; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | sub _set_columns { | ||
| 559 | my ($class, $args, $insert) = @_; | ||
| 560 | |||
| 561 | my $schema = $class->schema; | ||
| 562 | my $dbd = $class->dbd; | ||
| 563 | my $quote = $dbd->quote; | ||
| 564 | my $name_sep = $dbd->name_sep; | ||
| 565 | |||
| 566 | my (@columns, @bind_columns, @quoted_columns); | ||
| 567 | for my $col (keys %{ $args }) { | ||
| 568 | my $quoted_col = _quote($col, $quote, $name_sep); | ||
| 569 | if (ref($args->{$col}) eq 'SCALAR') { | ||
| 570 | push @columns, ($insert ? ${ $args->{$col} } :"$quoted_col = " . ${ $args->{$col} }); | ||
| 571 | } else { | ||
| 572 | push @columns, ($insert ? '?' : "$quoted_col = ?"); | ||
| 573 | push @bind_columns, [$col, $schema->utf8_off($col, $args->{$col})]; | ||
| 574 | } | ||
| 575 | push @quoted_columns, $quoted_col; | ||
| 576 | } | ||
| 577 | |||
| 578 | return (\@columns, \@bind_columns, \@quoted_columns); | ||
| 579 | } | ||
| 580 | |||
| 581 | sub _insert_or_replace { | ||
| 582 | my ($class, $is_replace, $table, $args) = @_; | ||
| 583 | |||
| 584 | my $schema = $class->schema; | ||
| 585 | |||
| 586 | # deflate | ||
| 587 | for my $col (keys %{$args}) { | ||
| 588 | $args->{$col} = $schema->call_deflate($col, $args->{$col}); | ||
| 589 | } | ||
| 590 | |||
| 591 | my ($columns, $bind_columns, $quoted_columns) = $class->_set_columns($args, 1); | ||
| 592 | |||
| 593 | my $sql = $is_replace ? 'REPLACE' : 'INSERT'; | ||
| 594 | $sql .= " INTO $table\n"; | ||
| 595 | $sql .= '(' . join(', ', @$quoted_columns) .')' . "\n" . | ||
| 596 | 'VALUES (' . join(', ', @$columns) . ')' . "\n"; | ||
| 597 | |||
| 598 | my $sth = $class->_execute($sql, $bind_columns, $table); | ||
| 599 | $class->_close_sth($sth); | ||
| 600 | |||
| 601 | my $pk = $class->schema->schema_info->{$table}->{pk}; | ||
| 602 | |||
| 603 | if (defined $pk && not(ref $pk) && not(defined $args->{$pk})) { | ||
| 604 | $args->{$pk} = $class->_last_insert_id($table); | ||
| 605 | } | ||
| 606 | |||
| 607 | return $args if $class->suppress_row_objects; | ||
| 608 | |||
| 609 | my $row_class = $class->_get_row_class($sql, $table); | ||
| 610 | if (! $row_class) { | ||
| 611 | Carp::croak( "Could not find row definition for table '$table' (did you make sure to install_table()?)" ); | ||
| 612 | } | ||
| 613 | |||
| 614 | my $obj = $row_class->new( | ||
| 615 | { | ||
| 616 | row_data => $args, | ||
| 617 | skinny => $class, | ||
| 618 | opt_table_info => $table, | ||
| 619 | } | ||
| 620 | ); | ||
| 621 | $obj->setup; | ||
| 622 | |||
| 623 | $obj; | ||
| 624 | } | ||
| 625 | |||
| 626 | sub _last_insert_id { | ||
| 627 | my ($class, $table) = @_; | ||
| 628 | |||
| 629 | my $dbh = $class->dbh; | ||
| 630 | my $driver = $class->_attributes->{driver_name}; | ||
| 631 | if ( $driver eq 'mysql' ) { | ||
| 632 | return $dbh->{mysql_insertid}; | ||
| 633 | } elsif ( $driver eq 'Pg' ) { | ||
| 634 | return $dbh->last_insert_id( undef, undef, undef, undef,{ sequence => join( '_', $table, 'id', 'seq' ) } ); | ||
| 635 | } elsif ( $driver eq 'SQLite' ) { | ||
| 636 | return $dbh->func('last_insert_rowid'); | ||
| 637 | } elsif ( $driver eq 'Oracle' ) { | ||
| 638 | return; | ||
| 639 | } else { | ||
| 640 | Carp::croak "Don't know how to get last insert id for $driver"; | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | *create = \*insert; | ||
| 645 | sub insert { | ||
| 646 | my ($class, $table, $args) = @_; | ||
| 647 | |||
| 648 | my $schema = $class->schema; | ||
| 649 | $class->call_schema_trigger('pre_insert', $schema, $table, $args); | ||
| 650 | |||
| 651 | my $obj = $class->_insert_or_replace(0, $table, $args); | ||
| 652 | |||
| 653 | $class->call_schema_trigger('post_insert', $schema, $table, $obj); | ||
| 654 | |||
| 655 | $obj; | ||
| 656 | } | ||
| 657 | |||
| 658 | sub replace { | ||
| 659 | my ($class, $table, $args) = @_; | ||
| 660 | |||
| 661 | my $schema = $class->schema; | ||
| 662 | $class->call_schema_trigger('pre_insert', $schema, $table, $args); | ||
| 663 | |||
| 664 | my $obj = $class->_insert_or_replace(1, $table, $args); | ||
| 665 | |||
| 666 | $class->call_schema_trigger('post_insert', $schema, $table, $obj); | ||
| 667 | |||
| 668 | $obj; | ||
| 669 | } | ||
| 670 | |||
| 671 | sub bulk_insert { | ||
| 672 | my ($class, $table, $args) = @_; | ||
| 673 | |||
| 674 | my $code = $class->_attributes->{dbd}->can('bulk_insert') or Carp::croak "dbd don't provide bulk_insert method"; | ||
| 675 | $code->($class, $table, $args); | ||
| 676 | } | ||
| 677 | |||
| 678 | sub update { | ||
| 679 | my ($class, $table, $args, $where) = @_; | ||
| 680 | |||
| 681 | my $schema = $class->schema; | ||
| 682 | $class->call_schema_trigger('pre_update', $schema, $table, $args); | ||
| 683 | |||
| 684 | my $values = {}; | ||
| 685 | for my $col (keys %{$args}) { | ||
| 686 | $values->{$col} = $schema->call_deflate($col, $args->{$col}); | ||
| 687 | } | ||
| 688 | |||
| 689 | my ($columns, $bind_columns, undef) = $class->_set_columns($values, 0); | ||
| 690 | |||
| 691 | my $stmt = $class->resultset; | ||
| 692 | $class->_add_where($stmt, $where); | ||
| 693 | my @where_values = map {[$_ => $stmt->where_values->{$_}]} @{$stmt->bind_col}; | ||
| 694 | |||
| 695 | push @{$bind_columns}, @where_values; | ||
| 696 | |||
| 697 | my $sql = "UPDATE $table SET " . join(', ', @$columns) . ' ' . $stmt->as_sql_where; | ||
| 698 | my $sth = $class->_execute($sql, $bind_columns, $table); | ||
| 699 | |||
| 700 | my $rows = $sth->rows; | ||
| 701 | |||
| 702 | $class->_close_sth($sth); | ||
| 703 | $class->call_schema_trigger('post_update', $schema, $table, $rows); | ||
| 704 | |||
| 705 | return $rows; | ||
| 706 | } | ||
| 707 | |||
| 708 | sub update_by_sql { | ||
| 709 | my ($class, $sql, $bind) = @_; | ||
| 710 | |||
| 711 | Carp::carp( 'update_by_sql has been deprecated. Please use $skinny->do($sql, undef, @bind)' ); | ||
| 712 | $class->do($sql, undef, @$bind); | ||
| 713 | } | ||
| 714 | |||
| 715 | sub delete { | ||
| 716 | my ($class, $table, $where) = @_; | ||
| 717 | |||
| 718 | my $schema = $class->schema; | ||
| 719 | $class->call_schema_trigger('pre_delete', $schema, $table, $where); | ||
| 720 | |||
| 721 | my $stmt = $class->resultset( | ||
| 722 | { | ||
| 723 | from => [$table], | ||
| 724 | } | ||
| 725 | ); | ||
| 726 | |||
| 727 | $class->_add_where($stmt, $where); | ||
| 728 | |||
| 729 | my $sql = "DELETE " . $stmt->as_sql; | ||
| 730 | my @where_values = map {[$_ => $stmt->where_values->{$_}]} @{$stmt->bind_col}; | ||
| 731 | my $sth = $class->_execute($sql, \@where_values, $table); | ||
| 732 | my $rows = $sth->rows; | ||
| 733 | |||
| 734 | $class->call_schema_trigger('post_delete', $schema, $table, $rows); | ||
| 735 | |||
| 736 | $class->_close_sth($sth); | ||
| 737 | $rows; | ||
| 738 | } | ||
| 739 | |||
| 740 | sub delete_by_sql { | ||
| 741 | my ($class, $sql, $bind) = @_; | ||
| 742 | |||
| 743 | Carp::carp( 'delete_by_sql has been deprecated. Please use $skinny->do($sql, undef, @bind)' ); | ||
| 744 | $class->do($sql, undef, @$bind); | ||
| 745 | } | ||
| 746 | |||
| 747 | *find_or_insert = \*find_or_create; | ||
| 748 | sub find_or_create { | ||
| 749 | my ($class, $table, $args) = @_; | ||
| 750 | my $row = $class->single($table, $args); | ||
| 751 | return $row if $row; | ||
| 752 | $class->insert($table, $args)->refetch; | ||
| 753 | } | ||
| 754 | |||
| 755 | sub _add_where { | ||
| 756 | my ($class, $stmt, $where) = @_; | ||
| 757 | for my $col (keys %{$where}) { | ||
| 758 | $stmt->add_where($col => $where->{$col}); | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | sub _execute { | ||
| 763 | my ($class, $stmt, $args, $table) = @_; | ||
| 764 | |||
| 765 | my ($sth, $bind); | ||
| 766 | if ($table) { | ||
| 767 | $bind = [map {(ref $_->[1]) eq 'ARRAY' ? @{$_->[1]} : $_->[1]} @$args]; | ||
| 768 | $class->profiler($stmt, $bind); | ||
| 769 | eval { | ||
| 770 | $sth = $class->dbh->prepare($stmt) or die $class->dbh->errstr; | ||
| 771 | $class->bind_params($table, $args, $sth); | ||
| 772 | $sth->execute; | ||
| 773 | }; | ||
| 774 | } else { | ||
| 775 | $bind = $args; | ||
| 776 | $class->profiler($stmt, $bind); | ||
| 777 | eval { | ||
| 778 | $sth = $class->dbh->prepare($stmt) or die $class->dbh->errstr; | ||
| 779 | $sth->execute(@{$args}); | ||
| 780 | }; | ||
| 781 | } | ||
| 782 | |||
| 783 | if ($@) { | ||
| 784 | $class->_stack_trace($sth, $stmt, $bind, $@); | ||
| 785 | } | ||
| 786 | return $sth; | ||
| 787 | } | ||
| 788 | |||
| 789 | # stack trace | ||
| 790 | sub _stack_trace { | ||
| 791 | my ($class, $sth, $stmt, $bind, $reason) = @_; | ||
| 792 | require Data::Dumper; | ||
| 793 | |||
| 794 | if ($sth) { | ||
| 795 | $class->_close_sth($sth); | ||
| 796 | } | ||
| 797 | |||
| 798 | $stmt =~ s/\n/\n /gm; | ||
| 799 | Carp::croak sprintf <<"TRACE", $reason, $stmt, Data::Dumper::Dumper($bind); | ||
| 800 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | ||
| 801 | @@@@@ DBIx::Skinny 's Exception @@@@@ | ||
| 802 | Reason : %s | ||
| 803 | SQL : %s | ||
| 804 | BIND : %s | ||
| 805 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | ||
| 806 | TRACE | ||
| 807 | } | ||
| 808 | |||
| 809 | sub _close_sth { | ||
| 810 | my ($class, $sth) = @_; | ||
| 811 | $sth->finish; | ||
| 812 | undef $sth; | ||
| 813 | } | ||
| 814 | |||
| 815 | 1; | ||
| 816 | |||
| 817 | __END__ |