more dob updates
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
CommitLineData
ea2e61bf 1package DBIx::Class::Table;
2
3use strict;
4use warnings;
5
223b8fe3 6use DBIx::Class::ResultSet;
2a21de92 7use Data::Page;
95a70f01 8
1edd1722 9use base qw/DBIx::Class/;
ea2e61bf 10
11__PACKAGE__->mk_classdata('_columns' => {});
12
ea2e61bf 13__PACKAGE__->mk_classdata('_table_name');
14
34d52be2 15__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
16
223b8fe3 17__PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
95a70f01 18
223b8fe3 19sub iterator_class { shift->_resultset_class(@_) }
525035fb 20
34d52be2 21=head1 NAME
22
23DBIx::Class::Table - Basic table methods
24
25=head1 SYNOPSIS
26
27=head1 DESCRIPTION
28
958bcea5 29This class is responsible for defining and doing table-level operations on
30L<DBIx::Class> classes.
34d52be2 31
32=head1 METHODS
33
39fe0e65 34=cut
35
ea2e61bf 36sub _register_columns {
37 my ($class, @cols) = @_;
38 my $names = { %{$class->_columns} };
39 $names->{$_} ||= {} for @cols;
40 $class->_columns($names);
41}
42
43sub _mk_column_accessors {
44 my ($class, @cols) = @_;
510ca912 45 $class->mk_group_accessors('column' => @cols);
ea2e61bf 46}
47
130c6439 48=head2 add_columns
39fe0e65 49
50 __PACKAGE__->add_columns(qw/col1 col2 col3/);
51
958bcea5 52Adds columns to the current class and creates accessors for them.
39fe0e65 53
54=cut
55
510ca912 56sub add_columns {
8fe001e1 57 my ($class, @cols) = @_;
58 $class->_register_columns(@cols);
59 $class->_mk_column_accessors(@cols);
60}
61
130c6439 62=head2 search_literal
39fe0e65 63
656796f2 64 my @obj = $class->search_literal($literal_where_cond, @bind);
65 my $cursor = $class->search_literal($literal_where_cond, @bind);
39fe0e65 66
67=cut
68
656796f2 69sub search_literal {
8fe001e1 70 my ($class, $cond, @vals) = @_;
a3018bd3 71 $cond =~ s/^\s*WHERE//i;
8da9889b 72 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
73 $attrs->{bind} = \@vals;
74 return $class->search(\$cond, $attrs);
510ca912 75}
76
130c6439 77=head2 count_literal
39fe0e65 78
656796f2 79 my $count = $class->count_literal($literal_where_cond);
39fe0e65 80
81=cut
82
656796f2 83sub count_literal {
7624b19f 84 my $class = shift;
85 return $class->search_literal(@_)->count;
fcbc5f29 86}
87
130c6439 88=head2 count
39fe0e65 89
90 my $count = $class->count({ foo => 3 });
91
92=cut
93
06d90c6b 94sub count {
95 my $class = shift;
7624b19f 96 return $class->search(@_)->count;
c1d23573 97}
98
130c6439 99=head2 search
39fe0e65 100
20f67ac7 101 my @obj = $class->search({ foo => 3 }); # "... WHERE foo = 3"
39fe0e65 102 my $cursor = $class->search({ foo => 3 });
103
20f67ac7 104To retrieve all rows, simply call C<search()> with no condition parameter,
105
106 my @all = $class->search(); # equivalent to search({})
107
108If you need to pass in additional attributes (see
109L<DBIx::Class::ResultSet/Attributes> for details) an empty hash indicates
110no condition,
111
112 my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table"
113
39fe0e65 114=cut
115
8fe001e1 116sub search {
12bbb339 117 my $class = shift;
8b445e33 118 #warn "@_";
12bbb339 119 my $attrs = { };
120 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
121 $attrs = { %{ pop(@_) } };
122 }
7624b19f 123 $attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
2a21de92 124
7624b19f 125 my $rs = $class->resultset($attrs);
2a21de92 126
7624b19f 127 return (wantarray ? $rs->all : $rs);
128}
129
130sub resultset {
131 my $class = shift;
132
133 my $rs_class = $class->_resultset_class;
134 eval "use $rs_class;";
135 my $rs = $rs_class->new($class, @_);
a3018bd3 136}
137
130c6439 138=head2 search_like
39fe0e65 139
140Identical to search except defaults to 'LIKE' instead of '=' in condition
141
142=cut
143
a3018bd3 144sub search_like {
145 my $class = shift;
12bbb339 146 my $attrs = { };
147 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
148 $attrs = pop(@_);
149 }
223b8fe3 150 my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
151 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
152 return $class->search($query, { %$attrs });
8fe001e1 153}
154
155sub _select_columns {
156 return keys %{$_[0]->_columns};
157}
158
130c6439 159=head2 table
39fe0e65 160
161 __PACKAGE__->table('tbl_name');
958bcea5 162
163Gets or sets the table name.
39fe0e65 164
165=cut
166
510ca912 167sub table {
168 shift->_table_name(@_);
169}
170
130c6439 171=head2 find_or_create
39fe0e65 172
173 $class->find_or_create({ key => $val, ... });
174
175Searches for a record matching the search condition; if it doesn't find one,
958bcea5 176creates one and returns that instead.
39fe0e65 177
178=cut
179
95a70f01 180sub find_or_create {
181 my $class = shift;
182 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
1a14aa3f 183 my $exists = $class->find($hash);
95a70f01 184 return defined($exists) ? $exists : $class->create($hash);
185}
186
130c6439 187=head2 has_column
103647d5 188
189 if ($obj->has_column($col)) { ... }
190
958bcea5 191Returns 1 if the class has a column of this name, 0 otherwise.
103647d5 192
193=cut
194
195sub has_column {
196 my ($self, $column) = @_;
197 return exists $self->_columns->{$column};
198}
199
130c6439 200=head2 column_info
103647d5 201
202 my $info = $obj->column_info($col);
203
958bcea5 204Returns the column metadata hashref for a column.
103647d5 205
206=cut
207
208sub column_info {
209 my ($self, $column) = @_;
210 die "No such column $column" unless exists $self->_columns->{$column};
211 return $self->_columns->{$column};
212}
213
130c6439 214=head2 columns
103647d5 215
216 my @column_names = $obj->columns;
217
218=cut
219
e7513319 220sub columns {
221 die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
222 return keys %{shift->_columns};
223}
8b445e33 224
ea2e61bf 2251;
34d52be2 226
34d52be2 227=head1 AUTHORS
228
daec44b8 229Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 230
231=head1 LICENSE
232
233You may distribute this code under the same terms as Perl itself.
234
235=cut
236