X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSource.pm;h=ef1559a4f40327bfae504739bd3877d76b5cc932;hb=034d0be414a18cf3730c1e6e260acf6e70df476c;hp=ebe2ff1af6cc9bd1fed162d2a11e7b42bb8a20a4;hpb=455a33cbbf34ec2e40402dbd5cd88c1f92ebac17;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index ebe2ff1..ef1559a 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -8,6 +8,9 @@ use DBIx::Class::ResultSourceHandle; use DBIx::Class::Exception; use Carp::Clan qw/^DBIx::Class/; +use Try::Tiny; +use List::Util 'first'; +use namespace::clean; use base qw/DBIx::Class/; @@ -142,7 +145,7 @@ by supplying an L in the column_info hash. If a column name beginning with a plus sign ('+col1') is provided, the attributes provided will be merged with any existing attributes for the column, with the new attributes taking precedence in the case that an -attribute already exists. Using this without a hashref +attribute already exists. Using this without a hashref (C<< $source->add_columns(qw/+col1 +col2/) >>) is legal, but useless -- it does the same thing it would do without the plus. @@ -174,7 +177,7 @@ the name of the column will be used. This contains the column type. It is automatically filled if you use the L producer, or the -L module. +L module. Currently there is no standard set of values for the data_type. Use whatever your database supports. @@ -367,9 +370,10 @@ sub column_info { $self->{_columns_info_loaded}++; my $info = {}; my $lc_info = {}; - # eval for the case of storage without table - eval { $info = $self->storage->columns_info_for( $self->from ) }; - unless ($@) { + + # try for the case of storage without table + try { + $info = $self->storage->columns_info_for( $self->from ); for my $realcol ( keys %{$info} ) { $lc_info->{lc $realcol} = $info->{$realcol}; } @@ -379,7 +383,7 @@ sub column_info { %{ $info->{$col} || $lc_info->{lc $col} || {} } }; } - } + }; } return $self->_columns->{$column}; } @@ -483,7 +487,7 @@ named C. Note: you normally do want to define a primary key on your sources B. See -L +L for more info. =cut @@ -519,6 +523,9 @@ sub primary_columns { return @{shift->_primaries||[]}; } +# a helper method that will automatically die with a descriptive message if +# no pk is defined on the source in question. For internal use to save +# on if @pks... boilerplate sub _pri_cols { my $self = shift; my @pcols = $self->primary_columns @@ -566,8 +573,22 @@ the result source. sub add_unique_constraint { my $self = shift; + + if (@_ > 2) { + $self->throw_exception( + 'add_unique_constraint() does not accept multiple constraints, use ' + . 'add_unique_constraints() instead' + ); + } + my $cols = pop @_; - my $name = shift; + if (ref $cols ne 'ARRAY') { + $self->throw_exception ( + 'Expecting an arrayref of constraint columns, got ' . ($cols||'NOTHING') + ); + } + + my $name = shift @_; $name ||= $self->name_unique_constraint($cols); @@ -581,18 +602,70 @@ sub add_unique_constraint { $self->_unique_constraints(\%unique_constraints); } +=head2 add_unique_constraints + +=over 4 + +=item Arguments: @constraints + +=item Return value: undefined + +=back + +Declare multiple unique constraints on this source. + + __PACKAGE__->add_unique_constraints( + constraint_name1 => [ qw/column1 column2/ ], + constraint_name2 => [ qw/column2 column3/ ], + ); + +Alternatively, you can specify only the columns: + + __PACKAGE__->add_unique_constraints( + [ qw/column1 column2/ ], + [ qw/column3 column4/ ] + ); + +This will result in unique constraints named C and +C, where C is replaced with the table name. + +Throws an error if any of the given column names do not yet exist on +the result source. + +See also L. + +=cut + +sub add_unique_constraints { + my $self = shift; + my @constraints = @_; + + if ( !(@constraints % 2) && first { ref $_ ne 'ARRAY' } @constraints ) { + # with constraint name + while (my ($name, $constraint) = splice @constraints, 0, 2) { + $self->add_unique_constraint($name => $constraint); + } + } + else { + # no constraint name + foreach my $constraint (@constraints) { + $self->add_unique_constraint($constraint); + } + } +} + =head2 name_unique_constraint =over 4 -=item Arguments: @colnames +=item Arguments: \@colnames =item Return value: Constraint name =back $source->table('mytable'); - $source->name_unique_constraint('col1', 'col2'); + $source->name_unique_constraint(['col1', 'col2']); # returns 'mytable_col1_col2' @@ -894,7 +967,7 @@ clause contents. my $schema = $source->schema(); -Returns the L object that this result source +Returns the L object that this result source belongs to. =head2 storage @@ -1019,7 +1092,7 @@ sub add_relationship { return $self; - # XXX disabled. doesn't work properly currently. skip in tests. +# XXX disabled. doesn't work properly currently. skip in tests. my $f_source = $self->schema->source($f_source_name); unless ($f_source) { @@ -1032,13 +1105,14 @@ sub add_relationship { } return unless $f_source; # Can't test rel without f_source - eval { $self->_resolve_join($rel, 'me', {}, []) }; - - if ($@) { # If the resolve failed, back out and re-throw the error - delete $rels{$rel}; # + try { $self->_resolve_join($rel, 'me', {}, []) } + catch { + # If the resolve failed, back out and re-throw the error + delete $rels{$rel}; $self->_relationships(\%rels); - $self->throw_exception("Error creating relationship $rel: $@"); - } + $self->throw_exception("Error creating relationship $rel: $_"); + }; + 1; } @@ -1285,7 +1359,7 @@ sub _resolve_join { -is_single => ( $rel_info->{attrs}{accessor} && - List::Util::first { $rel_info->{attrs}{accessor} eq $_ } (qw/single filter/) + first { $rel_info->{attrs}{accessor} eq $_ } (qw/single filter/) ), -alias => $as, -relation_chain_depth => $seen->{-relation_chain_depth} || 0, @@ -1465,14 +1539,14 @@ sub _resolve_prefetch { } #my @col = map { (/^self\.(.+)$/ ? ("${as_prefix}.$1") : ()); } # values %{$rel_info->{cond}}; - $collapse->{".${as_prefix}${pre}"} = [ $rel_source->primary_columns ]; + $collapse->{".${as_prefix}${pre}"} = [ $rel_source->_pri_cols ]; # action at a distance. prepending the '.' allows simpler code # in ResultSet->_collapse_result my @key = map { (/^foreign\.(.+)$/ ? ($1) : ()); } keys %{$rel_info->{cond}}; my @ord = (ref($rel_info->{attrs}{order_by}) eq 'ARRAY' ? @{$rel_info->{attrs}{order_by}} - + : (defined $rel_info->{attrs}{order_by} ? ($rel_info->{attrs}{order_by}) : ())); @@ -1530,7 +1604,7 @@ sub related_class { =head2 handle -Obtain a new handle to this source. Returns an instance of a +Obtain a new handle to this source. Returns an instance of a L. =cut