Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | DBIx::Class::CDBICompat::ColumnGroups; |
ea2e61bf |
3 | |
4 | use strict; |
5 | use warnings; |
ddc0a6c8 |
6 | use Sub::Name (); |
96eab6f8 |
7 | use Storable 'dclone'; |
8 | |
75a23b3e |
9 | use base qw/DBIx::Class::Row/; |
ea2e61bf |
10 | |
11 | __PACKAGE__->mk_classdata('_column_groups' => { }); |
12 | |
ea2e61bf |
13 | sub columns { |
14 | my $proto = shift; |
15 | my $class = ref $proto || $proto; |
16 | my $group = shift || "All"; |
e60dc79f |
17 | $class->_init_result_source_instance(); |
18 | |
510ca912 |
19 | $class->_add_column_group($group => @_) if @_; |
ea2e61bf |
20 | return $class->all_columns if $group eq "All"; |
21 | return $class->primary_column if $group eq "Primary"; |
22 | return keys %{$class->_column_groups->{$group}}; |
23 | } |
24 | |
510ca912 |
25 | sub _add_column_group { |
ea2e61bf |
26 | my ($class, $group, @cols) = @_; |
c0eb27e8 |
27 | $class->mk_group_accessors(column => @cols); |
510ca912 |
28 | $class->add_columns(@cols); |
6a94f7f4 |
29 | $class->_register_column_group($group => @cols); |
ea2e61bf |
30 | } |
31 | |
c0eb27e8 |
32 | sub add_columns { |
33 | my ($class, @cols) = @_; |
34 | $class->result_source_instance->add_columns(@cols); |
35 | } |
36 | |
ea2e61bf |
37 | sub _register_column_group { |
38 | my ($class, $group, @cols) = @_; |
b8e1e21f |
39 | |
96eab6f8 |
40 | # Must do a complete deep copy else column groups |
41 | # might accidentally be shared. |
42 | my $groups = dclone $class->_column_groups; |
b8e1e21f |
43 | |
ea2e61bf |
44 | if ($group eq 'Primary') { |
510ca912 |
45 | $class->set_primary_key(@cols); |
8eae3205 |
46 | $groups->{'Essential'}{$_} ||= 1 for @cols; |
ea2e61bf |
47 | } |
48 | |
ea2e61bf |
49 | if ($group eq 'All') { |
510ca912 |
50 | unless (exists $class->_column_groups->{'Primary'}) { |
8eae3205 |
51 | $groups->{'Primary'}{$cols[0]} = 1; |
510ca912 |
52 | $class->set_primary_key($cols[0]); |
ea2e61bf |
53 | } |
510ca912 |
54 | unless (exists $class->_column_groups->{'Essential'}) { |
8eae3205 |
55 | $groups->{'Essential'}{$cols[0]} = 1; |
ea2e61bf |
56 | } |
57 | } |
58 | |
8eae3205 |
59 | $groups->{$group}{$_} ||= 1 for @cols; |
b8e1e21f |
60 | |
ea2e61bf |
61 | $class->_column_groups($groups); |
62 | } |
63 | |
8da46543 |
64 | # CDBI will never overwrite an accessor, but it only uses one |
65 | # accessor for all column types. DBIC uses many different |
66 | # accessor types so, for example, if you declare a column() |
67 | # and then a has_a() for that same column it must overwrite. |
68 | # |
69 | # To make this work CDBICompat has decide if an accessor |
70 | # method was put there by itself and only then overwrite. |
71 | { |
72 | my %our_accessors; |
73 | |
74 | sub _has_custom_accessor { |
75 | my($class, $name) = @_; |
d4daee7b |
76 | |
8da46543 |
77 | no strict 'refs'; |
78 | my $existing_accessor = *{$class .'::'. $name}{CODE}; |
79 | return $existing_accessor && !$our_accessors{$existing_accessor}; |
80 | } |
81 | |
82 | sub _deploy_accessor { |
83 | my($class, $name, $accessor) = @_; |
84 | |
85 | return if $class->_has_custom_accessor($name); |
86 | |
87 | { |
88 | no strict 'refs'; |
89 | no warnings 'redefine'; |
ddc0a6c8 |
90 | my $fullname = join '::', $class, $name; |
91 | *$fullname = Sub::Name::subname $fullname, $accessor; |
8da46543 |
92 | } |
d4daee7b |
93 | |
8da46543 |
94 | $our_accessors{$accessor}++; |
95 | |
96 | return 1; |
97 | } |
98 | } |
99 | |
100 | sub _mk_group_accessors { |
101 | my ($class, $type, $group, @fields) = @_; |
102 | |
103 | # So we don't have to do lots of lookups inside the loop. |
104 | my $maker = $class->can($type) unless ref $type; |
105 | |
106 | # warn "$class $type $group\n"; |
107 | foreach my $field (@fields) { |
108 | if( $field eq 'DESTROY' ) { |
109 | carp("Having a data accessor named DESTROY in ". |
110 | "'$class' is unwise."); |
111 | } |
112 | |
113 | my $name = $field; |
114 | |
115 | ($name, $field) = @$field if ref $field; |
116 | |
117 | my $accessor = $class->$maker($group, $field); |
118 | my $alias = "_${name}_accessor"; |
119 | |
120 | # warn " $field $alias\n"; |
121 | { |
122 | no strict 'refs'; |
d4daee7b |
123 | |
8da46543 |
124 | $class->_deploy_accessor($name, $accessor); |
125 | $class->_deploy_accessor($alias, $accessor); |
126 | } |
127 | } |
128 | } |
129 | |
8c49f629 |
130 | sub all_columns { return shift->result_source_instance->columns; } |
ea2e61bf |
131 | |
132 | sub primary_column { |
133 | my ($class) = @_; |
103647d5 |
134 | my @pri = $class->primary_columns; |
ea2e61bf |
135 | return wantarray ? @pri : $pri[0]; |
136 | } |
137 | |
902133a3 |
138 | sub _essential { |
139 | return shift->columns("Essential"); |
140 | } |
141 | |
ea2e61bf |
142 | sub find_column { |
143 | my ($class, $col) = @_; |
103647d5 |
144 | return $col if $class->has_column($col); |
ea2e61bf |
145 | } |
146 | |
147 | sub __grouper { |
148 | my ($class) = @_; |
04786a4c |
149 | my $grouper = { class => $class }; |
150 | return bless($grouper, 'DBIx::Class::CDBICompat::ColumnGroups::GrouperShim'); |
ea2e61bf |
151 | } |
152 | |
153 | sub _find_columns { |
154 | my ($class, @col) = @_; |
155 | return map { $class->find_column($_) } @col; |
156 | } |
157 | |
b24d86a1 |
158 | package # hide from PAUSE (should be harmless, no POD no Version) |
159 | DBIx::Class::CDBICompat::ColumnGroups::GrouperShim; |
ea2e61bf |
160 | |
161 | sub groups_for { |
162 | my ($self, @cols) = @_; |
163 | my %groups; |
164 | foreach my $col (@cols) { |
165 | foreach my $group (keys %{$self->{class}->_column_groups}) { |
166 | $groups{$group} = 1 if $self->{class}->_column_groups->{$group}->{$col}; |
167 | } |
168 | } |
169 | return keys %groups; |
170 | } |
ea2e61bf |
171 | |
172 | 1; |