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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jun 5 13:59:19 EDT 2006


Author: seanmil
Date: Mon Jun  5 13:59:17 2006
New Revision: 1162

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
   Jifty-DBI/trunk/t/02-column_constraints.t

Log:
 r359 at pc102:  sean | 2006-06-05 13:59:06 -0400
 * Cleanup 'distinct' column check and tests


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 Jun  5 13:59:17 2006
@@ -591,19 +591,8 @@
 
     # Implement 'is distinct' checking
     if ( $column->distinct ) {
-        my $new_record = $self->new( $self->_handle );
-        $new_record->load_by_cols ( $column->name => $args{'value'} );
-        if( $new_record->id ) {
-            $ret->as_array( 0, 'Value already exists for distinct column ' . 
-                $column->name );
-            $ret->as_error(
-                errno        => 3,
-                do_backtrace => 0,
-                message      => 'Value already exists for distinct column ' .
-                    $column->name,
-            );
-            return ( $ret->return_value );
-        }
+        my $ret = $self->is_distinct( $column->name, $args{'value'} );
+        return ( $ret ) if not ( $ret );
     }
 
     # The blob handling will destroy $args{'value'}. But we assign
@@ -879,20 +868,8 @@
 
         # Implement 'is distinct' checking
         if ( $column->distinct ) {
-            my $new_record = $self->new( $self->_handle );
-            $new_record->load_by_cols ( $column_name => $attribs{$column_name} );
-            if( $new_record->id ) {
-                my $ret = Class::ReturnValue->new();
-                $ret->as_array( 0, 'Value already exists for distinct column ' . 
-                    $column->name );
-                $ret->as_error(
-                    errno        => 3,
-                    do_backtrace => 0,
-                    message      => 'Value already exists for distinct column ' .
-                        $column->name,
-                );
-                return ( $ret->return_value );
-            }
+            my $ret = $self->is_distinct( $column_name, $attribs{$column_name} );
+            return ( $ret ) if not ( $ret );
         }
 
         if ( $column->type =~ /^(text|longtext|clob|blob|lob|bytea)$/i ) {
@@ -1113,6 +1090,39 @@
     }
 }
 
+=head2 is_distinct COLUMN_NAME, VALUE
+
+Checks to see if there is already a record in the database where
+COLUMN_NAME equals VALUE.  If no such record exists then the
+COLUMN_NAME and VALUE pair is considered distinct and it returns 1.
+If a value is already present the test is considered to have failed
+and it returns a L<Class::ReturnValue> with the error.
+
+=cut 
+
+sub is_distinct {
+    my $self = shift;
+    my $column = shift;
+    my $value = shift;
+
+    my $record = $self->new( $self->_handle );
+    $record->load_by_cols ( $column => $value );
+
+    my $ret = Class::ReturnValue->new();
+
+    if( $record->id ) {
+        $ret->as_array( 0, "Value already exists for unique column $column");
+        $ret->as_error(
+            errno        => 3,
+            do_backtrace => 0,
+            message      => "Value already exists for unique column $column",
+        );
+        return ( $ret->return_value );
+    } else {
+        return (1);
+    }
+}
+
 1;
 
 __END__

Modified: Jifty-DBI/trunk/t/02-column_constraints.t
==============================================================================
--- Jifty-DBI/trunk/t/02-column_constraints.t	(original)
+++ Jifty-DBI/trunk/t/02-column_constraints.t	Mon Jun  5 13:59:17 2006
@@ -45,7 +45,6 @@
         ok(!$e_id2, "is_distinct prevents us from creating another record");
         my $obj = TestApp::Employee->new($handle);
         $obj->load( $e_id );
-        is($obj->id, $e_id, "Loaded the object");
         ok(!$obj->set_employee_num('123'), "is_distinct prevents us from modifying a record to a duplicate value");
 
         cleanup_schema( 'TestApp', $handle );


More information about the Jifty-commit mailing list