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

Jifty commits jifty-commit at lists.jifty.org
Fri Feb 27 04:46:03 EST 2009


Author: ruz
Date: Fri Feb 27 04:46:00 2009
New Revision: 6518

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/SchemaGenerator.pm
   Jifty-DBI/branches/tisql/t/02records_cachable.t
   Jifty-DBI/branches/tisql/t/06filter_boolean.t

Log:
* sync from trunk
 r6309 at ruslan-zakirovs-computer (orig r6284):  alexmv | 2009-02-03 01:48:21 +0300
  r41336 at kohr-ah:  chmrr | 2009-02-02 15:39:19 -0500
   * Perltidy, primarily for indentation fixes
 
 r6310 at ruslan-zakirovs-computer (orig r6285):  alexmv | 2009-02-03 01:48:30 +0300
  r41337 at kohr-ah:  chmrr | 2009-02-02 17:47:32 -0500
   * Collapse fetched and raw_fetched; fetched wasn't being properly set, leaving to lack of decoding when reading from cache
 
 r6311 at ruslan-zakirovs-computer (orig r6286):  sunnavy | 2009-02-03 05:58:37 +0300
 when fetched is set, we need to set raw_values too
 r6312 at ruslan-zakirovs-computer (orig r6287):  sunnavy | 2009-02-03 06:01:50 +0300
 already set fetched above, no need to set it again
 r6369 at ruslan-zakirovs-computer (orig r6344):  alexmv | 2009-02-18 02:18:40 +0300
  r42154 at kohr-ah:  chmrr | 2009-02-17 18:18:04 -0500
   * apply filters to "default is ..." values, so "is boolean, default is 0" works on pg, for instance
 


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 Feb 27 04:46:00 2009
@@ -880,7 +880,6 @@
 
 This performs the join.
 
