1 package DBIx::Class::Table;
6 use DBIx::Class::ResultSet;
9 use base qw/Class::Data::Inheritable/;
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 __PACKAGE__->mk_classdata('_page_object');
21 sub iterator_class { shift->_resultset_class(@_) }
25 DBIx::Class::Table - Basic table methods
31 This class is responsible for defining and doing basic operations on
32 L<DBIx::Class> objects.
40 sub _register_columns {
41 my ($class, @cols) = @_;
42 my $names = { %{$class->_columns} };
43 $names->{$_} ||= {} for @cols;
44 $class->_columns($names);
47 sub _mk_column_accessors {
48 my ($class, @cols) = @_;
49 $class->mk_group_accessors('column' => @cols);
54 __PACKAGE__->add_columns(qw/col1 col2 col3/);
56 Adds columns to the current package, and creates accessors for them
61 my ($class, @cols) = @_;
62 $class->_register_columns(@cols);
63 $class->_mk_column_accessors(@cols);
68 my @obj = $class->search_literal($literal_where_cond, @bind);
69 my $cursor = $class->search_literal($literal_where_cond, @bind);
74 my ($class, $cond, @vals) = @_;
75 $cond =~ s/^\s*WHERE//i;
76 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
77 $attrs->{bind} = \@vals;
78 return $class->search(\$cond, $attrs);
83 my $count = $class->count_literal($literal_where_cond);
89 return $class->search_literal(@_)->count;
94 my $count = $class->count({ foo => 3 });
100 return $class->search(@_)->count;
105 my @obj = $class->search({ foo => 3 });
106 my $cursor = $class->search({ foo => 3 });
114 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
115 $attrs = { %{ pop(@_) } };
117 $attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
119 # for pagination, we create the resultset with no limit and slice it later
121 if ( $attrs->{page} ) {
122 map { $page->{$_} = $attrs->{$_} } qw/rows page/;
123 delete $attrs->{$_} for qw/rows offset page/;
126 my $rs = $class->resultset($attrs);
128 if ( $page->{page} ) {
129 my $pager = Data::Page->new(
132 $page->{page} || 1 );
133 $class->_page_object( $pager );
134 return $rs->slice( $pager->skipped,
135 $pager->skipped + $pager->entries_per_page - 1 );
138 return (wantarray ? $rs->all : $rs);
144 my $rs_class = $class->_resultset_class;
145 eval "use $rs_class;";
146 my $rs = $rs_class->new($class, @_);
151 Identical to search except defaults to 'LIKE' instead of '=' in condition
158 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
161 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
162 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
163 return $class->search($query, { %$attrs });
166 sub _select_columns {
167 return keys %{$_[0]->_columns};
172 __PACKAGE__->table('tbl_name');
177 shift->_table_name(@_);
182 $class->find_or_create({ key => $val, ... });
184 Searches for a record matching the search condition; if it doesn't find one,
185 creates one and returns that instead
191 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
192 my $exists = $class->find($hash);
193 return defined($exists) ? $exists : $class->create($hash);
196 sub columns { return keys %{shift->_columns}; }
200 $pager = $class->page;
202 Returns a Data::Page object for the most recent search that was performed
203 using the page parameter.
207 sub page { shift->_page_object }
215 Matt S. Trout <mst@shadowcatsystems.co.uk>
219 You may distribute this code under the same terms as Perl itself.