DBI/Schema.pm

filter: Statementstmt 80.5% 157 / 195 Branchbran 64.5% 40 / 62 Conditioncond 53.8% 14 / 26 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 74.4% 35 / 47 Podpod 100% 9 / 9 totaltotal 73.7% 261 / 354 CC 44 SCAR 21.1
CC
44
Cov
75%
_init_column_for
21.8
filter_die
17.3
_init_column
8.1
CRAP 8.2
« DBI/Handle/Sybase.pm ↑ Summary DBI/Collection/Unique.pm »
1 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<column> 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<content> that is of type C<text>. It will be
33 rendered with the label C<Content> (note the capital) and as a C<textarea> in
34 a HTML form.
35  
36 Jifty::DBI::Schema builds a L<Jifty::DBI::Column>. 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<defer> and C<lazy> from L<Scalar::Defer>, 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<Scalar::Defer> for more information about C<defer>.
48  
49 =cut
50  
51 29 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 29 ▶use Scalar::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 29 ▶use base qw(Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 29 __PACKAGE__->mk_classdata('TYPES' => {});
55  
56 use Object::Declare (
57 mapping => {
58 94 ▶ column => sub { Jifty::DBI::Column->new({@_}) } ,
Subroutine __ANON__: called CC=1 SCAR=0.0
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 ▶ is => sub { return @_ if $#_;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if $#_ 129 21 150
70 21 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 ▶ return ($_[0] => 1) unless $typehandler;
Branch: 1 branch
BranchTrueFalseTotal
unless $typehandler 9 12 21
75 12 return $typehandler->();
76 },
77 are => '',
78 as => '',
79 ajax => 'ajax_',
80 3 ▶ refers_to => sub { refers_to => @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 5 ▶ references => sub { refers_to => @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 },
83 29 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
84 29 ▶use Class::Data::Inheritable;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 29 ▶use UNIVERSAL::require ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 29 our @EXPORT = qw( defer lazy column schema by render_as since till literal);
88  
89 3 ▶sub by ($) { @_ }
Subroutine by: called CC=1 SCAR=0.0
Pod documented
90 5 ▶sub render_as ($) { render as @_ }
Subroutine render_as: called CC=1 SCAR=0.0
Pod documented
91 2 ▶sub since ($) { since is @_ }
Subroutine since: called CC=1 SCAR=0.0
Pod documented
92 4 ▶sub till ($) { till is @_ }
Subroutine till: called CC=1 SCAR=0.0
Pod documented
93  
94 sub literal($) {
95 0 ▶ my $value = shift;
Subroutine literal: not called CC=1 SCAR=6.9
Pod documented
96 0 return \$value;
97 }
98  
99 29 our $SCHEMA;
100 29 our $SORT_ORDERS = {};
101  
102 29 ▶use Exporter::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
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 29 my $old_sig_die;
106  
107 sub import {
108 29 ▶ no warnings qw( uninitialized numeric );
Subroutine BEGIN: called CC=1 SCAR=0.0
109 42 ▶ $old_sig_die ||= $SIG{__DIE__};
Subroutine import: called CC=4 SCAR=13.9
Condition100%3/3
$old_sig_die ||= $SIG{"__DIE__"}
l!l&&r!l&&!r
4929
MC/DC100%2/2
$old_sig_die ||= $SIG{"__DIE__"}
$old_sig_die $SIG{"__DIE__"}
Truth table100%3/3
A ||= B
A= $old_sig_dieB= $SIG{"__DIE__"}
ABresult
1X1
011
000
110 42 ▶ $SIG{__DIE__} = \&filter_die unless $SIG{__DIE__} and $SIG{__DIE__} == \&filter_die;
Branch: 1 branch
BranchTrueFalseTotal
unless $SIG{"__DIE__"} and $SIG{"__DIE__"} == \&Jifty::DBI::Schema::filter_die 29 13 42
Condition66%2/3
$SIG{"__DIE__"} and $SIG{"__DIE__"} == \&Jifty::DBI::Schema::filter_die
!ll&&!rl&&r
29013
MC/DC50%1/2
$SIG{"__DIE__"} and $SIG{"__DIE__"} == \&Jifty::DBI::Schema::filter_die
$SIG{"__DIE__"} $SIG{"__DIE__"} == \&Jifty::DBI::Schema::filter_die
Truth table66%2/3
A and B
A= $SIG{"__DIE__"}B= $SIG{"__DIE__"} == \&Jifty::DBI::Schema::filter_die
ABresult
0X0
100
111
111  
112 42 strict->import;
113 42 warnings->import;
114  
115 42 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 12 ▶ $SIG{__DIE__} = $old_sig_die;
Subroutine filter_die: called CC=6 SCAR=28.5
Pod documented
125 12 ▶ if ($_[0] =~ /near "is (\d+)"/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /near "is (\d+)"/) { } 0 12 12
elsif ($_[0] =~ /Undefined subroutine &Jifty::DBI::Schema::column|Can't locate object method "type" via package "(?:is|are)"/) { } 0 12 12
126 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 exit 1;
148 }
149 elsif ($_[0] =~ /Undefined subroutine &Jifty::DBI::Schema::column|Can't locate object method "type" via package "(?:is|are)"/) {
150 0 my $from = (caller)[0];
151 0 $from =~ s/::Schema$//;
152 0 ▶ my $base = $INC{'Jifty/Record.pm'} ? "Jifty::Record" : "Jifty::DBI::Record";
Branch: 1 branch
BranchTrueFalseTotal
$INC{'Jifty/Record.pm'} ? : 0 0 0
153  
154 29 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
155 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 ▶ use @{[(${"$from\::ISA"} || [$base])->[0] || $base]} schema {
Condition0%0/5
+(${"${from}::ISA";} || [$base])->[0] || $base
l!l&&r!l&&!r
000
${"${from}::ISA";} || [$base]
l!l
00
MC/DC0%0/2
+(${"${from}::ISA";} || [$base])->[0] || $base
${"${from}::ISA";} $base
Truth table0%0/3
A || B
A= ${"${from}::ISA";}B= $base
ABresult
1X1
011
000
168 column foo => ...; # VALID
169 };
170  
171 Sorry for the inconvenience.
172  
173 *********************************************************
174 .
175 }
176  
177 12 die @_;
178 }
179  
180  
181 =head1 FUNCTIONS
182  
183 All these functions are exported. However, if you use the C<schema> helper function,
184 they will be unimported at the end of the block passed to C<schema>.
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<BEGIN> 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<schema {}> method documented above.
203  
204 =cut
205  
206 sub schema (&) {
207 42 ▶ my $code = shift;
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
208 42 my $from = caller;
209  
210 my $new_code = sub {
211 29 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
212 18 ▶ local *_ = sub { my $args = \@_; defer { _(@$args) } };
Subroutine __ANON__: called CC=7 SCAR=19.5
213 30 $from->_init_columns;
214  
215 35 my @columns = &declare($code);
216  
217 # Unimport all our symbols from the calling package,
218 # except for "lazy" and "defer".
219 42 foreach my $sym (@EXPORT) {
220 186 ▶ next if $sym eq 'lazy' or $sym eq 'defer';
Branch: 1 branch
BranchTrueFalseTotal
if $sym eq "lazy" or $sym eq "defer" 84 294 378
Condition100%3/3
$sym eq "lazy" or $sym eq "defer"
l!l&&r!l&&!r
4242294
MC/DC100%2/2
$sym eq "lazy" or $sym eq "defer"
$sym eq "lazy" $sym eq "defer"
Truth table100%3/3
A or B
A= $sym eq "lazy"B= $sym eq "defer"
ABresult
1X1
011
000
221  
222 29 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
223 308 undef *{"$from\::$sym"}
224 246 ▶ if \&{"$from\::$sym"} == \&$sym;
Branch: 1 branch
BranchTrueFalseTotal
if \&{"${from}::$sym";} == \&$sym 294 0 294
225 }
226  
227 186 foreach my $column (@columns) {
228 178 ▶ next if !ref($column);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $column 94 94 188
229 153 _init_column($column);
230 }
231  
232 72 $from->_init_methods_for_columns;
233 42 };
234  
235 42 return ('-base' => $new_code);
236 }
237  
238 29 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
239  
240 29 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
241 use constant MERGE_PARAM_BEHAVIOUR => {
242 SCALAR => {
243 0 ▶ SCALAR => sub { CORE::length($_[1]) ? $_[1] : $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 0 0
244 0 ▶ ARRAY => sub { [ @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 ▶ HASH => sub { $_[1] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 ARRAY => {
247 0 ▶ SCALAR => sub { CORE::length($_[1]) ? $_[1] : $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 0 0
248 0 ▶ ARRAY => sub { [ @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 ▶ HASH => sub { $_[1] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 HASH => {
251 0 ▶ SCALAR => sub { CORE::length($_[1]) ? $_[1] : $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 0 0
252 0 ▶ ARRAY => sub { [ @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 ▶ HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 29 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ my $prev_behaviour = Hash::Merge::get_behavior();
Subroutine merge_params: not called CC=1 SCAR=6.9
Pod documented
272 0 Hash::Merge::specify_behavior( MERGE_PARAM_BEHAVIOUR, "merge_params" );
273 0 my $rv = Hash::Merge::merge(@_);
274 0 Hash::Merge::set_behavior( $prev_behaviour );
275 0 return $rv;
276 }
277  
278  
279 sub _init_column {
280 94 ▶ my $column = shift;
Subroutine _init_column: called CC=5 SCAR=20.9
281 94 my $name = $column->name;
282  
283 94 my $from = (caller(2))[0];
284 94 ▶ if ($from =~ s/::Schema$// && $from !~ /Script/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ s/::Schema$// and not $from =~ /Script/) 0 94 94
Condition33%1/3
$from =~ s/::Schema$// and not $from =~ /Script/
!ll&&!rl&&r
9400
MC/DC0%0/2
$from =~ s/::Schema$// and not $from =~ /Script/
$from =~ s/::Schema$// not $from =~ /Script/
Truth table33%1/3
A and not(B)
A= $from =~ s/::Schema$//B= not $from =~ /Script/
ABresult
0X0
100
111
285 29 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
286  
287 0 ▶ carp << "." unless $from->{_seen_column_warning}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $from->{"_seen_column_warning"}++ 0 0 0
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 ▶ use @{[${"$from\::ISA"}[0] || "Jifty::DBI::Record"]} schema {
Condition0%0/2
do { || 'Jifty::DBI::Record'
l!l
00
MC/DC0%0/1
do { "${from}::ISA" }->[0] || 'Jifty::DBI::Record'
do { "${from}::ISA" }->[0]
Truth table0%0/2
A
A= do { "${from}::ISA" }->[0]
Aresult
11
00
301 column $name => ...; # VALID
302 };
303  
304 Sorry for the inconvenience.
305  
306 *********************************************************
307 .
308 }
309 94 return _init_column_for($column, $from, @_);
310 }
311  
312 sub _init_column_for {
313 96 ▶ my $column = shift;
Subroutine _init_column_for: called CC=20 SCAR=30.8
314 96 my $from = shift;
315 96 my $name = $column->name;
316  
317 96 ▶ croak "Base of schema class $from is not a Jifty::DBI::Record"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($from, "Jifty::DBI::Record") 0 96 96
318 unless UNIVERSAL::isa($from, "Jifty::DBI::Record");
319  
320 croak "Illegal column definition for column $name in $from"
321 96 ▶ if grep {not UNIVERSAL::isa($_, "Jifty::DBI::Schema::Trait")} @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not UNIVERSAL::isa($_, "Jifty::DBI::Schema::Trait");} @_ 0 96 96
322  
323 96 $column->readable(!(delete $column->attributes->{unreadable}));
324 96 $column->writable(!(delete $column->attributes->{immutable}));
325  
326 # XXX: deprecated
327 96 ▶ $column->mandatory(1) if delete $column->attributes->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if delete $column->attributes->{"not_null"} 0 96 96
328  
329 96 ▶ $column->sort_order($SORT_ORDERS->{$from}++)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column->sort_order 95 1 96
330 unless defined $column->sort_order;
331  
332 96 ▶ $column->input_filters($column->{input_filters} || []);
Condition100%2/2
$column->{'input_filters'} || []
l!l
2175
MC/DC100%1/1
$column->{'input_filters'} || []
$column->{'input_filters'}
Truth table100%2/2
A
A= $column->{'input_filters'}
Aresult
11
00
333 96 ▶ $column->output_filters($column->{output_filters} || []);
Condition50%1/2
$column->{'output_filters'} || []
l!l
096
MC/DC0%0/1
$column->{'output_filters'} || []
$column->{'output_filters'}
Truth table50%1/2
A
A= $column->{'output_filters'}
Aresult
11
00
334  
335 # Set up relationships to other records and collections
336 96 ▶ if ( my $refclass = $column->refers_to ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refclass = $column->refers_to) { } 8 88 96
337
338 # Handle refers_to SomeCollection by 'foo'
339 8 ▶ if (ref($refclass) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $refclass eq "ARRAY") 3 5 8
340 3 $column->by($refclass->[1]);
341 3 $column->refers_to($refclass = $refclass->[0]);
342 }
343  
344 # Load the class we reference
345 8 ▶ unless (UNIVERSAL::isa($refclass, 'Jifty::DBI::Record') || UNIVERSAL::isa($refclass, 'Jifty::DBI::Collection')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($refclass, "Jifty::DBI::Record") or UNIVERSAL::isa($refclass, "Jifty::DBI::Collection")) 0 8 8
Condition66%2/3
UNIVERSAL::isa($refclass, "Jifty::DBI::Record") or UNIVERSAL::isa($refclass, "Jifty::DBI::Collection")
l!l&&r!l&&!r
620
MC/DC0%0/2
UNIVERSAL::isa($refclass, "Jifty::DBI::Record") or UNIVERSAL::isa($refclass, "Jifty::DBI::Collection")
UNIVERSAL::isa($refclass, "Jifty::DBI::Record") UNIVERSAL::isa($refclass, "Jifty::DBI::Collection")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($refclass, "Jifty::DBI::Record")B= UNIVERSAL::isa($refclass, "Jifty::DBI::Collection")
ABresult
1X1
011
000
346 0 local $UNIVERSAL::require::ERROR;
347 0 $refclass->require();
348 0 ▶ die $UNIVERSAL::require::ERROR if ($UNIVERSAL::require::ERROR);
Branch: 1 branch
BranchTrueFalseTotal
if $UNIVERSAL::require::ERROR 0 0 0
349 }
350 # A one-to-one or one-to-many relationship is requested
351 8 ▶ if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($refclass, 'Jifty::DBI::Record')) { } 6 2 8
elsif (UNIVERSAL::isa($refclass, 'Jifty::DBI::Collection')) { } 2 0 2
352 # Assume we refer to the ID column unless told otherwise
353 6 ▶ $column->by('id') unless $column->by;
Branch: 1 branch
BranchTrueFalseTotal
unless $column->by 5 1 6
354  
355 6 my $by = $column->by;
356 6 ▶ unless ( $column->type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column->type) 5 1 6
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 $column->type('integer');
363 }
364  
365 # Handle *_id reference columns specially
366 6 ▶ if ( $name =~ /(.*)_\Q$by\E$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /(.*)_\Q$by\E$/) { } 1 5 6
367 1 my $aliased_as = $1;
368 1 my $virtual_column = $from->add_column($aliased_as);
369  
370 # Clone ourselves into the virtual column
371 1 %$virtual_column = %$column;
372  
373 # This column is now just the ID, the virtual holds the ref
374 1 $column->refers_to(undef);
375  
376 # Note the original column
377 1 $virtual_column->aliased_as($aliased_as);
378 1 $virtual_column->alias_for_column($name);
379 1 $virtual_column->virtual(1);
380  
381 # Create the helper methods for the virtual column too
382 1 $from->_init_methods_for_column($virtual_column);
383 1 $from->COLUMNS->{$aliased_as} = $virtual_column;
384 } else {
385 5 my $aliased_as = $name .'_'. $by;
386 5 my $virtual_column = $from->add_column($aliased_as);
387  
388 # Clone ourselves into the virtual column
389 5 %$virtual_column = %$column;
390  
391 # This column is now just the ID, the virtual holds the ref
392 5 $virtual_column->refers_to(undef); $virtual_column->by(undef);
393  
394 # Note the original column
395 5 $virtual_column->aliased_as($aliased_as);
396 5 $virtual_column->alias_for_column($name);
397 5 $virtual_column->virtual(1);
398  
399 # Create the helper methods for the virtual column too
400 5 $from->_init_methods_for_column($virtual_column);
401 5 $from->COLUMNS->{$aliased_as} = $virtual_column;
402 }
403 } elsif ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Collection' ) ) {
404 2 ▶ $column->by('id') unless $column->by;
Branch: 1 branch
BranchTrueFalseTotal
unless $column->by 0 2 2
405 2 $column->virtual('1');
406 } else {
407 0 warn "Error in $from: $refclass neither Record nor Collection. Perhaps it couldn't be loaded?";
408 }
409 } else {
410 88 ▶ $column->type('varchar(255)') unless $column->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $column->type 3 85 88
411 }
412  
413 96 ▶ if (my $handler = $column->attributes->{_init_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $column->attributes->{"_init_handler"}) 6 90 96
414 6 $handler->($column, $from);
415 }
416  
417 96 $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 ▶ my $class = shift;
Subroutine register_types: called CC=2 SCAR=6.9
Pod documented
432 32 ▶ while (my ($type, $sub) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 36 32 68
433 36 $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 5 ▶ my ($column, $from) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.5
446 29 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
447 Class::Trigger::add_trigger($from, name => "canonicalize_" . $column->name, callback => sub {
448 0 ▶ my ($self,$value) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
449 0 $self->_apply_output_filters(
450 column => $column,
451 value_ref => \$value,
452 );
453 0 return $value;
454 5 });
455 },
456 5 ▶ },
Subroutine __ANON__: called CC=1 SCAR=0.0
457 29 );
458  
459 29 1;
460  
461 __END__
462  
463 =head2 references
464  
465 Indicates that the column references an object or a collection of objects in another
466 class. You may refer to either a class that inherits from L<Jifty::Record> by a primary
467 key in that class or to a class that inherits from L<Jifty::Collection>.
468  
469 =head3 referencing a record
470  
471 Correct usage is C<references Application::Model::OtherClass by 'column_name'>, where
472 Application::Model::OtherClass is a valid Jifty model, subclass of L<Jifty::Record>,
473 and C<'column_name'> is a distinct column of OtherClass. You can omit C<by 'column_name'>
474 and the column name 'id' will be used.
475  
476 At this moment you must specify type of the column your self to match type of the column
477 you refer to.
478  
479 You can name a column as combination of 'name' and 'by', for example:
480  
481 column user_name => references App::Model::User by 'name', type is 'varchar(64)';
482  
483 Then user, user_name and respective setters will be generated. user method will return
484 object, user_name will return actual value. Note that if you're using some magic on load
485 for user records then to get real name of loaded record you should use C<< $record->user->name >>
486 instead.
487  
488 In the above case name of the column in the DB will be 'user_name'. If you don't like
489 suffixes like '_id', '_name' and other in the DB then you can name column without suffix,
490 for example:
491  
492 column user => references App::Model::User by 'name', type is 'varchar(64)';
493  
494 In this case name of the column in the DB will be 'user', accessors will be the same
495 as in above example.
496  
497 =head3 referencing a collection
498  
499 Correct usage is C<references Application::Model::OtherCollection by 'column_name'>, where
500 Application::Model::OtherCollection is a valid Jifty model, subclass of L<Jifty::Collection>,
501 and C<'column_name'> is a column of records in OtherCollection. In this case C<by 'column_name'>
502 is not optional.
503  
504 Columns that refers to a collection are virtual and can be changed. So such columns in a model
505 doesn't create real columns in the DB, but instead it's way to name collection of records that
506 refer to this records.
507  
508 =head3 example
509  
510 Simple model with users and multiple phone records per user:
511  
512 package TestApp::Model::User;
513 use Jifty::DBI::Schema;
514 use Jifty::DBI::Record schema {
515 column name => type is 'varchar(18)';
516 ...
517 column phones => references TestApp::Model::PhoneCollection by 'user';
518 };
519  
520 package TestApp::Model::Phone;
521 use Jifty::DBI::Schema;
522 use Jifty::DBI::Record schema {
523 column user => references TestApp::Model::User by 'id',
524 is mandatory;
525 column type => ...;
526 column value => ...;
527 };
528  
529 From a user record you get his phones and do something:
530  
531 my $phones = $user->phones;
532 while ( my $phone = $phones->next ) {
533 ...
534 }
535  
536 From a phone record you can get its owner or change it:
537  
538 my $user_object = $phone->user;
539 my $user_id = $phone->user_id;
540  
541 $phone->set_user( $new_owner_object );
542 $phone->set_user( 123 ); # using id
543 $phone->set_user_id( 123 ); # the same, but only using id
544  
545 =head2 refers_to
546  
547 Synonym for C<references>.
548  
549 =cut
550  
551 =head2 by
552  
553 Helper for C<references>. Used to specify what column name should be
554 used in the referenced model. See the documentation for C<references>.
555  
556 =head2 type
557  
558 type passed to our database abstraction layer, which should resolve it
559 to a database-specific type. Correct usage is C<type is 'text'>.
560  
561 Currently type is passed directly to the database. There is no
562 intermediary mapping from abstract type names to database specific
563 types.
564  
565 The impact of this is that not all column types are portable between
566 databases. For example blobs have different names between
567 mysql and postgres.
568  
569 =head2 default
570  
571 Give a default value for the column. Correct usage is C<default is
572 'foo'>.
573  
574 =head2 literal
575  
576 Used for default values, to connote that they should not be quoted
577 before being supplied as the default value for the column. Correct
578 usage is C<default is literal 'now()'>.
579  
580 =head2 validator
581  
582 Defines a subroutine which returns a true value only for valid values
583 this column can have. Correct usage is C<validator is \&foo>.
584  
585 =head2 immutable
586  
587 States that this column is not writable. This is useful for
588 properties that are set at creation time but not modifiable
589 thereafter, like 'created by'. Correct usage is C<is immutable>.
590  
591 =head2 unreadable
592  
593 States that this column is not directly readable by the application
594 using C<< $record->column >>; this is useful for password columns and
595 the like. The data is still accessible via C<< $record->_value('') >>.
596 Correct usage is C<is unreadable>.
597  
598 =head2 max_length
599  
600 Sets a maximum max_length to store in the database; values longer than
601 this are truncated before being inserted into the database, using
602 L<Jifty::DBI::Filter::Truncate>. Note that this is in B<bytes>, not
603 B<characters>. Correct usage is C<max_length is 42>.
604  
605  
606 =head2 mandatory
607  
608 Mark as a required column. May be used for generating user
609 interfaces. Correct usage is C<is mandatory>.
610  
611 =head2 not_null
612  
613 Same as L</mandatory>. This is deprecated. Correct usage would be
614 C<is not_null>.
615  
616 =head2 autocompleted
617  
618 Mark as an autocompleted column. May be used for generating user
619 interfaces. Correct usage is C<is autocompleted>.
620  
621 =head2 distinct
622  
623 Declares that a column should only have distinct values. This
624 currently is implemented via database queries prior to updates
625 and creates instead of constraints on the database columns
626 themselves. This is because there is no support for distinct
627 columns implemented in L<DBIx::DBSchema> at this time.
628 Correct usage is C<is distinct>.
629  
630 =head2 virtual
631  
632 Used to declare that a column references a collection, which hides
633 it from many parts of Jifty. You probably do not want to set this manually,
634 use C<references> instead.
635  
636 =head2 computed
637  
638 Declares that a column is not backed by an actual column in the
639 database, but is instead computed on-the-fly using a method written by
640 the application author. Such columns cannot (yet) be used in searching,
641 sorting, and so on, only inspected on an individual record.
642  
643 =head2 sort_order
644  
645 Declares an integer sort value for this column. By default, Jifty will sort
646 columns in the order they are defined.
647  
648 =head2 order
649  
650 Alias for C<sort_order>.
651  
652 =head2 input_filters
653  
654 Sets a list of input filters on the data. Correct usage is
655 C<input_filters are 'Jifty::DBI::Filter::DateTime'>. See
656 L<Jifty::DBI::Filter>.
657  
658 =head2 output_filters
659  
660 Sets a list of output filters on the data. Correct usage is
661 C<output_filters are 'Jifty::DBI::Filter::DateTime'>. See
662 L<Jifty::DBI::Filter>. You usually don't need to set this, as the
663 output filters default to the input filters in reverse order.
664  
665 =head2 filters
666  
667 Sets a list of filters on the data. These are applied when reading
668 B<and> writing to the database. Correct usage is C<filters are
669 'Jifty::DBI::Filter::DateTime'>. See L<Jifty::DBI::Filter>. In
670 actuality, this is the exact same as L</input_filters>, since output
671 filters default to the input filters, reversed.
672  
673 =head2 since
674  
675 What application version this column was last changed. Correct usage
676 is C<since '0.1.5'>.
677  
678 =head2 till
679  
680 The version after this column was supported. The column is not available in
681 the version named, but would have been in the version immediately prior.
682  
683 Correct usage is C<till '0.2.5'>. This indicates that the column is not available in version C<0.2.5>, but was available in C<0.2.4>. The value specified for L</since> must be less than this version.
684  
685 =cut
686  
687 sub till {
688 _list( till => @_ );
689 }
690  
691 =head2 valid_values
692  
693 A list of valid values for this column. Jifty will use this to
694 automatically construct a validator for you. This list may also be used to
695 generate the user interface. Correct usage is C<valid_values are
696 qw/foo bar baz/>.
697  
698 If you want to display different values than are stored in the DB
699 you can pass a list of hashrefs, each containing two keys, display
700 and value.
701  
702 valid_values are
703 { display => 'Blue', value => 'blue' },
704 { display => 'Red', value => 'red' }
705  
706 =head2 valid
707  
708 Alias for C<valid_values>.
709  
710 =head2 label
711  
712 Designates a human-readable label for the column, for use in user
713 interfaces. Correct usage is C<label is 'Your foo value'>.
714  
715 =head2 hints
716  
717 A sentence or two to display in long-form user interfaces about what
718 might go in this column. Correct usage is C<hints is 'Used by the
719 frobnicator to do strange things'>.
720  
721 =head2 display_length
722  
723 The displayed length of form fields. Though you may be able to fit
724 500 characters in the field, you would not want to display an HTML
725 form with a size 500 input box.
726  
727 =head2 render_as
728  
729 Used in user interface generation to know how to render the column.
730  
731 The values for this attribute are the same as the names of the modules under
732 L<Jifty::Web::Form::Field>, i.e.
733  
734 =over
735  
736 =item * Button
737  
738 =item * Checkbox
739  
740 =item * Combobox
741  
742 =item * Date
743  
744 =item * Hidden
745  
746 =item * InlineButton
747  
748 =item * Password
749  
750 =item * Radio
751  
752 =item * Select
753  
754 =item * Textarea
755  
756 =item * Upload
757  
758 =item * Unrendered
759  
760 =back
761  
762 You may also use the same names with the initial character in lowercase.
763  
764 The "Unrendered" may seem counter-intuitive, but is there to allow for
765 internal fields that should not actually be displayed.
766  
767 If these don't meet your needs, you can write your own subclass of
768 L<Jifty::Web::Form::Field>. See the documentation for that module.
769  
770 =head2 render
771  
772 Alias for C<render_as>.
773  
774 =head2 indexed
775  
776 An index will be built on this column
777 Correct usage is C<is indexed>
778  
779  
780 =head1 EXAMPLE
781  
782 =head1 AUTHOR
783  
784 =head1 BUGS
785  
786 =head1 SUPPORT
787  
788 =head1 COPYRIGHT & LICENSE
789  
790 This program is free software; you can redistribute it and/or modify it
791 under the same terms as Perl itself.
792  
793 =cut
794  
795 1;
« DBI/Handle/Sybase.pm ↑ Summary DBI/Collection/Unique.pm »