Commit | Line | Data |
cda04c3a |
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('_resultset_class' => 'DBIx::Class::ResultSet'); |
12 | |
ec77fadc |
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 } |
cda04c3a |
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) = @_; |
ec77fadc |
46 | $class->table_instance->add_columns(@cols); |
cda04c3a |
47 | $class->_mk_column_accessors(@cols); |
48 | } |
49 | |
50 | sub resultset_instance { |
51 | my $class = shift; |
ec77fadc |
52 | my $table = $class->table_instance->new($class->table_instance); |
2d679367 |
53 | $table->storage($class->storage_instance); |
ec77fadc |
54 | $table->result_class($class); |
55 | return $table->resultset; |
cda04c3a |
56 | } |
57 | |
58 | sub _select_columns { |
ec77fadc |
59 | return shift->table_instance->columns; |
cda04c3a |
60 | } |
61 | |
62 | =head2 table |
63 | |
64 | __PACKAGE__->table('tbl_name'); |
65 | |
66 | Gets or sets the table name. |
67 | |
68 | =cut |
69 | |
70 | sub table { |
71 | my ($class, $table) = @_; |
ec77fadc |
72 | return $class->table_instance->name unless $table; |
cda04c3a |
73 | unless (ref $table) { |
74 | $table = DBIx::Class::Table->new( |
75 | { |
76 | name => $table, |
77 | result_class => $class, |
78 | #storage => $class->storage, |
79 | }); |
ec77fadc |
80 | if ($class->can('table_instance')) { |
81 | $table->{_columns} = { %{$class->table_instance->{_columns}||{}} }; |
82 | } |
cda04c3a |
83 | } |
ec77fadc |
84 | $class->mk_classdata('table_instance' => $table); |
cda04c3a |
85 | } |
86 | |
87 | =head2 find_or_create |
88 | |
89 | $class->find_or_create({ key => $val, ... }); |
90 | |
91 | Searches for a record matching the search condition; if it doesn't find one, |
92 | creates one and returns that instead. |
93 | |
94 | =cut |
95 | |
96 | sub find_or_create { |
97 | my $class = shift; |
98 | my $hash = ref $_[0] eq "HASH" ? shift: {@_}; |
99 | my $exists = $class->find($hash); |
100 | return defined($exists) ? $exists : $class->create($hash); |
101 | } |
102 | |
103 | =head2 has_column |
104 | |
105 | if ($obj->has_column($col)) { ... } |
106 | |
107 | Returns 1 if the class has a column of this name, 0 otherwise. |
108 | |
109 | =cut |
110 | |
111 | sub has_column { |
112 | my ($self, $column) = @_; |
ec77fadc |
113 | return $self->table_instance->has_column($column); |
cda04c3a |
114 | } |
115 | |
116 | =head2 column_info |
117 | |
118 | my $info = $obj->column_info($col); |
119 | |
120 | Returns the column metadata hashref for a column. |
121 | |
122 | =cut |
123 | |
124 | sub column_info { |
125 | my ($self, $column) = @_; |
ec77fadc |
126 | return $self->table_instance->column_info($column); |
cda04c3a |
127 | } |
128 | |
129 | =head2 columns |
130 | |
131 | my @column_names = $obj->columns; |
132 | |
133 | =cut |
134 | |
135 | sub columns { |
ec77fadc |
136 | return shift->table_instance->columns(@_); |
cda04c3a |
137 | } |
138 | |
139 | 1; |
140 | |
141 | =head1 AUTHORS |
142 | |
143 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
144 | |
145 | =head1 LICENSE |
146 | |
147 | You may distribute this code under the same terms as Perl itself. |
148 | |
149 | =cut |
150 | |