1 package DBIx::Class::Table;
6 use DBIx::Class::ResultSet;
9 use base qw/DBIx::Class/;
11 __PACKAGE__->mk_classdata('_columns' => {});
13 __PACKAGE__->mk_classdata('_table_name');
15 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
17 __PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
19 sub iterator_class { shift->_resultset_class(@_) }
23 DBIx::Class::Table - Basic table methods
29 This class is responsible for defining and doing table-level operations on
30 L<DBIx::Class> classes.
36 sub _register_columns {
37 my ($class, @cols) = @_;
38 my $names = { %{$class->_columns} };
39 $names->{$_} ||= {} for @cols;
40 $class->_columns($names);
43 sub _mk_column_accessors {
44 my ($class, @cols) = @_;
45 $class->mk_group_accessors('column' => @cols);
50 __PACKAGE__->add_columns(qw/col1 col2 col3/);
52 Adds columns to the current class and creates accessors for them.
57 my ($class, @cols) = @_;
58 $class->_register_columns(@cols);
59 $class->_mk_column_accessors(@cols);
64 my @obj = $class->search_literal($literal_where_cond, @bind);
65 my $cursor = $class->search_literal($literal_where_cond, @bind);
70 my ($class, $cond, @vals) = @_;
71 $cond =~ s/^\s*WHERE//i;
72 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
73 $attrs->{bind} = \@vals;
74 return $class->search(\$cond, $attrs);
79 my $count = $class->count_literal($literal_where_cond);
85 return $class->search_literal(@_)->count;
90 my $count = $class->count({ foo => 3 });
96 return $class->search(@_)->count;
101 my @obj = $class->search({ foo => 3 }); # "... WHERE foo = 3"
102 my $cursor = $class->search({ foo => 3 });
104 To retrieve all rows, simply call C<search()> with no condition parameter,
106 my @all = $class->search(); # equivalent to search({})
108 If you need to pass in additional attributes (see
109 L<DBIx::Class::ResultSet/Attributes> for details) an empty hash indicates
112 my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table"
120 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
121 $attrs = { %{ pop(@_) } };
123 $attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
125 my $rs = $class->resultset($attrs);
127 return (wantarray ? $rs->all : $rs);
133 my $rs_class = $class->_resultset_class;
134 eval "use $rs_class;";
135 my $rs = $rs_class->new($class, @_);
140 Identical to search except defaults to 'LIKE' instead of '=' in condition
147 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
150 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
151 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
152 return $class->search($query, { %$attrs });
155 sub _select_columns {
156 return keys %{$_[0]->_columns};
161 __PACKAGE__->table('tbl_name');
163 Gets or sets the table name.
168 shift->_table_name(@_);
171 =head2 find_or_create
173 $class->find_or_create({ key => $val, ... });
175 Searches for a record matching the search condition; if it doesn't find one,
176 creates one and returns that instead.
182 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
183 my $exists = $class->find($hash);
184 return defined($exists) ? $exists : $class->create($hash);
189 if ($obj->has_column($col)) { ... }
191 Returns 1 if the class has a column of this name, 0 otherwise.
196 my ($self, $column) = @_;
197 return exists $self->_columns->{$column};
202 my $info = $obj->column_info($col);
204 Returns the column metadata hashref for a column.
209 my ($self, $column) = @_;
210 die "No such column $column" unless exists $self->_columns->{$column};
211 return $self->_columns->{$column};
216 my @column_names = $obj->columns;
221 die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
222 return keys %{shift->_columns};
229 Matt S. Trout <mst@shadowcatsystems.co.uk>
233 You may distribute this code under the same terms as Perl itself.