1 package DBIx::Class::Table;
6 use DBIx::Class::ResultSet;
8 use base qw/DBIx::Class/;
10 __PACKAGE__->mk_classdata('_columns' => {});
12 __PACKAGE__->mk_classdata('_table_name');
14 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
16 __PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
18 sub iterator_class { shift->_resultset_class(@_) }
22 DBIx::Class::Table - Basic table methods
28 This class is responsible for defining and doing basic operations on
29 L<DBIx::Class> objects.
37 sub _register_columns {
38 my ($class, @cols) = @_;
39 my $names = { %{$class->_columns} };
40 while (my $col = shift @cols) {
41 $names->{$col} = (ref $cols[0] ? shift : {});
43 $class->_columns($names);
46 sub _mk_column_accessors {
47 my ($class, @cols) = @_;
48 $class->mk_group_accessors('column' => @cols);
53 __PACKAGE__->add_columns(qw/col1 col2 col3/);
55 Adds columns to the current package, and creates accessors for them
60 my ($class, @cols) = @_;
61 $class->_register_columns(@cols);
62 $class->_mk_column_accessors(@cols);
67 my @obj = $class->search_literal($literal_where_cond, @bind);
68 my $cursor = $class->search_literal($literal_where_cond, @bind);
73 my ($class, $cond, @vals) = @_;
74 $cond =~ s/^\s*WHERE//i;
75 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
76 $attrs->{bind} = \@vals;
77 return $class->search(\$cond, $attrs);
82 my $count = $class->count_literal($literal_where_cond);
88 return $class->search_literal(@_)->count;
93 my $count = $class->count({ foo => 3 });
99 return $class->search(@_)->count;
104 my @obj = $class->search({ foo => 3 }); # "... WHERE foo = 3"
105 my $cursor = $class->search({ foo => 3 });
107 To retrieve all rows, simply call C<search()> with no condition parameter,
109 my @all = $class->search(); # equivalent to search({})
111 If you need to pass in additional attributes (see
112 L<DBIx::Class::ResultSet/Attributes> for details) an empty hash indicates
115 my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table"
122 my $rs_class = $class->_resultset_class;
123 eval "use $rs_class;";
124 my $rs = $rs_class->new($class, @_);
129 Identical to search except defaults to 'LIKE' instead of '=' in condition
136 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
139 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
140 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
141 return $class->search($query, { %$attrs });
144 sub _select_columns {
145 return keys %{$_[0]->_columns};
150 __PACKAGE__->table('tbl_name');
155 shift->_table_name(@_);
160 $class->find_or_create({ key => $val, ... });
162 Searches for a record matching the search condition; if it doesn't find one,
163 creates one and returns that instead
169 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
170 my $exists = $class->find($hash);
171 return defined($exists) ? $exists : $class->create($hash);
176 if ($obj->has_column($col)) { ... }
178 Returns 1 if the object has a column of this name, 0 otherwise
183 my ($self, $column) = @_;
184 return exists $self->_columns->{$column};
189 my $info = $obj->column_info($col);
191 Returns the column metadata hashref for the column
196 my ($self, $column) = @_;
197 die "No such column $column" unless exists $self->_columns->{$column};
198 return $self->_columns->{$column};
203 my @column_names = $obj->columns;
207 sub columns { return keys %{shift->_columns}; }
215 Matt S. Trout <mst@shadowcatsystems.co.uk>
219 You may distribute this code under the same terms as Perl itself.