From: Matt S Trout Date: Fri, 12 May 2006 14:16:48 +0000 (+0000) Subject: Merge 'trunk' into 'DBIx-Class-current' X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e6a8b3fc4dc400af1a7ed827eaf9752ec99ad289;p=dbsrgits%2FDBIx-Class-Historic.git Merge 'trunk' into 'DBIx-Class-current' r5900@cain (orig r1613): jguenther | 2006-05-11 19:20:59 +0000 Added a couple examples to the cookbook r5901@cain (orig r1614): jguenther | 2006-05-11 21:53:25 +0000 Fixed cookbook example to actually work r5902@cain (orig r1615): matthewt | 2006-05-12 00:56:54 +0000 performance fix for cascade_update r5903@cain (orig r1616): matthewt | 2006-05-12 01:04:37 +0000 fixup to gen-schema.pl r5904@cain (orig r1617): matthewt | 2006-05-12 02:17:18 +0000 fixup for stringify that can be false in find_or_create_related --- e6a8b3fc4dc400af1a7ed827eaf9752ec99ad289 diff --cc Changes index e94364d,54514fb..6b40d18 --- a/Changes +++ b/Changes @@@ -1,22 -1,8 +1,24 @@@ Revision history for DBIx::Class + - modified SQLT parser to skip dupe table names + - added remove_column(s) to ResultSource/ResultSourceProxy + - added add_column alias to ResultSourceProxy + - added source_name to ResultSource + - load_classes now uses source_name and sets it if necessary + - add update_or_create_related to Relationship::Base + - add find_or_new to ResultSet/ResultSetProxy and find_or_new_related + to Relationship::Base + - add accessors for unique constraint names and coulums to + ResultSource/ResultSourceProxy + - rework ResultSet::find() to search unique constraints + - CDBICompat: modify retrieve to fix column casing when ColumnCase is + loaded + - CDBICompat: override find_or_create to fix column casing when + ColumnCase is loaded + 0.06003 + - make find_or_create_related check defined() instead of truth + - don't unnecessarily fetch rels for cascade_update - don't set_columns explicitly in update_or_create; instead use update($hashref) so InflateColumn works - fix for has_many prefetch with 0 related rows diff --cc lib/DBIx/Class/Relationship/Base.pm index bfe63b3,05f4c52..0401c0a --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@@ -293,24 -264,10 +293,25 @@@ Lfind_related(@_) || $self->create_related(@_); + my $obj = $self->find_related(@_); + return (defined($obj) ? $obj : $self->create_related(@_)); } +=head2 update_or_create_related + + my $updated_item = $obj->update_or_create_related('relname', \%col_data, \%attrs?); + +Update or create an item of a related class. See +L for details. + +=cut + +sub update_or_create_related { + my $self = shift; + my $rel = shift; + return $self->related_resultset($rel)->update_or_create(@_); +} + =head2 set_from_related $book->set_from_related('author', $author_obj); diff --cc lib/DBIx/Class/ResultSet.pm index 5b2473a,2651034..d6f0dd2 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@@ -462,8 -390,12 +462,12 @@@ sub cursor my $cd = $schema->resultset('CD')->single({ year => 2001 }); Inflates the first result without creating a cursor if the resultset has -any records in it; if not returns nothing. Used by find() as an optimisation. +any records in it; if not returns nothing. Used by L as an optimisation. + Can optionally take an additional condition *only* - this is a fast-code-path + method; if you need to add extra joins or similar call ->search and then + ->single without a condition on the $rs returned from that. + =cut sub single { diff --cc t/lib/DBICTest/Schema.pm index e882ee7,595db5a..d69abc0 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@@ -7,8 -7,9 +7,10 @@@ no warnings qw/qw/ __PACKAGE__->load_classes(qw/ Artist + Employee CD + Link + Bookmark #dummy Track Tag diff --cc t/run/01core.tl index 5287124,68d34aa..05e4dd3 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@@ -1,7 -1,7 +1,7 @@@ sub run_tests { my $schema = shift; - plan tests => 57; -plan tests => 47; ++plan tests => 58; # figure out if we've got a version of sqlite that is older than 3.2.6, in # which case COUNT(DISTINCT()) doesn't work @@@ -225,43 -200,27 +225,51 @@@ ok($schema->storage(), 'Storage availab is($art->name, 'Test _cond_for_update_delete', 'updated second artist name'); } -#test cascade_delete thru many_many relations -my $art_del = $schema->resultset("Artist")->find({ artistid => 1 }); -$art_del->delete; -cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.'); -cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.'); +# test source_name +{ + # source_name should be set for normal modules + is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker'); -$schema->source("Artist")->{_columns}{'artistid'} = {}; + # test the result source that sets source_name explictly + ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists'); -my $typeinfo = $schema->source("Artist")->column_info('artistid'); -is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); -$schema->source("Artist")->column_info('artistid'); -ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set'); + my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' }); + cmp_ok(@artsn, '==', 4, "Four artists returned"); +} + my $newbook = $schema->resultset( 'Bookmark' )->find(1); + + $@ = ''; + eval { + my $newlink = $newbook->link; + }; + ok(!$@, "stringify to false value doesn't cause error"); + +# test cascade_delete through many_to_many relations +{ + my $art_del = $schema->resultset("Artist")->find({ artistid => 1 }); + $art_del->delete; + cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.'); + cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.'); +} + +# test column_info +{ + $schema->source("Artist")->{_columns}{'artistid'} = {}; + + my $typeinfo = $schema->source("Artist")->column_info('artistid'); + is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); + $schema->source("Artist")->column_info('artistid'); + ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set'); +} + +# test remove_columns +{ + is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]); + $schema->source('CD')->remove_columns('year'); + is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]); +} + } 1;