Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class::Table; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
223b8fe3 |
6 | use DBIx::Class::ResultSet; |
95a70f01 |
7 | |
181d4574 |
8 | use Carp qw/croak/; |
9 | |
1edd1722 |
10 | use base qw/DBIx::Class/; |
cda04c3a |
11 | __PACKAGE__->load_components(qw/AccessorGroup/); |
ea2e61bf |
12 | |
cda04c3a |
13 | __PACKAGE__->mk_group_accessors('simple' => |
bfb2bd4f |
14 | qw/_columns _primaries name resultset_class result_class schema/); |
525035fb |
15 | |
34d52be2 |
16 | =head1 NAME |
17 | |
cda04c3a |
18 | DBIx::Class::Table - Table object |
34d52be2 |
19 | |
20 | =head1 SYNOPSIS |
21 | |
22 | =head1 DESCRIPTION |
23 | |
8091aa91 |
24 | This class is responsible for defining and doing table-level operations on |
25 | L<DBIx::Class> classes. |
34d52be2 |
26 | |
27 | =head1 METHODS |
28 | |
39fe0e65 |
29 | =cut |
30 | |
cda04c3a |
31 | sub new { |
32 | my ($class, $attrs) = @_; |
33 | $class = ref $class if ref $class; |
ec77fadc |
34 | my $new = bless({ %{$attrs || {}} }, $class); |
cda04c3a |
35 | $new->{resultset_class} ||= 'DBIx::Class::ResultSet'; |
36 | $new->{_columns} ||= {}; |
37 | $new->{name} ||= "!!NAME NOT SET!!"; |
38 | return $new; |
ea2e61bf |
39 | } |
40 | |
510ca912 |
41 | sub add_columns { |
cda04c3a |
42 | my ($self, @cols) = @_; |
43 | while (my $col = shift @cols) { |
d7156e50 |
44 | $self->_columns->{$col} = (ref $cols[0] ? shift : {}); |
cda04c3a |
45 | } |
a3018bd3 |
46 | } |
47 | |
d7156e50 |
48 | *add_column = \&add_columns; |
8fe001e1 |
49 | |
cda04c3a |
50 | =head2 add_columns |
510ca912 |
51 | |
cda04c3a |
52 | $table->add_columns(qw/col1 col2 col3/); |
39fe0e65 |
53 | |
cda04c3a |
54 | $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...); |
39fe0e65 |
55 | |
cda04c3a |
56 | Adds columns to the table object. If supplied key => hashref pairs uses |
57 | the hashref as the column_info for that column. |
39fe0e65 |
58 | |
d7156e50 |
59 | =head2 add_column |
60 | |
61 | $table->add_column('col' => \%info?); |
62 | |
63 | Convenience alias to add_columns |
64 | |
39fe0e65 |
65 | =cut |
66 | |
cda04c3a |
67 | sub resultset { |
68 | my $self = shift; |
ea20d0fd |
69 | return $self->resultset_class->new($self); |
95a70f01 |
70 | } |
71 | |
8091aa91 |
72 | =head2 has_column |
103647d5 |
73 | |
74 | if ($obj->has_column($col)) { ... } |
75 | |
cda04c3a |
76 | Returns 1 if the table has a column of this name, 0 otherwise. |
103647d5 |
77 | |
78 | =cut |
79 | |
80 | sub has_column { |
81 | my ($self, $column) = @_; |
82 | return exists $self->_columns->{$column}; |
83 | } |
84 | |
8091aa91 |
85 | =head2 column_info |
103647d5 |
86 | |
87 | my $info = $obj->column_info($col); |
88 | |
8091aa91 |
89 | Returns the column metadata hashref for a column. |
103647d5 |
90 | |
91 | =cut |
92 | |
93 | sub column_info { |
94 | my ($self, $column) = @_; |
181d4574 |
95 | croak "No such column $column" unless exists $self->_columns->{$column}; |
103647d5 |
96 | return $self->_columns->{$column}; |
97 | } |
98 | |
d7156e50 |
99 | =head2 columns |
100 | |
103647d5 |
101 | my @column_names = $obj->columns; |
102 | |
103 | =cut |
104 | |
e7513319 |
105 | sub columns { |
181d4574 |
106 | croak "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1); |
e7513319 |
107 | return keys %{shift->_columns}; |
108 | } |
8b445e33 |
109 | |
d7156e50 |
110 | =head2 set_primary_key(@cols) |
111 | |
112 | Defines one or more columns as primary key for this table. Should be |
113 | called after C<add_columns>. |
114 | |
115 | =cut |
116 | |
117 | sub set_primary_key { |
118 | my ($self, @cols) = @_; |
119 | # check if primary key columns are valid columns |
120 | for (@cols) { |
121 | $self->throw("No such column $_ on table ".$self->name) |
122 | unless $self->has_column($_); |
123 | } |
124 | $self->_primaries(\@cols); |
125 | } |
126 | |
127 | =head2 primary_columns |
128 | |
129 | Read-only accessor which returns the list of primary keys. |
130 | |
131 | =cut |
132 | |
133 | sub primary_columns { |
134 | return @{shift->_primaries||[]}; |
135 | } |
136 | |
fea3d045 |
137 | =head2 from |
138 | |
139 | Returns the FROM entry for the table (i.e. the table name) |
140 | |
141 | =cut |
142 | |
bfb2bd4f |
143 | sub from { shift->name; } |
fea3d045 |
144 | |
bfb2bd4f |
145 | =head2 storage |
146 | |
147 | Returns the storage handle for the current schema |
148 | |
149 | =cut |
150 | |
151 | sub storage { shift->schema->storage; } |
d7156e50 |
152 | |
ea2e61bf |
153 | 1; |
34d52be2 |
154 | |
34d52be2 |
155 | =head1 AUTHORS |
156 | |
daec44b8 |
157 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
158 | |
159 | =head1 LICENSE |
160 | |
161 | You may distribute this code under the same terms as Perl itself. |
162 | |
163 | =cut |
164 | |