| 1 | |||||||||||||||||||||||||||||||||
| 2 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | package Jifty::DBI::Filter::Truncate; | ||||||||||||||||||||||||||||||||
| 6 | 27 | ▶ | use base qw/Jifty::DBI::Filter/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 27 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | Jifty::DBI::Filter::Truncate - Filter used to enforce max_length column trait | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | You do not need to use this filter explicitly. This filter is used internally to enforce the L<Jifty::DBI::Schema/max_length> restrictions on columns: | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | column name => | ||||||||||||||||||||||||||||||||
| 18 | type is 'text', | ||||||||||||||||||||||||||||||||
| 19 | max_length is 10; | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | In this case, the filter would be automatically added to the column named C<name> and any value put into the column longer than 10 characters would be truncated to 10 characters. | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =head2 encode | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | This method performs the work of performing truncation, when necessary. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | sub encode { | ||||||||||||||||||||||||||||||||
| 32 | 338 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 338 | my $value_ref = $self->value_ref; | |||||||||||||||||||||||||||||||
| 35 | 338 | ▶ | return undef unless ( defined($$value_ref) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 299 | my $column = $self->column(); | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 299 | my $truncate_to; | |||||||||||||||||||||||||||||||
| 40 | 299 | ▶ | if ( $column->max_length && !$column->is_numeric ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$column->max_length and not $column->is_numeric
MC/DC100%2/2
$column->max_length and not $column->is_numeric
$column->max_length
not $column->is_numeric
Truth table100%3/3
A and not(B)
A= $column->max_lengthB= not $column->is_numeric
| |||||||||||||||||||||||||||||||||
| 41 | 3 | $truncate_to = $column->max_length; | |||||||||||||||||||||||||||||||
| 42 | ▶ | } elsif ( $column->type && $column->type =~ /char\((\d+)\)/ ) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$column->type and $column->type =~ /char\((\d+)\)/
MC/DC50%1/2
$column->type and $column->type =~ /char\((\d+)\)/
$column->type
$column->type =~ /char\((\d+)\)/
Truth table66%2/3
A and B
A= $column->typeB= $column->type =~ /char\((\d+)\)/
| |||||||||||||||||||||||||||||||||
| 43 | 92 | $truncate_to = $1; | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 299 | ▶ | return unless ($truncate_to); # don't need to truncate | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 95 | my $utf8 = Encode::is_utf8($$value_ref); | |||||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||||||
| 50 | 27 | ▶ | use bytes; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 51 | 95 | $$value_ref = substr( $$value_ref, 0, $truncate_to ); | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | 95 | ▶ | if ($utf8) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | # return utf8 flag back, but use Encode::FB_QUIET because | ||||||||||||||||||||||||||||||||
| 56 | # we could broke tail char | ||||||||||||||||||||||||||||||||
| 57 | 2 | $$value_ref = Encode::decode_utf8( $$value_ref, Encode::FB_QUIET ); | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | Jifty::DBI is Copyright 2005-2007 Best Practical Solutions, LLC. | ||||||||||||||||||||||||||||||||
| 64 | Jifty::DBI is distributed under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 27 | 1; | |||||||||||||||||||||||||||||||