moved find_or_create onto ResultSet
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / TableInstance.pm
1 package DBIx::Class::TableInstance;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class/;
7 use DBIx::Class::Table;
8
9 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
10
11 __PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::Table');
12
13 sub iterator_class { shift->table_instance->resultset_class(@_) }
14 sub resultset_class { shift->table_instance->resultset_class(@_) }
15 sub _table_name { shift->table_instance->name }
16
17 =head1 NAME 
18
19 DBIx::Class::TableInstance - provides a classdata table object and method proxies
20
21 =head1 SYNOPSIS
22
23   __PACKAGE__->table('foo');
24   __PACKAGE__->add_columns(qw/id bar baz/);
25   __PACKAGE__->set_primary_key('id');
26
27 =head1 METHODS
28
29 =cut
30
31 sub _mk_column_accessors {
32   my ($class, @cols) = @_;
33   $class->mk_group_accessors('column' => @cols);
34 }
35
36 =head2 add_columns
37
38   __PACKAGE__->add_columns(qw/col1 col2 col3/);
39
40 Adds columns to the current class and creates accessors for them.
41
42 =cut
43
44 sub add_columns {
45   my ($class, @cols) = @_;
46   $class->table_instance->add_columns(@cols);
47   $class->_mk_column_accessors(@cols);
48 }
49
50 sub _select_columns {
51   return shift->table_instance->columns;
52 }
53
54 =head2 table
55
56   __PACKAGE__->table('tbl_name');
57   
58 Gets or sets the table name.
59
60 =cut
61
62 sub table {
63   my ($class, $table) = @_;
64   return $class->table_instance->name unless $table;
65   unless (ref $table) {
66     $table = $class->table_class->new(
67       {
68         name => $table,
69         result_class => $class,
70       });
71     if ($class->can('table_instance')) {
72       $table->{_columns} = { %{$class->table_instance->{_columns}||{}} };
73     }
74   }
75   $class->mk_classdata('table_instance' => $table);
76 }
77
78 =head2 has_column                                                                
79                                                                                 
80   if ($obj->has_column($col)) { ... }                                           
81                                                                                 
82 Returns 1 if the class has a column of this name, 0 otherwise.                  
83                                                                                 
84 =cut                                                                            
85
86 sub has_column {
87   my ($self, $column) = @_;
88   return $self->table_instance->has_column($column);
89 }
90
91 =head2 column_info                                                               
92                                                                                 
93   my $info = $obj->column_info($col);                                           
94                                                                                 
95 Returns the column metadata hashref for a column.
96                                                                                 
97 =cut                                                                            
98
99 sub column_info {
100   my ($self, $column) = @_;
101   return $self->table_instance->column_info($column);
102 }
103
104 =head2 columns
105
106   my @column_names = $obj->columns;                                             
107                                                                                 
108 =cut                                                                            
109
110 sub columns {
111   return shift->table_instance->columns(@_);
112 }
113
114 sub result_source {
115   return shift->table_instance(@_);
116 }
117
118 sub set_primary_key { shift->table_instance->set_primary_key(@_); }
119 sub primary_columns { shift->table_instance->primary_columns(@_); }
120
121 1;
122
123 =head1 AUTHORS
124
125 Matt S. Trout <mst@shadowcatsystems.co.uk>
126
127 =head1 LICENSE
128
129 You may distribute this code under the same terms as Perl itself.
130
131 =cut
132