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 | |
e5053694 |
12 | __PACKAGE__->mk_classaccessor(table_class => 'DBIx::Class::ResultSource::Table'); |
cda04c3a |
13 | |
e5053694 |
14 | # FIXME: Doesn't actually do anything yet! |
15 | __PACKAGE__->mk_group_accessors( inherited => 'table_alias' ); |
cda04c3a |
16 | |
e60dc79f |
17 | sub _init_result_source_instance { |
18 | my $class = shift; |
19 | |
e5053694 |
20 | $class->mk_group_accessors( inherited => 'result_source_instance' ) |
21 | unless $class->can('result_source_instance'); |
e60dc79f |
22 | |
23 | my $table = $class->result_source_instance; |
e5053694 |
24 | return $table |
25 | if $table and $table->result_class eq $class; |
e60dc79f |
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({ |
4376a157 |
90 | $class->can('result_source_instance') |
91 | ? %{$class->result_source_instance||{}} |
92 | : () |
93 | , |
cda04c3a |
94 | name => $table, |
95 | result_class => $class, |
5a879106 |
96 | }); |
cda04c3a |
97 | } |
e87bedbe |
98 | |
e5053694 |
99 | $class->mk_group_accessors(inherited => 'result_source_instance') |
e87bedbe |
100 | unless $class->can('result_source_instance'); |
101 | |
102 | $class->result_source_instance($table); |
103 | |
ade8df5b |
104 | return $class->result_source_instance->name; |
cda04c3a |
105 | } |
106 | |
8893ffd0 |
107 | =head2 table_class |
108 | |
109 | __PACKAGE__->table_class('DBIx::Class::ResultSource::Table'); |
110 | |
111 | Gets or sets the table class used for construction and validation. |
112 | |
988bf309 |
113 | =head2 has_column |
114 | |
115 | if ($obj->has_column($col)) { ... } |
116 | |
117 | Returns 1 if the class has a column of this name, 0 otherwise. |
118 | |
988bf309 |
119 | =head2 column_info |
120 | |
121 | my $info = $obj->column_info($col); |
122 | |
123 | Returns the column metadata hashref for a column. For a description of |
124 | the various types of column data in this hashref, see |
75d07914 |
125 | L<DBIx::Class::ResultSource/add_column> |
988bf309 |
126 | |
d7156e50 |
127 | =head2 columns |
128 | |
988bf309 |
129 | my @column_names = $obj->columns; |
130 | |
a2bd3796 |
131 | =head1 FURTHER QUESTIONS? |
cda04c3a |
132 | |
a2bd3796 |
133 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
cda04c3a |
134 | |
a2bd3796 |
135 | =head1 COPYRIGHT AND LICENSE |
cda04c3a |
136 | |
a2bd3796 |
137 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
138 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
139 | redistribute it and/or modify it under the same terms as the |
140 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
cda04c3a |
141 | |
a2bd3796 |
142 | =cut |
cda04c3a |
143 | |
a2bd3796 |
144 | 1; |
cda04c3a |
145 | |
cda04c3a |
146 | |