clarify need for scalarref when using db functions with 'default_value' attribute
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource.pm
1 package DBIx::Class::ResultSource;
2
3 use strict;
4 use warnings;
5
6 use DBIx::Class::ResultSet;
7 use DBIx::Class::ResultSourceHandle;
8 use Carp::Clan qw/^DBIx::Class/;
9 use Storable;
10
11 use base qw/DBIx::Class/;
12
13 __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns
14   _columns _primaries _unique_constraints name resultset_attributes
15   schema from _relationships column_info_from_storage source_info
16   source_name sqlt_deploy_callback/);
17
18 __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class
19   result_class/);
20
21 =head1 NAME
22
23 DBIx::Class::ResultSource - Result source object
24
25 =head1 SYNOPSIS
26
27 =head1 DESCRIPTION
28
29 A ResultSource is a component of a schema from which results can be directly
30 retrieved, most usually a table (see L<DBIx::Class::ResultSource::Table>)
31
32 Basic view support also exists, see L<<DBIx::Class::ResultSource::View>.
33
34 =head1 METHODS
35
36 =pod
37
38 =cut
39
40 sub new {
41   my ($class, $attrs) = @_;
42   $class = ref $class if ref $class;
43
44   my $new = bless { %{$attrs || {}} }, $class;
45   $new->{resultset_class} ||= 'DBIx::Class::ResultSet';
46   $new->{resultset_attributes} = { %{$new->{resultset_attributes} || {}} };
47   $new->{_ordered_columns} = [ @{$new->{_ordered_columns}||[]}];
48   $new->{_columns} = { %{$new->{_columns}||{}} };
49   $new->{_relationships} = { %{$new->{_relationships}||{}} };
50   $new->{name} ||= "!!NAME NOT SET!!";
51   $new->{_columns_info_loaded} ||= 0;
52   $new->{sqlt_deploy_callback} ||= "default_sqlt_deploy_hook";
53   return $new;
54 }
55
56 =pod
57
58 =head2 add_columns
59
60 =over
61
62 =item Arguments: @columns
63
64 =item Return value: The ResultSource object
65
66 =back
67
68   $source->add_columns(qw/col1 col2 col3/);
69
70   $source->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...);
71
72 Adds columns to the result source. If supplied key => hashref pairs, uses
73 the hashref as the column_info for that column. Repeated calls of this
74 method will add more columns, not replace them.
75
76 The column names given will be created as accessor methods on your
77 L<DBIx::Class::Row> objects. You can change the name of the accessor
78 by supplying an L</accessor> in the column_info hash.
79
80 The contents of the column_info are not set in stone. The following
81 keys are currently recognised/used by DBIx::Class:
82
83 =over 4
84
85 =item accessor
86
87 Use this to set the name of the accessor method for this column. If unset,
88 the name of the column will be used.
89
90 =item data_type
91
92 This contains the column type. It is automatically filled by the
93 L<SQL::Translator::Producer::DBIx::Class::File> producer, and the
94 L<DBIx::Class::Schema::Loader> module. If you do not enter a
95 data_type, DBIx::Class will attempt to retrieve it from the
96 database for you, using L<DBI>'s column_info method. The values of this
97 key are typically upper-cased.
98
99 Currently there is no standard set of values for the data_type. Use
100 whatever your database supports.
101
102 =item size
103
104 The length of your column, if it is a column type that can have a size
105 restriction. This is currently only used by L<DBIx::Class::Schema/deploy>.
106
107 =item is_nullable
108
109 Set this to a true value for a columns that is allowed to contain
110 NULL values. This is currently only used by L<DBIx::Class::Schema/deploy>.
111
112 =item is_auto_increment
113
114 Set this to a true value for a column whose value is somehow
115 automatically set. This is used to determine which columns to empty
116 when cloning objects using C<copy>. It is also used by
117 L<DBIx::Class::Schema/deploy>.
118
119 =item is_foreign_key
120
121 Set this to a true value for a column that contains a key from a
122 foreign table. This is currently only used by
123 L<DBIx::Class::Schema/deploy>.
124
125 =item default_value
126
127 Set this to the default value which will be inserted into a column
128 by the database. Can contain either a value or a function (use a
129 reference to a scalar e.g. C<\'now()'> if you want a function). This
130 is currently only used by L<DBIx::Class::Schema/deploy>.
131
132 =item sequence
133
134 Set this on a primary key column to the name of the sequence used to
135 generate a new key value. If not specified, L<DBIx::Class::PK::Auto>
136 will attempt to retrieve the name of the sequence from the database
137 automatically.
138
139 =item auto_nextval
140
141 Set this to a true value for a column whose value is retrieved
142 automatically from an oracle sequence. If you do not use an Oracle
143 trigger to get the nextval, you have to set sequence as well.
144
145 =item extra
146
147 This is used by L<DBIx::Class::Schema/deploy> and L<SQL::Translator>
148 to add extra non-generic data to the column. For example: C<< extra
149 => { unsigned => 1} >> is used by the MySQL producer to set an integer
150 column to unsigned. For more details, see
151 L<SQL::Translator::Producer::MySQL>.
152
153 =back
154
155 =head2 add_column
156
157 =over
158
159 =item Arguments: $colname, [ \%columninfo ]
160
161 =item Return value: 1/0 (true/false)
162
163 =back
164
165   $source->add_column('col' => \%info?);
166
167 Add a single column and optional column info. Uses the same column
168 info keys as L</add_columns>.
169
170 =cut
171
172 sub add_columns {
173   my ($self, @cols) = @_;
174   $self->_ordered_columns(\@cols) unless $self->_ordered_columns;
175
176   my @added;
177   my $columns = $self->_columns;
178   while (my $col = shift @cols) {
179     # If next entry is { ... } use that for the column info, if not
180     # use an empty hashref
181     my $column_info = ref $cols[0] ? shift(@cols) : {};
182     push(@added, $col) unless exists $columns->{$col};
183     $columns->{$col} = $column_info;
184   }
185   push @{ $self->_ordered_columns }, @added;
186   return $self;
187 }
188
189 sub add_column { shift->add_columns(@_); } # DO NOT CHANGE THIS TO GLOB
190
191 =head2 has_column
192
193 =over
194
195 =item Arguments: $colname
196
197 =item Return value: 1/0 (true/false)
198
199 =back
200
201   if ($source->has_column($colname)) { ... }
202
203 Returns true if the source has a column of this name, false otherwise.
204
205 =cut
206
207 sub has_column {
208   my ($self, $column) = @_;
209   return exists $self->_columns->{$column};
210 }
211
212 =head2 column_info
213
214 =over
215
216 =item Arguments: $colname
217
218 =item Return value: Hashref of info
219
220 =back
221
222   my $info = $source->column_info($col);
223
224 Returns the column metadata hashref for a column, as originally passed
225 to L</add_columns>. See the description of L</add_columns> for information
226 on the contents of the hashref.
227
228 =cut
229
230 sub column_info {
231   my ($self, $column) = @_;
232   $self->throw_exception("No such column $column")
233     unless exists $self->_columns->{$column};
234   #warn $self->{_columns_info_loaded}, "\n";
235   if ( ! $self->_columns->{$column}{data_type}
236        and $self->column_info_from_storage
237        and ! $self->{_columns_info_loaded}
238        and $self->schema and $self->storage )
239   {
240     $self->{_columns_info_loaded}++;
241     my $info = {};
242     my $lc_info = {};
243     # eval for the case of storage without table
244     eval { $info = $self->storage->columns_info_for( $self->from ) };
245     unless ($@) {
246       for my $realcol ( keys %{$info} ) {
247         $lc_info->{lc $realcol} = $info->{$realcol};
248       }
249       foreach my $col ( keys %{$self->_columns} ) {
250         $self->_columns->{$col} = {
251           %{ $self->_columns->{$col} },
252           %{ $info->{$col} || $lc_info->{lc $col} || {} }
253         };
254       }
255     }
256   }
257   return $self->_columns->{$column};
258 }
259
260 =head2 columns
261
262 =over
263
264 =item Arguments: None
265
266 =item Return value: Ordered list of column names
267
268 =back
269
270   my @column_names = $source->columns;
271
272 Returns all column names in the order they were declared to L</add_columns>.
273
274 =cut
275
276 sub columns {
277   my $self = shift;
278   $self->throw_exception(
279     "columns() is a read-only accessor, did you mean add_columns()?"
280   ) if (@_ > 1);
281   return @{$self->{_ordered_columns}||[]};
282 }
283
284 =head2 remove_columns
285
286 =over
287
288 =item Arguments: @colnames
289
290 =item Return value: undefined
291
292 =back
293
294   $source->remove_columns(qw/col1 col2 col3/);
295
296 Removes the given list of columns by name, from the result source.
297
298 B<Warning>: Removing a column that is also used in the sources primary
299 key, or in one of the sources unique constraints, B<will> result in a
300 broken result source.
301
302 =head2 remove_column
303
304 =over
305
306 =item Arguments: $colname
307
308 =item Return value: undefined
309
310 =back
311
312   $source->remove_column('col');
313
314 Remove a single column by name from the result source, similar to
315 L</remove_columns>.
316
317 B<Warning>: Removing a column that is also used in the sources primary
318 key, or in one of the sources unique constraints, B<will> result in a
319 broken result source.
320
321 =cut
322
323 sub remove_columns {
324   my ($self, @to_remove) = @_;
325
326   my $columns = $self->_columns
327     or return;
328
329   my %to_remove;
330   for (@to_remove) {
331     delete $columns->{$_};
332     ++$to_remove{$_};
333   }
334
335   $self->_ordered_columns([ grep { not $to_remove{$_} } @{$self->_ordered_columns} ]);
336 }
337
338 sub remove_column { shift->remove_columns(@_); } # DO NOT CHANGE THIS TO GLOB
339
340 =head2 set_primary_key
341
342 =over 4
343
344 =item Arguments: @cols
345
346 =item Return value: undefined
347
348 =back
349
350 Defines one or more columns as primary key for this source. Should be
351 called after L</add_columns>.
352
353 Additionally, defines a L<unique constraint|add_unique_constraint>
354 named C<primary>.
355
356 The primary key columns are used by L<DBIx::Class::PK::Auto> to
357 retrieve automatically created values from the database.
358
359 =cut
360
361 sub set_primary_key {
362   my ($self, @cols) = @_;
363   # check if primary key columns are valid columns
364   foreach my $col (@cols) {
365     $self->throw_exception("No such column $col on table " . $self->name)
366       unless $self->has_column($col);
367   }
368   $self->_primaries(\@cols);
369
370   $self->add_unique_constraint(primary => \@cols);
371 }
372
373 =head2 primary_columns
374
375 =over 4
376
377 =item Arguments: None
378
379 =item Return value: Ordered list of primary column names
380
381 =back
382
383 Read-only accessor which returns the list of primary keys, supplied by
384 L</set_primary_key>.
385
386 =cut
387
388 sub primary_columns {
389   return @{shift->_primaries||[]};
390 }
391
392 =head2 add_unique_constraint
393
394 =over 4
395
396 =item Arguments: [ $name ], \@colnames
397
398 =item Return value: undefined
399
400 =back
401
402 Declare a unique constraint on this source. Call once for each unique
403 constraint.
404
405   # For UNIQUE (column1, column2)
406   __PACKAGE__->add_unique_constraint(
407     constraint_name => [ qw/column1 column2/ ],
408   );
409
410 Alternatively, you can specify only the columns:
411
412   __PACKAGE__->add_unique_constraint([ qw/column1 column2/ ]);
413
414 This will result in a unique constraint named C<table_column1_column2>, where
415 C<table> is replaced with the table name.
416
417 Unique constraints are used, for example, when you call
418 L<DBIx::Class::ResultSet/find>. Only columns in the constraint are searched.
419
420 Throws an error if any of the given column names do not yet exist on
421 the result source.
422
423 =cut
424
425 sub add_unique_constraint {
426   my $self = shift;
427   my $cols = pop @_;
428   my $name = shift;
429
430   $name ||= $self->name_unique_constraint($cols);
431
432   foreach my $col (@$cols) {
433     $self->throw_exception("No such column $col on table " . $self->name)
434       unless $self->has_column($col);
435   }
436
437   my %unique_constraints = $self->unique_constraints;
438   $unique_constraints{$name} = $cols;
439   $self->_unique_constraints(\%unique_constraints);
440 }
441
442 =head2 name_unique_constraint
443
444 =over 4
445
446 =item Arguments: @colnames
447
448 =item Return value: Constraint name
449
450 =back
451
452   $source->table('mytable');
453   $source->name_unique_constraint('col1', 'col2');
454   # returns
455   'mytable_col1_col2'
456
457 Return a name for a unique constraint containing the specified
458 columns. The name is created by joining the table name and each column
459 name, using an underscore character.
460
461 For example, a constraint on a table named C<cd> containing the columns
462 C<artist> and C<title> would result in a constraint name of C<cd_artist_title>.
463
464 This is used by L</add_unique_constraint> if you do not specify the
465 optional constraint name.
466
467 =cut
468
469 sub name_unique_constraint {
470   my ($self, $cols) = @_;
471
472   return join '_', $self->name, @$cols;
473 }
474
475 =head2 unique_constraints
476
477 =over 4
478
479 =item Arguments: None
480
481 =item Return value: Hash of unique constraint data
482
483 =back
484
485   $source->unique_constraints();
486
487 Read-only accessor which returns a hash of unique constraints on this source.
488
489 The hash is keyed by constraint name, and contains an arrayref of
490 column names as values.
491
492 =cut
493
494 sub unique_constraints {
495   return %{shift->_unique_constraints||{}};
496 }
497
498 =head2 unique_constraint_names
499
500 =over 4
501
502 =item Arguments: None
503
504 =item Return value: Unique constraint names
505
506 =back
507
508   $source->unique_constraint_names();
509
510 Returns the list of unique constraint names defined on this source.
511
512 =cut
513
514 sub unique_constraint_names {
515   my ($self) = @_;
516
517   my %unique_constraints = $self->unique_constraints;
518
519   return keys %unique_constraints;
520 }
521
522 =head2 unique_constraint_columns
523
524 =over 4
525
526 =item Arguments: $constraintname
527
528 =item Return value: List of constraint columns
529
530 =back
531
532   $source->unique_constraint_columns('myconstraint');
533
534 Returns the list of columns that make up the specified unique constraint.
535
536 =cut
537
538 sub unique_constraint_columns {
539   my ($self, $constraint_name) = @_;
540
541   my %unique_constraints = $self->unique_constraints;
542
543   $self->throw_exception(
544     "Unknown unique constraint $constraint_name on '" . $self->name . "'"
545   ) unless exists $unique_constraints{$constraint_name};
546
547   return @{ $unique_constraints{$constraint_name} };
548 }
549
550 =head2 sqlt_deploy_callback
551
552 =over
553
554 =item Arguments: $callback
555
556 =back
557
558   __PACKAGE__->sqlt_deploy_callback('mycallbackmethod');
559
560 An accessor to set a callback to be called during deployment of
561 the schema via L<DBIx::Class::Schema/create_ddl_dir> or
562 L<DBIx::Class::Schema/deploy>.
563
564 The callback can be set as either a code reference or the name of a
565 method in the current result class.
566
567 If not set, the L</default_sqlt_deploy_hook> is called.
568
569 Your callback will be passed the $source object representing the
570 ResultSource instance being deployed, and the
571 L<SQL::Translator::Schema::Table> object being created from it. The
572 callback can be used to manipulate the table object or add your own
573 customised indexes. If you need to manipulate a non-table object, use
574 the L<DBIx::Class::Schema/sqlt_deploy_hook>.
575
576 See L<DBIx::Class::Manual::Cookbook/Adding Indexes And Functions To
577 Your SQL> for examples.
578
579 This sqlt deployment callback can only be used to manipulate
580 SQL::Translator objects as they get turned into SQL. To execute
581 post-deploy statements which SQL::Translator does not currently
582 handle, override L<DBIx::Class::Schema/deploy> in your Schema class
583 and call L<dbh_do|DBIx::Class::Storage::DBI/dbh_do>.
584
585 =head2 default_sqlt_deploy_hook
586
587 =over
588
589 =item Arguments: $source, $sqlt_table
590
591 =item Return value: undefined
592
593 =back
594
595 This is the sensible default for L</sqlt_deploy_callback>.
596
597 If a method named C<sqlt_deploy_hook> exists in your Result class, it
598 will be called and passed the current C<$source> and the
599 C<$sqlt_table> being deployed.
600
601 =cut
602
603 sub default_sqlt_deploy_hook {
604   my $self = shift;
605
606   my $class = $self->result_class;
607
608   if ($class and $class->can('sqlt_deploy_hook')) {
609     $class->sqlt_deploy_hook(@_);
610   }
611 }
612
613 sub _invoke_sqlt_deploy_hook {
614   my $self = shift;
615   if ( my $hook = $self->sqlt_deploy_callback) {
616     $self->$hook(@_);
617   }
618 }
619
620 =head2 resultset
621
622 =over 4
623
624 =item Arguments: None
625
626 =item Return value: $resultset
627
628 =back
629
630 Returns a resultset for the given source. This will initially be created
631 on demand by calling
632
633   $self->resultset_class->new($self, $self->resultset_attributes)
634
635 but is cached from then on unless resultset_class changes.
636
637 =head2 resultset_class
638
639 =over 4
640
641 =item Arguments: $classname
642
643 =item Return value: $classname
644
645 =back
646
647   package My::ResultSetClass;
648   use base 'DBIx::Class::ResultSet';
649   ...
650
651   $source->resultset_class('My::ResultSet::Class');
652
653 Set the class of the resultset. This is useful if you want to create your
654 own resultset methods. Create your own class derived from
655 L<DBIx::Class::ResultSet>, and set it here. If called with no arguments,
656 this method returns the name of the existing resultset class, if one
657 exists.
658
659 =head2 resultset_attributes
660
661 =over 4
662
663 =item Arguments: \%attrs
664
665 =item Return value: \%attrs
666
667 =back
668
669   $source->resultset_attributes({ order_by => [ 'id' ] });
670
671 Store a collection of resultset attributes, that will be set on every
672 L<DBIx::Class::ResultSet> produced from this result source. For a full
673 list see L<DBIx::Class::ResultSet/ATTRIBUTES>.
674
675 =cut
676
677 sub resultset {
678   my $self = shift;
679   $self->throw_exception(
680     'resultset does not take any arguments. If you want another resultset, '.
681     'call it on the schema instead.'
682   ) if scalar @_;
683
684   return $self->resultset_class->new(
685     $self,
686     {
687       %{$self->{resultset_attributes}},
688       %{$self->schema->default_resultset_attributes}
689     },
690   );
691 }
692
693 =head2 source_name
694
695 =over 4
696
697 =item Arguments: $source_name
698
699 =item Result value: $source_name
700
701 =back
702
703 Set an alternate name for the result source when it is loaded into a schema.
704 This is useful if you want to refer to a result source by a name other than
705 its class name.
706
707   package ArchivedBooks;
708   use base qw/DBIx::Class/;
709   __PACKAGE__->table('books_archive');
710   __PACKAGE__->source_name('Books');
711
712   # from your schema...
713   $schema->resultset('Books')->find(1);
714
715 =head2 from
716
717 =over 4
718
719 =item Arguments: None
720
721 =item Return value: FROM clause
722
723 =back
724
725   my $from_clause = $source->from();
726
727 Returns an expression of the source to be supplied to storage to specify
728 retrieval from this source. In the case of a database, the required FROM
729 clause contents.
730
731 =head2 schema
732
733 =over 4
734
735 =item Arguments: None
736
737 =item Return value: A schema object
738
739 =back
740
741   my $schema = $source->schema();
742
743 Returns the L<DBIx::Class::Schema> object that this result source 
744 belongs to.
745
746 =head2 storage
747
748 =over 4
749
750 =item Arguments: None
751
752 =item Return value: A Storage object
753
754 =back
755
756   $source->storage->debug(1);
757
758 Returns the storage handle for the current schema.
759
760 See also: L<DBIx::Class::Storage>
761
762 =cut
763
764 sub storage { shift->schema->storage; }
765
766 =head2 add_relationship
767
768 =over 4
769
770 =item Arguments: $relname, $related_source_name, \%cond, [ \%attrs ]
771
772 =item Return value: 1/true if it succeeded
773
774 =back
775
776   $source->add_relationship('relname', 'related_source', $cond, $attrs);
777
778 L<DBIx::Class::Relationship> describes a series of methods which
779 create pre-defined useful types of relationships. Look there first
780 before using this method directly.
781
782 The relationship name can be arbitrary, but must be unique for each
783 relationship attached to this result source. 'related_source' should
784 be the name with which the related result source was registered with
785 the current schema. For example:
786
787   $schema->source('Book')->add_relationship('reviews', 'Review', {
788     'foreign.book_id' => 'self.id',
789   });
790
791 The condition C<$cond> needs to be an L<SQL::Abstract>-style
792 representation of the join between the tables. For example, if you're
793 creating a relation from Author to Book,
794
795   { 'foreign.author_id' => 'self.id' }
796
797 will result in the JOIN clause
798
799   author me JOIN book foreign ON foreign.author_id = me.id
800
801 You can specify as many foreign => self mappings as necessary.
802
803 Valid attributes are as follows:
804
805 =over 4
806
807 =item join_type
808
809 Explicitly specifies the type of join to use in the relationship. Any
810 SQL join type is valid, e.g. C<LEFT> or C<RIGHT>. It will be placed in
811 the SQL command immediately before C<JOIN>.
812
813 =item proxy
814
815 An arrayref containing a list of accessors in the foreign class to proxy in
816 the main class. If, for example, you do the following:
817
818   CD->might_have(liner_notes => 'LinerNotes', undef, {
819     proxy => [ qw/notes/ ],
820   });
821
822 Then, assuming LinerNotes has an accessor named notes, you can do:
823
824   my $cd = CD->find(1);
825   # set notes -- LinerNotes object is created if it doesn't exist
826   $cd->notes('Notes go here');
827
828 =item accessor
829
830 Specifies the type of accessor that should be created for the
831 relationship. Valid values are C<single> (for when there is only a single
832 related object), C<multi> (when there can be many), and C<filter> (for
833 when there is a single related object, but you also want the relationship
834 accessor to double as a column accessor). For C<multi> accessors, an
835 add_to_* method is also created, which calls C<create_related> for the
836 relationship.
837
838 =back
839
840 Throws an exception if the condition is improperly supplied, or cannot
841 be resolved using L</resolve_join>.
842
843 =cut
844
845 sub add_relationship {
846   my ($self, $rel, $f_source_name, $cond, $attrs) = @_;
847   $self->throw_exception("Can't create relationship without join condition")
848     unless $cond;
849   $attrs ||= {};
850
851   # Check foreign and self are right in cond
852   if ( (ref $cond ||'') eq 'HASH') {
853     for (keys %$cond) {
854       $self->throw_exception("Keys of condition should be of form 'foreign.col', not '$_'")
855         if /\./ && !/^foreign\./;
856     }
857   }
858
859   my %rels = %{ $self->_relationships };
860   $rels{$rel} = { class => $f_source_name,
861                   source => $f_source_name,
862                   cond  => $cond,
863                   attrs => $attrs };
864   $self->_relationships(\%rels);
865
866   return $self;
867
868   # XXX disabled. doesn't work properly currently. skip in tests.
869
870   my $f_source = $self->schema->source($f_source_name);
871   unless ($f_source) {
872     $self->ensure_class_loaded($f_source_name);
873     $f_source = $f_source_name->result_source;
874     #my $s_class = ref($self->schema);
875     #$f_source_name =~ m/^${s_class}::(.*)$/;
876     #$self->schema->register_class(($1 || $f_source_name), $f_source_name);
877     #$f_source = $self->schema->source($f_source_name);
878   }
879   return unless $f_source; # Can't test rel without f_source
880
881   eval { $self->resolve_join($rel, 'me') };
882
883   if ($@) { # If the resolve failed, back out and re-throw the error
884     delete $rels{$rel}; #
885     $self->_relationships(\%rels);
886     $self->throw_exception("Error creating relationship $rel: $@");
887   }
888   1;
889 }
890
891 =head2 relationships
892
893 =over 4
894
895 =item Arguments: None
896
897 =item Return value: List of relationship names
898
899 =back
900
901   my @relnames = $source->relationships();
902
903 Returns all relationship names for this source.
904
905 =cut
906
907 sub relationships {
908   return keys %{shift->_relationships};
909 }
910
911 =head2 relationship_info
912
913 =over 4
914
915 =item Arguments: $relname
916
917 =item Return value: Hashref of relation data,
918
919 =back
920
921 Returns a hash of relationship information for the specified relationship
922 name. The keys/values are as specified for L</add_relationship>.
923
924 =cut
925
926 sub relationship_info {
927   my ($self, $rel) = @_;
928   return $self->_relationships->{$rel};
929 }
930
931 =head2 has_relationship
932
933 =over 4
934
935 =item Arguments: $rel
936
937 =item Return value: 1/0 (true/false)
938
939 =back
940
941 Returns true if the source has a relationship of this name, false otherwise.
942
943 =cut
944
945 sub has_relationship {
946   my ($self, $rel) = @_;
947   return exists $self->_relationships->{$rel};
948 }
949
950 =head2 reverse_relationship_info
951
952 =over 4
953
954 =item Arguments: $relname
955
956 =item Return value: Hashref of relationship data
957
958 =back
959
960 Looks through all the relationships on the source this relationship
961 points to, looking for one whose condition is the reverse of the
962 condition on this relationship.
963
964 A common use of this is to find the name of the C<belongs_to> relation
965 opposing a C<has_many> relation. For definition of these look in
966 L<DBIx::Class::Relationship>.
967
968 The returned hashref is keyed by the name of the opposing
969 relationship, and contains it's data in the same manner as
970 L</relationship_info>.
971
972 =cut
973
974 sub reverse_relationship_info {
975   my ($self, $rel) = @_;
976   my $rel_info = $self->relationship_info($rel);
977   my $ret = {};
978
979   return $ret unless ((ref $rel_info->{cond}) eq 'HASH');
980
981   my @cond = keys(%{$rel_info->{cond}});
982   my @refkeys = map {/^\w+\.(\w+)$/} @cond;
983   my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
984
985   # Get the related result source for this relationship
986   my $othertable = $self->related_source($rel);
987
988   # Get all the relationships for that source that related to this source
989   # whose foreign column set are our self columns on $rel and whose self
990   # columns are our foreign columns on $rel.
991   my @otherrels = $othertable->relationships();
992   my $otherrelationship;
993   foreach my $otherrel (@otherrels) {
994     my $otherrel_info = $othertable->relationship_info($otherrel);
995
996     my $back = $othertable->related_source($otherrel);
997     next unless $back->source_name eq $self->source_name;
998
999     my @othertestconds;
1000
1001     if (ref $otherrel_info->{cond} eq 'HASH') {
1002       @othertestconds = ($otherrel_info->{cond});
1003     }
1004     elsif (ref $otherrel_info->{cond} eq 'ARRAY') {
1005       @othertestconds = @{$otherrel_info->{cond}};
1006     }
1007     else {
1008       next;
1009     }
1010
1011     foreach my $othercond (@othertestconds) {
1012       my @other_cond = keys(%$othercond);
1013       my @other_refkeys = map {/^\w+\.(\w+)$/} @other_cond;
1014       my @other_keys = map {$othercond->{$_} =~ /^\w+\.(\w+)$/} @other_cond;
1015       next if (!$self->compare_relationship_keys(\@refkeys, \@other_keys) ||
1016                !$self->compare_relationship_keys(\@other_refkeys, \@keys));
1017       $ret->{$otherrel} =  $otherrel_info;
1018     }
1019   }
1020   return $ret;
1021 }
1022
1023 =head2 compare_relationship_keys
1024
1025 =over 4
1026
1027 =item Arguments: \@keys1, \@keys2
1028
1029 =item Return value: 1/0 (true/false)
1030
1031 =back
1032
1033 Returns true if both sets of keynames are the same, false otherwise.
1034
1035 =cut
1036
1037 sub compare_relationship_keys {
1038   my ($self, $keys1, $keys2) = @_;
1039
1040   # Make sure every keys1 is in keys2
1041   my $found;
1042   foreach my $key (@$keys1) {
1043     $found = 0;
1044     foreach my $prim (@$keys2) {
1045       if ($prim eq $key) {
1046         $found = 1;
1047         last;
1048       }
1049     }
1050     last unless $found;
1051   }
1052
1053   # Make sure every key2 is in key1
1054   if ($found) {
1055     foreach my $prim (@$keys2) {
1056       $found = 0;
1057       foreach my $key (@$keys1) {
1058         if ($prim eq $key) {
1059           $found = 1;
1060           last;
1061         }
1062       }
1063       last unless $found;
1064     }
1065   }
1066
1067   return $found;
1068 }
1069
1070 =head2 resolve_join
1071
1072 =over 4
1073
1074 =item Arguments: $relation
1075
1076 =item Return value: Join condition arrayref
1077
1078 =back
1079
1080 Returns the join structure required for the related result source.
1081
1082 =cut
1083
1084 sub resolve_join {
1085   my ($self, $join, $alias, $seen, $force_left) = @_;
1086   $seen ||= {};
1087   $force_left ||= { force => 0 };
1088   if (ref $join eq 'ARRAY') {
1089     return map { $self->resolve_join($_, $alias, $seen) } @$join;
1090   } elsif (ref $join eq 'HASH') {
1091     return
1092       map {
1093         my $as = ($seen->{$_} ? $_.'_'.($seen->{$_}+1) : $_);
1094         local $force_left->{force};
1095         (
1096           $self->resolve_join($_, $alias, $seen, $force_left),
1097           $self->related_source($_)->resolve_join(
1098             $join->{$_}, $as, $seen, $force_left
1099           )
1100         );
1101       } keys %$join;
1102   } elsif (ref $join) {
1103     $self->throw_exception("No idea how to resolve join reftype ".ref $join);
1104   } else {
1105     my $count = ++$seen->{$join};
1106     #use Data::Dumper; warn Dumper($seen);
1107     my $as = ($count > 1 ? "${join}_${count}" : $join);
1108     my $rel_info = $self->relationship_info($join);
1109     $self->throw_exception("No such relationship ${join}") unless $rel_info;
1110     my $type;
1111     if ($force_left->{force}) {
1112       $type = 'left';
1113     } else {
1114       $type = $rel_info->{attrs}{join_type} || '';
1115       $force_left->{force} = 1 if lc($type) eq 'left';
1116     }
1117     return [ { $as => $self->related_source($join)->from,
1118                -join_type => $type },
1119              $self->resolve_condition($rel_info->{cond}, $as, $alias) ];
1120   }
1121 }
1122
1123 =head2 pk_depends_on
1124
1125 =over 4
1126
1127 =item Arguments: $relname, $rel_data
1128
1129 =item Return value: 1/0 (true/false)
1130
1131 =back
1132
1133 Determines whether a relation is dependent on an object from this source
1134 having already been inserted. Takes the name of the relationship and a
1135 hashref of columns of the related object.
1136
1137 =cut
1138
1139 sub pk_depends_on {
1140   my ($self, $relname, $rel_data) = @_;
1141   my $cond = $self->relationship_info($relname)->{cond};
1142
1143   return 0 unless ref($cond) eq 'HASH';
1144
1145   # map { foreign.foo => 'self.bar' } to { bar => 'foo' }
1146
1147   my $keyhash = { map { my $x = $_; $x =~ s/.*\.//; $x; } reverse %$cond };
1148
1149   # assume anything that references our PK probably is dependent on us
1150   # rather than vice versa, unless the far side is (a) defined or (b)
1151   # auto-increment
1152
1153   my $rel_source = $self->related_source($relname);
1154
1155   foreach my $p ($self->primary_columns) {
1156     if (exists $keyhash->{$p}) {
1157       unless (defined($rel_data->{$keyhash->{$p}})
1158               || $rel_source->column_info($keyhash->{$p})
1159                             ->{is_auto_increment}) {
1160         return 0;
1161       }
1162     }
1163   }
1164
1165   return 1;
1166 }
1167
1168 =head2 resolve_condition
1169
1170 =over 4
1171
1172 =item Arguments: $cond, $as, $alias|$object
1173
1174 =back
1175
1176 Resolves the passed condition to a concrete query fragment. If given an alias,
1177 returns a join condition; if given an object, inverts that object to produce
1178 a related conditional from that object.
1179
1180 =cut
1181
1182 our $UNRESOLVABLE_CONDITION = \'1 = 0';
1183
1184 sub resolve_condition {
1185   my ($self, $cond, $as, $for) = @_;
1186   #warn %$cond;
1187   if (ref $cond eq 'HASH') {
1188     my %ret;
1189     foreach my $k (keys %{$cond}) {
1190       my $v = $cond->{$k};
1191       # XXX should probably check these are valid columns
1192       $k =~ s/^foreign\.// ||
1193         $self->throw_exception("Invalid rel cond key ${k}");
1194       $v =~ s/^self\.// ||
1195         $self->throw_exception("Invalid rel cond val ${v}");
1196       if (ref $for) { # Object
1197         #warn "$self $k $for $v";
1198         unless ($for->has_column_loaded($v)) {
1199           if ($for->in_storage) {
1200             $self->throw_exception("Column ${v} not loaded on ${for} trying to resolve relationship");
1201           }
1202           return $UNRESOLVABLE_CONDITION;
1203         }
1204         $ret{$k} = $for->get_column($v);
1205         #$ret{$k} = $for->get_column($v) if $for->has_column_loaded($v);
1206         #warn %ret;
1207       } elsif (!defined $for) { # undef, i.e. "no object"
1208         $ret{$k} = undef;
1209       } elsif (ref $as eq 'HASH') { # reverse hashref
1210         $ret{$v} = $as->{$k};
1211       } elsif (ref $as) { # reverse object
1212         $ret{$v} = $as->get_column($k);
1213       } elsif (!defined $as) { # undef, i.e. "no reverse object"
1214         $ret{$v} = undef;
1215       } else {
1216         $ret{"${as}.${k}"} = "${for}.${v}";
1217       }
1218     }
1219     return \%ret;
1220   } elsif (ref $cond eq 'ARRAY') {
1221     return [ map { $self->resolve_condition($_, $as, $for) } @$cond ];
1222   } else {
1223    die("Can't handle this yet :(");
1224   }
1225 }
1226
1227 =head2 resolve_prefetch
1228
1229 =over 4
1230
1231 =item Arguments: hashref/arrayref/scalar
1232
1233 =back
1234
1235 Accepts one or more relationships for the current source and returns an
1236 array of column names for each of those relationships. Column names are
1237 prefixed relative to the current source, in accordance with where they appear
1238 in the supplied relationships. Examples:
1239
1240   my $source = $schema->resultset('Tag')->source;
1241   @columns = $source->resolve_prefetch( { cd => 'artist' } );
1242
1243   # @columns =
1244   #(
1245   #  'cd.cdid',
1246   #  'cd.artist',
1247   #  'cd.title',
1248   #  'cd.year',
1249   #  'cd.artist.artistid',
1250   #  'cd.artist.name'
1251   #)
1252
1253   @columns = $source->resolve_prefetch( qw[/ cd /] );
1254
1255   # @columns =
1256   #(
1257   #   'cd.cdid',
1258   #   'cd.artist',
1259   #   'cd.title',
1260   #   'cd.year'
1261   #)
1262
1263   $source = $schema->resultset('CD')->source;
1264   @columns = $source->resolve_prefetch( qw[/ artist producer /] );
1265
1266   # @columns =
1267   #(
1268   #  'artist.artistid',
1269   #  'artist.name',
1270   #  'producer.producerid',
1271   #  'producer.name'
1272   #)
1273
1274 =cut
1275
1276 sub resolve_prefetch {
1277   my ($self, $pre, $alias, $seen, $order, $collapse) = @_;
1278   $seen ||= {};
1279   #$alias ||= $self->name;
1280   #warn $alias, Dumper $pre;
1281   if( ref $pre eq 'ARRAY' ) {
1282     return
1283       map { $self->resolve_prefetch( $_, $alias, $seen, $order, $collapse ) }
1284         @$pre;
1285   }
1286   elsif( ref $pre eq 'HASH' ) {
1287     my @ret =
1288     map {
1289       $self->resolve_prefetch($_, $alias, $seen, $order, $collapse),
1290       $self->related_source($_)->resolve_prefetch(
1291                $pre->{$_}, "${alias}.$_", $seen, $order, $collapse)
1292     } keys %$pre;
1293     #die Dumper \@ret;
1294     return @ret;
1295   }
1296   elsif( ref $pre ) {
1297     $self->throw_exception(
1298       "don't know how to resolve prefetch reftype ".ref($pre));
1299   }
1300   else {
1301     my $count = ++$seen->{$pre};
1302     my $as = ($count > 1 ? "${pre}_${count}" : $pre);
1303     my $rel_info = $self->relationship_info( $pre );
1304     $self->throw_exception( $self->name . " has no such relationship '$pre'" )
1305       unless $rel_info;
1306     my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
1307     my $rel_source = $self->related_source($pre);
1308
1309     if (exists $rel_info->{attrs}{accessor}
1310          && $rel_info->{attrs}{accessor} eq 'multi') {
1311       $self->throw_exception(
1312         "Can't prefetch has_many ${pre} (join cond too complex)")
1313         unless ref($rel_info->{cond}) eq 'HASH';
1314       my $dots = @{[$as_prefix =~ m/\./g]} + 1; # +1 to match the ".${as_prefix}"
1315       if (my ($fail) = grep { @{[$_ =~ m/\./g]} == $dots }
1316                          keys %{$collapse}) {
1317         my ($last) = ($fail =~ /([^\.]+)$/);
1318         carp (
1319           "Prefetching multiple has_many rels ${last} and ${pre} "
1320           .(length($as_prefix)
1321             ? "at the same level (${as_prefix}) "
1322             : "at top level "
1323           )
1324           . 'will currently disrupt both the functionality of $rs->count(), '
1325           . 'and the amount of objects retrievable via $rs->next(). '
1326           . 'Use at your own risk.'
1327         );
1328       }
1329       #my @col = map { (/^self\.(.+)$/ ? ("${as_prefix}.$1") : ()); }
1330       #              values %{$rel_info->{cond}};
1331       $collapse->{".${as_prefix}${pre}"} = [ $rel_source->primary_columns ];
1332         # action at a distance. prepending the '.' allows simpler code
1333         # in ResultSet->_collapse_result
1334       my @key = map { (/^foreign\.(.+)$/ ? ($1) : ()); }
1335                     keys %{$rel_info->{cond}};
1336       my @ord = (ref($rel_info->{attrs}{order_by}) eq 'ARRAY'
1337                    ? @{$rel_info->{attrs}{order_by}}
1338                    : (defined $rel_info->{attrs}{order_by}
1339                        ? ($rel_info->{attrs}{order_by})
1340                        : ()));
1341       push(@$order, map { "${as}.$_" } (@key, @ord));
1342     }
1343
1344     return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
1345       $rel_source->columns;
1346     #warn $alias, Dumper (\@ret);
1347     #return @ret;
1348   }
1349 }
1350
1351 =head2 related_source
1352
1353 =over 4
1354
1355 =item Arguments: $relname
1356
1357 =item Return value: $source
1358
1359 =back
1360
1361 Returns the result source object for the given relationship.
1362
1363 =cut
1364
1365 sub related_source {
1366   my ($self, $rel) = @_;
1367   if( !$self->has_relationship( $rel ) ) {
1368     $self->throw_exception("No such relationship '$rel'");
1369   }
1370   return $self->schema->source($self->relationship_info($rel)->{source});
1371 }
1372
1373 =head2 related_class
1374
1375 =over 4
1376
1377 =item Arguments: $relname
1378
1379 =item Return value: $classname
1380
1381 =back
1382
1383 Returns the class name for objects in the given relationship.
1384
1385 =cut
1386
1387 sub related_class {
1388   my ($self, $rel) = @_;
1389   if( !$self->has_relationship( $rel ) ) {
1390     $self->throw_exception("No such relationship '$rel'");
1391   }
1392   return $self->schema->class($self->relationship_info($rel)->{source});
1393 }
1394
1395 =head2 handle
1396
1397 Obtain a new handle to this source. Returns an instance of a 
1398 L<DBIx::Class::ResultSourceHandle>.
1399
1400 =cut
1401
1402 sub handle {
1403     return new DBIx::Class::ResultSourceHandle({
1404         schema         => $_[0]->schema,
1405         source_moniker => $_[0]->source_name
1406     });
1407 }
1408
1409 =head2 throw_exception
1410
1411 See L<DBIx::Class::Schema/"throw_exception">.
1412
1413 =cut
1414
1415 sub throw_exception {
1416   my $self = shift;
1417   if (defined $self->schema) {
1418     $self->schema->throw_exception(@_);
1419   } else {
1420     croak(@_);
1421   }
1422 }
1423
1424 =head2 source_info
1425
1426 Stores a hashref of per-source metadata.  No specific key names
1427 have yet been standardized, the examples below are purely hypothetical
1428 and don't actually accomplish anything on their own:
1429
1430   __PACKAGE__->source_info({
1431     "_tablespace" => 'fast_disk_array_3',
1432     "_engine" => 'InnoDB',
1433   });
1434
1435 =head2 new
1436
1437   $class->new();
1438
1439   $class->new({attribute_name => value});
1440
1441 Creates a new ResultSource object.  Not normally called directly by end users.
1442
1443 =head2 column_info_from_storage
1444
1445 =over
1446
1447 =item Arguments: 1/0 (default: 0)
1448
1449 =item Return value: 1/0
1450
1451 =back
1452
1453   __PACKAGE__->column_info_from_storage(1);
1454
1455 Enables the on-demand automatic loading of the above column
1456 metadata from storage as neccesary.  This is *deprecated*, and
1457 should not be used.  It will be removed before 1.0.
1458
1459
1460 =head1 AUTHORS
1461
1462 Matt S. Trout <mst@shadowcatsystems.co.uk>
1463
1464 =head1 LICENSE
1465
1466 You may distribute this code under the same terms as Perl itself.
1467
1468 =cut
1469
1470 1;