-
 =cut
 
 sub join {

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 Feb 27 04:46:00 2009
@@ -787,9 +787,9 @@
     # 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->{'fetched'}{$column_name};
 
-    if ( !$self->{'raw_fetched'}{$column_name} and my $id = $self->id() ) {
+    if ( !$self->{'fetched'}{$column_name} and my $id = $self->id() ) {
         my $pkey = $self->_primary_key();
         my $query_string =
             "SELECT "
@@ -800,7 +800,7 @@
         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;
+        $self->{'fetched'}{$column_name} = 1;
     }
 
     return $self->{'raw_values'}{$column_name};
@@ -842,15 +842,17 @@
 
     my $column_name = $column->name;
 
-
     # In the default case of "yeah, we have a value", return it as
     # fast as we can.
     return $self->{'values'}{$column_name}
         if ( $self->{'fetched'}{$column_name}
         && $self->{'decoded'}{$column_name} );
 
-    $self->{'values'}{$column_name}  = $self->__raw_value( $column_name )
-        unless $self->{'fetched'}{$column_name};
+    unless ($self->{'fetched'}{$column_name}) {
+        # Fetch it, and mark it as not decoded
+        $self->{'values'}{$column_name} = $self->__raw_value( $column_name );
+        $self->{'decoded'}{$column_name} = 0;
+    }
 
     unless ( $self->{'decoded'}{$column_name} ) {
         $self->_apply_output_filters(
@@ -1203,9 +1205,8 @@
     }
 
     $self->{'values'} = {};
-    $self->{'fetched'} = {};
     $self->{'raw_values'} = {};
-    $self->{'raw_fetched'} = {};
+    $self->{'fetched'} = {};
 
     foreach my $col ( grep exists $hashref->{ lc $_ }, map $_->name, $self->columns ) {
         $self->{'fetched'}{$col} = 1;
@@ -1236,7 +1237,6 @@
     my $hashref = $sth->fetchrow_hashref;
     delete $self->{'values'};
     delete $self->{'raw_values'};
-    $self->{'raw_fetched'} = {};
     $self->{'fetched'} = {};
     $self->{'decoded'} = {};
 
@@ -1245,6 +1245,7 @@
         next unless exists $hashref->{ lc($col) };
         $self->{'fetched'}{$col} = 1;
         $self->{'values'}->{$col} = $hashref->{ lc($col) };
+        $self->{'raw_values'}->{$col} = $hashref->{ lc($col) };
     }
     if ( !$self->{'values'} && $sth->err ) {
         return ( 0, "Couldn't fetch row: " . $sth->err );
@@ -1261,9 +1262,6 @@
         return ( 0, "Missing a primary key?" );
     }
 
-    foreach my $f ( keys %{ $self->{'values'} } ) {
-        $self->{'fetched'}{$f} = 1;
-    }
     return ( 1, "Found object" );
 
 }
@@ -1807,7 +1805,7 @@
 sub unload_value {
     my $self = shift;
     my $column = shift;
-    delete $self->{$_}{$column} for qw/values raw_values fetched raw_fetched decoded _prefetched/;
+    delete $self->{$_}{$column} for qw/values raw_values 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 Feb 27 04:46:00 2009
@@ -53,8 +53,9 @@
 }
 
 sub _key_cache {
-    my $self  = shift;
-    my $cache = $self->_handle->dsn
+    my $self = shift;
+    my $cache
+        = $self->_handle->dsn
         . "-KEYS--"
         . ( $self->{'_class'} || $self->table );
     $self->_setup_cache($cache) unless exists( $_CACHES{$cache} );
@@ -69,8 +70,9 @@
 =cut
 
 sub _flush_key_cache {
-    my $self  = shift;
-    my $cache = $self->_handle->dsn
+    my $self = shift;
+    my $cache
+        = $self->_handle->dsn
         . "-KEYS--"
         . ( $self->{'_class'} || $self->table );
     $self->_setup_cache($cache);
@@ -91,7 +93,6 @@
 
 =cut
 
-
 sub load_from_hash {
     my $self = shift;
 
@@ -127,15 +128,14 @@
     if ( ref($class) ) {
         ( $self, $class ) = ( $class, undef );
     } else {
-        $self = $class->new(
-            handle => ( delete $attr{'_handle'} || undef ) );
+        $self = $class->new( handle => ( delete $attr{'_handle'} || undef ) );
     }
 
     ## Generate the cache key
     my $alt_key = $self->_gen_record_cache_key(%attr);
     if ( $self->_fetch($alt_key) ) {
-        if ($class) { return $self }
-        else { return ( 1, "Fetched from cache" ) }
+        if   ($class) { return $self }
+        else          { return ( 1, "Fetched from cache" ) }
     }
 
     # Blow away the primary cache key since we're loading.
@@ -191,9 +191,10 @@
 
 sub _expire (\$) {
     my $self = shift;
-    $self->_record_cache->set( $self->_primary_record_cache_key, undef, time - 1 );
+    $self->_record_cache->set( $self->_primary_record_cache_key,
+        undef, time - 1 );
 
-    # We should be doing something more surgical to clean out the key cache. but we do need to expire it
+# We should be doing something more surgical to clean out the key cache. but we do need to expire it
     $self->_flush_key_cache;
 
 }
@@ -206,22 +207,20 @@
 
 sub _fetch () {
     my ( $self, $cache_key ) = @_;
-        # If the alternate key is really the primary one
-       
-        
-        my $data = $self->_record_cache->fetch($cache_key);
-
-  unless ($data) {
-    $cache_key = $self->_key_cache->fetch( $cache_key );
-    $data = $self->_record_cache->fetch( $cache_key ) if $cache_key;
-  }
 
-  return undef unless ($data);
+    # If the alternate key is really the primary one
 
-  @{$self}{ keys %$data } = values %$data;    # deserialize
-  return 1;
+    my $data = $self->_record_cache->fetch($cache_key);
+
+    unless ($data) {
+        $cache_key = $self->_key_cache->fetch($cache_key);
+        $data = $self->_record_cache->fetch($cache_key) if $cache_key;
+    }
 
+    return undef unless ($data);
 
+    @{$self}{ keys %$data } = values %$data;    # deserialize
+    return 1;
 }
 
 #sub __value {
@@ -240,18 +239,17 @@
 
 sub _store (\$) {
     my $self = shift;
-    $self->_record_cache->set( $self->_primary_record_cache_key,
-        {   values  => $self->{'values'},
-            table   => $self->table,
-            fetched => $self->{'fetched'},
-            decoded => $self->{'decoded'},
-            raw_values => $self->{'raw_values'},
-            raw_fetched => $self->{'raw_fetched'},
+    $self->_record_cache->set(
+        $self->_primary_record_cache_key,
+        {   values      => $self->{'values'},
+            table       => $self->table,
+            fetched     => $self->{'fetched'},
+            decoded     => $self->{'decoded'},
+            raw_values  => $self->{'raw_values'},
         }
     );
 }
 
-
 # Function: _gen_record_cache_key
 # Type    : private instance
 # Args    : hash (attr)
@@ -259,26 +257,25 @@
 # Desc    : Takes a perl hash and generates a key from it.
 
 sub _gen_record_cache_key {
-  my ( $self, %attr ) = @_;
+    my ( $self, %attr ) = @_;
 
-  my @cols;
+    my @cols;
 
-  while ( my ( $key, $value ) = each %attr ) {
-    unless ( defined $value ) {
-      push @cols, lc($key) . '=__undef';
-    }
-    elsif ( ref($value) eq "HASH" ) {
-      push @cols, lc($key) . ( $value->{operator} || '=' )
-          . defined $value->{value}? $value->{value}: '__undef';
-    }
-    elsif ( blessed $value and $value->isa('Jifty::DBI::Record') ) {
-      push @cols, lc($key) . '=' . ( $value->id );
-    }
-    else {
-      push @cols, lc($key) . "=" . $value;
+    while ( my ( $key, $value ) = each %attr ) {
+        unless ( defined $value ) {
+            push @cols, lc($key) . '=__undef';
+        } elsif ( ref($value) eq "HASH" ) {
+            push @cols,
+                  lc($key)
+                . ( $value->{operator} || '=' )
+                . defined $value->{value} ? $value->{value} : '__undef';
+        } elsif ( blessed $value and $value->isa('Jifty::DBI::Record') ) {
+            push @cols, lc($key) . '=' . ( $value->id );
+        } else {
+            push @cols, lc($key) . "=" . $value;
+        }
     }
-  }
-  return ( $self->table() . ':' . join( ',', @cols ) );
+    return ( $self->table() . ':' . join( ',', @cols ) );
 }
 
 # Function: _fetch_record_cache_key
@@ -306,13 +303,13 @@
 
         my @attributes;
         my %pk = $self->primary_keys;
-        while ( my ($key, $value) = each %pk ) {
+        while ( my ( $key, $value ) = each %pk ) {
             return unless defined $value;
-            push @attributes, lc( $key ) . '=' . $value;
+            push @attributes, lc($key) . '=' . $value;
         }
 
-        $self->{'_jifty_cache_pkey'} = $self->table .':'
-            . join ',', @attributes;
+        $self->{'_jifty_cache_pkey'} = $self->table . ':' . join ',',
+            @attributes;
     }
     return ( $self->{'_jifty_cache_pkey'} );
 

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 Feb 27 04:46:00 2009
@@ -184,7 +184,6 @@
             table   => $self->table,
             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/SchemaGenerator.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/SchemaGenerator.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/SchemaGenerator.pm	Fri Feb 27 04:46:00 2009
@@ -277,12 +277,25 @@
         next if (!$model->can('schema_version') or !defined $model->schema_version)
             and defined $column->till;
 
+        # Encode default values
+        my $default = $column->default;
+        if (defined $default) {
+            $model->_handle($self->handle);
+            $model->_apply_input_filters(
+                column    => $column,
+                value_ref => \$default,
+            );
+            $model->_handle(undef);
+        } else {
+            $default = '';
+        }
+
         push @cols,
             DBIx::DBSchema::Column->new(
             {   name     => $column->name,
                 type     => $column->type,
                 null     => $column->mandatory ? 0 : 1,
-                default  => $column->default ||'',
+                default  => $default,
             }
             );
 

Modified: Jifty-DBI/branches/tisql/t/02records_cachable.t
==============================================================================
--- Jifty-DBI/branches/tisql/t/02records_cachable.t	(original)
+++ Jifty-DBI/branches/tisql/t/02records_cachable.t	Fri Feb 27 04:46:00 2009
@@ -6,7 +6,7 @@
 use Test::More;
 BEGIN { require "t/utils.pl" }
 
-use constant TESTS_PER_DRIVER => 35;
+use constant TESTS_PER_DRIVER => 42;
 
 our (@available_drivers);
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
@@ -116,6 +116,30 @@
 
         Jifty::DBI::Record::Cachable->flush_cache;
 
+        {
+            my $rec = TestApp::Address->new( handle => $handle );
+            my ($id) = $rec->create( name => 'Metadata', metadata => { some => "values" } );
+            ok( $id, "Created record #$id" );
+
+            # Do a search, but only load the 'id' column
+            my $search = TestApp::AddressCollection->new( handle => $handle );
+            $search->columns(qw/id/);
+            $search->limit( column => 'name', value => 'Metadata');
+
+            $rec = $search->first;
+            is( $rec->id, $id, "The record has its id" );
+            is_deeply( $rec->metadata, { some => "values" } , "Got decoded values");
+
+            my $cache = TestApp::Address->new( handle => $handle );
+            my ( $status, $msg ) = $cache->load($id);
+            ok( $status, 'loaded record' );
+            is( $cache->id, $id, 'the same record as we created' );
+            is( $msg, 'Fetched from cache', 'we fetched record from cache' );
+            is_deeply( $cache->metadata, { some => "values" } , "Got decoded values");
+        }
+
+        Jifty::DBI::Record::Cachable->flush_cache;
+
         cleanup_schema( 'TestApp::Address', $handle );
         disconnect_handle($handle);
     }
@@ -134,6 +158,7 @@
         phone varchar(18),
         address varchar(50),
         employee_id int(8),
+        metadata text,
         PRIMARY KEY (id))
 EOF
 
@@ -146,7 +171,8 @@
         name varchar,
         phone varchar,
         address varchar,
-        employee_id integer
+        employee_id integer,
+        metadata text
 )
 EOF
 
@@ -160,7 +186,8 @@
         name varchar(36),
         phone varchar(18),
         address varchar(50),
-        employee_id int(8))
+        employee_id int(8),
+        metadata text)
 EOF
 
 }
@@ -171,7 +198,8 @@
         id integer CONSTRAINT addresses_key PRIMARY KEY,
         name varchar(36),
         phone varchar(18),
-        employee_id integer
+        employee_id integer,
+        metadata text
     )",
 ] }
 
