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 | |
d46eac43 |
12 | # FIXME - both of these *PROBABLY* need to be 'inherited_ro_instance' type |
e5053694 |
13 | __PACKAGE__->mk_classaccessor(table_class => 'DBIx::Class::ResultSource::Table'); |
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 | |
d46eac43 |
23 | # might be pre-made for us courtesy of DBIC::DB::result_source_instance() |
24 | my $rsrc = $class->result_source_instance; |
25 | |
26 | return $rsrc |
27 | if $rsrc and $rsrc->result_class eq $class; |
e60dc79f |
28 | |
846e17a6 |
29 | my $table_class = $class->table_class; |
30 | $class->ensure_class_loaded($table_class); |
31 | |
d46eac43 |
32 | if( $rsrc ) { |
33 | $rsrc = $table_class->new({ |
34 | %$rsrc, |
e60dc79f |
35 | result_class => $class, |
36 | source_name => undef, |
37 | schema => undef |
38 | }); |
39 | } |
40 | else { |
d46eac43 |
41 | $rsrc = $table_class->new({ |
e60dc79f |
42 | name => undef, |
43 | result_class => $class, |
44 | source_name => undef, |
45 | }); |
46 | } |
47 | |
d46eac43 |
48 | $class->result_source_instance($rsrc); |
e60dc79f |
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 { |
d46eac43 |
81 | return $_[0]->result_source_instance->name unless @_ > 1; |
82 | |
cda04c3a |
83 | my ($class, $table) = @_; |
c2b7c5dc |
84 | |
6298a324 |
85 | unless (blessed $table && $table->isa($class->table_class)) { |
846e17a6 |
86 | |
87 | my $table_class = $class->table_class; |
88 | $class->ensure_class_loaded($table_class); |
89 | |
90 | $table = $table_class->new({ |
4376a157 |
91 | $class->can('result_source_instance') |
92 | ? %{$class->result_source_instance||{}} |
93 | : () |
94 | , |
cda04c3a |
95 | name => $table, |
96 | result_class => $class, |
5a879106 |
97 | }); |
cda04c3a |
98 | } |
e87bedbe |
99 | |
e5053694 |
100 | $class->mk_group_accessors(inherited => 'result_source_instance') |
e87bedbe |
101 | unless $class->can('result_source_instance'); |
102 | |
d46eac43 |
103 | $class->result_source_instance($table)->name; |
cda04c3a |
104 | } |
105 | |
8893ffd0 |
106 | =head2 table_class |
107 | |
108 | __PACKAGE__->table_class('DBIx::Class::ResultSource::Table'); |
109 | |
110 | Gets or sets the table class used for construction and validation. |
111 | |
988bf309 |
112 | =head2 has_column |
113 | |
114 | if ($obj->has_column($col)) { ... } |
115 | |
116 | Returns 1 if the class has a column of this name, 0 otherwise. |
117 | |
988bf309 |
118 | =head2 column_info |
119 | |
120 | my $info = $obj->column_info($col); |
121 | |
122 | Returns the column metadata hashref for a column. For a description of |
123 | the various types of column data in this hashref, see |
75d07914 |
124 | L<DBIx::Class::ResultSource/add_column> |
988bf309 |
125 | |
d7156e50 |
126 | =head2 columns |
127 | |
988bf309 |
128 | my @column_names = $obj->columns; |
129 | |
a2bd3796 |
130 | =head1 FURTHER QUESTIONS? |
cda04c3a |
131 | |
a2bd3796 |
132 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
cda04c3a |
133 | |
a2bd3796 |
134 | =head1 COPYRIGHT AND LICENSE |
cda04c3a |
135 | |
a2bd3796 |
136 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
137 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
138 | redistribute it and/or modify it under the same terms as the |
139 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
cda04c3a |
140 | |
a2bd3796 |
141 | =cut |
cda04c3a |
142 | |
a2bd3796 |
143 | 1; |
cda04c3a |
144 | |
cda04c3a |
145 | |