[Jifty-commit] r4147 - in Jifty-DBI/trunk: . lib/Jifty/DBI

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Sep 24 18:46:35 EDT 2007


Author: clkao
Date: Mon Sep 24 18:46:34 2007
New Revision: 4147

Added:
   Jifty-DBI/trunk/t/case_sensitivity.t
Modified:
   Jifty-DBI/trunk/MANIFEST
   Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm

Log:
* New column attribute: is case_sensitive
* Make load_by_cols respect column case sensitivity.

  NOTE this is a behaviour change as columns are by default
  case-insensitive, and the call was doing a case sensitive
  search, which is different from Collection searching.

  To make Jifty::DBI load_by_cols work in the old behaviour:

  Use $rec->load_by_cols( name => { value => 'foobar',
                                    case_sensitive => 0,
                                    operator => '=' });


Modified: Jifty-DBI/trunk/MANIFEST
==============================================================================
--- Jifty-DBI/trunk/MANIFEST	(original)
+++ Jifty-DBI/trunk/MANIFEST	Mon Sep 24 18:46:34 2007
@@ -81,6 +81,7 @@
 t/15types.t
 t/16inheritance.t
 t/17virtualtypes.t
+t/case_sensitivity.t
 t/metadata.t
 t/pod-coverage.t
 t/pod.t

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm	Mon Sep 24 18:46:34 2007
@@ -1065,14 +1065,17 @@
 
     # If it's a new value or we're overwriting this sort of restriction,
 
+    # XXX: when is column_obj undefined?
+    my $column_obj = $self->new_item()->column( $args{column} );
+    my $case_sensitive = $column_obj ? $column_obj->case_sensitive : 0;
+    $case_sensitive = $args{'case_sensitive'} if defined $args{'case_sensitive'};
     if (   $self->_handle->case_sensitive
         && defined $args{'value'}
         && $args{'quote_value'}
-        && !$args{'case_sensitive'} )
+        && !$case_sensitive )
     {
 
         # don't worry about case for numeric columns_in_db
-        my $column_obj = $self->new_item()->column( $args{column} );
         if ( defined $column_obj ? $column_obj->is_string : 1 ) {
             ( $qualified_column, $args{'operator'}, $args{'value'} )
                 = $self->_handle->_make_clause_case_insensitive(

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	Mon Sep 24 18:46:34 2007
@@ -27,6 +27,7 @@
     _checked_for_validate_sub
     record_class
     attributes
+    case_sensitive
     /;
 
 # these actually live in the attributes hash

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	Mon Sep 24 18:46:34 2007
@@ -939,10 +939,14 @@
             my $op;
             my $value;
             my $function = "?";
+            my $column_obj = $self->column( $key );
+            my $case_sensitive = $column_obj->case_sensitive;
             if ( ref $hash{$key} eq 'HASH' ) {
                 $op       = $hash{$key}->{operator};
                 $value    = $hash{$key}->{value};
                 $function = $hash{$key}->{function} || "?";
+                $case_sensitive = $hash{$key}->{case_sensitive}
+                    if exists $hash{$key}->{case_sensitive};
             } else {
                 $op    = '=';
                 $value = $hash{$key};
@@ -953,6 +957,13 @@
                 $value = $value->id;
             }
 
+            # if the handle is in a case_sensitive world and we need to make
+            # a case-insensitive query
+            if ( $self->_handle->case_sensitive && $value ) {
+                if ( $column_obj->is_string && !$case_sensitive ) {
+                    ( $key, $op, $function ) = $self->_handle->_make_clause_case_insensitive( $key, $op, $function );
+        	}
+            }
 
             push @phrases, "$key $op $function";
             push @bind,    $value;

Added: Jifty-DBI/trunk/t/case_sensitivity.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/t/case_sensitivity.t	Mon Sep 24 18:46:34 2007
@@ -0,0 +1,109 @@
+#!/usr/bin/env perl -w
+
+use strict;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 9;
+
+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');
+
+        my ($id) = $rec->create( name => 'Foobar', interests => 'Slacking' );
+        ok($id, "Successfuly created ticket");
+
+	$rec->load_by_cols( name => 'foobar');
+	is($rec->id, undef);
+
+	$rec->load_by_cols( name => { value => 'foobar', case_sensitive => 0, operator => '=' });
+	is($rec->id, $id);
+
+	$rec->load_by_cols( name => 'Foobar');
+	is($rec->id, $id);
+
+	$rec->load_by_cols( interests => 'slacking');
+	is($rec->id, $id);;
+
+	$rec->load_by_cols( interests => 'Slacking');
+	is($rec->id, $id);;
+
+        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,
+        name varchar,
+        interests varchar
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id integer auto_increment primary key,
+        name varchar,
+        interests varchar
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id serial primary key,
+        name varchar,
+        interests varchar
+)
+EOF
+
+}
+
+use Jifty::DBI::Schema;
+
+use Jifty::DBI::Record schema {
+    column name      => type is 'varchar', label is 'Name', is case_sensitive;
+    column interests => type is 'varchar';
+};
+
+
+1;
+


More information about the Jifty-commit mailing list