Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class::Table; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
223b8fe3 |
6 | use DBIx::Class::ResultSet; |
2a21de92 |
7 | use Data::Page; |
95a70f01 |
8 | |
1edd1722 |
9 | use 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 |
19 | sub iterator_class { shift->_resultset_class(@_) } |
525035fb |
20 | |
34d52be2 |
21 | =head1 NAME |
22 | |
23 | DBIx::Class::Table - Basic table methods |
24 | |
25 | =head1 SYNOPSIS |
26 | |
27 | =head1 DESCRIPTION |
28 | |
29 | This class is responsible for defining and doing basic operations on |
30 | L<DBIx::Class> objects. |
31 | |
32 | =head1 METHODS |
33 | |
39fe0e65 |
34 | =cut |
35 | |
ea2e61bf |
36 | sub _register_columns { |
37 | my ($class, @cols) = @_; |
38 | my $names = { %{$class->_columns} }; |
39 | $names->{$_} ||= {} for @cols; |
40 | $class->_columns($names); |
41 | } |
42 | |
43 | sub _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 | |
52 | Adds columns to the current package, and creates accessors for them |
53 | |
54 | =cut |
55 | |
510ca912 |
56 | sub 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 |
69 | sub 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 |
83 | sub 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 |
94 | sub 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 |
104 | To retrieve all rows, simply call C<search()> with no condition parameter, |
105 | |
106 | my @all = $class->search(); # equivalent to search({}) |
107 | |
108 | If you need to pass in additional attributes (see |
109 | L<DBIx::Class::ResultSet/Attributes> for details) an empty hash indicates |
110 | no condition, |
111 | |
112 | my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table" |
113 | |
39fe0e65 |
114 | =cut |
115 | |
8fe001e1 |
116 | sub 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 | |
130 | sub 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 | |
140 | Identical to search except defaults to 'LIKE' instead of '=' in condition |
141 | |
142 | =cut |
143 | |
a3018bd3 |
144 | sub 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 | |
155 | sub _select_columns { |
156 | return keys %{$_[0]->_columns}; |
157 | } |
158 | |
130c6439 |
159 | =head2 table |
39fe0e65 |
160 | |
161 | __PACKAGE__->table('tbl_name'); |
162 | |
163 | =cut |
164 | |
510ca912 |
165 | sub table { |
166 | shift->_table_name(@_); |
167 | } |
168 | |
130c6439 |
169 | =head2 find_or_create |
39fe0e65 |
170 | |
171 | $class->find_or_create({ key => $val, ... }); |
172 | |
173 | Searches for a record matching the search condition; if it doesn't find one, |
174 | creates one and returns that instead |
175 | |
176 | =cut |
177 | |
95a70f01 |
178 | sub find_or_create { |
179 | my $class = shift; |
180 | my $hash = ref $_[0] eq "HASH" ? shift: {@_}; |
1a14aa3f |
181 | my $exists = $class->find($hash); |
95a70f01 |
182 | return defined($exists) ? $exists : $class->create($hash); |
183 | } |
184 | |
130c6439 |
185 | =head2 has_column |
103647d5 |
186 | |
187 | if ($obj->has_column($col)) { ... } |
188 | |
189 | Returns 1 if the object has a column of this name, 0 otherwise |
190 | |
191 | =cut |
192 | |
193 | sub has_column { |
194 | my ($self, $column) = @_; |
195 | return exists $self->_columns->{$column}; |
196 | } |
197 | |
130c6439 |
198 | =head2 column_info |
103647d5 |
199 | |
200 | my $info = $obj->column_info($col); |
201 | |
202 | Returns the column metadata hashref for the column |
203 | |
204 | =cut |
205 | |
206 | sub column_info { |
207 | my ($self, $column) = @_; |
208 | die "No such column $column" unless exists $self->_columns->{$column}; |
209 | return $self->_columns->{$column}; |
210 | } |
211 | |
130c6439 |
212 | =head2 columns |
103647d5 |
213 | |
214 | my @column_names = $obj->columns; |
215 | |
216 | =cut |
217 | |
e7513319 |
218 | sub columns { |
219 | die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1); |
220 | return keys %{shift->_columns}; |
221 | } |
8b445e33 |
222 | |
ea2e61bf |
223 | 1; |
34d52be2 |
224 | |
34d52be2 |
225 | =head1 AUTHORS |
226 | |
daec44b8 |
227 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
228 | |
229 | =head1 LICENSE |
230 | |
231 | You may distribute this code under the same terms as Perl itself. |
232 | |
233 | =cut |
234 | |