cce20eff18068ad7739859689d89fce3777336bc
[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 table-level operations on 
29 L<DBIx::Class> classes.
30
31 =head1 METHODS
32
33 =cut
34
35 sub _register_columns {
36   my ($class, @cols) = @_;
37   my $names = { %{$class->_columns} };
38   while (my $col = shift @cols) {
39     $names->{$col} = (ref $cols[0] ? shift : {});
40   }
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 =head2 add_columns
50
51   __PACKAGE__->add_columns(qw/col1 col2 col3/);
52
53 Adds columns to the current class 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 sub resultset_instance {
64   my $class = shift;
65   $class->next::method($class->construct_resultset);
66 }
67
68 sub construct_resultset {
69   my $class = shift;
70   my $rs_class = $class->_resultset_class;
71   eval "use $rs_class;";
72   return $rs_class->new($class);
73 }
74
75 sub _select_columns {
76   return keys %{$_[0]->_columns};
77 }
78
79 =head2 table
80
81   __PACKAGE__->table('tbl_name');
82   
83 Gets or sets the table name.
84
85 =cut
86
87 sub table {
88   shift->_table_name(@_);
89 }
90
91 =head2 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 =head2 has_column                                                                
108                                                                                 
109   if ($obj->has_column($col)) { ... }                                           
110                                                                                 
111 Returns 1 if the class 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 =head2 column_info                                                               
121                                                                                 
122   my $info = $obj->column_info($col);                                           
123                                                                                 
124 Returns the column metadata hashref for a 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 =head2 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 =head1 AUTHORS
148
149 Matt S. Trout <mst@shadowcatsystems.co.uk>
150
151 =head1 LICENSE
152
153 You may distribute this code under the same terms as Perl itself.
154
155 =cut
156