Commit | Line | Data |
80c90f5d |
1 | package DBIx::Class::ResultSourceProxy::Table; |
cda04c3a |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
80c90f5d |
6 | use base qw/DBIx::Class::ResultSourceProxy/; |
cda04c3a |
7 | |
3e110410 |
8 | use DBIx::Class::ResultSource::Table; |
9 | |
10 | __PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table'); |
cda04c3a |
11 | |
24d67825 |
12 | __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do |
75d07914 |
13 | # anything yet! |
cda04c3a |
14 | |
e60dc79f |
15 | sub _init_result_source_instance { |
16 | my $class = shift; |
17 | |
18 | $class->mk_classdata('result_source_instance') |
19 | unless $class->can('result_source_instance'); |
20 | |
21 | my $table = $class->result_source_instance; |
22 | my $class_has_table_instance = ($table and $table->result_class eq $class); |
23 | return $table if $class_has_table_instance; |
24 | |
25 | if( $table ) { |
26 | $table = $class->table_class->new({ |
27 | %$table, |
28 | result_class => $class, |
29 | source_name => undef, |
30 | schema => undef |
31 | }); |
32 | } |
33 | else { |
34 | $table = $class->table_class->new({ |
35 | name => undef, |
36 | result_class => $class, |
37 | source_name => undef, |
38 | }); |
39 | } |
40 | |
41 | $class->result_source_instance($table); |
42 | |
e60dc79f |
43 | return $table; |
44 | } |
45 | |
75d07914 |
46 | =head1 NAME |
cda04c3a |
47 | |
24d67825 |
48 | DBIx::Class::ResultSourceProxy::Table - provides a classdata table |
49 | object and method proxies |
cda04c3a |
50 | |
51 | =head1 SYNOPSIS |
52 | |
24d67825 |
53 | __PACKAGE__->table('cd'); |
54 | __PACKAGE__->add_columns(qw/cdid artist title year/); |
55 | __PACKAGE__->set_primary_key('cdid'); |
cda04c3a |
56 | |
57 | =head1 METHODS |
58 | |
cda04c3a |
59 | =head2 add_columns |
60 | |
24d67825 |
61 | __PACKAGE__->add_columns(qw/cdid artist title year/); |
cda04c3a |
62 | |
63 | Adds columns to the current class and creates accessors for them. |
64 | |
65 | =cut |
66 | |
cda04c3a |
67 | =head2 table |
68 | |
69 | __PACKAGE__->table('tbl_name'); |
70 | |
71 | Gets or sets the table name. |
72 | |
73 | =cut |
74 | |
75 | sub table { |
76 | my ($class, $table) = @_; |
b98e75f6 |
77 | return $class->result_source_instance->name unless $table; |
c2b7c5dc |
78 | |
79 | unless (Scalar::Util::blessed($table) && $table->isa($class->table_class)) { |
5a879106 |
80 | $table = $class->table_class->new({ |
24d67825 |
81 | $class->can('result_source_instance') ? |
e60dc79f |
82 | %{$class->result_source_instance||{}} : (), |
cda04c3a |
83 | name => $table, |
84 | result_class => $class, |
b1fb2c94 |
85 | source_name => undef, |
5a879106 |
86 | }); |
cda04c3a |
87 | } |
e87bedbe |
88 | |
89 | $class->mk_classdata('result_source_instance') |
90 | unless $class->can('result_source_instance'); |
91 | |
92 | $class->result_source_instance($table); |
93 | |
ade8df5b |
94 | return $class->result_source_instance->name; |
cda04c3a |
95 | } |
96 | |
988bf309 |
97 | =head2 has_column |
98 | |
99 | if ($obj->has_column($col)) { ... } |
100 | |
101 | Returns 1 if the class has a column of this name, 0 otherwise. |
102 | |
103 | =cut |
104 | |
105 | =head2 column_info |
106 | |
107 | my $info = $obj->column_info($col); |
108 | |
109 | Returns the column metadata hashref for a column. For a description of |
110 | the various types of column data in this hashref, see |
75d07914 |
111 | L<DBIx::Class::ResultSource/add_column> |
988bf309 |
112 | |
113 | =cut |
cda04c3a |
114 | |
d7156e50 |
115 | =head2 columns |
116 | |
988bf309 |
117 | my @column_names = $obj->columns; |
118 | |
119 | =cut |
cda04c3a |
120 | |
cda04c3a |
121 | 1; |
122 | |
123 | =head1 AUTHORS |
124 | |
125 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
126 | |
127 | =head1 LICENSE |
128 | |
129 | You may distribute this code under the same terms as Perl itself. |
130 | |
131 | =cut |
132 | |