[Rt-commit] r3860 - in Jifty-DBI/trunk: . lib/Jifty/DBI
lib/Jifty/DBI/Filter
jesse at bestpractical.com
jesse at bestpractical.com
Sun Sep 18 00:38:48 EDT 2005
Author: jesse
Date: Sun Sep 18 00:38:48 2005
New Revision: 3860
Added:
Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Truncate.pm
Modified:
Jifty-DBI/trunk/ (props changed)
Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Filter.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
Log:
r15792 at hualien: jesse | 2005-09-18 00:33:50 -0400
* Truncation of content moved to a filter
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm Sun Sep 18 00:38:48 2005
@@ -4,6 +4,8 @@
package Jifty::DBI::Column;
use base qw/Class::Accessor/;
+use UNIVERSAL::require;
+
__PACKAGE__->mk_accessors qw/
name
@@ -17,10 +19,11 @@
refers_to_record_class
alias_for_column
filters
- input_filters
output_filters
/;
+# input_filters
+
=head1 NAME
Jifty::DB::Column
@@ -61,17 +64,26 @@
my $self = shift;
my $value_ref = shift;
$self->_apply_filters( value_ref => $value_ref,
- filters => (reverse $self->filters, $self->output_filters),
+ filters => $self->output_filters,
action => 'decode'
);
}
+sub input_filters {
+ my $self = shift;
+
+ return (['Jifty::DBI::Filter::Truncate']);
+
+}
+
+
sub encode_value {
my $self = shift;
my $value_ref = shift;
$self->_apply_filters( value_ref => $value_ref,
- filters => ($self->input_filters, $self->filters),
+ filters =>
+ $self->input_filters,
action => 'encode'
);
}
@@ -88,9 +100,9 @@
my $action = $args{'action'};
foreach my $filter_class ( @{ $args{filters} } ) {
$filter_class->require();
-
+ my $filter = $filter_class->new( column => $self, value_ref => $args{'value_ref'});
# XXX TODO error proof this
- $filter_class->$action( $args{value_ref} );
+ $filter->$action();
}
}
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Filter.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Filter.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter.pm Sun Sep 18 00:38:48 2005
@@ -2,25 +2,14 @@
use strict;
package Jifty::DBI::Filter;
+use base 'Class::Accessor';
+__PACKAGE__->mk_accessors(qw(column value_ref));
-sub new {
- my $class = shift;
- my $self = {};
- bless $self, $class;
- return ($self);
-
-}
-
-=head2 encode
-
-C<encode> takes data that users are handing to us and marshals it into
-a form suitable for sticking it in the database. This could be anything
-from flattening a L<DateTime> object into an ISO date to making sure
-that data is utf8 clean.
+=head2 new
-C<encode> takes two named parameters:
+Takes
=over
@@ -35,48 +24,54 @@
=back
-
=cut
+sub new {
+ my $class = shift;
+ my %args = (column => undef,
+ value_ref => undef,
+ @_);
+ my $self = {};
+ bless $self, $class;
+
+ for ( keys %args) {
+ if ($self->can($_)) {
+ $self->$_($args{$_});
+ }
-sub encode {
- my $self = shift;
- my %args = ( value_ref => undef,
- column => undef,
- @_);
+ }
+
+ return ($self);
}
-=head2 decode
+=head2 encode
-C<decode> takes data that the database is handing back to us and gets it into a form that's OK to hand back to the user. This could be anything
+C<encode> takes data that users are handing to us and marshals it into
+a form suitable for sticking it in the database. This could be anything
from flattening a L<DateTime> object into an ISO date to making sure
that data is utf8 clean.
-C<decode> takes two named parameters:
-=over
-=item value_ref
+=cut
-A reference to the current value you're going to be massaging. C<decode> works in place, massaging whatever value_ref refers to.
-=item column
+sub encode {
-A L<Jifty::DBI::Column> object, whatever sort of column we're working with here.
+}
-=back
+=head2 decode
+C<decode> takes data that the database is handing back to us and gets it into a form that's OK to hand back to the user. This could be anything
+from flattening a L<DateTime> object into an ISO date to making sure
+that data is utf8 clean.
=cut
sub decode {
- my $self = shfit;
- my %args = ( value_ref => undef,
- column => undef,
- @_);
}
Added: Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Truncate.pm
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Truncate.pm Sun Sep 18 00:38:48 2005
@@ -0,0 +1,53 @@
+
+use strict;
+use warnings;
+
+package Jifty::DBI::Filter::Truncate;
+use base qw/Jifty::DBI::Filter/;
+
+sub encode {
+ my $self = shift;
+
+ my $value_ref = $self->value_ref;
+ return undef unless ( defined( $$value_ref ) );
+
+ my $column = $self->column();
+
+ my $truncate_to;
+ if ( $column->length && !$column->is_numeric ) {
+ $truncate_to = $column->length;
+ }
+ elsif ( $column->type && $column->type =~ /char\((\d+)\)/ ) {
+ $truncate_to = $1;
+ }
+
+ return unless ($truncate_to); # don't need to truncate
+
+ # Perl 5.6 didn't speak unicode
+ $$value_ref = substr( $$value_ref, 0, $truncate_to )
+ unless ( $] >= 5.007 );
+
+ require Encode;
+
+ if ( Encode::is_utf8( $$value_ref ) ) {
+ $$value_ref = Encode::decode(
+ utf8 => substr(
+ Encode::encode( utf8 => $$value_ref ),
+ 0, $truncate_to
+ ),
+ Encode::FB_QUIET(),
+ );
+ }
+ else {
+ $$value_ref = Encode::encode(
+ utf8 => Encode::decode(
+ utf8 => substr( $$value_ref, 0, $truncate_to ),
+ Encode::FB_QUIET(),
+ )
+ );
+
+ }
+
+}
+
+1;
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm Sun Sep 18 00:38:48 2005
@@ -629,27 +629,34 @@
sub __value {
my $self = shift;
- my $field = lc shift;
+ my $column_name = shift;
- Carp::confess unless ($field);
# If the requested column is actually an alias for another, resolve it.
- while ( $self->column($field) and defined $self->column($field)->alias_for_column) {
- $field = $self->column($field)->alias_for_column()
+ while ( $self->column($column_name)
+ and defined $self->column($column_name)->alias_for_column) {
+ $column_name = $self->column($column_name)->alias_for_column()
}
- if ( !$self->{'fetched'}{$field} and my $id = $self->id() ) {
+ my $column = $self->column($column_name);
+
+ return unless ($column);
+ #Carp::confess unless ($column);
+
+
+ if ( !$self->{'fetched'}{$column->name} and my $id = $self->id() ) {
my $pkey = $self->_primary_key();
my $QueryString
- = "SELECT $field FROM " . $self->table . " WHERE $pkey = ?";
+ = "SELECT ".$column->name." FROM " . $self->table . " WHERE $pkey = ?";
my $sth = $self->_handle->simple_query( $QueryString, $id );
my ($value) = eval { $sth->fetchrow_array() };
warn $@ if $@;
- $self->{'values'}{$field} = $value;
- $self->{'fetched'}{$field} = 1;
+ $column->decode_value(\$value);
+ $self->{'values'}{$column->name} = $value;
+ $self->{'fetched'}{$column->name} = 1;
}
- return $self->{'values'}{$field};
+ return $self->{'values'}{$column->name};
}
=head2 _value
@@ -692,13 +699,10 @@
Carp::cluck("field in ->set is deprecated");
$args{'column'} = delete $args{'field'};
}
- if ($args{'is_sql'}) {
- Carp::cluck("is_sql in ->set is deprecated");
- $args{'is_sql_function'} = delete $args{'is_sql'};
- }
+
my $ret = Class::ReturnValue->new();
- my $column = $self->column(lc $args{'column'});
+ my $column = $self->column($args{'column'});
unless ( $column) {
$ret->as_array( 0, 'No column specified' );
$ret->as_error(
@@ -708,6 +712,11 @@
);
return ( $ret->return_value );
}
+
+
+ $column->encode_value(\$args{'value'});
+
+
if ( !defined( $args{'value'} ) ) {
$ret->as_array( 0, "No value passed to _set" );
$ret->as_error(
@@ -729,10 +738,8 @@
return ( $ret->return_value );
}
- # First, we truncate the value, if we need to.
- $args{'value'} = $self->truncate_value( $column->name, $args{'value'} );
my $method = "validate_" . $column->name;
unless ( $self->$method( $args{'value'} ) ) {
@@ -791,45 +798,6 @@
return ( $ret->return_value );
}
-=head2 _canonicalize PARAMHASH
-
-This routine massages an input value (VALUE) for FIELD into something that's
-going to be acceptable.
-
-Takes
-
-=over
-
-=item FIELD
-
-=item VALUE
-
-=item FUNCTION
-
-=back
-
-
-Takes:
-
-=over
-
-=item FIELD
-
-=item VALUE
-
-=item FUNCTION
-
-=back
-
-Returns a replacement VALUE.
-
-=cut
-
-sub _canonicalize {
- my $self = shift;
- my $field = shift;
-
-}
=head2 _Validate FIELD VALUE
@@ -1081,6 +1049,9 @@
if UNIVERSAL::isa( $attribs{$column_name}, 'Jifty::DBI::Record' );
}
+
+ $column->encode_value( \$attribs{$column_name});
+
#Truncate things that are too long for their datatypes
$attribs{$column_name} = $self->truncate_value( $column_name => $attribs{$column_name} );
More information about the Rt-commit
mailing list