@@ -197,6 +225,14 @@
         default is '';
 
     column employee_id => type is 'int(8)';
+
+    column metadata => type is 'text',
+        filters are 'Jifty::DBI::Filter::YAML';
     }
 }
+
+package TestApp::AddressCollection;
+use base qw/Jifty::DBI::Collection/;
+use constant table => "addresses";
+
 1;

Modified: Jifty-DBI/branches/tisql/t/06filter_boolean.t
==============================================================================
--- Jifty-DBI/branches/tisql/t/06filter_boolean.t	(original)
+++ Jifty-DBI/branches/tisql/t/06filter_boolean.t	Fri Feb 27 04:46:00 2009
@@ -6,7 +6,7 @@
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 136;
+use constant TESTS_PER_DRIVER => 139;
 
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -105,6 +105,9 @@
     ok($rec->load($id), 'loaded record');
     is($rec->id, $id, 'record id matches');
     is($rec->other_data, 0, 'default mandatory column is false, not undef');
+    is($rec->def_t, 1, 'default is correct if given as "t"');
+    is($rec->def_one, 1, 'default is correct if given as 1');
+    is($rec->def_zero, 0, 'default is correct if given as 0');
 
     $rec->set_other_data(1);
     is($rec->other_data, 1, 'mandatory column is now true');
@@ -126,7 +129,10 @@
 CREATE table users (
     id integer primary key,
     my_data boolean,
-    other_data boolean not null
+    other_data boolean not null,
+    def_t boolean default true,
+    def_one boolean default true,
+    def_zero boolean default true
 )
 EOF
 
@@ -138,7 +144,10 @@
 CREATE TEMPORARY table users (
     id integer auto_increment primary key,
     my_data boolean,
-    other_data boolean not null
+    other_data boolean not null,
+    def_t boolean default true,
+    def_one boolean default true,
+    def_zero boolean default false
 )
 EOF
 
@@ -150,7 +159,10 @@
 CREATE TEMPORARY table users (
     id serial primary key,
     my_data boolean,
-    other_data boolean not null
+    other_data boolean not null,
+    def_t boolean default true,
+    def_one boolean default true,
+    def_zero boolean default false
 )
 EOF
 
@@ -166,6 +178,18 @@
     column other_data =>
         is boolean,
         is mandatory;
+
+    column def_t =>
+        is boolean,
+        default is 't';
+
+    column def_one =>
+        is boolean,
+        default is 1;
+
+    column def_zero =>
+        is boolean,
+        default is 0;
     }
 }
 


More information about the Jifty-commit mailing list