From: Rafael Kitover Date: Wed, 5 Aug 2009 09:19:57 +0000 (+0000) Subject: Merge 'trunk' into 'reduce_pings' X-Git-Tag: v0.08109~30^2~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=84ccbc01107eeadbbac452d5368b4dd74f12ab8e;hp=2b0abe0e1bc53bacd415d4d9f577bdf99415bc83;p=dbsrgits%2FDBIx-Class.git Merge 'trunk' into 'reduce_pings' r6389@hlagh (orig r7140): caelum | 2009-07-30 08:46:04 -0400 update sqlite test schema r6394@hlagh (orig r7145): robkinyon | 2009-07-30 10:13:21 -0400 Added prefetch caveats r6395@hlagh (orig r7146): robkinyon | 2009-07-30 10:20:02 -0400 Fixed caveats r6398@hlagh (orig r7149): caelum | 2009-07-30 11:56:01 -0400 make ::Oracle::Generic load without DBD::Oracle r6399@hlagh (orig r7150): caelum | 2009-07-30 12:04:47 -0400 make sure DBD::Oracle is loaded when using constants from it r6404@hlagh (orig r7154): castaway | 2009-07-30 16:17:33 -0400 Mangled Rob's example somewhat, still needs explaining whch circs exactly cause the borken results r6405@hlagh (orig r7158): mo | 2009-07-31 06:51:20 -0400 POD fix r6406@hlagh (orig r7159): mo | 2009-07-31 06:52:42 -0400 undo that attributes merge stuff r9037@hlagh (orig r7166): castaway | 2009-08-02 06:41:25 -0400 Mention ResultSet, ResultSource and Row in synopsis r9038@hlagh (orig r7167): castaway | 2009-08-02 08:10:53 -0400 Docs: Explainations of result sources and how to find them r9042@hlagh (orig r7172): ribasushi | 2009-08-03 05:01:44 -0400 Disable Pod::Inherit makefile calls, until we get to version 0.02 r9046@hlagh (orig r7176): ribasushi | 2009-08-03 05:51:42 -0400 r6983@Thesaurus (orig r6982): ribasushi | 2009-07-04 11:46:57 +0200 New branch to experiment with a sanifying mysql on_connect_call r6984@Thesaurus (orig r6983): ribasushi | 2009-07-04 11:49:44 +0200 Initial set_ansi_mode code - make sure to utilize _do_query instead of dbh->do, so the result is visible in the trace r6987@Thesaurus (orig r6986): ribasushi | 2009-07-04 12:40:47 +0200 Fix POD r7178@Thesaurus (orig r7175): ribasushi | 2009-08-03 11:51:15 +0200 Wrap up set_strict_mode for mysql r9048@hlagh (orig r7178): ribasushi | 2009-08-03 06:41:32 -0400 Sanify unqualified column bindtype handling Silence a warning when using a custom {from} r9068@hlagh (orig r7198): caelum | 2009-08-04 16:18:27 -0400 update Changes r9075@hlagh (orig r7205): ribasushi | 2009-08-05 02:34:25 -0400 Bump dependencies: Test::More for the new no_plan/done_testing goodies File::Temp as per RT#48431 r9077@hlagh (orig r7207): ribasushi | 2009-08-05 02:36:32 -0400 r7156@Thesaurus (orig r7153): robkinyon | 2009-07-30 20:06:04 +0200 Create prefetch_redux branch r7164@Thesaurus (orig r7161): robkinyon | 2009-07-31 22:41:01 +0200 Added MooseX::Traits to Makefile.PL r7172@Thesaurus (orig r7169): robkinyon | 2009-08-03 05:49:59 +0200 Added two tests and marked one todo_skip r7187@Thesaurus (orig r7184): ribasushi | 2009-08-03 17:24:41 +0200 Use goto to preserve correct error-at-line reporting r7189@Thesaurus (orig r7186): ribasushi | 2009-08-04 12:34:58 +0200 Add an extra test specifically for distinct/prefetch Remove duplicate test in count/prefetch Switch to as_query instead of debug overloading r7190@Thesaurus (orig r7187): ribasushi | 2009-08-04 12:35:57 +0200 Fix how a distinct-induced group_by is calculated, taking in consideration the new prefetch mechanism r7197@Thesaurus (orig r7194): ribasushi | 2009-08-04 17:31:33 +0200 Traits not needed by anything currently in dbic r7198@Thesaurus (orig r7195): ribasushi | 2009-08-04 17:41:14 +0200 Move around tests a bit r7199@Thesaurus (orig r7196): mo | 2009-08-04 21:10:57 +0200 prefetch-grouped fails, again r7204@Thesaurus (orig r7201): ribasushi | 2009-08-04 22:50:51 +0200 Split the search_related prefetch tests into a standalone testfile r7205@Thesaurus (orig r7202): ribasushi | 2009-08-04 23:05:03 +0200 Move norbi's test to prefetch_redux - it's the same idea r7209@Thesaurus (orig r7206): ribasushi | 2009-08-05 08:35:48 +0200 Tadaaaa (even more prefetch insanity) r9079@hlagh (orig r7209): ribasushi | 2009-08-05 02:38:41 -0400 r7107@Thesaurus (orig r7104): caelum | 2009-07-24 06:51:57 +0200 new branch to move common mssql functionality into the base class, and other tweaks r7109@Thesaurus (orig r7106): caelum | 2009-07-24 07:28:11 +0200 moved code to ::DBI::MSSQL and added DT inflation test r7112@Thesaurus (orig r7109): caelum | 2009-07-24 08:46:16 +0200 merge in some more MSSQL code, including odbc dynamic cursor support r7113@Thesaurus (orig r7110): caelum | 2009-07-24 08:49:54 +0200 fix a warning in SQLAHacks r7114@Thesaurus (orig r7111): caelum | 2009-07-24 09:22:33 +0200 add placeholder support detection for mssql through dbd::sybase r7118@Thesaurus (orig r7115): caelum | 2009-07-24 16:39:06 +0200 minor doc clarification r7122@Thesaurus (orig r7119): caelum | 2009-07-25 16:10:30 +0200 move placeholder support detection into ::Sybase::Base r7123@Thesaurus (orig r7120): caelum | 2009-07-25 16:12:01 +0200 add a comment r7127@Thesaurus (orig r7124): caelum | 2009-07-26 18:04:29 +0200 SAVEPOINT methods for MSSQL r7140@Thesaurus (orig r7137): caelum | 2009-07-30 10:12:45 +0200 better tests for "smalldatetime" support in MSSQL r7142@Thesaurus (orig r7139): caelum | 2009-07-30 13:29:19 +0200 MSSQL GUID support r7147@Thesaurus (orig r7144): caelum | 2009-07-30 15:38:33 +0200 update sqlite test schema r7150@Thesaurus (orig r7147): caelum | 2009-07-30 16:26:47 +0200 make sure the new mssql insert method works on an un-reblessed storage r7151@Thesaurus (orig r7148): caelum | 2009-07-30 16:55:35 +0200 better rebless check for insert r7154@Thesaurus (orig r7151): caelum | 2009-07-30 18:57:22 +0200 add missing file r7155@Thesaurus (orig r7152): caelum | 2009-07-30 19:00:40 +0200 fix syntax error r7163@Thesaurus (orig r7160): caelum | 2009-07-31 15:52:41 +0200 fix a bug in _determine_driver r7166@Thesaurus (orig r7163): caelum | 2009-08-01 18:10:23 +0200 default collist for storage _resolve_column_info r7182@Thesaurus (orig r7179): caelum | 2009-08-03 13:42:31 +0200 check that dynamic cursors are functional if enabled r7184@Thesaurus (orig r7181): ribasushi | 2009-08-03 14:23:37 +0200 Adjust expected sql to match the new 'Track' table definition r7186@Thesaurus (orig r7183): ribasushi | 2009-08-03 15:16:10 +0200 Simplify code and add some comments r7200@Thesaurus (orig r7197): caelum | 2009-08-04 21:31:16 +0200 update oracle tests for new "track" table r7203@Thesaurus (orig r7200): caelum | 2009-08-04 22:39:57 +0200 update Changes r9081@hlagh (orig r7211): ribasushi | 2009-08-05 02:40:39 -0400 r7213@Thesaurus (orig r7210): ribasushi | 2009-08-05 08:40:20 +0200 Really sanify _resolve_column_info r9083@hlagh (orig r7213): ribasushi | 2009-08-05 04:19:37 -0400 Reminder about discard_changes and friends r9084@hlagh (orig r7214): ribasushi | 2009-08-05 04:26:20 -0400 Reformat and fill-in changes r9085@hlagh (orig r7215): caelum | 2009-08-05 04:37:12 -0400 rename connect_call_use_mars to connect_call_use_MARS r9086@hlagh (orig r7216): ribasushi | 2009-08-05 04:38:14 -0400 Silence a TODO test r9087@hlagh (orig r7217): caelum | 2009-08-05 04:46:11 -0400 minor Changes update --- diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index b37ec5e..57b4b99 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -42,7 +42,7 @@ DBIx::Class::Schema - composable schemas $dsn, $user, $password, - { AutoCommit => 0 }, + { AutoCommit => 1 }, ); my $schema2 = Library::Schema->connect($coderef_returning_dbh); diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 32be00f..7a7bd60 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -437,12 +437,21 @@ sub connect_info { } } - %attrs = () if (ref $args[0] eq 'CODE'); # _connect() never looks past $args[0] in this case + if (ref $args[0] eq 'CODE') { + # _connect() never looks past $args[0] in this case + %attrs = () + } else { + %attrs = (%{ $self->_dbi_connect_attributes }, %attrs); + } $self->_dbi_connect_info([@args, keys %attrs ? \%attrs : ()]); $self->_connect_info; } +sub _dbi_connect_attributes { + return { AutoCommit => 1 }; +} + =head2 on_connect_do This method is deprecated in favour of setting via L. @@ -621,7 +630,7 @@ database is not in C mode. sub disconnect { my ($self) = @_; - if( $self->connected ) { + if( $self->_dbh ) { my @actions; push @actions, ( $self->on_disconnect_call || () ); @@ -716,14 +725,32 @@ Returns the dbh - a data base handle of class L. sub dbh { my ($self) = @_; - $self->ensure_connected; + if (not $self->_dbh) { + $self->_populate_dbh; + } else { + $self->ensure_connected; + } + return $self->_dbh; +} + +sub _get_dbh { + my $self = shift; + + if (not $self->_dbh) { + $self->_populate_dbh; + } return $self->_dbh; } sub _sql_maker_args { my ($self) = @_; - return ( bindtype=>'columns', array_datatypes => 1, limit_dialect => $self->dbh, %{$self->_sql_maker_opts} ); + return ( + bindtype=>'columns', + array_datatypes => 1, + limit_dialect => $self->_get_dbh, + %{$self->_sql_maker_opts} + ); } sub sql_maker { @@ -740,6 +767,7 @@ sub _rebless {} sub _populate_dbh { my ($self) = @_; + my @info = @{$self->_dbi_connect_info || []}; $self->_dbh($self->_connect(@info)); @@ -752,6 +780,11 @@ sub _populate_dbh { # there is no transaction in progress by definition $self->{transaction_depth} = $self->_dbh_autocommit ? 0 : 1; + $self->_run_connection_actions unless $self->{_in_determine_driver}; +} + +sub _run_connection_actions { + my $self = shift; my @actions; push @actions, ( $self->on_connect_call || () ); @@ -766,6 +799,8 @@ sub _determine_driver { if (not $self->_driver_determined) { if (ref($self) eq __PACKAGE__) { my $driver; + my $started_unconnected = 0; + local $self->{_in_determine_driver} = 1; if ($self->_dbh) { # we are connected $driver = $self->_dbh->{Driver}{Name}; @@ -781,9 +816,13 @@ sub _determine_driver { bless $self, $storage_class; $self->_rebless(); } + $started_unconnected = 1; } $self->_driver_determined(1); + + $self->_run_connection_actions + if $started_unconnected && defined $self->_dbh; } } @@ -983,14 +1022,13 @@ sub _svp_generate_name { sub txn_begin { my $self = shift; - $self->ensure_connected(); if($self->{transaction_depth} == 0) { $self->debugobj->txn_begin() if $self->debug; # this isn't ->_dbh-> because # we should reconnect on begin_work # for AutoCommit users - $self->dbh->begin_work; + $self->dbh_do(sub { $_[1]->begin_work }); } elsif ($self->auto_savepoint) { $self->svp_begin; } @@ -1162,7 +1200,11 @@ sub insert { my $col_info = $source->column_info($col); if ( $col_info->{auto_nextval} ) { - $updated_cols->{$col} = $to_insert->{$col} = $self->_sequence_fetch( 'nextval', $col_info->{sequence} || $self->_dbh_get_autoinc_seq($self->dbh, $source) ); + $updated_cols->{$col} = $to_insert->{$col} = $self->_sequence_fetch( + 'nextval', + $col_info->{sequence} || + $self->_dbh_get_autoinc_seq($self->_get_dbh, $source) + ); } } } @@ -1183,6 +1225,8 @@ sub insert_bulk { @colvalues{@$cols} = (0..$#$cols); my ($sql, @bind) = $self->sql_maker->insert($table, \%colvalues); + $self->_determine_driver; + $self->_query_start( $sql, @bind ); my $sth = $self->sth($sql); @@ -1242,6 +1286,7 @@ sub insert_bulk { sub update { my $self = shift @_; my $source = shift @_; + $self->_determine_driver; my $bind_attributes = $self->source_bind_attributes($source); return $self->_execute('update' => [], $source, $bind_attributes, @_); @@ -1251,7 +1296,7 @@ sub update { sub delete { my $self = shift @_; my $source = shift @_; - + $self->_determine_driver; my $bind_attrs = $self->source_bind_attributes($source); return $self->_execute('delete' => [], $source, $bind_attrs, @_); @@ -1948,7 +1993,7 @@ Returns the database driver name. =cut -sub sqlt_type { shift->dbh->{Driver}->{Name} } +sub sqlt_type { shift->_get_dbh->{Driver}->{Name} } =head2 bind_attribute_by_data_type @@ -2195,7 +2240,7 @@ See L for a list of values for C<$sqlt_args>. sub deployment_statements { my ($self, $schema, $type, $version, $dir, $sqltargs) = @_; # Need to be connected to get the correct sqlt_type - $self->ensure_connected() unless $type; + $self->_get_dbh() unless $type; $type ||= $self->sqlt_type; $version ||= $schema->schema_version || '1.x'; $dir ||= './'; @@ -2240,7 +2285,7 @@ sub deploy { return if $line =~ /^\s+$/; # skip whitespace only $self->_query_start($line); eval { - $self->dbh->do($line); # shouldn't be using ->dbh ? + $self->_get_dbh->do($line); }; if ($@) { carp qq{$@ (running "${line}")}; @@ -2269,7 +2314,7 @@ Returns the datetime parser class sub datetime_parser { my $self = shift; return $self->{datetime_parser} ||= do { - $self->ensure_connected; + $self->_get_dbh; $self->build_datetime_parser(@_); }; } diff --git a/lib/DBIx/Class/Storage/DBI/ODBC.pm b/lib/DBIx/Class/Storage/DBI/ODBC.pm index 6f905af..d9b810a 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC.pm @@ -8,7 +8,8 @@ use mro 'c3'; sub _rebless { my ($self) = @_; - my $dbtype = eval { $self->dbh->get_info(17) }; + my $dbtype = eval { $self->_get_dbh->get_info(17) }; + unless ( $@ ) { # Translate the backend name into a perl identifier $dbtype =~ s/\W/_/gi; diff --git a/lib/DBIx/Class/Storage/DBI/Oracle.pm b/lib/DBIx/Class/Storage/DBI/Oracle.pm index 1bb9f79..7a49b50 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle.pm @@ -9,7 +9,7 @@ use mro 'c3'; sub _rebless { my ($self) = @_; - my $version = eval { $self->dbh->get_info(18); }; + my $version = eval { $self->_get_dbh->get_info(18); }; if ( !$@ ) { my ($major, $minor, $patchlevel) = split(/\./, $version); diff --git a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm index 1593978..b97e34f 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm @@ -76,7 +76,7 @@ sub _dbh_get_autoinc_seq { sub _sequence_fetch { my ( $self, $type, $seq ) = @_; - my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL"); + my ($id) = $self->_get_dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL"); return $id; } @@ -209,7 +209,7 @@ sub connect_call_datetime_setup { sub _svp_begin { my ($self, $name) = @_; - $self->dbh->do("SAVEPOINT $name"); + $self->_get_dbh->do("SAVEPOINT $name"); } =head2 source_bind_attributes @@ -263,7 +263,7 @@ sub _svp_release { 1 } sub _svp_rollback { my ($self, $name) = @_; - $self->dbh->do("ROLLBACK TO SAVEPOINT $name") + $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name") } =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index 3935a49..ac24ad3 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -15,7 +15,7 @@ warn "DBD::Pg 2.9.2 or greater is strongly recommended\n" sub with_deferred_fk_checks { my ($self, $sub) = @_; - $self->dbh->do('SET CONSTRAINTS ALL DEFERRED'); + $self->_get_dbh->do('SET CONSTRAINTS ALL DEFERRED'); $sub->(); } @@ -82,26 +82,26 @@ sub bind_attribute_by_data_type { sub _sequence_fetch { my ( $self, $type, $seq ) = @_; - my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')"); + my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')"); return $id; } sub _svp_begin { my ($self, $name) = @_; - $self->dbh->pg_savepoint($name); + $self->_get_dbh->pg_savepoint($name); } sub _svp_release { my ($self, $name) = @_; - $self->dbh->pg_release($name); + $self->_get_dbh->pg_release($name); } sub _svp_rollback { my ($self, $name) = @_; - $self->dbh->pg_rollback_to($name); + $self->_get_dbh->pg_rollback_to($name); } 1; diff --git a/lib/DBIx/Class/Storage/DBI/Sybase.pm b/lib/DBIx/Class/Storage/DBI/Sybase.pm index 0a2cfb8..41b0c81 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase.pm @@ -12,7 +12,11 @@ use mro 'c3'; sub _rebless { my $self = shift; - my $dbtype = eval { @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2] }; + my $dbtype = eval { + @{$self->_get_dbh + ->selectrow_arrayref(qq{sp_server_info \@attribute_id=1}) + }[2] + }; unless ( $@ ) { $dbtype =~ s/\W/_/gi; my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}"; diff --git a/lib/DBIx/Class/Storage/DBI/mysql.pm b/lib/DBIx/Class/Storage/DBI/mysql.pm index bce1a07..89624f4 100644 --- a/lib/DBIx/Class/Storage/DBI/mysql.pm +++ b/lib/DBIx/Class/Storage/DBI/mysql.pm @@ -40,28 +40,28 @@ sub sqlt_type { sub _svp_begin { my ($self, $name) = @_; - $self->dbh->do("SAVEPOINT $name"); + $self->_get_dbh->do("SAVEPOINT $name"); } sub _svp_release { my ($self, $name) = @_; - $self->dbh->do("RELEASE SAVEPOINT $name"); + $self->_get_dbh->do("RELEASE SAVEPOINT $name"); } sub _svp_rollback { my ($self, $name) = @_; - $self->dbh->do("ROLLBACK TO SAVEPOINT $name") + $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name") } sub is_replicating { - my $status = shift->dbh->selectrow_hashref('show slave status'); + my $status = shift->_get_dbh->selectrow_hashref('show slave status'); return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes'); } sub lag_behind_master { - return shift->dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master}; + return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master}; } # MySql can not do subquery update/deletes, only way is slow per-row operations. diff --git a/t/92storage_ping_count.t b/t/92storage_ping_count.t new file mode 100644 index 0000000..efbddf6 --- /dev/null +++ b/t/92storage_ping_count.t @@ -0,0 +1,164 @@ +use strict; +use warnings; + +# Stolen from 76joins.t (a good test for this purpose) + +use Test::More; +use lib qw(t/lib); +use DBICTest; +use Data::Dumper; +use DBIC::SqlMakerTest; + +my $ping_count = 0; + +{ + local $SIG{__WARN__} = sub {}; + require DBIx::Class::Storage::DBI; + + my $ping = \&DBIx::Class::Storage::DBI::_ping; + + *DBIx::Class::Storage::DBI::_ping = sub { + $ping_count++; + goto &$ping; + }; +} + +my $schema = DBICTest->init_schema(); + +my $orig_debug = $schema->storage->debug; + +use IO::File; + +BEGIN { + eval "use DBD::SQLite"; + plan $@ + ? ( skip_all => 'needs DBD::SQLite for testing' ) + : ( tests => 28 ); +} + +my $rs = $schema->resultset("CD")->search( + { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, + { from => [ { 'me' => 'cd' }, + [ + { artist => 'artist' }, + { 'me.artist' => 'artist.artistid' } + ] ] } + ); + +is( $rs + 0, 1, "Single record in resultset"); + +is($rs->first->title, 'Forkful of bees', 'Correct record returned'); + +$rs = $schema->resultset("CD")->search( + { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, + { join => 'artist' }); + +is( $rs + 0, 1, "Single record in resultset"); + +is($rs->first->title, 'Forkful of bees', 'Correct record returned'); + +$rs = $schema->resultset("CD")->search( + { 'artist.name' => 'We Are Goth', + 'liner_notes.notes' => 'Kill Yourself!' }, + { join => [ qw/artist liner_notes/ ] }); + +is( $rs + 0, 1, "Single record in resultset"); + +is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned'); + +# when using join attribute, make sure slice()ing all objects has same count as all() +$rs = $schema->resultset("CD")->search( + { 'artist' => 1 }, + { join => [qw/artist/], order_by => 'artist.name' } +); +is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' ); + +ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count, + 'Slicing beyond end of rs returns a zero count'); + +$rs = $schema->resultset("Artist")->search( + { 'liner_notes.notes' => 'Kill Yourself!' }, + { join => { 'cds' => 'liner_notes' } }); + +is( $rs->count, 1, "Single record in resultset"); + +is($rs->first->name, 'We Are Goth', 'Correct record returned'); + + +{ + $schema->populate('Artist', [ + [ qw/artistid name/ ], + [ 4, 'Another Boy Band' ], + ]); + $schema->populate('CD', [ + [ qw/cdid artist title year/ ], + [ 6, 2, "Greatest Hits", 2001 ], + [ 7, 4, "Greatest Hits", 2005 ], + [ 8, 4, "BoyBandBlues", 2008 ], + ]); + $schema->populate('TwoKeys', [ + [ qw/artist cd/ ], + [ 2, 4 ], + [ 2, 6 ], + [ 4, 7 ], + [ 4, 8 ], + ]); + + sub cd_count { + return $schema->resultset("CD")->count; + } + sub tk_count { + return $schema->resultset("TwoKeys")->count; + } + + is(cd_count(), 8, '8 rows in table cd'); + is(tk_count(), 7, '7 rows in table twokeys'); + + sub artist1 { + return $schema->resultset("CD")->search( + { 'artist.name' => 'Caterwauler McCrae' }, + { join => [qw/artist/]} + ); + } + sub artist2 { + return $schema->resultset("CD")->search( + { 'artist.name' => 'Random Boy Band' }, + { join => [qw/artist/]} + ); + } + + is( artist1()->count, 3, '3 Caterwauler McCrae CDs' ); + ok( artist1()->delete, 'Successfully deleted 3 CDs' ); + is( artist1()->count, 0, '0 Caterwauler McCrae CDs' ); + is( artist2()->count, 2, '3 Random Boy Band CDs' ); + ok( artist2()->update( { 'artist' => 1 } ) ); + is( artist2()->count, 0, '0 Random Boy Band CDs' ); + is( artist1()->count, 2, '2 Caterwauler McCrae CDs' ); + + # test update on multi-column-pk + sub tk1 { + return $schema->resultset("TwoKeys")->search( + { + 'artist.name' => { like => '%Boy Band' }, + 'cd.title' => 'Greatest Hits', + }, + { join => [qw/artist cd/] } + ); + } + sub tk2 { + return $schema->resultset("TwoKeys")->search( + { 'artist.name' => 'Caterwauler McCrae' }, + { join => [qw/artist/]} + ); + } + is( tk2()->count, 2, 'TwoKeys count == 2' ); + is( tk1()->count, 2, 'TwoKeys count == 2' ); + ok( tk1()->update( { artist => 1 } ) ); + is( tk1()->count, 0, 'TwoKeys count == 0' ); + is( tk2()->count, 4, '2 Caterwauler McCrae CDs' ); + ok( tk2()->delete, 'Successfully deleted 4 CDs' ); + is(cd_count(), 5, '5 rows in table cd'); + is(tk_count(), 3, '3 rows in table twokeys'); +} + +is $ping_count, 0, 'no _ping() calls';