1 package DBIx::Class::ResultSource;
6 use DBIx::Class::ResultSet;
7 use DBIx::Class::ResultSourceHandle;
9 use DBIx::Class::Exception;
10 use Carp::Clan qw/^DBIx::Class/;
12 use List::Util 'first';
15 use base qw/DBIx::Class/;
17 __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns
18 _columns _primaries _unique_constraints name resultset_attributes
19 schema from _relationships column_info_from_storage source_info
20 source_name sqlt_deploy_callback/);
22 __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class
27 DBIx::Class::ResultSource - Result source object
31 # Create a table based result source, in a result class.
33 package MyDB::Schema::Result::Artist;
34 use base qw/DBIx::Class::Core/;
36 __PACKAGE__->table('artist');
37 __PACKAGE__->add_columns(qw/ artistid name /);
38 __PACKAGE__->set_primary_key('artistid');
39 __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');
43 # Create a query (view) based result source, in a result class
44 package MyDB::Schema::Result::Year2000CDs;
45 use base qw/DBIx::Class::Core/;
47 __PACKAGE__->load_components('InflateColumn::DateTime');
48 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
50 __PACKAGE__->table('year2000cds');
51 __PACKAGE__->result_source_instance->is_virtual(1);
52 __PACKAGE__->result_source_instance->view_definition(
53 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
59 A ResultSource is an object that represents a source of data for querying.
61 This class is a base class for various specialised types of result
62 sources, for example L<DBIx::Class::ResultSource::Table>. Table is the
63 default result source type, so one is created for you when defining a
64 result class as described in the synopsis above.
66 More specifically, the L<DBIx::Class::Core> base class pulls in the
67 L<DBIx::Class::ResultSourceProxy::Table> component, which defines
68 the L<table|DBIx::Class::ResultSourceProxy::Table/table> method.
69 When called, C<table> creates and stores an instance of
70 L<DBIx::Class::ResultSoure::Table>. Luckily, to use tables as result
71 sources, you don't need to remember any of this.
73 Result sources representing select queries, or views, can also be
74 created, see L<DBIx::Class::ResultSource::View> for full details.
76 =head2 Finding result source objects
78 As mentioned above, a result source instance is created and stored for
79 you when you define a L<Result Class|DBIx::Class::Manual::Glossary/Result Class>.
81 You can retrieve the result source at runtime in the following ways:
85 =item From a Schema object:
87 $schema->source($source_name);
89 =item From a Row object:
93 =item From a ResultSet object:
106 my ($class, $attrs) = @_;
107 $class = ref $class if ref $class;
109 my $new = bless { %{$attrs || {}} }, $class;
110 $new->{resultset_class} ||= 'DBIx::Class::ResultSet';
111 $new->{resultset_attributes} = { %{$new->{resultset_attributes} || {}} };
112 $new->{_ordered_columns} = [ @{$new->{_ordered_columns}||[]}];
113 $new->{_columns} = { %{$new->{_columns}||{}} };
114 $new->{_relationships} = { %{$new->{_relationships}||{}} };
115 $new->{name} ||= "!!NAME NOT SET!!";
116 $new->{_columns_info_loaded} ||= 0;
117 $new->{sqlt_deploy_callback} ||= "default_sqlt_deploy_hook";
127 =item Arguments: @columns
129 =item Return value: The ResultSource object
133 $source->add_columns(qw/col1 col2 col3/);
135 $source->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...);
137 Adds columns to the result source. If supplied colname => hashref
138 pairs, uses the hashref as the L</column_info> for that column. Repeated
139 calls of this method will add more columns, not replace them.
141 The column names given will be created as accessor methods on your
142 L<DBIx::Class::Row> objects. You can change the name of the accessor
143 by supplying an L</accessor> in the column_info hash.
145 If a column name beginning with a plus sign ('+col1') is provided, the
146 attributes provided will be merged with any existing attributes for the
147 column, with the new attributes taking precedence in the case that an
148 attribute already exists. Using this without a hashref
149 (C<< $source->add_columns(qw/+col1 +col2/) >>) is legal, but useless --
150 it does the same thing it would do without the plus.
152 The contents of the column_info are not set in stone. The following
153 keys are currently recognised/used by DBIx::Class:
159 { accessor => '_name' }
161 # example use, replace standard accessor with one of your own:
163 my ($self, $value) = @_;
165 die "Name cannot contain digits!" if($value =~ /\d/);
166 $self->_name($value);
168 return $self->_name();
171 Use this to set the name of the accessor method for this column. If unset,
172 the name of the column will be used.
176 { data_type => 'integer' }
178 This contains the column type. It is automatically filled if you use the
179 L<SQL::Translator::Producer::DBIx::Class::File> producer, or the
180 L<DBIx::Class::Schema::Loader> module.
182 Currently there is no standard set of values for the data_type. Use
183 whatever your database supports.
189 The length of your column, if it is a column type that can have a size
190 restriction. This is currently only used to create tables from your
191 schema, see L<DBIx::Class::Schema/deploy>.
197 Set this to a true value for a columns that is allowed to contain NULL
198 values, default is false. This is currently only used to create tables
199 from your schema, see L<DBIx::Class::Schema/deploy>.
201 =item is_auto_increment
203 { is_auto_increment => 1 }
205 Set this to a true value for a column whose value is somehow
206 automatically set, defaults to false. This is used to determine which
207 columns to empty when cloning objects using
208 L<DBIx::Class::Row/copy>. It is also used by
209 L<DBIx::Class::Schema/deploy>.
215 Set this to a true or false value (not C<undef>) to explicitly specify
216 if this column contains numeric data. This controls how set_column
217 decides whether to consider a column dirty after an update: if
218 C<is_numeric> is true a numeric comparison C<< != >> will take place
219 instead of the usual C<eq>
221 If not specified the storage class will attempt to figure this out on
222 first access to the column, based on the column C<data_type>. The
223 result will be cached in this attribute.
227 { is_foreign_key => 1 }
229 Set this to a true value for a column that contains a key from a
230 foreign table, defaults to false. This is currently only used to
231 create tables from your schema, see L<DBIx::Class::Schema/deploy>.
235 { default_value => \'now()' }
237 Set this to the default value which will be inserted into a column by
238 the database. Can contain either a value or a function (use a
239 reference to a scalar e.g. C<\'now()'> if you want a function). This
240 is currently only used to create tables from your schema, see
241 L<DBIx::Class::Schema/deploy>.
243 See the note on L<DBIx::Class::Row/new> for more information about possible
244 issues related to db-side default values.
248 { sequence => 'my_table_seq' }
250 Set this on a primary key column to the name of the sequence used to
251 generate a new key value. If not specified, L<DBIx::Class::PK::Auto>
252 will attempt to retrieve the name of the sequence from the database
257 Set this to a true value for a column whose value is retrieved automatically
258 from a sequence or function (if supported by your Storage driver.) For a
259 sequence, if you do not use a trigger to get the nextval, you have to set the
260 L</sequence> value as well.
262 Also set this for MSSQL columns with the 'uniqueidentifier'
263 L<data_type|DBIx::Class::ResultSource/data_type> whose values you want to
264 automatically generate using C<NEWID()>, unless they are a primary key in which
265 case this will be done anyway.
269 This is used by L<DBIx::Class::Schema/deploy> and L<SQL::Translator>
270 to add extra non-generic data to the column. For example: C<< extra
271 => { unsigned => 1} >> is used by the MySQL producer to set an integer
272 column to unsigned. For more details, see
273 L<SQL::Translator::Producer::MySQL>.
281 =item Arguments: $colname, \%columninfo?
283 =item Return value: 1/0 (true/false)
287 $source->add_column('col' => \%info);
289 Add a single column and optional column info. Uses the same column
290 info keys as L</add_columns>.
295 my ($self, @cols) = @_;
296 $self->_ordered_columns(\@cols) unless $self->_ordered_columns;
299 my $columns = $self->_columns;
300 while (my $col = shift @cols) {
301 my $column_info = {};
302 if ($col =~ s/^\+//) {
303 $column_info = $self->column_info($col);
306 # If next entry is { ... } use that for the column info, if not
307 # use an empty hashref
309 my $new_info = shift(@cols);
310 %$column_info = (%$column_info, %$new_info);
312 push(@added, $col) unless exists $columns->{$col};
313 $columns->{$col} = $column_info;
315 push @{ $self->_ordered_columns }, @added;
319 sub add_column { shift->add_columns(@_); } # DO NOT CHANGE THIS TO GLOB
325 =item Arguments: $colname
327 =item Return value: 1/0 (true/false)
331 if ($source->has_column($colname)) { ... }
333 Returns true if the source has a column of this name, false otherwise.
338 my ($self, $column) = @_;
339 return exists $self->_columns->{$column};
346 =item Arguments: $colname
348 =item Return value: Hashref of info
352 my $info = $source->column_info($col);
354 Returns the column metadata hashref for a column, as originally passed
355 to L</add_columns>. See L</add_columns> above for information on the
356 contents of the hashref.
361 my ($self, $column) = @_;
362 $self->throw_exception("No such column $column")
363 unless exists $self->_columns->{$column};
364 #warn $self->{_columns_info_loaded}, "\n";
365 if ( ! $self->_columns->{$column}{data_type}
366 and $self->column_info_from_storage
367 and ! $self->{_columns_info_loaded}
368 and $self->schema and $self->storage )
370 $self->{_columns_info_loaded}++;
374 # try for the case of storage without table
376 $info = $self->storage->columns_info_for( $self->from );
377 for my $realcol ( keys %{$info} ) {
378 $lc_info->{lc $realcol} = $info->{$realcol};
380 foreach my $col ( keys %{$self->_columns} ) {
381 $self->_columns->{$col} = {
382 %{ $self->_columns->{$col} },
383 %{ $info->{$col} || $lc_info->{lc $col} || {} }
388 return $self->_columns->{$column};
395 =item Arguments: None
397 =item Return value: Ordered list of column names
401 my @column_names = $source->columns;
403 Returns all column names in the order they were declared to L</add_columns>.
409 $self->throw_exception(
410 "columns() is a read-only accessor, did you mean add_columns()?"
412 return @{$self->{_ordered_columns}||[]};
415 =head2 remove_columns
419 =item Arguments: @colnames
421 =item Return value: undefined
425 $source->remove_columns(qw/col1 col2 col3/);
427 Removes the given list of columns by name, from the result source.
429 B<Warning>: Removing a column that is also used in the sources primary
430 key, or in one of the sources unique constraints, B<will> result in a
431 broken result source.
437 =item Arguments: $colname
439 =item Return value: undefined
443 $source->remove_column('col');
445 Remove a single column by name from the result source, similar to
448 B<Warning>: Removing a column that is also used in the sources primary
449 key, or in one of the sources unique constraints, B<will> result in a
450 broken result source.
455 my ($self, @to_remove) = @_;
457 my $columns = $self->_columns
462 delete $columns->{$_};
466 $self->_ordered_columns([ grep { not $to_remove{$_} } @{$self->_ordered_columns} ]);
469 sub remove_column { shift->remove_columns(@_); } # DO NOT CHANGE THIS TO GLOB
471 =head2 set_primary_key
475 =item Arguments: @cols
477 =item Return value: undefined
481 Defines one or more columns as primary key for this source. Must be
482 called after L</add_columns>.
484 Additionally, defines a L<unique constraint|add_unique_constraint>
487 Note: you normally do want to define a primary key on your sources
488 B<even if the underlying database table does not have a primary key>.
490 L<DBIx::Class::Intro/The Significance and Importance of Primary Keys>
495 sub set_primary_key {
496 my ($self, @cols) = @_;
497 # check if primary key columns are valid columns
498 foreach my $col (@cols) {
499 $self->throw_exception("No such column $col on table " . $self->name)
500 unless $self->has_column($col);
502 $self->_primaries(\@cols);
504 $self->add_unique_constraint(primary => \@cols);
507 =head2 primary_columns
511 =item Arguments: None
513 =item Return value: Ordered list of primary column names
517 Read-only accessor which returns the list of primary keys, supplied by
522 sub primary_columns {
523 return @{shift->_primaries||[]};
526 # a helper method that will automatically die with a descriptive message if
527 # no pk is defined on the source in question. For internal use to save
528 # on if @pks... boilerplate
531 my @pcols = $self->primary_columns
532 or $self->throw_exception (sprintf(
533 "Operation requires a primary key to be declared on '%s' via set_primary_key",
539 =head2 add_unique_constraint
543 =item Arguments: $name?, \@colnames
545 =item Return value: undefined
549 Declare a unique constraint on this source. Call once for each unique
552 # For UNIQUE (column1, column2)
553 __PACKAGE__->add_unique_constraint(
554 constraint_name => [ qw/column1 column2/ ],
557 Alternatively, you can specify only the columns:
559 __PACKAGE__->add_unique_constraint([ qw/column1 column2/ ]);
561 This will result in a unique constraint named
562 C<table_column1_column2>, where C<table> is replaced with the table
565 Unique constraints are used, for example, when you pass the constraint
566 name as the C<key> attribute to L<DBIx::Class::ResultSet/find>. Then
567 only columns in the constraint are searched.
569 Throws an error if any of the given column names do not yet exist on
574 sub add_unique_constraint {
579 $name ||= $self->name_unique_constraint($cols);
581 foreach my $col (@$cols) {
582 $self->throw_exception("No such column $col on table " . $self->name)
583 unless $self->has_column($col);
586 my %unique_constraints = $self->unique_constraints;
587 $unique_constraints{$name} = $cols;
588 $self->_unique_constraints(\%unique_constraints);
591 =head2 name_unique_constraint
595 =item Arguments: @colnames
597 =item Return value: Constraint name
601 $source->table('mytable');
602 $source->name_unique_constraint('col1', 'col2');
606 Return a name for a unique constraint containing the specified
607 columns. The name is created by joining the table name and each column
608 name, using an underscore character.
610 For example, a constraint on a table named C<cd> containing the columns
611 C<artist> and C<title> would result in a constraint name of C<cd_artist_title>.
613 This is used by L</add_unique_constraint> if you do not specify the
614 optional constraint name.
618 sub name_unique_constraint {
619 my ($self, $cols) = @_;
621 my $name = $self->name;
622 $name = $$name if (ref $name eq 'SCALAR');
624 return join '_', $name, @$cols;
627 =head2 unique_constraints
631 =item Arguments: None
633 =item Return value: Hash of unique constraint data
637 $source->unique_constraints();
639 Read-only accessor which returns a hash of unique constraints on this
642 The hash is keyed by constraint name, and contains an arrayref of
643 column names as values.
647 sub unique_constraints {
648 return %{shift->_unique_constraints||{}};
651 =head2 unique_constraint_names
655 =item Arguments: None
657 =item Return value: Unique constraint names
661 $source->unique_constraint_names();
663 Returns the list of unique constraint names defined on this source.
667 sub unique_constraint_names {
670 my %unique_constraints = $self->unique_constraints;
672 return keys %unique_constraints;
675 =head2 unique_constraint_columns
679 =item Arguments: $constraintname
681 =item Return value: List of constraint columns
685 $source->unique_constraint_columns('myconstraint');
687 Returns the list of columns that make up the specified unique constraint.
691 sub unique_constraint_columns {
692 my ($self, $constraint_name) = @_;
694 my %unique_constraints = $self->unique_constraints;
696 $self->throw_exception(
697 "Unknown unique constraint $constraint_name on '" . $self->name . "'"
698 ) unless exists $unique_constraints{$constraint_name};
700 return @{ $unique_constraints{$constraint_name} };
703 =head2 sqlt_deploy_callback
707 =item Arguments: $callback
711 __PACKAGE__->sqlt_deploy_callback('mycallbackmethod');
713 An accessor to set a callback to be called during deployment of
714 the schema via L<DBIx::Class::Schema/create_ddl_dir> or
715 L<DBIx::Class::Schema/deploy>.
717 The callback can be set as either a code reference or the name of a
718 method in the current result class.
720 If not set, the L</default_sqlt_deploy_hook> is called.
722 Your callback will be passed the $source object representing the
723 ResultSource instance being deployed, and the
724 L<SQL::Translator::Schema::Table> object being created from it. The
725 callback can be used to manipulate the table object or add your own
726 customised indexes. If you need to manipulate a non-table object, use
727 the L<DBIx::Class::Schema/sqlt_deploy_hook>.
729 See L<DBIx::Class::Manual::Cookbook/Adding Indexes And Functions To
730 Your SQL> for examples.
732 This sqlt deployment callback can only be used to manipulate
733 SQL::Translator objects as they get turned into SQL. To execute
734 post-deploy statements which SQL::Translator does not currently
735 handle, override L<DBIx::Class::Schema/deploy> in your Schema class
736 and call L<dbh_do|DBIx::Class::Storage::DBI/dbh_do>.
738 =head2 default_sqlt_deploy_hook
742 =item Arguments: $source, $sqlt_table
744 =item Return value: undefined
748 This is the sensible default for L</sqlt_deploy_callback>.
750 If a method named C<sqlt_deploy_hook> exists in your Result class, it
751 will be called and passed the current C<$source> and the
752 C<$sqlt_table> being deployed.
756 sub default_sqlt_deploy_hook {
759 my $class = $self->result_class;
761 if ($class and $class->can('sqlt_deploy_hook')) {
762 $class->sqlt_deploy_hook(@_);
766 sub _invoke_sqlt_deploy_hook {
768 if ( my $hook = $self->sqlt_deploy_callback) {
777 =item Arguments: None
779 =item Return value: $resultset
783 Returns a resultset for the given source. This will initially be created
786 $self->resultset_class->new($self, $self->resultset_attributes)
788 but is cached from then on unless resultset_class changes.
790 =head2 resultset_class
794 =item Arguments: $classname
796 =item Return value: $classname
800 package My::Schema::ResultSet::Artist;
801 use base 'DBIx::Class::ResultSet';
804 # In the result class
805 __PACKAGE__->resultset_class('My::Schema::ResultSet::Artist');
808 $source->resultset_class('My::Schema::ResultSet::Artist');
810 Set the class of the resultset. This is useful if you want to create your
811 own resultset methods. Create your own class derived from
812 L<DBIx::Class::ResultSet>, and set it here. If called with no arguments,
813 this method returns the name of the existing resultset class, if one
816 =head2 resultset_attributes
820 =item Arguments: \%attrs
822 =item Return value: \%attrs
826 # In the result class
827 __PACKAGE__->resultset_attributes({ order_by => [ 'id' ] });
830 $source->resultset_attributes({ order_by => [ 'id' ] });
832 Store a collection of resultset attributes, that will be set on every
833 L<DBIx::Class::ResultSet> produced from this result source. For a full
834 list see L<DBIx::Class::ResultSet/ATTRIBUTES>.
840 $self->throw_exception(
841 'resultset does not take any arguments. If you want another resultset, '.
842 'call it on the schema instead.'
845 return $self->resultset_class->new(
848 %{$self->{resultset_attributes}},
849 %{$self->schema->default_resultset_attributes}
858 =item Arguments: $source_name
860 =item Result value: $source_name
864 Set an alternate name for the result source when it is loaded into a schema.
865 This is useful if you want to refer to a result source by a name other than
868 package ArchivedBooks;
869 use base qw/DBIx::Class/;
870 __PACKAGE__->table('books_archive');
871 __PACKAGE__->source_name('Books');
873 # from your schema...
874 $schema->resultset('Books')->find(1);
880 =item Arguments: None
882 =item Return value: FROM clause
886 my $from_clause = $source->from();
888 Returns an expression of the source to be supplied to storage to specify
889 retrieval from this source. In the case of a database, the required FROM
896 =item Arguments: None
898 =item Return value: A schema object
902 my $schema = $source->schema();
904 Returns the L<DBIx::Class::Schema> object that this result source
911 =item Arguments: None
913 =item Return value: A Storage object
917 $source->storage->debug(1);
919 Returns the storage handle for the current schema.
921 See also: L<DBIx::Class::Storage>
925 sub storage { shift->schema->storage; }
927 =head2 add_relationship
931 =item Arguments: $relname, $related_source_name, \%cond, [ \%attrs ]
933 =item Return value: 1/true if it succeeded
937 $source->add_relationship('relname', 'related_source', $cond, $attrs);
939 L<DBIx::Class::Relationship> describes a series of methods which
940 create pre-defined useful types of relationships. Look there first
941 before using this method directly.
943 The relationship name can be arbitrary, but must be unique for each
944 relationship attached to this result source. 'related_source' should
945 be the name with which the related result source was registered with
946 the current schema. For example:
948 $schema->source('Book')->add_relationship('reviews', 'Review', {
949 'foreign.book_id' => 'self.id',
952 The condition C<$cond> needs to be an L<SQL::Abstract>-style
953 representation of the join between the tables. For example, if you're
954 creating a relation from Author to Book,
956 { 'foreign.author_id' => 'self.id' }
958 will result in the JOIN clause
960 author me JOIN book foreign ON foreign.author_id = me.id
962 You can specify as many foreign => self mappings as necessary.
964 Valid attributes are as follows:
970 Explicitly specifies the type of join to use in the relationship. Any
971 SQL join type is valid, e.g. C<LEFT> or C<RIGHT>. It will be placed in
972 the SQL command immediately before C<JOIN>.
976 An arrayref containing a list of accessors in the foreign class to proxy in
977 the main class. If, for example, you do the following:
979 CD->might_have(liner_notes => 'LinerNotes', undef, {
980 proxy => [ qw/notes/ ],
983 Then, assuming LinerNotes has an accessor named notes, you can do:
985 my $cd = CD->find(1);
986 # set notes -- LinerNotes object is created if it doesn't exist
987 $cd->notes('Notes go here');
991 Specifies the type of accessor that should be created for the
992 relationship. Valid values are C<single> (for when there is only a single
993 related object), C<multi> (when there can be many), and C<filter> (for
994 when there is a single related object, but you also want the relationship
995 accessor to double as a column accessor). For C<multi> accessors, an
996 add_to_* method is also created, which calls C<create_related> for the
1001 Throws an exception if the condition is improperly supplied, or cannot
1006 sub add_relationship {
1007 my ($self, $rel, $f_source_name, $cond, $attrs) = @_;
1008 $self->throw_exception("Can't create relationship without join condition")
1012 # Check foreign and self are right in cond
1013 if ( (ref $cond ||'') eq 'HASH') {
1015 $self->throw_exception("Keys of condition should be of form 'foreign.col', not '$_'")
1016 if /\./ && !/^foreign\./;
1020 my %rels = %{ $self->_relationships };
1021 $rels{$rel} = { class => $f_source_name,
1022 source => $f_source_name,
1025 $self->_relationships(\%rels);
1029 # XXX disabled. doesn't work properly currently. skip in tests.
1031 my $f_source = $self->schema->source($f_source_name);
1032 unless ($f_source) {
1033 $self->ensure_class_loaded($f_source_name);
1034 $f_source = $f_source_name->result_source;
1035 #my $s_class = ref($self->schema);
1036 #$f_source_name =~ m/^${s_class}::(.*)$/;
1037 #$self->schema->register_class(($1 || $f_source_name), $f_source_name);
1038 #$f_source = $self->schema->source($f_source_name);
1040 return unless $f_source; # Can't test rel without f_source
1042 try { $self->_resolve_join($rel, 'me', {}, []) }
1044 # If the resolve failed, back out and re-throw the error
1046 $self->_relationships(\%rels);
1047 $self->throw_exception("Error creating relationship $rel: $_");
1053 =head2 relationships
1057 =item Arguments: None
1059 =item Return value: List of relationship names
1063 my @relnames = $source->relationships();
1065 Returns all relationship names for this source.
1070 return keys %{shift->_relationships};
1073 =head2 relationship_info
1077 =item Arguments: $relname
1079 =item Return value: Hashref of relation data,
1083 Returns a hash of relationship information for the specified relationship
1084 name. The keys/values are as specified for L</add_relationship>.
1088 sub relationship_info {
1089 my ($self, $rel) = @_;
1090 return $self->_relationships->{$rel};
1093 =head2 has_relationship
1097 =item Arguments: $rel
1099 =item Return value: 1/0 (true/false)
1103 Returns true if the source has a relationship of this name, false otherwise.
1107 sub has_relationship {
1108 my ($self, $rel) = @_;
1109 return exists $self->_relationships->{$rel};
1112 =head2 reverse_relationship_info
1116 =item Arguments: $relname
1118 =item Return value: Hashref of relationship data
1122 Looks through all the relationships on the source this relationship
1123 points to, looking for one whose condition is the reverse of the
1124 condition on this relationship.
1126 A common use of this is to find the name of the C<belongs_to> relation
1127 opposing a C<has_many> relation. For definition of these look in
1128 L<DBIx::Class::Relationship>.
1130 The returned hashref is keyed by the name of the opposing
1131 relationship, and contains its data in the same manner as
1132 L</relationship_info>.
1136 sub reverse_relationship_info {
1137 my ($self, $rel) = @_;
1138 my $rel_info = $self->relationship_info($rel);
1141 return $ret unless ((ref $rel_info->{cond}) eq 'HASH');
1143 my @cond = keys(%{$rel_info->{cond}});
1144 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
1145 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
1147 # Get the related result source for this relationship
1148 my $othertable = $self->related_source($rel);
1150 # Get all the relationships for that source that related to this source
1151 # whose foreign column set are our self columns on $rel and whose self
1152 # columns are our foreign columns on $rel.
1153 my @otherrels = $othertable->relationships();
1154 my $otherrelationship;
1155 foreach my $otherrel (@otherrels) {
1156 my $otherrel_info = $othertable->relationship_info($otherrel);
1158 my $back = $othertable->related_source($otherrel);
1159 next unless $back->source_name eq $self->source_name;
1163 if (ref $otherrel_info->{cond} eq 'HASH') {
1164 @othertestconds = ($otherrel_info->{cond});
1166 elsif (ref $otherrel_info->{cond} eq 'ARRAY') {
1167 @othertestconds = @{$otherrel_info->{cond}};
1173 foreach my $othercond (@othertestconds) {
1174 my @other_cond = keys(%$othercond);
1175 my @other_refkeys = map {/^\w+\.(\w+)$/} @other_cond;
1176 my @other_keys = map {$othercond->{$_} =~ /^\w+\.(\w+)$/} @other_cond;
1177 next if (!$self->_compare_relationship_keys(\@refkeys, \@other_keys) ||
1178 !$self->_compare_relationship_keys(\@other_refkeys, \@keys));
1179 $ret->{$otherrel} = $otherrel_info;
1185 sub compare_relationship_keys {
1186 carp 'compare_relationship_keys is a private method, stop calling it';
1188 $self->_compare_relationship_keys (@_);
1191 # Returns true if both sets of keynames are the same, false otherwise.
1192 sub _compare_relationship_keys {
1193 my ($self, $keys1, $keys2) = @_;
1195 # Make sure every keys1 is in keys2
1197 foreach my $key (@$keys1) {
1199 foreach my $prim (@$keys2) {
1200 if ($prim eq $key) {
1208 # Make sure every key2 is in key1
1210 foreach my $prim (@$keys2) {
1212 foreach my $key (@$keys1) {
1213 if ($prim eq $key) {
1225 # Returns the {from} structure used to express JOIN conditions
1227 my ($self, $join, $alias, $seen, $jpath, $parent_force_left) = @_;
1229 # we need a supplied one, because we do in-place modifications, no returns
1230 $self->throw_exception ('You must supply a seen hashref as the 3rd argument to _resolve_join')
1231 unless ref $seen eq 'HASH';
1233 $self->throw_exception ('You must supply a joinpath arrayref as the 4th argument to _resolve_join')
1234 unless ref $jpath eq 'ARRAY';
1236 $jpath = [@$jpath]; # copy
1238 if (not defined $join) {
1241 elsif (ref $join eq 'ARRAY') {
1244 $self->_resolve_join($_, $alias, $seen, $jpath, $parent_force_left);
1247 elsif (ref $join eq 'HASH') {
1250 for my $rel (keys %$join) {
1252 my $rel_info = $self->relationship_info($rel)
1253 or $self->throw_exception("No such relationship '$rel' on " . $self->source_name);
1255 my $force_left = $parent_force_left;
1256 $force_left ||= lc($rel_info->{attrs}{join_type}||'') eq 'left';
1258 # the actual seen value will be incremented by the recursion
1259 my $as = $self->storage->relname_to_table_alias(
1260 $rel, ($seen->{$rel} && $seen->{$rel} + 1)
1264 $self->_resolve_join($rel, $alias, $seen, [@$jpath], $force_left),
1265 $self->related_source($rel)->_resolve_join(
1266 $join->{$rel}, $as, $seen, [@$jpath, { $rel => $as }], $force_left
1274 $self->throw_exception("No idea how to resolve join reftype ".ref $join);
1277 my $count = ++$seen->{$join};
1278 my $as = $self->storage->relname_to_table_alias(
1279 $join, ($count > 1 && $count)
1282 my $rel_info = $self->relationship_info($join)
1283 or $self->throw_exception("No such relationship $join on " . $self->source_name);
1285 my $rel_src = $self->related_source($join);
1286 return [ { $as => $rel_src->from,
1287 -source_handle => $rel_src->handle,
1288 -join_type => $parent_force_left
1290 : $rel_info->{attrs}{join_type}
1292 -join_path => [@$jpath, { $join => $as } ],
1294 $rel_info->{attrs}{accessor}
1296 first { $rel_info->{attrs}{accessor} eq $_ } (qw/single filter/)
1299 -relation_chain_depth => $seen->{-relation_chain_depth} || 0,
1301 $self->_resolve_condition($rel_info->{cond}, $as, $alias) ];
1306 carp 'pk_depends_on is a private method, stop calling it';
1308 $self->_pk_depends_on (@_);
1311 # Determines whether a relation is dependent on an object from this source
1312 # having already been inserted. Takes the name of the relationship and a
1313 # hashref of columns of the related object.
1314 sub _pk_depends_on {
1315 my ($self, $relname, $rel_data) = @_;
1317 my $relinfo = $self->relationship_info($relname);
1319 # don't assume things if the relationship direction is specified
1320 return $relinfo->{attrs}{is_foreign_key_constraint}
1321 if exists ($relinfo->{attrs}{is_foreign_key_constraint});
1323 my $cond = $relinfo->{cond};
1324 return 0 unless ref($cond) eq 'HASH';
1326 # map { foreign.foo => 'self.bar' } to { bar => 'foo' }
1327 my $keyhash = { map { my $x = $_; $x =~ s/.*\.//; $x; } reverse %$cond };
1329 # assume anything that references our PK probably is dependent on us
1330 # rather than vice versa, unless the far side is (a) defined or (b)
1332 my $rel_source = $self->related_source($relname);
1334 foreach my $p ($self->primary_columns) {
1335 if (exists $keyhash->{$p}) {
1336 unless (defined($rel_data->{$keyhash->{$p}})
1337 || $rel_source->column_info($keyhash->{$p})
1338 ->{is_auto_increment}) {
1347 sub resolve_condition {
1348 carp 'resolve_condition is a private method, stop calling it';
1350 $self->_resolve_condition (@_);
1353 # Resolves the passed condition to a concrete query fragment. If given an alias,
1354 # returns a join condition; if given an object, inverts that object to produce
1355 # a related conditional from that object.
1356 our $UNRESOLVABLE_CONDITION = \'1 = 0';
1358 sub _resolve_condition {
1359 my ($self, $cond, $as, $for) = @_;
1360 if (ref $cond eq 'HASH') {
1362 foreach my $k (keys %{$cond}) {
1363 my $v = $cond->{$k};
1364 # XXX should probably check these are valid columns
1365 $k =~ s/^foreign\.// ||
1366 $self->throw_exception("Invalid rel cond key ${k}");
1367 $v =~ s/^self\.// ||
1368 $self->throw_exception("Invalid rel cond val ${v}");
1369 if (ref $for) { # Object
1370 #warn "$self $k $for $v";
1371 unless ($for->has_column_loaded($v)) {
1372 if ($for->in_storage) {
1373 $self->throw_exception(sprintf
1374 "Unable to resolve relationship '%s' from object %s: column '%s' not "
1375 . 'loaded from storage (or not passed to new() prior to insert()). You '
1376 . 'probably need to call ->discard_changes to get the server-side defaults '
1377 . 'from the database.',
1383 return $UNRESOLVABLE_CONDITION;
1385 $ret{$k} = $for->get_column($v);
1386 #$ret{$k} = $for->get_column($v) if $for->has_column_loaded($v);
1388 } elsif (!defined $for) { # undef, i.e. "no object"
1390 } elsif (ref $as eq 'HASH') { # reverse hashref
1391 $ret{$v} = $as->{$k};
1392 } elsif (ref $as) { # reverse object
1393 $ret{$v} = $as->get_column($k);
1394 } elsif (!defined $as) { # undef, i.e. "no reverse object"
1397 $ret{"${as}.${k}"} = "${for}.${v}";
1401 } elsif (ref $cond eq 'ARRAY') {
1402 return [ map { $self->_resolve_condition($_, $as, $for) } @$cond ];
1404 die("Can't handle condition $cond yet :(");
1409 # Accepts one or more relationships for the current source and returns an
1410 # array of column names for each of those relationships. Column names are
1411 # prefixed relative to the current source, in accordance with where they appear
1412 # in the supplied relationships.
1414 sub _resolve_prefetch {
1415 my ($self, $pre, $alias, $alias_map, $order, $collapse, $pref_path) = @_;
1418 if (not defined $pre) {
1421 elsif( ref $pre eq 'ARRAY' ) {
1423 map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, $collapse, [ @$pref_path ] ) }
1426 elsif( ref $pre eq 'HASH' ) {
1429 $self->_resolve_prefetch($_, $alias, $alias_map, $order, $collapse, [ @$pref_path ] ),
1430 $self->related_source($_)->_resolve_prefetch(
1431 $pre->{$_}, "${alias}.$_", $alias_map, $order, $collapse, [ @$pref_path, $_] )
1436 $self->throw_exception(
1437 "don't know how to resolve prefetch reftype ".ref($pre));
1441 $p = $p->{$_} for (@$pref_path, $pre);
1443 $self->throw_exception (
1444 "Unable to resolve prefetch '$pre' - join alias map does not contain an entry for path: "
1445 . join (' -> ', @$pref_path, $pre)
1446 ) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} );
1448 my $as = shift @{$p->{-join_aliases}};
1450 my $rel_info = $self->relationship_info( $pre );
1451 $self->throw_exception( $self->source_name . " has no such relationship '$pre'" )
1453 my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
1454 my $rel_source = $self->related_source($pre);
1456 if ($rel_info->{attrs}{accessor} && $rel_info->{attrs}{accessor} eq 'multi') {
1457 $self->throw_exception(
1458 "Can't prefetch has_many ${pre} (join cond too complex)")
1459 unless ref($rel_info->{cond}) eq 'HASH';
1460 my $dots = @{[$as_prefix =~ m/\./g]} + 1; # +1 to match the ".${as_prefix}"
1461 if (my ($fail) = grep { @{[$_ =~ m/\./g]} == $dots }
1462 keys %{$collapse}) {
1463 my ($last) = ($fail =~ /([^\.]+)$/);
1465 "Prefetching multiple has_many rels ${last} and ${pre} "
1466 .(length($as_prefix)
1467 ? "at the same level (${as_prefix}) "
1470 . 'will explode the number of row objects retrievable via ->next or ->all. '
1471 . 'Use at your own risk.'
1474 #my @col = map { (/^self\.(.+)$/ ? ("${as_prefix}.$1") : ()); }
1475 # values %{$rel_info->{cond}};
1476 $collapse->{".${as_prefix}${pre}"} = [ $rel_source->_pri_cols ];
1477 # action at a distance. prepending the '.' allows simpler code
1478 # in ResultSet->_collapse_result
1479 my @key = map { (/^foreign\.(.+)$/ ? ($1) : ()); }
1480 keys %{$rel_info->{cond}};
1481 my @ord = (ref($rel_info->{attrs}{order_by}) eq 'ARRAY'
1482 ? @{$rel_info->{attrs}{order_by}}
1484 : (defined $rel_info->{attrs}{order_by}
1485 ? ($rel_info->{attrs}{order_by})
1487 push(@$order, map { "${as}.$_" } (@key, @ord));
1490 return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
1491 $rel_source->columns;
1495 =head2 related_source
1499 =item Arguments: $relname
1501 =item Return value: $source
1505 Returns the result source object for the given relationship.
1509 sub related_source {
1510 my ($self, $rel) = @_;
1511 if( !$self->has_relationship( $rel ) ) {
1512 $self->throw_exception("No such relationship '$rel' on " . $self->source_name);
1514 return $self->schema->source($self->relationship_info($rel)->{source});
1517 =head2 related_class
1521 =item Arguments: $relname
1523 =item Return value: $classname
1527 Returns the class name for objects in the given relationship.
1532 my ($self, $rel) = @_;
1533 if( !$self->has_relationship( $rel ) ) {
1534 $self->throw_exception("No such relationship '$rel' on " . $self->source_name);
1536 return $self->schema->class($self->relationship_info($rel)->{source});
1541 Obtain a new handle to this source. Returns an instance of a
1542 L<DBIx::Class::ResultSourceHandle>.
1547 return DBIx::Class::ResultSourceHandle->new({
1548 schema => $_[0]->schema,
1549 source_moniker => $_[0]->source_name
1553 =head2 throw_exception
1555 See L<DBIx::Class::Schema/"throw_exception">.
1559 sub throw_exception {
1562 if (defined $self->schema) {
1563 $self->schema->throw_exception(@_);
1566 DBIx::Class::Exception->throw(@_);
1572 Stores a hashref of per-source metadata. No specific key names
1573 have yet been standardized, the examples below are purely hypothetical
1574 and don't actually accomplish anything on their own:
1576 __PACKAGE__->source_info({
1577 "_tablespace" => 'fast_disk_array_3',
1578 "_engine" => 'InnoDB',
1585 $class->new({attribute_name => value});
1587 Creates a new ResultSource object. Not normally called directly by end users.
1589 =head2 column_info_from_storage
1593 =item Arguments: 1/0 (default: 0)
1595 =item Return value: 1/0
1599 __PACKAGE__->column_info_from_storage(1);
1601 Enables the on-demand automatic loading of the above column
1602 metadata from storage as necessary. This is *deprecated*, and
1603 should not be used. It will be removed before 1.0.
1608 Matt S. Trout <mst@shadowcatsystems.co.uk>
1612 You may distribute this code under the same terms as Perl itself.