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