Nuked Data::Page use call in Table
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
1 package DBIx::Class::Table;
2
3 use strict;
4 use warnings;
5
6 use DBIx::Class::ResultSet;
7
8 use base qw/DBIx::Class/;
9
10 __PACKAGE__->mk_classdata('_columns' => {});
11
12 __PACKAGE__->mk_classdata('_table_name');
13
14 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
15
16 __PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
17
18 sub iterator_class { shift->_resultset_class(@_) }
19
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
35 =cut
36
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) = @_;
46   $class->mk_group_accessors('column' => @cols);
47 }
48
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
57 sub add_columns {
58   my ($class, @cols) = @_;
59   $class->_register_columns(@cols);
60   $class->_mk_column_accessors(@cols);
61 }
62
63 =item search_literal
64
65   my @obj    = $class->search_literal($literal_where_cond, @bind);
66   my $cursor = $class->search_literal($literal_where_cond, @bind);
67
68 =cut
69
70 sub search_literal {
71   my ($class, $cond, @vals) = @_;
72   $cond =~ s/^\s*WHERE//i;
73   my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
74   $attrs->{bind} = \@vals;
75   return $class->search(\$cond, $attrs);
76 }
77
78 =item count_literal
79
80   my $count = $class->count_literal($literal_where_cond);
81
82 =cut
83
84 sub count_literal {
85   my $class = shift;
86   return $class->search_literal(@_)->count;
87 }
88
89 =item count
90
91   my $count = $class->count({ foo => 3 });
92
93 =cut
94
95 sub count {
96   my $class = shift;
97   return $class->search(@_)->count;
98 }
99
100 =item search 
101
102   my @obj    = $class->search({ foo => 3 }); # "... WHERE foo = 3"
103   my $cursor = $class->search({ foo => 3 });
104
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
115 =cut
116
117 sub search {
118   my $class = shift;
119   #warn "@_";
120   my $attrs = { };
121   if (@_ > 1 && ref $_[$#_] eq 'HASH') {
122     $attrs = { %{ pop(@_) } };
123   }
124   $attrs->{where} = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
125   
126   my $rs = $class->resultset($attrs);
127   
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, @_);
137 }
138
139 =item search_like
140
141 Identical to search except defaults to 'LIKE' instead of '=' in condition
142
143 =cut
144
145 sub search_like {
146   my $class    = shift;
147   my $attrs = { };
148   if (@_ > 1 && ref $_[$#_] eq 'HASH') {
149     $attrs = pop(@_);
150   }
151   my $query    = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
152   $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
153   return $class->search($query, { %$attrs });
154 }
155
156 sub _select_columns {
157   return keys %{$_[0]->_columns};
158 }
159
160 =item table
161
162   __PACKAGE__->table('tbl_name');
163
164 =cut
165
166 sub table {
167   shift->_table_name(@_);
168 }
169
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
179 sub find_or_create {
180   my $class    = shift;
181   my $hash     = ref $_[0] eq "HASH" ? shift: {@_};
182   my $exists = $class->find($hash);
183   return defined($exists) ? $exists : $class->create($hash);
184 }
185
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
219 sub columns { return keys %{shift->_columns}; }
220
221 1;
222
223 =back
224
225 =head1 AUTHORS
226
227 Matt S. Trout <mst@shadowcatsystems.co.uk>
228
229 =head1 LICENSE
230
231 You may distribute this code under the same terms as Perl itself.
232
233 =cut
234