create now on resultset as well
[dbsrgits/DBIx-Class-Historic.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
f6858c33 11__PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::Table');
cda04c3a 12
ec77fadc 13sub iterator_class { shift->table_instance->resultset_class(@_) }
14sub resultset_class { shift->table_instance->resultset_class(@_) }
15sub _table_name { shift->table_instance->name }
cda04c3a 16
17=head1 NAME
18
19DBIx::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
31sub _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
40Adds columns to the current class and creates accessors for them.
41
42=cut
43
44sub add_columns {
45 my ($class, @cols) = @_;
ec77fadc 46 $class->table_instance->add_columns(@cols);
cda04c3a 47 $class->_mk_column_accessors(@cols);
48}
49
cda04c3a 50sub _select_columns {
ec77fadc 51 return shift->table_instance->columns;
cda04c3a 52}
53
54=head2 table
55
56 __PACKAGE__->table('tbl_name');
57
58Gets or sets the table name.
59
60=cut
61
62sub table {
63 my ($class, $table) = @_;
ec77fadc 64 return $class->table_instance->name unless $table;
cda04c3a 65 unless (ref $table) {
f6858c33 66 $table = $class->table_class->new(
cda04c3a 67 {
68 name => $table,
69 result_class => $class,
cda04c3a 70 });
ec77fadc 71 if ($class->can('table_instance')) {
72 $table->{_columns} = { %{$class->table_instance->{_columns}||{}} };
73 }
cda04c3a 74 }
ec77fadc 75 $class->mk_classdata('table_instance' => $table);
cda04c3a 76}
77
78=head2 find_or_create
79
80 $class->find_or_create({ key => $val, ... });
81
82Searches for a record matching the search condition; if it doesn't find one,
83creates one and returns that instead.
84
85=cut
86
87sub 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
98Returns 1 if the class has a column of this name, 0 otherwise.
99
100=cut
101
102sub has_column {
103 my ($self, $column) = @_;
ec77fadc 104 return $self->table_instance->has_column($column);
cda04c3a 105}
106
107=head2 column_info
108
109 my $info = $obj->column_info($col);
110
111Returns the column metadata hashref for a column.
112
113=cut
114
115sub column_info {
116 my ($self, $column) = @_;
ec77fadc 117 return $self->table_instance->column_info($column);
cda04c3a 118}
119
d7156e50 120=head2 columns
121
cda04c3a 122 my @column_names = $obj->columns;
123
124=cut
125
126sub columns {
ec77fadc 127 return shift->table_instance->columns(@_);
cda04c3a 128}
129
6aeb9185 130sub result_source {
131 return shift->table_instance(@_);
132}
133
d7156e50 134sub set_primary_key { shift->table_instance->set_primary_key(@_); }
135sub primary_columns { shift->table_instance->primary_columns(@_); }
136
cda04c3a 1371;
138
139=head1 AUTHORS
140
141Matt S. Trout <mst@shadowcatsystems.co.uk>
142
143=head1 LICENSE
144
145You may distribute this code under the same terms as Perl itself.
146
147=cut
148