Moved search_like to resultset
[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 sub _select_columns {
78   return keys %{$_[0]->_columns};
79 }
80
81 =item table
82
83   __PACKAGE__->table('tbl_name');
84
85 =cut
86
87 sub table {
88   shift->_table_name(@_);
89 }
90
91 =item find_or_create
92
93   $class->find_or_create({ key => $val, ... });
94
95 Searches for a record matching the search condition; if it doesn't find one,
96 creates one and returns that instead
97
98 =cut
99
100 sub find_or_create {
101   my $class    = shift;
102   my $hash     = ref $_[0] eq "HASH" ? shift: {@_};
103   my $exists = $class->find($hash);
104   return defined($exists) ? $exists : $class->create($hash);
105 }
106
107 =item has_column                                                                
108                                                                                 
109   if ($obj->has_column($col)) { ... }                                           
110                                                                                 
111 Returns 1 if the object has a column of this name, 0 otherwise                  
112                                                                                 
113 =cut                                                                            
114
115 sub has_column {
116   my ($self, $column) = @_;
117   return exists $self->_columns->{$column};
118 }
119
120 =item column_info                                                               
121                                                                                 
122   my $info = $obj->column_info($col);                                           
123                                                                                 
124 Returns the column metadata hashref for the column                              
125                                                                                 
126 =cut                                                                            
127
128 sub column_info {
129   my ($self, $column) = @_;
130   die "No such column $column" unless exists $self->_columns->{$column};
131   return $self->_columns->{$column};
132 }
133
134 =item columns                                                                   
135                                                                                 
136   my @column_names = $obj->columns;                                             
137                                                                                 
138 =cut                                                                            
139
140 sub columns {
141   die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
142   return keys %{shift->_columns};
143 }
144
145 1;
146
147 =back
148
149 =head1 AUTHORS
150
151 Matt S. Trout <mst@shadowcatsystems.co.uk>
152
153 =head1 LICENSE
154
155 You may distribute this code under the same terms as Perl itself.
156
157 =cut
158