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