Lo, doth everything now use resultset_instance
[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   while (my $col = shift @cols) {
41     $names->{$col} = (ref $cols[0] ? shift : {});
42   }
43   $class->_columns($names); 
44 }
45
46 sub _mk_column_accessors {
47   my ($class, @cols) = @_;
48   $class->mk_group_accessors('column' => @cols);
49 }
50
51 =item add_columns
52
53   __PACKAGE__->add_columns(qw/col1 col2 col3/);
54
55 Adds columns to the current package, and creates accessors for them
56
57 =cut
58
59 sub add_columns {
60   my ($class, @cols) = @_;
61   $class->_register_columns(@cols);
62   $class->_mk_column_accessors(@cols);
63 }
64
65 sub resultset_instance {
66   my $class = shift;
67   $class->next::method($class->construct_resultset);
68 }
69
70 sub construct_resultset {
71   my $class = shift;
72   my $rs_class = $class->_resultset_class;
73   eval "use $rs_class;";
74   return $rs_class->new($class);
75 }
76
77 =item search_like
78
79 Identical to search except defaults to 'LIKE' instead of '=' in condition
80
81 =cut
82
83 sub search_like {
84   my $class    = shift;
85   my $attrs = { };
86   if (@_ > 1 && ref $_[$#_] eq 'HASH') {
87     $attrs = pop(@_);
88   }
89   my $query    = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
90   $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
91   return $class->search($query, { %$attrs });
92 }
93
94 sub _select_columns {
95   return keys %{$_[0]->_columns};
96 }
97
98 =item table
99
100   __PACKAGE__->table('tbl_name');
101
102 =cut
103
104 sub table {
105   shift->_table_name(@_);
106 }
107
108 =item find_or_create
109
110   $class->find_or_create({ key => $val, ... });
111
112 Searches for a record matching the search condition; if it doesn't find one,
113 creates one and returns that instead
114
115 =cut
116
117 sub find_or_create {
118   my $class    = shift;
119   my $hash     = ref $_[0] eq "HASH" ? shift: {@_};
120   my $exists = $class->find($hash);
121   return defined($exists) ? $exists : $class->create($hash);
122 }
123
124 =item has_column                                                                
125                                                                                 
126   if ($obj->has_column($col)) { ... }                                           
127                                                                                 
128 Returns 1 if the object has a column of this name, 0 otherwise                  
129                                                                                 
130 =cut                                                                            
131
132 sub has_column {
133   my ($self, $column) = @_;
134   return exists $self->_columns->{$column};
135 }
136
137 =item column_info                                                               
138                                                                                 
139   my $info = $obj->column_info($col);                                           
140                                                                                 
141 Returns the column metadata hashref for the column                              
142                                                                                 
143 =cut                                                                            
144
145 sub column_info {
146   my ($self, $column) = @_;
147   die "No such column $column" unless exists $self->_columns->{$column};
148   return $self->_columns->{$column};
149 }
150
151 =item columns                                                                   
152                                                                                 
153   my @column_names = $obj->columns;                                             
154                                                                                 
155 =cut                                                                            
156
157 sub columns {
158   die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
159   return keys %{shift->_columns};
160 }
161
162 1;
163
164 =back
165
166 =head1 AUTHORS
167
168 Matt S. Trout <mst@shadowcatsystems.co.uk>
169
170 =head1 LICENSE
171
172 You may distribute this code under the same terms as Perl itself.
173
174 =cut
175