e23700905ee0156539d5bdafe62a65c7a38e6254
[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 find_or_create
79
80   $class->find_or_create({ key => $val, ... });
81
82 Searches for a record matching the search condition; if it doesn't find one,
83 creates one and returns that instead.
84
85 =cut
86
87 sub find_or_create {
88   my $class    = shift;
89   my $hash     = ref $_[0] eq "HASH" ? shift: {@_};
90   my $exists = $class->find($hash);
91   return defined($exists) ? $exists : $class->create($hash);
92 }
93
94 =head2 has_column                                                                
95                                                                                 
96   if ($obj->has_column($col)) { ... }                                           
97                                                                                 
98 Returns 1 if the class has a column of this name, 0 otherwise.                  
99                                                                                 
100 =cut                                                                            
101
102 sub has_column {
103   my ($self, $column) = @_;
104   return $self->table_instance->has_column($column);
105 }
106
107 =head2 column_info                                                               
108                                                                                 
109   my $info = $obj->column_info($col);                                           
110                                                                                 
111 Returns the column metadata hashref for a column.
112                                                                                 
113 =cut                                                                            
114
115 sub column_info {
116   my ($self, $column) = @_;
117   return $self->table_instance->column_info($column);
118 }
119
120 =head2 columns                                                                   
121                                                                                 
122   my @column_names = $obj->columns;                                             
123                                                                                 
124 =cut                                                                            
125
126 sub columns {
127   return shift->table_instance->columns(@_);
128 }
129
130 sub result_source {
131   return shift->table_instance(@_);
132 }
133
134 1;
135
136 =head1 AUTHORS
137
138 Matt S. Trout <mst@shadowcatsystems.co.uk>
139
140 =head1 LICENSE
141
142 You may distribute this code under the same terms as Perl itself.
143
144 =cut
145