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