Commit | Line | Data |
cda04c3a |
1 | package DBIx::Class::TableInstance; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
b98e75f6 |
6 | use base qw/DBIx::Class::ResultSourceInstance/; |
cda04c3a |
7 | use 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 | |
cda04c3a |
13 | =head1 NAME |
14 | |
15 | DBIx::Class::TableInstance - provides a classdata table object and method proxies |
16 | |
17 | =head1 SYNOPSIS |
18 | |
19 | __PACKAGE__->table('foo'); |
20 | __PACKAGE__->add_columns(qw/id bar baz/); |
21 | __PACKAGE__->set_primary_key('id'); |
22 | |
23 | =head1 METHODS |
24 | |
25 | =cut |
26 | |
27 | sub _mk_column_accessors { |
28 | my ($class, @cols) = @_; |
30126ac7 |
29 | $class->mk_group_accessors('column' => grep { !ref } @cols); |
cda04c3a |
30 | } |
31 | |
32 | =head2 add_columns |
33 | |
34 | __PACKAGE__->add_columns(qw/col1 col2 col3/); |
35 | |
36 | Adds columns to the current class and creates accessors for them. |
37 | |
38 | =cut |
39 | |
cda04c3a |
40 | =head2 table |
41 | |
42 | __PACKAGE__->table('tbl_name'); |
43 | |
44 | Gets or sets the table name. |
45 | |
46 | =cut |
47 | |
48 | sub table { |
49 | my ($class, $table) = @_; |
b98e75f6 |
50 | return $class->result_source_instance->name unless $table; |
cda04c3a |
51 | unless (ref $table) { |
f6858c33 |
52 | $table = $class->table_class->new( |
cda04c3a |
53 | { |
54 | name => $table, |
55 | result_class => $class, |
cda04c3a |
56 | }); |
b98e75f6 |
57 | if ($class->can('result_source_instance')) { |
58 | $table->{_columns} = { %{$class->result_source_instance->{_columns}||{}} }; |
20518cb4 |
59 | $table->{_ordered_columns} = |
60 | [ @{$class->result_source_instance->{_ordered_columns}||[]} ]; |
ec77fadc |
61 | } |
cda04c3a |
62 | } |
b98e75f6 |
63 | $class->mk_classdata('result_source_instance' => $table); |
7fb16f1a |
64 | if ($class->can('schema_instance')) { |
65 | $class =~ m/([^:]+)$/; |
66 | $class->schema_instance->register_class($class, $class); |
67 | } |
cda04c3a |
68 | } |
69 | |
cda04c3a |
70 | =head2 has_column |
71 | |
72 | if ($obj->has_column($col)) { ... } |
73 | |
74 | Returns 1 if the class has a column of this name, 0 otherwise. |
75 | |
76 | =cut |
77 | |
cda04c3a |
78 | =head2 column_info |
79 | |
80 | my $info = $obj->column_info($col); |
81 | |
82 | Returns the column metadata hashref for a column. |
83 | |
84 | =cut |
85 | |
d7156e50 |
86 | =head2 columns |
87 | |
cda04c3a |
88 | my @column_names = $obj->columns; |
89 | |
90 | =cut |
91 | |
cda04c3a |
92 | 1; |
93 | |
94 | =head1 AUTHORS |
95 | |
96 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
97 | |
98 | =head1 LICENSE |
99 | |
100 | You may distribute this code under the same terms as Perl itself. |
101 | |
102 | =cut |
103 | |