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 basic operations on
30 L<DBIx::Class> objects.
38 sub _register_columns {
39 my ($class, @cols) = @_;
40 my $names = { %{$class->_columns} };
41 $names->{$_} ||= {} for @cols;
42 $class->_columns($names);
45 sub _mk_column_accessors {
46 my ($class, @cols) = @_;
47 $class->mk_group_accessors('column' => @cols);
52 __PACKAGE__->add_columns(qw/col1 col2 col3/);
54 Adds columns to the current package, and creates accessors for them
59 my ($class, @cols) = @_;
60 $class->_register_columns(@cols);
61 $class->_mk_column_accessors(@cols);
66 my @obj = $class->search_literal($literal_where_cond, @bind);
67 my $cursor = $class->search_literal($literal_where_cond, @bind);
72 my ($class, $cond, @vals) = @_;
73 $cond =~ s/^\s*WHERE//i;
74 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
75 $attrs->{bind} = \@vals;
76 return $class->search(\$cond, $attrs);
81 my $count = $class->count_literal($literal_where_cond);
87 return $class->search_literal(@_)->count;
92 my $count = $class->count({ foo => 3 });
98 return $class->search(@_)->count;
103 my @obj = $class->search({ foo => 3 }); # "... WHERE foo = 3"
104 my $cursor = $class->search({ foo => 3 });
106 To retrieve all rows, simply call C<search()> with no condition parameter,
108 my @all = $class->search(); # equivalent to search({})
110 If you need to pass in additional attributes (see
111 L<DBIx::Class::ResultSet/Attributes> for details) an empty hash indicates
114 my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table"
122 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
123 $attrs = { %{ pop(@_) } };
125 $attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
127 my $rs = $class->resultset($attrs);
129 return (wantarray ? $rs->all : $rs);
135 my $rs_class = $class->_resultset_class;
136 eval "use $rs_class;";
137 my $rs = $rs_class->new($class, @_);
142 Identical to search except defaults to 'LIKE' instead of '=' in condition
149 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
152 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
153 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
154 return $class->search($query, { %$attrs });
157 sub _select_columns {
158 return keys %{$_[0]->_columns};
163 __PACKAGE__->table('tbl_name');
168 shift->_table_name(@_);
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 object 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 the 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;
220 sub columns { return keys %{shift->_columns}; }
228 Matt S. Trout <mst@shadowcatsystems.co.uk>
232 You may distribute this code under the same terms as Perl itself.