[Jifty-commit] r6266 - in Jifty-DBI/branches/tisql: lib/Jifty/DBI lib/Jifty/DBI/Record t

Jifty commits jifty-commit at lists.jifty.org
Fri Jan 23 18:20:52 EST 2009


Author: ruz
Date: Fri Jan 23 18:20:52 2009
New Revision: 6266

Added:
   Jifty-DBI/branches/tisql/t/05raw_value.t
Modified:
   Jifty-DBI/branches/tisql/   (props changed)
   Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm
   Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record.pm
   Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Cachable.pm
   Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Memcached.pm
   Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Plugin.pm

Log:
* sync from trunk

Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm	Fri Jan 23 18:20:52 2009
@@ -19,8 +19,7 @@
         'Jifty::DBI::Handle::simple_query',
         pre => sub {
             return unless $_[1] =~ m/$pattern/;
-            warn $_[1] . '   ' . CORE::join( ',', @_[ 2 .. $#_ ] ) . "\n";
-            Carp::cluck;
+            Carp::cluck($_[1] . '   ' . CORE::join( ',', @_[ 2 .. $#_ ] ));
         }
     );
 }

Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record.pm	Fri Jan 23 18:20:52 2009
@@ -769,26 +769,79 @@
     return $value;
 }
 
-=head2 __value
+=head2 __raw_value
 
-Takes a column name and returns that column's value. Subclasses should
-never override __value.
+Takes a column name and returns that column's raw value.
+Subclasses should never override __raw_value.
 
 =cut
 
-sub __value {
+sub __raw_value {
     my $self = shift;
 
+    my $column = $self->resolve_column( shift );
+    return unless $column;
+
+    my $column_name = $column->name;
+
+    # In the default case of "yeah, we have a value", return it as
+    # fast as we can.
+    return $self->{'raw_values'}{$column_name}
+      if $self->{'raw_fetched'}{$column_name};
+
+    if ( !$self->{'raw_fetched'}{$column_name} and my $id = $self->id() ) {
+        my $pkey = $self->_primary_key();
+        my $query_string =
+            "SELECT "
+          . $column_name
+          . " FROM "
+          . $self->table
+          . " WHERE $pkey = ?";
+        my $sth = $self->_handle->simple_query( $query_string, $id );
+        my ($value) = eval { $sth->fetchrow_array() };
+        $self->{'raw_values'}{$column_name}  = $value;
+        $self->{'raw_fetched'}{$column_name} = 1;
+    }
+
+    return $self->{'raw_values'}{$column_name};
+}
+
+=head2 resolve_column
+
+given a column name, resolve it, even if it's actually an alias 
+return the column object
+
+=cut
+
+sub resolve_column {
+    my $self = shift;
     my $column_name = shift;
+    return unless $column_name;
 
     # If the requested column is actually an alias for another, resolve it.
     my $column = $self->column($column_name);
     if ( $column and defined $column->alias_for_column ) {
         $column      = $self->column( $column->alias_for_column() );
-        $column_name = $column->name;
     }
+    return unless $column;
+    return $column;
+}
+
+=head2 __value
+
+Takes a column name and returns that column's value. Subclasses should
+never override __value.
+
+=cut
+
+sub __value {
+    my $self = shift;
+
+    my $column = $self->resolve_column( shift );
+    return unless $column;
+
+    my $column_name = $column->name;
 
-    return unless ($column);
 
     # In the default case of "yeah, we have a value", return it as
     # fast as we can.
@@ -796,19 +849,9 @@
         if ( $self->{'fetched'}{$column_name}
         && $self->{'decoded'}{$column_name} );
 
-    if ( !$self->{'fetched'}{$column_name} and my $id = $self->id() ) {
-        my $pkey = $self->_primary_key();
-        my $query_string
-            = "SELECT "
-            . $column_name
-            . " FROM "
-            . $self->table
-            . " WHERE $pkey = ?";
-        my $sth = $self->_handle->simple_query( $query_string, $id );
-        my ($value) = eval { $sth->fetchrow_array() };
-        $self->{'values'}{$column_name}  = $value;
-        $self->{'fetched'}{$column_name} = 1;
-    }
+    $self->{'values'}{$column_name}  = $self->__raw_value( $column_name )
+        unless $self->{'fetched'}{$column_name};
+
     unless ( $self->{'decoded'}{$column_name} ) {
         $self->_apply_output_filters(
             column    => $column,
@@ -914,7 +957,7 @@
         value_ref => \$args{'value'}
     );
 
-    # if value is not fetched or it's allready decoded
+    # if value is not fetched or it's already decoded
     # then we don't check eqality
     # we also don't call __value because it decodes value, but
     # we need encoded value
@@ -996,6 +1039,7 @@
         # XXX TODO primary_keys
         $self->load_by_cols( id => $self->id );
     } else {
+        $self->{'raw_values'}{ $column->name }  = $unmunged_value;
         $self->{'values'}{ $column->name }  = $unmunged_value;
         $self->{'decoded'}{ $column->name } = 0;
     }
@@ -1160,6 +1204,8 @@
 
     $self->{'values'} = {};
     $self->{'fetched'} = {};
+    $self->{'raw_values'} = {};
+    $self->{'raw_fetched'} = {};
 
     foreach my $col ( grep exists $hashref->{ lc $_ }, map $_->name, $self->columns ) {
         $self->{'fetched'}{$col} = 1;
@@ -1188,7 +1234,9 @@
     return ( 0, "Couldn't execute query" ) unless $sth;
 
     my $hashref = $sth->fetchrow_hashref;
-    delete $self->{values};
+    delete $self->{'values'};
+    delete $self->{'raw_values'};
+    $self->{'raw_fetched'} = {};
     $self->{'fetched'} = {};
     $self->{'decoded'} = {};
 
@@ -1759,7 +1807,7 @@
 sub unload_value {
     my $self = shift;
     my $column = shift;
-    delete $self->{$_}{$column} for qw/values fetched decoded _prefetched/;
+    delete $self->{$_}{$column} for qw/values raw_values fetched raw_fetched decoded _prefetched/;
 }
 
 1;

Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Cachable.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Cachable.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Cachable.pm	Fri Jan 23 18:20:52 2009
@@ -245,6 +245,8 @@
             table   => $self->table,
             fetched => $self->{'fetched'},
             decoded => $self->{'decoded'},
+            raw_values => $self->{'raw_values'},
+            raw_fetched => $self->{'raw_fetched'},
         }
     );
 }

Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Memcached.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Memcached.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Memcached.pm	Fri Jan 23 18:20:52 2009
@@ -182,7 +182,9 @@
     $MEMCACHED->set( $self->_primary_cache_key,
         {   values  => $self->{'values'},
             table   => $self->table,
-            fetched => $self->{'fetched'}
+            fetched => $self->{'fetched'},
+            raw_values => $self->{'raw_values'},
+            raw_fetched => $self->{'raw_fetched'},
         },
         $self->_cache_config->{'cache_for_sec'}
     );

Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Plugin.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Plugin.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Record/Plugin.pm	Fri Jan 23 18:20:52 2009
@@ -176,8 +176,8 @@
     my $self = shift;
     my $caller = caller;
     for ($self->columns) {
-            $caller->COLUMNS->{$_->name} = $_ ;
-            $caller->_init_methods_for_column($_);
+        $caller->_init_methods_for_column($_);
+        $caller->COLUMNS->{ $_->name } = $_ unless $_->virtual;
     }
     $self->export_to_level(1,undef);
     
@@ -186,7 +186,7 @@
     }
 
     if (my $triggers_for_column =  $self->can('register_triggers_for_column') ) {
-        for my $column (map { $_->name } $caller->columns) {
+        for my $column (keys %{$caller->_columns_hashref}) {
             $triggers_for_column->($caller, $column)
         }
     }

Added: Jifty-DBI/branches/tisql/t/05raw_value.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/branches/tisql/t/05raw_value.t	Fri Jan 23 18:20:52 2009
@@ -0,0 +1,113 @@
+#!/usr/bin/env perl -w
+
+use strict;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 14;
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+use DateTime ();
+
+foreach my $d ( @available_drivers ) {
+SKIP: {
+        unless( has_schema( 'TestApp::User', $d ) ) {
+                skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+        }
+        unless( should_test( $d ) ) {
+                skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+        }
+        diag("start testing with '$d' handle") if $ENV{TEST_VERBOSE};
+
+        my $handle = get_handle( $d );
+        connect_handle( $handle );
+        isa_ok($handle->dbh, 'DBI::db');
+
+        {my $ret = init_schema( 'TestApp::User', $handle );
+        isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back" );}
+
+        my $rec = TestApp::User->new( handle => $handle );
+        isa_ok($rec, 'Jifty::DBI::Record');
+
+        use URI;
+        my $uri = URI->new( 'http://bestpractical.com/foo' );
+        my ($id) = $rec->create(uri => $uri);
+        ok($id, "Successfuly created a user");
+        ok($rec->load($id), "Loaded the record");
+        is($rec->id, $id, "The record has its id");
+
+        isa_ok( $rec->uri, 'URI' );
+        is($rec->uri->as_string, $uri, "Corrent uri");
+        is($rec->__raw_value( 'uri' ), $uri->as_string, 'Correct raw uri' );
+
+        # undef/NULL
+        $rec->set_uri;
+        is($rec->uri, undef, "Set undef value" );
+        is($rec->__raw_value( 'uri' ), undef, 'Correct raw uri' );
+
+        my $new_uri = 'http://jifty.org/bar';
+        $rec->set_uri( $new_uri );
+        isa_ok( $rec->uri, 'URI' );
+        is($rec->uri->as_string, $new_uri, "The record has its id");
+        is($rec->__raw_value( 'uri' ), $new_uri, 'Correct raw value' );
+
+        cleanup_schema( 'TestApp', $handle );
+        disconnect_handle( $handle );
+}
+}
+
+package TestApp::User;
+use base qw/Jifty::DBI::Record/;
+
+1;
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE table users (
+        id integer primary key,
+        uri varchar(64)
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id integer auto_increment primary key,
+        uri varchar(64)
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id serial primary key,
+        uri varchar(64)
+)
+EOF
+
+}
+
+BEGIN {
+    use Jifty::DBI::Schema;
+
+    use Jifty::DBI::Record schema {
+    column uri =>
+      type is 'text',
+      filters are qw/Jifty::DBI::Filter::URI/,
+      default is undef;
+    }
+}
+
+1;
+


More information about the Jifty-commit mailing list