line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
29
|
|
|
29
|
|
16021
|
use warnings; |
|
29
|
|
|
|
|
47
|
|
|
29
|
|
|
|
|
892
|
|
2
|
29
|
|
|
29
|
|
117
|
use strict; |
|
29
|
|
|
|
|
35
|
|
|
29
|
|
|
|
|
1187
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Jifty::DBI::Schema; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Jifty::DBI::Schema - Use a simple syntax to describe a Jifty table. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
package MyApp::Model::Page; |
13
|
|
|
|
|
|
|
use Jifty::DBI::Schema; |
14
|
|
|
|
|
|
|
use Jifty::DBI::Record schema { |
15
|
|
|
|
|
|
|
# ... your columns here ... |
16
|
|
|
|
|
|
|
}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Each Jifty Application::Model::Class module describes a record class |
23
|
|
|
|
|
|
|
for a Jifty application. Each C statement sets out the name |
24
|
|
|
|
|
|
|
and attributes used to describe the column in a backend database, in |
25
|
|
|
|
|
|
|
user interfaces, and other contexts. For example: |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
column content => |
28
|
|
|
|
|
|
|
type is 'text', |
29
|
|
|
|
|
|
|
label is 'Content', |
30
|
|
|
|
|
|
|
render as 'textarea'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
defines a column called C that is of type C. It will be |
33
|
|
|
|
|
|
|
rendered with the label C (note the capital) and as a C |
34
|
|
|
|
|
|
|
a HTML form. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Jifty::DBI::Schema builds a L. That class defines |
37
|
|
|
|
|
|
|
other attributes for database structure that are not exposed directly |
38
|
|
|
|
|
|
|
here. One example of this is the "refers_to" method used to create |
39
|
|
|
|
|
|
|
associations between classes. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
It re-exports C and C from L, for setting |
42
|
|
|
|
|
|
|
parameter fields that must be recomputed at request-time: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
column name => |
45
|
|
|
|
|
|
|
default is defer { Jifty->web->current_user->name }; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
See L for more information about C. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
29
|
|
|
29
|
|
108
|
use Carp qw/croak carp/; |
|
29
|
|
|
|
|
31
|
|
|
29
|
|
|
|
|
1408
|
|
52
|
29
|
|
|
29
|
|
1680
|
use Scalar::Defer; |
|
29
|
|
|
|
|
29900
|
|
|
29
|
|
|
|
|
180
|
|
53
|
29
|
|
|
29
|
|
1598
|
use base qw(Class::Data::Inheritable); |
|
29
|
|
|
|
|
44
|
|
|
29
|
|
|
|
|
6055
|
|
54
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata('TYPES' => {}); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
use Object::Declare ( |
57
|
|
|
|
|
|
|
mapping => { |
58
|
94
|
|
|
|
|
4528
|
column => sub { Jifty::DBI::Column->new({@_}) } , |
59
|
|
|
|
|
|
|
}, |
60
|
|
|
|
|
|
|
aliases => { |
61
|
|
|
|
|
|
|
default_value => 'default', |
62
|
|
|
|
|
|
|
available => 'available_values', |
63
|
|
|
|
|
|
|
valid => 'valid_values', |
64
|
|
|
|
|
|
|
render => 'render_as', |
65
|
|
|
|
|
|
|
order => 'sort_order', |
66
|
|
|
|
|
|
|
filters => 'input_filters', |
67
|
|
|
|
|
|
|
}, |
68
|
|
|
|
|
|
|
copula => { |
69
|
150
|
100
|
|
|
|
9289
|
is => sub { return @_ if $#_; |
70
|
21
|
|
|
|
|
50
|
my $typehandler = __PACKAGE__->TYPES->{$_[0]}; |
71
|
|
|
|
|
|
|
# XXX: when we have a type name |
72
|
|
|
|
|
|
|
# convention, give a warning when it |
73
|
|
|
|
|
|
|
# looks like a type name but not found |
74
|
21
|
100
|
|
|
|
136
|
return ($_[0] => 1) unless $typehandler; |
75
|
12
|
|
|
|
|
20
|
return $typehandler->(); |
76
|
|
|
|
|
|
|
}, |
77
|
|
|
|
|
|
|
are => '', |
78
|
|
|
|
|
|
|
as => '', |
79
|
|
|
|
|
|
|
ajax => 'ajax_', |
80
|
3
|
|
|
|
|
206
|
refers_to => sub { refers_to => @_ }, |
81
|
5
|
|
|
|
|
404
|
references => sub { refers_to => @_ }, |
82
|
|
|
|
|
|
|
}, |
83
|
29
|
|
|
29
|
|
13508
|
); |
|
29
|
|
|
|
|
40428
|
|
|
29
|
|
|
|
|
469
|
|
84
|
29
|
|
|
29
|
|
2788
|
use Class::Data::Inheritable; |
|
29
|
|
|
|
|
38
|
|
|
29
|
|
|
|
|
273
|
|
85
|
29
|
|
|
29
|
|
1897
|
use UNIVERSAL::require (); |
|
29
|
|
|
|
|
3559
|
|
|
29
|
|
|
|
|
3577
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
our @EXPORT = qw( defer lazy column schema by render_as since till literal); |
88
|
|
|
|
|
|
|
|
89
|
3
|
|
|
3
|
1
|
198
|
sub by ($) { @_ } |
90
|
5
|
|
|
5
|
1
|
18
|
sub render_as ($) { render as @_ } |
91
|
2
|
|
|
2
|
1
|
23
|
sub since ($) { since is @_ } |
92
|
4
|
|
|
4
|
1
|
412
|
sub till ($) { till is @_ } |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub literal($) { |
95
|
0
|
|
|
0
|
1
|
0
|
my $value = shift; |
96
|
0
|
|
|
|
|
0
|
return \$value; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
our $SCHEMA; |
100
|
|
|
|
|
|
|
our $SORT_ORDERS = {}; |
101
|
|
|
|
|
|
|
|
102
|
29
|
|
|
29
|
|
127
|
use Exporter::Lite (); |
|
29
|
|
|
|
|
35
|
|
|
29
|
|
|
|
|
675
|
|
103
|
|
|
|
|
|
|
# TODO - This "sub import" is strictly here to catch the deprecated "length is 40". |
104
|
|
|
|
|
|
|
# Once the deprecation cycle is over we should take the SIGDIE swapping away |
105
|
|
|
|
|
|
|
my $old_sig_die; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub import { |
108
|
29
|
|
|
29
|
|
102
|
no warnings qw( uninitialized numeric ); |
|
29
|
|
|
|
|
32
|
|
|
29
|
|
|
|
|
7258
|
|
109
|
42
|
|
100
|
42
|
|
2429
|
$old_sig_die ||= $SIG{__DIE__}; |
110
|
42
|
100
|
66
|
|
|
285
|
$SIG{__DIE__} = \&filter_die unless $SIG{__DIE__} and $SIG{__DIE__} == \&filter_die; |
111
|
|
|
|
|
|
|
|
112
|
42
|
|
|
|
|
407
|
strict->import; |
113
|
42
|
|
|
|
|
360
|
warnings->import; |
114
|
|
|
|
|
|
|
|
115
|
42
|
|
|
|
|
159
|
goto &Exporter::Lite::import; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 filter_die |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub filter_die { |
123
|
|
|
|
|
|
|
# Calling it by hand means we restore the old sighandler. |
124
|
42
|
|
|
42
|
1
|
131
|
$SIG{__DIE__} = $old_sig_die; |
125
|
42
|
50
|
|
|
|
493
|
if ($_[0] =~ /near "is (\d+)"/) { |
|
|
50
|
|
|
|
|
|
126
|
0
|
|
|
|
|
0
|
carp @_, << "."; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
********************************************************* |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Due to an incompatible API change, the "length" field in |
131
|
|
|
|
|
|
|
Jifty::DBI columns has been renamed to "max_length": |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
column foo => |
134
|
|
|
|
|
|
|
length is $1; # NOT VALID |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Please write this instead: |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
column foo => |
139
|
|
|
|
|
|
|
max_length is $1 # VALID |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Sorry for the inconvenience. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
********************************************************** |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
. |
147
|
0
|
|
|
|
|
0
|
exit 1; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
elsif ($_[0] =~ /Undefined subroutine &Jifty::DBI::Schema::column|Can't locate object method "type" via package "(?:is|are)"/) { |
150
|
0
|
|
|
|
|
0
|
my $from = (caller)[0]; |
151
|
0
|
|
|
|
|
0
|
$from =~ s/::Schema$//; |
152
|
0
|
0
|
|
|
|
0
|
my $base = $INC{'Jifty/Record.pm'} ? "Jifty::Record" : "Jifty::DBI::Record"; |
153
|
|
|
|
|
|
|
|
154
|
29
|
|
|
29
|
|
140
|
no strict 'refs'; |
|
29
|
|
|
|
|
36
|
|
|
29
|
|
|
|
|
3255
|
|
155
|
0
|
|
|
|
|
0
|
carp @_, << "."; |
156
|
|
|
|
|
|
|
********************************************************* |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Calling 'column' within a schema class is an error: |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
package $from\::Schema; |
161
|
|
|
|
|
|
|
column foo => ...; # NOT VALID |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Please write this instead: |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
package $from; |
166
|
|
|
|
|
|
|
use Jifty::DBI::Schema; |
167
|
0
|
|
0
|
|
|
0
|
use @{[(${"$from\::ISA"} || [$base])->[0] || $base]} schema { |
168
|
|
|
|
|
|
|
column foo => ...; # VALID |
169
|
|
|
|
|
|
|
}; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Sorry for the inconvenience. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
********************************************************* |
174
|
|
|
|
|
|
|
. |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
42
|
|
|
|
|
537
|
die @_; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 FUNCTIONS |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
All these functions are exported. However, if you use the C helper function, |
184
|
|
|
|
|
|
|
they will be unimported at the end of the block passed to C. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 schema |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Takes a block with schema declarations. Unimports all helper functions after |
189
|
|
|
|
|
|
|
executing the code block. Usually used at C time via this idiom: |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
use Jifty::DBI::Record schema { ... }; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
If your application subclasses C<::Record>, then write this instead: |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
use MyApp::Record schema { ... }; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 column |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
DEPRECATED. This method of defining columns will not work anymore. Please |
202
|
|
|
|
|
|
|
use the C method documented above. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=cut |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub schema (&) { |
207
|
42
|
|
|
42
|
1
|
5012
|
my $code = shift; |
208
|
42
|
|
|
|
|
93
|
my $from = caller; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
my $new_code = sub { |
211
|
29
|
|
|
29
|
|
125
|
no warnings 'redefine'; |
|
29
|
|
|
|
|
42
|
|
|
29
|
|
|
|
|
2772
|
|
212
|
42
|
|
|
42
|
|
166
|
local *_ = sub { my $args = \@_; defer { _(@$args) } }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
213
|
42
|
|
|
|
|
311
|
$from->_init_columns; |
214
|
|
|
|
|
|
|
|
215
|
42
|
|
|
|
|
155
|
my @columns = &declare($code); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# Unimport all our symbols from the calling package, |
218
|
|
|
|
|
|
|
# except for "lazy" and "defer". |
219
|
42
|
|
|
|
|
3121
|
foreach my $sym (@EXPORT) { |
220
|
378
|
100
|
100
|
|
|
1069
|
next if $sym eq 'lazy' or $sym eq 'defer'; |
221
|
|
|
|
|
|
|
|
222
|
29
|
|
|
29
|
|
112
|
no strict 'refs'; |
|
29
|
|
|
|
|
37
|
|
|
29
|
|
|
|
|
3311
|
|
223
|
294
|
|
|
|
|
596
|
undef *{"$from\::$sym"} |
|
294
|
|
|
|
|
780
|
|
224
|
294
|
50
|
|
|
|
208
|
if \&{"$from\::$sym"} == \&$sym; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
42
|
|
|
|
|
76
|
foreach my $column (@columns) { |
228
|
188
|
100
|
|
|
|
565
|
next if !ref($column); |
229
|
94
|
|
|
|
|
158
|
_init_column($column); |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
42
|
|
|
|
|
445
|
$from->_init_methods_for_columns; |
233
|
42
|
|
|
|
|
198
|
}; |
234
|
|
|
|
|
|
|
|
235
|
42
|
|
|
|
|
289
|
return ('-base' => $new_code); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
29
|
|
|
29
|
|
13394
|
use Hash::Merge (); |
|
29
|
|
|
|
|
50028
|
|
|
29
|
|
|
|
|
604
|
|
239
|
|
|
|
|
|
|
|
240
|
29
|
|
|
29
|
|
150
|
no warnings 'uninitialized'; |
|
29
|
|
|
|
|
48
|
|
|
29
|
|
|
|
|
4694
|
|
241
|
|
|
|
|
|
|
use constant MERGE_PARAM_BEHAVIOUR => { |
242
|
|
|
|
|
|
|
SCALAR => { |
243
|
0
|
0
|
|
|
|
0
|
SCALAR => sub { CORE::length($_[1]) ? $_[1] : $_[0] }, |
244
|
0
|
|
|
|
|
0
|
ARRAY => sub { [ @{$_[1]} ] }, |
|
0
|
|
|
|
|
0
|
|
245
|
0
|
|
|
|
|
0
|
HASH => sub { $_[1] } }, |
246
|
|
|
|
|
|
|
ARRAY => { |
247
|
0
|
0
|
|
|
|
0
|
SCALAR => sub { CORE::length($_[1]) ? $_[1] : $_[0] }, |
248
|
0
|
|
|
|
|
0
|
ARRAY => sub { [ @{$_[1]} ] }, |
|
0
|
|
|
|
|
0
|
|
249
|
0
|
|
|
|
|
0
|
HASH => sub { $_[1] } }, |
250
|
|
|
|
|
|
|
HASH => { |
251
|
0
|
0
|
|
|
|
0
|
SCALAR => sub { CORE::length($_[1]) ? $_[1] : $_[0] }, |
252
|
0
|
|
|
|
|
0
|
ARRAY => sub { [ @{$_[1]} ] }, |
|
0
|
|
|
|
|
0
|
|
253
|
0
|
|
|
|
|
0
|
HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) } } |
254
|
29
|
|
|
29
|
|
121
|
}; |
|
29
|
|
|
|
|
35
|
|
|
29
|
|
|
|
|
5153
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 merge_params HASHREF HASHREF |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Takes two hashrefs. Merges them together and returns the merged hashref. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
- Empty fields in subclasses don't override nonempty fields in superclass anymore. |
261
|
|
|
|
|
|
|
- Arrays don't merge; e.g. if parent class's valid_values is [1,2,3,4], and |
262
|
|
|
|
|
|
|
subclass's valid_values() is [1,2], they don't somehow become [1,2,3,4,1,2]. |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
BUG: This should either be a private routine or factored out into Jifty::Util |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=cut |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub merge_params { |
271
|
0
|
|
|
0
|
1
|
0
|
my $prev_behaviour = Hash::Merge::get_behavior(); |
272
|
0
|
|
|
|
|
0
|
Hash::Merge::specify_behavior( MERGE_PARAM_BEHAVIOUR, "merge_params" ); |
273
|
0
|
|
|
|
|
0
|
my $rv = Hash::Merge::merge(@_); |
274
|
0
|
|
|
|
|
0
|
Hash::Merge::set_behavior( $prev_behaviour ); |
275
|
0
|
|
|
|
|
0
|
return $rv; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
sub _init_column { |
280
|
94
|
|
|
94
|
|
90
|
my $column = shift; |
281
|
94
|
|
|
|
|
173
|
my $name = $column->name; |
282
|
|
|
|
|
|
|
|
283
|
94
|
|
|
|
|
587
|
my $from = (caller(2))[0]; |
284
|
94
|
50
|
33
|
|
|
328
|
if ($from =~ s/::Schema$// && $from !~ /Script/) { |
285
|
29
|
|
|
29
|
|
129
|
no strict 'refs'; |
|
29
|
|
|
|
|
37
|
|
|
29
|
|
|
|
|
17258
|
|
286
|
|
|
|
|
|
|
|
287
|
0
|
0
|
|
|
|
0
|
carp << "." unless $from->{_seen_column_warning}++; |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
********************************************************* |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Calling 'column' within a schema class is deprecated: |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
package $from\::Schema; |
294
|
|
|
|
|
|
|
column $name => ...; # NOT VALID |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
Please write this instead: |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
package $from; |
299
|
|
|
|
|
|
|
use Jifty::DBI::Schema; |
300
|
0
|
|
0
|
|
|
0
|
use @{[${"$from\::ISA"}[0] || "Jifty::DBI::Record"]} schema { |
301
|
|
|
|
|
|
|
column $name => ...; # VALID |
302
|
|
|
|
|
|
|
}; |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
Sorry for the inconvenience. |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
********************************************************* |
307
|
|
|
|
|
|
|
. |
308
|
|
|
|
|
|
|
} |
309
|
94
|
|
|
|
|
161
|
return _init_column_for($column, $from, @_); |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
sub _init_column_for { |
313
|
96
|
|
|
96
|
|
124
|
my $column = shift; |
314
|
96
|
|
|
|
|
81
|
my $from = shift; |
315
|
96
|
|
|
|
|
163
|
my $name = $column->name; |
316
|
|
|
|
|
|
|
|
317
|
96
|
50
|
|
|
|
447
|
croak "Base of schema class $from is not a Jifty::DBI::Record" |
318
|
|
|
|
|
|
|
unless UNIVERSAL::isa($from, "Jifty::DBI::Record"); |
319
|
|
|
|
|
|
|
|
320
|
0
|
|
|
|
|
0
|
croak "Illegal column definition for column $name in $from" |
321
|
96
|
50
|
|
|
|
175
|
if grep {not UNIVERSAL::isa($_, "Jifty::DBI::Schema::Trait")} @_; |
322
|
|
|
|
|
|
|
|
323
|
96
|
|
|
|
|
165
|
$column->readable(!(delete $column->attributes->{unreadable})); |
324
|
96
|
|
|
|
|
607
|
$column->writable(!(delete $column->attributes->{immutable})); |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
# XXX: deprecated |
327
|
96
|
50
|
|
|
|
491
|
$column->mandatory(1) if delete $column->attributes->{not_null}; |
328
|
|
|
|
|
|
|
|
329
|
96
|
100
|
|
|
|
392
|
$column->sort_order($SORT_ORDERS->{$from}++) |
330
|
|
|
|
|
|
|
unless defined $column->sort_order; |
331
|
|
|
|
|
|
|
|
332
|
96
|
|
100
|
|
|
1174
|
$column->input_filters($column->{input_filters} || []); |
333
|
96
|
|
50
|
|
|
570
|
$column->output_filters($column->{output_filters} || []); |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
# Set up relationships to other records and collections |
336
|
96
|
100
|
|
|
|
212
|
if ( my $refclass = $column->refers_to ) { |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
# Handle refers_to SomeCollection by 'foo' |
339
|
8
|
100
|
|
|
|
41
|
if (ref($refclass) eq 'ARRAY') { |
340
|
3
|
|
|
|
|
9
|
$column->by($refclass->[1]); |
341
|
3
|
|
|
|
|
16
|
$column->refers_to($refclass = $refclass->[0]); |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
# Load the class we reference |
345
|
8
|
50
|
66
|
|
|
60
|
unless (UNIVERSAL::isa($refclass, 'Jifty::DBI::Record') || UNIVERSAL::isa($refclass, 'Jifty::DBI::Collection')) { |
346
|
0
|
|
|
|
|
0
|
local $UNIVERSAL::require::ERROR; |
347
|
0
|
|
|
|
|
0
|
$refclass->require(); |
348
|
0
|
0
|
|
|
|
0
|
die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR); |
349
|
|
|
|
|
|
|
} |
350
|
|
|
|
|
|
|
# A one-to-one or one-to-many relationship is requested |
351
|
8
|
100
|
|
|
|
30
|
if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) { |
|
|
50
|
|
|
|
|
|
352
|
|
|
|
|
|
|
# Assume we refer to the ID column unless told otherwise |
353
|
6
|
100
|
|
|
|
13
|
$column->by('id') unless $column->by; |
354
|
|
|
|
|
|
|
|
355
|
6
|
|
|
|
|
55
|
my $by = $column->by; |
356
|
6
|
100
|
|
|
|
24
|
unless ( $column->type ) { |
357
|
|
|
|
|
|
|
# XXX, TODO: we must set column type to the same value |
358
|
|
|
|
|
|
|
# as column we refer to. |
359
|
|
|
|
|
|
|
# |
360
|
|
|
|
|
|
|
# my $referenced_column = $refclass->column( $by ); |
361
|
|
|
|
|
|
|
# $column->type( $referenced_column->type ); |
362
|
5
|
|
|
|
|
23
|
$column->type('integer'); |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
# Handle *_id reference columns specially |
366
|
6
|
100
|
|
|
|
95
|
if ( $name =~ /(.*)_\Q$by\E$/ ) { |
367
|
1
|
|
|
|
|
3
|
my $aliased_as = $1; |
368
|
1
|
|
|
|
|
4
|
my $virtual_column = $from->add_column($aliased_as); |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# Clone ourselves into the virtual column |
371
|
1
|
|
|
|
|
10
|
%$virtual_column = %$column; |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
# This column is now just the ID, the virtual holds the ref |
374
|
1
|
|
|
|
|
3
|
$column->refers_to(undef); |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
# Note the original column |
377
|
1
|
|
|
|
|
4
|
$virtual_column->aliased_as($aliased_as); |
378
|
1
|
|
|
|
|
4
|
$virtual_column->alias_for_column($name); |
379
|
1
|
|
|
|
|
4
|
$virtual_column->virtual(1); |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
# Create the helper methods for the virtual column too |
382
|
1
|
|
|
|
|
5
|
$from->_init_methods_for_column($virtual_column); |
383
|
1
|
|
|
|
|
3
|
$from->COLUMNS->{$aliased_as} = $virtual_column; |
384
|
|
|
|
|
|
|
} else { |
385
|
5
|
|
|
|
|
10
|
my $aliased_as = $name .'_'. $by; |
386
|
5
|
|
|
|
|
24
|
my $virtual_column = $from->add_column($aliased_as); |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
# Clone ourselves into the virtual column |
389
|
5
|
|
|
|
|
63
|
%$virtual_column = %$column; |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
# This column is now just the ID, the virtual holds the ref |
392
|
5
|
|
|
|
|
19
|
$virtual_column->refers_to(undef); $virtual_column->by(undef); |
|
5
|
|
|
|
|
22
|
|
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
# Note the original column |
395
|
5
|
|
|
|
|
20
|
$virtual_column->aliased_as($aliased_as); |
396
|
5
|
|
|
|
|
21
|
$virtual_column->alias_for_column($name); |
397
|
5
|
|
|
|
|
20
|
$virtual_column->virtual(1); |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
# Create the helper methods for the virtual column too |
400
|
5
|
|
|
|
|
31
|
$from->_init_methods_for_column($virtual_column); |
401
|
5
|
|
|
|
|
14
|
$from->COLUMNS->{$aliased_as} = $virtual_column; |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
} elsif ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Collection' ) ) { |
404
|
2
|
50
|
|
|
|
4
|
$column->by('id') unless $column->by; |
405
|
2
|
|
|
|
|
11
|
$column->virtual('1'); |
406
|
|
|
|
|
|
|
} else { |
407
|
0
|
|
|
|
|
0
|
warn "Error in $from: $refclass neither Record nor Collection. Perhaps it couldn't be loaded?"; |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
} else { |
410
|
88
|
100
|
|
|
|
362
|
$column->type('varchar(255)') unless $column->type; |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
96
|
100
|
|
|
|
409
|
if (my $handler = $column->attributes->{_init_handler}) { |
414
|
6
|
|
|
|
|
20
|
$handler->($column, $from); |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
96
|
|
|
|
|
547
|
$from->COLUMNS->{$name} = $column; |
418
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
# Heuristics: If we are called through Jifty::DBI::Schema, |
420
|
|
|
|
|
|
|
# then we know that we are going to initialize methods later |
421
|
|
|
|
|
|
|
# through the &schema wrapper, so we defer initialization here |
422
|
|
|
|
|
|
|
# to not upset column names such as "label" and "type". |
423
|
|
|
|
|
|
|
# (We may not *have* a caller(1) if the user is executing a .pm file.) |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
=head2 register_types |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
=cut |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
sub register_types { |
431
|
32
|
|
|
32
|
1
|
486
|
my $class = shift; |
432
|
32
|
|
|
|
|
145
|
while (my ($type, $sub) = splice(@_, 0, 2)) { |
433
|
36
|
|
|
|
|
169
|
$class->TYPES->{$type} = $sub; |
434
|
|
|
|
|
|
|
} |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
__PACKAGE__->register_types( |
438
|
|
|
|
|
|
|
boolean => sub { |
439
|
|
|
|
|
|
|
encode_on_select is 1, |
440
|
|
|
|
|
|
|
type is 'boolean', |
441
|
|
|
|
|
|
|
filters are qw(Jifty::DBI::Filter::Boolean), |
442
|
|
|
|
|
|
|
default is 'false', |
443
|
|
|
|
|
|
|
render_as 'Checkbox', |
444
|
|
|
|
|
|
|
_init_handler is sub { |
445
|
|
|
|
|
|
|
my ($column, $from) = @_; |
446
|
29
|
|
|
29
|
|
131
|
no strict 'refs'; |
|
29
|
|
|
|
|
34
|
|
|
29
|
|
|
|
|
2525
|
|
447
|
|
|
|
|
|
|
Class::Trigger::add_trigger($from, name => "canonicalize_" . $column->name, callback => sub { |
448
|
|
|
|
|
|
|
my ($self,$value) = @_; |
449
|
|
|
|
|
|
|
$self->_apply_output_filters( |
450
|
|
|
|
|
|
|
column => $column, |
451
|
|
|
|
|
|
|
value_ref => \$value, |
452
|
|
|
|
|
|
|
); |
453
|
|
|
|
|
|
|
return $value; |
454
|
|
|
|
|
|
|
}); |
455
|
|
|
|
|
|
|
}, |
456
|
|
|
|
|
|
|
}, |
457
|
|
|
|
|
|
|
); |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
1; |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
__END__ |