[Bps-public-commit] dbix-searchbuilder branch, set-undef-fix, created. 1.63-10-gd25a817

Ruslan Zakirov ruz at bestpractical.com
Sat Feb 2 07:46:01 EST 2013


The branch, set-undef-fix has been created
        at  d25a8176979eb3cfb118933df43b33a3064cb437 (commit)

- Log -----------------------------------------------------------------
commit 8333019c15a4afa49475f76f1ecee9b895e7496e
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat Feb 2 16:32:56 2013 +0400

    update change log, forgot to do it before 1.63

diff --git a/Changes b/Changes
index 2d5b7aa..3c0b285 100755
--- a/Changes
+++ b/Changes
@@ -1,5 +1,10 @@
 Revision history for Perl extension DBIx::SearchBuilder.
 
+1.63 Fri Sep 14 2012 01:19:38 GMT+0400 (MSK)
+
+* joins_are_distinct hint to indicate that distinct is not
+  required for the current set of joins.
+
 1.62 Mon Mar 26 09:31:05 UTC 2012
 
 * Bind values were ignored in SimpleUpdateFromSelect

commit 7d32afa2c80b8b7231c01a2da0adc3251e780d25
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu May 26 11:23:48 2011 +0800

    allow undef set to store NULLs

diff --git a/lib/DBIx/SearchBuilder/Record.pm b/lib/DBIx/SearchBuilder/Record.pm
index a8d188c..edc752c 100755
--- a/lib/DBIx/SearchBuilder/Record.pm
+++ b/lib/DBIx/SearchBuilder/Record.pm
@@ -782,17 +782,14 @@ sub __Set {
         }
     }
 
