| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
29
|
|
|
29
|
|
50214
|
use warnings; |
|
|
29
|
|
|
|
|
69
|
|
|
|
29
|
|
|
|
|
3032
|
|
|
2
|
29
|
|
|
29
|
|
178
|
use strict; |
|
|
29
|
|
|
|
|
781
|
|
|
|
29
|
|
|
|
|
3083
|
|
|
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
|
|
163
|
use Carp qw/croak carp/; |
|
|
29
|
|
|
|
|
72
|
|
|
|
29
|
|
|
|
|
4441
|
|
|
52
|
29
|
|
|
29
|
|
3687
|
use Scalar::Defer; |
|
|
29
|
|
|
|
|
67782
|
|
|
|
29
|
|
|
|
|
311
|
|
|
53
|
29
|
|
|
29
|
|
2517
|
use base qw(Class::Data::Inheritable); |
|
|
29
|
|
|
|
|
62
|
|
|
|
29
|
|
|
|
|
12771
|
|
|
54
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata('TYPES' => {}); |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
use Object::Declare ( |
|
57
|
|
|
|
|
|
|
mapping => { |
|
58
|
94
|
|
|
|
|
6822
|
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
|
|
|
|
14570
|
is => sub { return @_ if $#_; |
|
70
|
21
|
|
|
|
|
85
|
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
|
|
|
|
223
|
return ($_[0] => 1) unless $typehandler; |
|
75
|
12
|
|
|
|
|
27
|
return $typehandler->(); |
|
76
|
|
|
|
|
|
|
}, |
|
77
|
|
|
|
|
|
|
are => '', |
|
78
|
|
|
|
|
|
|
as => '', |
|
79
|
|
|
|
|
|
|
ajax => 'ajax_', |
|
80
|
3
|
|
|
|
|
472
|
refers_to => sub { refers_to => @_ }, |
|
81
|
5
|
|
|
|
|
822
|
references => sub { refers_to => @_ }, |
|
82
|
|
|
|
|
|
|
}, |
|
83
|
29
|
|
|
29
|
|
29272
|
); |
|
|
29
|
|
|
|
|
84053
|
|
|
|
29
|
|
|
|
|
781
|
|
|
84
|
29
|
|
|
29
|
|
4563
|
use Class::Data::Inheritable; |
|
|
29
|
|
|
|
|
59
|
|
|
|
29
|
|
|
|
|
454
|
|
|
85
|
29
|
|
|
29
|
|
7348
|
use UNIVERSAL::require (); |
|
|
29
|
|
|
|
|
7184
|
|
|
|
29
|
|
|
|
|
20885
|
|
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
our @EXPORT = qw( defer lazy column schema by render_as since till literal); |
|
88
|
|
|
|
|
|
|
|
|
89
|
3
|
|
|
3
|
1
|
339
|
sub by ($) { @_ } |
|
90
|
5
|
|
|
5
|
1
|
30
|
sub render_as ($) { render as @_ } |
|
91
|
2
|
|
|
2
|
1
|
37
|
sub since ($) { since is @_ } |
|
92
|
4
|
|
|
4
|
1
|
488
|
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
|
|
226
|
use Exporter::Lite (); |
|
|
29
|
|
|
|
|
61
|
|
|
|
29
|
|
|
|
|
1143
|
|
|
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
|
|
177
|
no warnings qw( uninitialized numeric ); |
|
|
29
|
|
|
|
|
72
|
|
|
|
29
|
|
|
|
|
11953
|
|
|
109
|
42
|
|
100
|
42
|
|
3703
|
$old_sig_die ||= $SIG{__DIE__}; |
|
110
|
42
|
100
|
66
|
|
|
415
|
$SIG{__DIE__} = \&filter_die unless $SIG{__DIE__} and $SIG{__DIE__} == \&filter_die; |
|
111
|
|
|
|
|
|
|
|
|
112
|
42
|
|
|
|
|
688
|
strict->import; |
|
113
|
42
|
|
|
|
|
580
|
warnings->import; |
|
114
|
|
|
|
|
|
|
|
|
115
|
42
|
|
|
|
|
7154
|
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
|
187
|
$SIG{__DIE__} = $old_sig_die; |
|
125
|
42
|
50
|
|
|
|
689
|
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
|
|
200
|
no strict 'refs'; |
|
|
29
|
|
|
|
|
70
|
|
|
|
29
|
|
|
|
|
4949
|
|
|
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
|
|
|
|
|
722
|
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
|
7821
|
my $code = shift; |
|
208
|
42
|
|
|
|
|
125
|
my $from = caller; |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
my $new_code = sub { |
|
211
|
29
|
|
|
29
|
|
209
|
no warnings 'redefine'; |
|
|
29
|
|
|
|
|
68
|
|
|
|
29
|
|
|
|
|
5114
|
|
|
212
|
42
|
|
|
42
|
|
296
|
local *_ = sub { my $args = \@_; defer { _(@$args) } }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
213
|
42
|
|
|
|
|
519
|
$from->_init_columns; |
|
214
|
|
|
|
|
|
|
|
|
215
|
42
|
|
|
|
|
269
|
my @columns = &declare($code); |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# Unimport all our symbols from the calling package, |
|
218
|
|
|
|
|
|
|
# except for "lazy" and "defer". |
|
219
|
42
|
|
|
|
|
5331
|
foreach my $sym (@EXPORT) { |
|
220
|
378
|
100
|
100
|
|
|
1748
|
next if $sym eq 'lazy' or $sym eq 'defer'; |
|
221
|
|
|
|
|
|
|
|
|
222
|
29
|
|
|
29
|
|
163
|
no strict 'refs'; |
|
|
29
|
|
|
|
|
129
|
|
|
|
29
|
|
|
|
|
4859
|
|
|
223
|
294
|
|
|
|
|
1074
|
undef *{"$from\::$sym"} |
|
|
294
|
|
|
|
|
1265
|
|
|
224
|
294
|
50
|
|
|
|
323
|
if \&{"$from\::$sym"} == \&$sym; |
|
225
|
|
|
|
|
|
|
} |
|
226
|
|
|
|
|
|
|
|
|
227
|
42
|
|
|
|
|
121
|
foreach my $column (@columns) { |
|
228
|
188
|
100
|
|
|
|
894
|
next if !ref($column); |
|
229
|
94
|
|
|
|
|
253
|
_init_column($column); |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
42
|
|
|
|
|
738
|
$from->_init_methods_for_columns; |
|
233
|
42
|
|
|
|
|
296
|
}; |
|
234
|
|
|
|
|
|
|
|
|
235
|
42
|
|
|
|
|
426
|
return ('-base' => $new_code); |
|
236
|
|
|
|
|
|
|
} |
|
237
|
|
|
|
|
|
|
|
|
238
|
29
|
|
|
29
|
|
30693
|
use Hash::Merge (); |
|
|
29
|
|
|
|
|
127648
|
|
|
|
29
|
|
|
|
|
867
|
|
|
239
|
|
|
|
|
|
|
|
|
240
|
29
|
|
|
29
|
|
305
|
no warnings 'uninitialized'; |
|
|
29
|
|
|
|
|
66
|
|
|
|
29
|
|
|
|
|
8283
|
|
|
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
|
|
188
|
}; |
|
|
29
|
|
|
|
|
62
|
|
|
|
29
|
|
|
|
|
8223
|
|
|
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
|
|
148
|
my $column = shift; |
|
281
|
94
|
|
|
|
|
307
|
my $name = $column->name; |
|
282
|
|
|
|
|
|
|
|
|
283
|
94
|
|
|
|
|
891
|
my $from = (caller(2))[0]; |
|
284
|
94
|
50
|
33
|
|
|
459
|
if ($from =~ s/::Schema$// && $from !~ /Script/) { |
|
285
|
29
|
|
|
29
|
|
350
|
no strict 'refs'; |
|
|
29
|
|
|
|
|
74
|
|
|
|
29
|
|
|
|
|
31654
|
|
|
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
|
|
|
|
|
1887
|
return _init_column_for($column, $from, @_); |
|
310
|
|
|
|
|
|
|
} |
|
311
|
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
sub _init_column_for { |
|
313
|
96
|
|
|
96
|
|
133
|
my $column = shift; |
|
314
|
96
|
|
|
|
|
133
|
my $from = shift; |
|
315
|
96
|
|
|
|
|
297
|
my $name = $column->name; |
|
316
|
|
|
|
|
|
|
|
|
317
|
96
|
50
|
|
|
|
828
|
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
|
|
|
|
288
|
if grep {not UNIVERSAL::isa($_, "Jifty::DBI::Schema::Trait")} @_; |
|
322
|
|
|
|
|
|
|
|
|
323
|
96
|
|
|
|
|
291
|
$column->readable(!(delete $column->attributes->{unreadable})); |
|
324
|
96
|
|
|
|
|
993
|
$column->writable(!(delete $column->attributes->{immutable})); |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
# XXX: deprecated |
|
327
|
96
|
50
|
|
|
|
889
|
$column->mandatory(1) if delete $column->attributes->{not_null}; |
|
328
|
|
|
|
|
|
|
|
|
329
|
96
|
100
|
|
|
|
715
|
$column->sort_order($SORT_ORDERS->{$from}++) |
|
330
|
|
|
|
|
|
|
unless defined $column->sort_order; |
|
331
|
|
|
|
|
|
|
|
|
332
|
96
|
|
100
|
|
|
2018
|
$column->input_filters($column->{input_filters} || []); |
|
333
|
96
|
|
50
|
|
|
906
|
$column->output_filters($column->{output_filters} || []); |
|
334
|
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
# Set up relationships to other records and collections |
|
336
|
96
|
100
|
|
|
|
346
|
if ( my $refclass = $column->refers_to ) { |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
# Handle refers_to SomeCollection by 'foo' |
|
339
|
8
|
100
|
|
|
|
62
|
if (ref($refclass) eq 'ARRAY') { |
|
340
|
3
|
|
|
|
|
26
|
$column->by($refclass->[1]); |
|
341
|
3
|
|
|
|
|
25
|
$column->refers_to($refclass = $refclass->[0]); |
|
342
|
|
|
|
|
|
|
} |
|
343
|
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
# Load the class we reference |
|
345
|
8
|
50
|
66
|
|
|
85
|
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
|
|
|
|
50
|
if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) { |
|
|
|
50
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
# Assume we refer to the ID column unless told otherwise |
|
353
|
6
|
100
|
|
|
|
30
|
$column->by('id') unless $column->by; |
|
354
|
|
|
|
|
|
|
|
|
355
|
6
|
|
|
|
|
79
|
my $by = $column->by; |
|
356
|
6
|
100
|
|
|
|
38
|
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
|
|
|
|
|
42
|
$column->type('integer'); |
|
363
|
|
|
|
|
|
|
} |
|
364
|
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
# Handle *_id reference columns specially |
|
366
|
6
|
100
|
|
|
|
161
|
if ( $name =~ /(.*)_\Q$by\E$/ ) { |
|
367
|
1
|
|
|
|
|
5
|
my $aliased_as = $1; |
|
368
|
1
|
|
|
|
|
8
|
my $virtual_column = $from->add_column($aliased_as); |
|
369
|
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# Clone ourselves into the virtual column |
|
371
|
1
|
|
|
|
|
15
|
%$virtual_column = %$column; |
|
372
|
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
# This column is now just the ID, the virtual holds the ref |
|
374
|
1
|
|
|
|
|
5
|
$column->refers_to(undef); |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
# Note the original column |
|
377
|
1
|
|
|
|
|
6
|
$virtual_column->aliased_as($aliased_as); |
|
378
|
1
|
|
|
|
|
7
|
$virtual_column->alias_for_column($name); |
|
379
|
1
|
|
|
|
|
5
|
$virtual_column->virtual(1); |
|
380
|
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
# Create the helper methods for the virtual column too |
|
382
|
1
|
|
|
|
|
8
|
$from->_init_methods_for_column($virtual_column); |
|
383
|
1
|
|
|
|
|
5
|
$from->COLUMNS->{$aliased_as} = $virtual_column; |
|
384
|
|
|
|
|
|
|
} else { |
|
385
|
5
|
|
|
|
|
21
|
my $aliased_as = $name .'_'. $by; |
|
386
|
5
|
|
|
|
|
39
|
my $virtual_column = $from->add_column($aliased_as); |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
# Clone ourselves into the virtual column |
|
389
|
5
|
|
|
|
|
84
|
%$virtual_column = %$column; |
|
390
|
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
# This column is now just the ID, the virtual holds the ref |
|
392
|
5
|
|
|
|
|
27
|
$virtual_column->refers_to(undef); $virtual_column->by(undef); |
|
|
5
|
|
|
|
|
39
|
|
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
# Note the original column |
|
395
|
5
|
|
|
|
|
33
|
$virtual_column->aliased_as($aliased_as); |
|
396
|
5
|
|
|
|
|
35
|
$virtual_column->alias_for_column($name); |
|
397
|
5
|
|
|
|
|
36
|
$virtual_column->virtual(1); |
|
398
|
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
# Create the helper methods for the virtual column too |
|
400
|
5
|
|
|
|
|
42
|
$from->_init_methods_for_column($virtual_column); |
|
401
|
5
|
|
|
|
|
21
|
$from->COLUMNS->{$aliased_as} = $virtual_column; |
|
402
|
|
|
|
|
|
|
} |
|
403
|
|
|
|
|
|
|
} elsif ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Collection' ) ) { |
|
404
|
2
|
50
|
|
|
|
14
|
$column->by('id') unless $column->by; |
|
405
|
2
|
|
|
|
|
19
|
$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
|
|
|
|
617
|
$column->type('varchar(255)') unless $column->type; |
|
411
|
|
|
|
|
|
|
} |
|
412
|
|
|
|
|
|
|
|
|
413
|
96
|
100
|
|
|
|
696
|
if (my $handler = $column->attributes->{_init_handler}) { |
|
414
|
6
|
|
|
|
|
43
|
$handler->($column, $from); |
|
415
|
|
|
|
|
|
|
} |
|
416
|
|
|
|
|
|
|
|
|
417
|
96
|
|
|
|
|
959
|
$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
|
717
|
my $class = shift; |
|
432
|
32
|
|
|
|
|
237
|
while (my ($type, $sub) = splice(@_, 0, 2)) { |
|
433
|
36
|
|
|
|
|
200
|
$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
|
|
232
|
no strict 'refs'; |
|
|
29
|
|
|
|
|
86
|
|
|
|
29
|
|
|
|
|
4277
|
|
|
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__ |