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