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 | |
1edd1722 |
8 | use base qw/DBIx::Class/; |
cda04c3a |
9 | __PACKAGE__->load_components(qw/AccessorGroup/); |
ea2e61bf |
10 | |
cda04c3a |
11 | __PACKAGE__->mk_group_accessors('simple' => |
12 | qw/_columns name resultset_class result_class storage/); |
525035fb |
13 | |
34d52be2 |
14 | =head1 NAME |
15 | |
cda04c3a |
16 | DBIx::Class::Table - Table object |
34d52be2 |
17 | |
18 | =head1 SYNOPSIS |
19 | |
20 | =head1 DESCRIPTION |
21 | |
8091aa91 |
22 | This class is responsible for defining and doing table-level operations on |
23 | L<DBIx::Class> classes. |
34d52be2 |
24 | |
25 | =head1 METHODS |
26 | |
39fe0e65 |
27 | =cut |
28 | |
cda04c3a |
29 | sub new { |
30 | my ($class, $attrs) = @_; |
31 | $class = ref $class if ref $class; |
32 | my $new = bless($attrs || {}, $class); |
33 | $new->{resultset_class} ||= 'DBIx::Class::ResultSet'; |
34 | $new->{_columns} ||= {}; |
35 | $new->{name} ||= "!!NAME NOT SET!!"; |
36 | return $new; |
ea2e61bf |
37 | } |
38 | |
510ca912 |
39 | sub add_columns { |
cda04c3a |
40 | my ($self, @cols) = @_; |
41 | while (my $col = shift @cols) { |
42 | $self->add_column($col => (ref $cols[0] ? shift : {})); |
43 | } |
a3018bd3 |
44 | } |
45 | |
cda04c3a |
46 | sub add_column { |
47 | my ($self, $col, $info) = @_; |
48 | $self->_columns->{$col} = $info || {}; |
8fe001e1 |
49 | } |
50 | |
cda04c3a |
51 | =head2 add_columns |
510ca912 |
52 | |
cda04c3a |
53 | $table->add_columns(qw/col1 col2 col3/); |
39fe0e65 |
54 | |
cda04c3a |
55 | $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...); |
39fe0e65 |
56 | |
cda04c3a |
57 | Adds columns to the table object. If supplied key => hashref pairs uses |
58 | the hashref as the column_info for that column. |
39fe0e65 |
59 | |
60 | =cut |
61 | |
cda04c3a |
62 | sub resultset { |
63 | my $self = shift; |
64 | my $rs_class = $self->resultset_class; |
65 | eval "use $rs_class;"; |
66 | return $rs_class->new($self); |
95a70f01 |
67 | } |
68 | |
8091aa91 |
69 | =head2 has_column |
103647d5 |
70 | |
71 | if ($obj->has_column($col)) { ... } |
72 | |
cda04c3a |
73 | Returns 1 if the table has a column of this name, 0 otherwise. |
103647d5 |
74 | |
75 | =cut |
76 | |
77 | sub has_column { |
78 | my ($self, $column) = @_; |
79 | return exists $self->_columns->{$column}; |
80 | } |
81 | |
8091aa91 |
82 | =head2 column_info |
103647d5 |
83 | |
84 | my $info = $obj->column_info($col); |
85 | |
8091aa91 |
86 | Returns the column metadata hashref for a column. |
103647d5 |
87 | |
88 | =cut |
89 | |
90 | sub column_info { |
91 | my ($self, $column) = @_; |
92 | die "No such column $column" unless exists $self->_columns->{$column}; |
93 | return $self->_columns->{$column}; |
94 | } |
95 | |
8091aa91 |
96 | =head2 columns |
103647d5 |
97 | |
98 | my @column_names = $obj->columns; |
99 | |
100 | =cut |
101 | |
e7513319 |
102 | sub columns { |
103 | die "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1); |
104 | return keys %{shift->_columns}; |
105 | } |
8b445e33 |
106 | |
ea2e61bf |
107 | 1; |
34d52be2 |
108 | |
34d52be2 |
109 | =head1 AUTHORS |
110 | |
daec44b8 |
111 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
112 | |
113 | =head1 LICENSE |
114 | |
115 | You may distribute this code under the same terms as Perl itself. |
116 | |
117 | =cut |
118 | |