1 package DBIx::Class::ResultSource;
6 use DBIx::Class::ResultSet;
10 use base qw/DBIx::Class/;
11 __PACKAGE__->load_components(qw/AccessorGroup/);
13 __PACKAGE__->mk_group_accessors('simple' =>
14 qw/_ordered_columns _columns _primaries name resultset_class result_class schema from/);
18 DBIx::Class::ResultSource - Result source object
24 A ResultSource is a component of a schema from which results can be directly
25 retrieved, most usually a table (see L<DBIx::Class::ResultSource::Table>)
32 my ($class, $attrs) = @_;
33 $class = ref $class if ref $class;
34 my $new = bless({ %{$attrs || {}} }, $class);
35 $new->{resultset_class} ||= 'DBIx::Class::ResultSet';
36 $new->{_ordered_columns} ||= [];
37 $new->{_columns} ||= {};
38 $new->{name} ||= "!!NAME NOT SET!!";
43 my ($self, @cols) = @_;
44 $self->_ordered_columns( \@cols )
45 if !$self->_ordered_columns;
46 push @{ $self->_ordered_columns }, @cols;
47 while (my $col = shift @cols) {
49 my $column_info = ref $cols[0] ? shift : {};
50 # If next entry is { ... } use that for the column info, if not
51 # use an empty hashref
53 $self->_columns->{$col} = $column_info;
57 *add_column = \&add_columns;
61 $table->add_columns(qw/col1 col2 col3/);
63 $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...);
65 Adds columns to the result source. If supplied key => hashref pairs uses
66 the hashref as the column_info for that column.
70 $table->add_column('col' => \%info?);
72 Convenience alias to add_columns
78 return $self->resultset_class->new($self);
83 if ($obj->has_column($col)) { ... }
85 Returns 1 if the source has a column of this name, 0 otherwise.
90 my ($self, $column) = @_;
91 return exists $self->_columns->{$column};
96 my $info = $obj->column_info($col);
98 Returns the column metadata hashref for a column.
103 my ($self, $column) = @_;
104 croak "No such column $column" unless exists $self->_columns->{$column};
105 return $self->_columns->{$column};
110 my @column_names = $obj->columns;
115 croak "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
116 return keys %{shift->_columns};
119 =head2 ordered_columns
121 my @column_names = $obj->ordered_columns;
123 Like columns(), but returns column names using the order in which they were
124 originally supplied to add_columns().
128 sub ordered_columns {
129 return @{shift->{_ordered_columns}||[]};
132 =head2 set_primary_key(@cols)
134 Defines one or more columns as primary key for this source. Should be
135 called after C<add_columns>.
139 sub set_primary_key {
140 my ($self, @cols) = @_;
141 # check if primary key columns are valid columns
143 $self->throw("No such column $_ on table ".$self->name)
144 unless $self->has_column($_);
146 $self->_primaries(\@cols);
149 =head2 primary_columns
151 Read-only accessor which returns the list of primary keys.
155 sub primary_columns {
156 return @{shift->_primaries||[]};
161 Returns an expression of the source to be supplied to storage to specify
162 retrieval from this source; in the case of a database the required FROM clause
169 Returns the storage handle for the current schema
173 sub storage { shift->schema->storage; }
179 Matt S. Trout <mst@shadowcatsystems.co.uk>
183 You may distribute this code under the same terms as Perl itself.