-    unless ( defined $args{'Value'} ) {
-        $ret->as_array( 0, "No value passed to _Set" );
-        $ret->as_error(
-            errno        => 2,
-            do_backtrace => 0,
-            message      => "No value passed to _Set"
-        );
-        return ( $ret->return_value );
-    }
-    elsif (    ( defined $self->__Value($column) )
-        and ( $args{'Value'} eq $self->__Value($column) ) )
+    my $current_value = $self->__Value($column);
+
+    if (
+        ( !defined $args{'Value'} && !defined $current_value )
+        || (   defined $args{'Value'}
+            && defined $current_value
+            && ( $args{'Value'} eq $current_value ) )
+      )
     {
         $ret->as_array( 0, "That is already the current value" );
         $ret->as_error(
@@ -844,8 +841,10 @@ sub __Set {
 
     my $val = $self->_Handle->UpdateRecordValue(%args);
     unless ($val) {
-        my $message = 
-            $args{'Column'} . " could not be set to " . $args{'Value'} . "." ;
+        my $message =
+            $args{'Column'}
+          . " could not be set to "
+          . ( defined $args{'Value'} ? $args{'Value'} : 'undef' ) . ".";
         $ret->as_array( 0, $message);
         $ret->as_error(
             errno        => 4,

commit 49e24aacca974f4fd7cc0a5a702855d029cbaea5
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu May 26 11:58:33 2011 +0800

    update test as we can set null now

diff --git a/t/01records.t b/t/01records.t
index 1730645..a1f1366 100644
--- a/t/01records.t
+++ b/t/01records.t
@@ -7,7 +7,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 67;
+use constant TESTS_PER_DRIVER => 66;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -194,11 +194,9 @@ SKIP: {
 	isa_ok( $val, 'Class::ReturnValue', "couldn't set invalid value, error returned");
 	is( ($val->as_array)[1], 'Illegal value for Name', "correct error message" );
 	is( $rec->Name, 'Obra', "old value is still there");
-# XXX TODO FIXME: this test cover current implementation that is broken //RUZ
-	$val = $rec->SetName( );
-	isa_ok( $val, 'Class::ReturnValue', "couldn't set empty/undef value, error returned");
-	is( ($val->as_array)[1], "No value passed to _Set", "correct error message" );
-	is( $rec->Name, 'Obra', "old value is still there");
+	( $val, $msg ) = $rec->SetName();
+    ok( $val, $msg );
+	is( $rec->Name, undef, "no value means null");
 
 # deletes
 	$newrec = TestApp::Address->new($handle);
@@ -231,6 +229,7 @@ sub _Init {
 sub ValidateName
 {
 	my ($self, $value) = @_;
+    return 1 unless defined $value;
 	return 0 if $value =~ /invalid/i;
 	return 1;
 }
diff --git a/t/02records_integers.t b/t/02records_integers.t
index a93a329..28d61cd 100644
--- a/t/02records_integers.t
+++ b/t/02records_integers.t
@@ -63,19 +63,17 @@ SKIP: {
         ok($status, "status ok") or diag $status->error_message;
         is($rec->Optional, 1, 'set optional field to 1');
 
+        $status = $rec->SetOptional( undef );
+        ok($status, "status ok") or diag $status->error_message;
+        is($rec->Optional, undef, 'undef equal to NULL');
+
         $status = $rec->SetOptional( '' );
         ok($status, "status ok") or diag $status->error_message;
         is($rec->Optional, 0, 'empty string should be threated as zero');
 
-        TODO: {
-            local $TODO = 'we have no way to set NULL value';
-            $status = $rec->SetOptional( undef );
-            ok($status, "status ok") or diag $status->error_message;
-            is($rec->Optional, undef, 'undef equal to NULL');
-            $status = $rec->SetOptional;
-            ok($status, "status ok") or diag $status->error_message;
-            is($rec->Optional, undef, 'no value is NULL too');
-        }
+        $status = $rec->SetOptional;
+        ok($status, "status ok") or diag $status->error_message;
+        is($rec->Optional, undef, 'no value is NULL too');
 
         # set operations on mandatory field
         $status = $rec->SetMandatory( 2 );

commit 2f942470264a2a228a238e179a5c0f664ef60a3f
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Mon May 30 09:27:11 2011 +0800

    fallback to default value if we are setting undef to a not-null field

diff --git a/lib/DBIx/SearchBuilder/Record.pm b/lib/DBIx/SearchBuilder/Record.pm
index edc752c..8351503 100755
--- a/lib/DBIx/SearchBuilder/Record.pm
+++ b/lib/DBIx/SearchBuilder/Record.pm
@@ -781,6 +781,11 @@ sub __Set {
             $args{'Value'} = 0;
         }
     }
+    else {
+        if ( $self->_Accessible( $args{Column}, 'no_nulls' ) ) {
+            $args{'Value'} = $self->_Accessible( $args{Column}, 'default' );
+        }
+    }
 
     my $current_value = $self->__Value($column);
 
diff --git a/t/02records_integers.t b/t/02records_integers.t
index 28d61cd..c886adf 100644
--- a/t/02records_integers.t
+++ b/t/02records_integers.t
@@ -80,20 +80,17 @@ SKIP: {
         ok($status, "status ok") or diag $status->error_message;
         is($rec->Mandatory, 2, 'set optional field to 2');
 
+        $status = $rec->SetMandatory( undef );
+        ok($status, "status ok") or diag $status->error_message;
+        is($rec->Mandatory, 1, 'fallback to default');
+
         $status = $rec->SetMandatory( '' );
         ok($status, "status ok") or diag $status->error_message;
         is($rec->Mandatory, 0, 'empty string should be threated as zero');
 
-        TODO: {
-            local $TODO = 'fallback to default value'
-                .' if field is NOT NULL and we try set it to NULL';
-            $status = $rec->SetMandatory( undef );
-            ok($status, "status ok") or diag $status->error_message;
-            is($rec->Mandatory, 1, 'fallback to default');
-            $status = $rec->SetMandatory;
-            ok($status, "status ok") or diag $status->error_message;
-            is($rec->Mandatory, 1, 'no value on set also fallback');
-        }
+        $status = $rec->SetMandatory;
+        ok($status, "status ok") or diag $status->error_message;
+        is($rec->Mandatory, 1, 'no value on set also fallback');
     }
 
     cleanup_schema( 'TestApp::Address', $handle );

commit 56131a6a480c59eda020e4c9ba19a7697f9e110a
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue May 31 00:24:41 2011 +0800

    test null set when the old value is already null

diff --git a/t/02records_integers.t b/t/02records_integers.t
index c886adf..5cf3e1c 100644
--- a/t/02records_integers.t
+++ b/t/02records_integers.t
@@ -6,7 +6,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 33;
+use constant TESTS_PER_DRIVER => 36;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -75,6 +75,15 @@ SKIP: {
         ok($status, "status ok") or diag $status->error_message;
         is($rec->Optional, undef, 'no value is NULL too');
 
+        $status = $rec->SetOptional;
+        ok(!$status, 'same null value set');
+        is(
+            ( $status->as_array )[1],
+            "That is already the current value",
+            "correct error message"
+        );
+        is($rec->Optional, undef, 'no value is NULL too');
+
         # set operations on mandatory field
         $status = $rec->SetMandatory( 2 );
         ok($status, "status ok") or diag $status->error_message;

commit 8d14de09b56a8adff227f087217b38261fbeca2b
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue May 31 00:32:43 2011 +0800

    test warning when setting a feild from null to not-null

diff --git a/t/02records_integers.t b/t/02records_integers.t
index 5cf3e1c..d13e2ee 100644
--- a/t/02records_integers.t
+++ b/t/02records_integers.t
@@ -6,7 +6,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 36;
+use constant TESTS_PER_DRIVER => 37;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -67,9 +67,17 @@ SKIP: {
         ok($status, "status ok") or diag $status->error_message;
         is($rec->Optional, undef, 'undef equal to NULL');
 
-        $status = $rec->SetOptional( '' );
-        ok($status, "status ok") or diag $status->error_message;
-        is($rec->Optional, 0, 'empty string should be threated as zero');
+        {
+            my $warn;
+            local $SIG{__WARN__} = sub {
+                $warn++;
+                warn @_;
+            };
+            $status = $rec->SetOptional('');
+            ok( $status, "status ok" ) or diag $status->error_message;
+            is( $rec->Optional, 0, 'empty string should be threated as zero' );
+            ok( !$warn, 'no warning to set value from null to not-null' );
+        }
 
         $status = $rec->SetOptional;
         ok($status, "status ok") or diag $status->error_message;

commit ac43c99dc54ccca251ef7c2229d063f3c38b3290
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue May 31 01:33:51 2011 +0800

    return error directly if set undef to a NOT NULL field which lacks DEFAULT

diff --git a/lib/DBIx/SearchBuilder/Record.pm b/lib/DBIx/SearchBuilder/Record.pm
index 8351503..8817260 100755
--- a/lib/DBIx/SearchBuilder/Record.pm
+++ b/lib/DBIx/SearchBuilder/Record.pm
@@ -783,7 +783,20 @@ sub __Set {
     }
     else {
         if ( $self->_Accessible( $args{Column}, 'no_nulls' ) ) {
-            $args{'Value'} = $self->_Accessible( $args{Column}, 'default' );
+            my $default = $self->_Accessible( $args{Column}, 'default' );
+
+            if ( defined $default ) {
+                $args{'Value'} = $default;
+            }
+            else {
+                $ret->as_array( 0, 'Illegal value for ' . $args{'Column'} );
+                $ret->as_error(
+                    errno        => 3,
+                    do_backtrace => 0,
+                    message      => "Illegal value for " . $args{'Column'}
+                );
+                return ( $ret->return_value );
+            }
         }
     }
 

commit f73cad436ceeff550f3825884942d48e1918cd4b
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue May 31 01:39:08 2011 +0800

    20set_edge_cases for more edge case tests
    
    test of setting undef/empty string to a NOT NULL field without DEFAULT

diff --git a/t/20set_edge_cases.t b/t/20set_edge_cases.t
new file mode 100644
index 0000000..39476d0
--- /dev/null
+++ b/t/20set_edge_cases.t
@@ -0,0 +1,141 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@AvailableDrivers);
+
+use constant TESTS_PER_DRIVER => 20;
+
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+foreach my $d (@AvailableDrivers) {
+  SKIP: {
+        unless ( has_schema( 'TestApp::Address', $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;
+        }
+
+        my $handle = get_handle($d);
+        connect_handle($handle);
+        isa_ok( $handle->dbh, 'DBI::db' );
+
+        my $ret = init_schema( 'TestApp::Address', $handle );
+        isa_ok( $ret, 'DBI::st',
+            "Inserted the schema. got a statement handle back" );
+
+        my $rec = TestApp::Address->new($handle);
+        my ($id) = $rec->Create( Name => 'foo', Number => 3 );
+        ok( $id,             "Created record " . $id );
+        ok( $rec->Load($id), "Loaded the record" );
+
+        is( $rec->Name,   'foo', "name is foo" );
+        is( $rec->Number, 3,     "number is 3" );
+
+        my ( $val, $msg ) = $rec->SetName('bar');
+        ok( $val, $msg );
+        is( $rec->Name, 'bar', "name is changed to bar" );
+
+        ( $val, $msg ) = $rec->SetName(undef);
+        ok( !$val, $msg );
+        is( $msg, 'Illegal value for Name', 'error message' );
+        is( $rec->Name, 'bar', 'name is still bar' );
+
+        ( $val, $msg ) = $rec->SetName('');
+        ok( $val, $msg );
+        is( $rec->Name, '', "name is changed to ''" );
+
+        ( $val, $msg ) = $rec->SetNumber(42);
+        ok( $val, $msg );
+        is( $rec->Number, 42, 'number is changed to 42' );
+
+        ( $val, $msg ) = $rec->SetNumber(undef);
+        ok( !$val, $msg );
+        is( $msg, 'Illegal value for Number', 'error message' );
+        is( $rec->Number, 42, 'number is still 42' );
+
+        ( $val, $msg ) = $rec->SetNumber('');
+        ok( $val, $msg );
+        is( $rec->Number, 0, 'empty string implies 0 for integer field' );
+
+        cleanup_schema( 'TestApp::Address', $handle );
+    }
+}
+
+1;
+
+package TestApp::Address;
+
+use base $ENV{SB_TEST_CACHABLE}
+  ? qw/DBIx::SearchBuilder::Record::Cachable/
+  : qw/DBIx::SearchBuilder::Record/;
+
+sub _Init {
+    my $self   = shift;
+    my $handle = shift;
+    $self->Table('Address');
+    $self->_Handle($handle);
+}
+
+sub _ClassAccessible {
+
+    {
+        id     => { read => 1, type  => 'int(11)', },
+        Name   => { read => 1, write => 1, type => 'varchar(14)', no_nulls => 1 },
+        Number => { read => 1, write => 1, type => 'int(8)', no_nulls => 1 },
+    };
+}
+
+sub schema_mysql {
+    <<EOF;
+CREATE TEMPORARY TABLE Address (
+        id integer AUTO_INCREMENT,
+        Name varchar(36) NOT NULL,
+        Number int(8) NOT NULL,
+  	PRIMARY KEY (id))
+EOF
+
+}
+
+sub schema_pg {
+    <<EOF;
+CREATE TEMPORARY TABLE Address (
+        id serial PRIMARY KEY,
+        Name varchar(36) NOT NULL,
+        Number integer NOT NULL
+)
+EOF
+
+}
+
+sub schema_sqlite {
+
+    <<EOF;
+CREATE TABLE Address (
+        id  integer primary key,
+        Name varchar(36) NOT NULL,
+        Number int(8) NOT NULL)
+EOF
+
+}
+
+sub schema_oracle {
+    [
+        "CREATE SEQUENCE Address_seq",
+        "CREATE TABLE Address (
+        id integer CONSTRAINT Address_Key PRIMARY KEY,
+        Name varchar(36) NOT NULL,
+        Number integer NOT NULL
+        )",
+    ];
+}
+
+sub cleanup_schema_oracle {
+    [ "DROP SEQUENCE Address_seq", "DROP TABLE Address", ];
+}
+
+1;

commit 580512cdfaaf5c98ab6f319adc7f7c0c7f86e1f0
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat Feb 2 16:32:22 2013 +0400

    return way to get old behaviour

diff --git a/lib/DBIx/SearchBuilder/Record.pm b/lib/DBIx/SearchBuilder/Record.pm
index 8817260..107cdac 100755
--- a/lib/DBIx/SearchBuilder/Record.pm
+++ b/lib/DBIx/SearchBuilder/Record.pm
@@ -773,6 +773,17 @@ sub __Set {
     }
     my $column = lc $args{'Column'};
 
+    # XXX: OLD behaviour, no_undefs_in_set will go away
+    if ( !defined $args{'Value'} && $self->{'no_undefs_in_set' } ) {
+        $ret->as_array( 0, "No value passed to _Set" );
+        $ret->as_error(
+            errno        => 2,
+            do_backtrace => 0,
+            message      => "No value passed to _Set"
+        );
+        return ( $ret->return_value );
+    }
+
     if ( defined $args{'Value'} ) {
         if ( $args{'Value'} eq '' &&
              ( $self->_Accessible( $args{'Column'}, 'is_numeric' )

commit d25a8176979eb3cfb118933df43b33a3064cb437
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat Feb 2 16:45:14 2013 +0400

    update changelog

diff --git a/Changes b/Changes
index 3c0b285..f44c1f4 100755
--- a/Changes
+++ b/Changes
@@ -1,5 +1,12 @@
 Revision history for Perl extension DBIx::SearchBuilder.
 
+1.64
+
+* _Set now can take undef as argument to mean default or NULL.
+  Still may result in error if default is not defined and no_nulls
+  is true for the column. If old behaviour is required set
+  $record->{'no_undefs_in_set'} to true value.
+
 1.63 Fri Sep 14 2012 01:19:38 GMT+0400 (MSK)
 
 * joins_are_distinct hint to indicate that distinct is not

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list