1 package DBIx::Class::Table;
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/_columns _primaries name resultset_class result_class schema/);
18 DBIx::Class::Table - Table object
24 This class is responsible for defining and doing table-level operations on
25 L<DBIx::Class> classes.
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->{_columns} ||= {};
37 $new->{name} ||= "!!NAME NOT SET!!";
42 my ($self, @cols) = @_;
43 while (my $col = shift @cols) {
44 $self->_columns->{$col} = (ref $cols[0] ? shift : {});
48 *add_column = \&add_columns;
52 $table->add_columns(qw/col1 col2 col3/);
54 $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...);
56 Adds columns to the table object. If supplied key => hashref pairs uses
57 the hashref as the column_info for that column.
61 $table->add_column('col' => \%info?);
63 Convenience alias to add_columns
69 return $self->resultset_class->new($self);
74 if ($obj->has_column($col)) { ... }
76 Returns 1 if the table has a column of this name, 0 otherwise.
81 my ($self, $column) = @_;
82 return exists $self->_columns->{$column};
87 my $info = $obj->column_info($col);
89 Returns the column metadata hashref for a column.
94 my ($self, $column) = @_;
95 croak "No such column $column" unless exists $self->_columns->{$column};
96 return $self->_columns->{$column};
101 my @column_names = $obj->columns;
106 croak "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
107 return keys %{shift->_columns};
110 =head2 set_primary_key(@cols)
112 Defines one or more columns as primary key for this table. Should be
113 called after C<add_columns>.
117 sub set_primary_key {
118 my ($self, @cols) = @_;
119 # check if primary key columns are valid columns
121 $self->throw("No such column $_ on table ".$self->name)
122 unless $self->has_column($_);
124 $self->_primaries(\@cols);
127 =head2 primary_columns
129 Read-only accessor which returns the list of primary keys.
133 sub primary_columns {
134 return @{shift->_primaries||[]};
139 Returns the FROM entry for the table (i.e. the table name)
143 sub from { shift->name; }
147 Returns the storage handle for the current schema
151 sub storage { shift->schema->storage; }
157 Matt S. Trout <mst@shadowcatsystems.co.uk>
161 You may distribute this code under the same terms as Perl itself.