[Rt-commit] r3069 - in DBIx-SearchBuilder/trunk: . SearchBuilder SearchBuilder/Handle SearchBuilder/Record t

glasser at bestpractical.com glasser at bestpractical.com
Fri Jun 3 18:09:10 EDT 2005


Author: glasser
Date: Fri Jun  3 18:09:09 2005
New Revision: 3069

Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/SearchBuilder.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Record/Cachable.pm
   DBIx-SearchBuilder/trunk/t/11schema_records.t
Log:
 r33848 at tin-foil:  glasser | 2005-06-03 15:49:10 -0400
  * change a whole bunch of $sb->{'table'} to $sb->Table
  * For Schemas which references a foreign collection, make method which creates this collection
  * For record-read elements, in ->Create convert from obj to id (just like in ->SetFoo)
  * lots more tests of references and collections


Modified: DBIx-SearchBuilder/trunk/SearchBuilder.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder.pm	Fri Jun  3 18:09:09 2005
@@ -422,7 +422,7 @@
 
     # DISTINCT query only required for multi-table selects
     if ($self->_isJoined) {
-        $self->_DistinctQuery(\$QueryString, $self->{'table'});
+        $self->_DistinctQuery(\$QueryString, $self->Table);
     } else {
         $QueryString = "SELECT main.* FROM $QueryString";
     }
@@ -720,7 +720,7 @@
 sub Limit {
     my $self = shift;
     my %args = (
-        TABLE           => $self->{'table'},
+        TABLE           => $self->Table,
         FIELD           => undef,
         VALUE           => undef,
         ALIAS           => undef,
@@ -831,7 +831,7 @@
 
 sub _GenericRestriction {
     my $self = shift;
-    my %args = ( TABLE           => $self->{'table'},
+    my %args = ( TABLE           => $self->Table,
                  FIELD           => undef,
                  VALUE           => undef,
                  ALIAS           => undef,
@@ -861,7 +861,7 @@
     unless ( $args{'ALIAS'} ) {
 
         #if the table we're looking at is the same as the main table
-        if ( $args{'TABLE'} eq $self->{'table'} ) {
+        if ( $args{'TABLE'} eq $self->Table ) {
 
             # TODO this code assumes no self joins on that table.
             # if someone can name a case where we'd want to do that,
@@ -1575,7 +1575,7 @@
             $alias;
         }
         else {
-            $self->{table};
+            $self->Table;
         }
     };
 
@@ -1598,7 +1598,7 @@
     }
 
     my $column = "col" . @{ $self->{columns} ||= [] };
-    $column = $args{FIELD} if $table eq $self->{table} and !$args{ALIAS};
+    $column = $args{FIELD} if $table eq $self->Table and !$args{ALIAS};
     push @{ $self->{columns} }, "$name AS \L$column";
     return $column;
 }

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	Fri Jun  3 18:09:09 2005
@@ -998,7 +998,7 @@
           }
     }
 
-    my $join_clause = $sb->{'table'} . " main ";
+    my $join_clause = $sb->Table . " main ";
 
 	
     my @keys = ( keys %{ $sb->{'left_joins'} } );

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm	Fri Jun  3 18:09:09 2005
@@ -116,7 +116,7 @@
           }
     }
 
-    my $join_clause = $sb->{'table'} . " main ";
+    my $join_clause = $sb->Table . " main ";
     
     my @keys = ( keys %{ $sb->{'left_joins'} } );
     my %seen;

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Record.pm	Fri Jun  3 18:09:09 2005
@@ -389,7 +389,8 @@
 
 sub Id  {
     my $pkey = $_[0]->_PrimaryKey();
-    $_[0]->{'values'}->{$pkey};
+    my $ret = $_[0]->{'values'}->{$pkey};
+    return $ret;
 }
 
 # }}}
@@ -437,6 +438,10 @@
         *{$AUTOLOAD} = sub { $_[0]->_ToRecord( $Attrib, $_[0]->_Value($Attrib) ) };
         goto &$AUTOLOAD;        
     }
+    elsif ( $self->_Accessible( $Attrib, 'foreign-collection') ) {
+        *{$AUTOLOAD} = sub { $_[0]->_CollectionValue( $Attrib ) };
+        goto &$AUTOLOAD;
+    }
     elsif ( $AUTOLOAD =~ /.*::[sS]et_?(\w+)/o ) {
         $Attrib = $1;
 
@@ -639,6 +644,27 @@
     return $object;
 }
 
+sub _CollectionValue {
+    my $self = shift;
+    
+    my $method_name =  shift;
+    return unless defined $method_name;
+    
+    my $schema = $self->Schema;
+    my $description = $schema->{$method_name};
+    return unless $description;
+    
+    my $classname = $description->{'REFERENCES'};
+
+    return unless UNIVERSAL::isa($classname, 'DBIx::SearchBuilder');
+    
+    my $coll = $classname->new( Handle => $self->_Handle );
+    
+    $coll->Limit( FIELD => $description->{'KEY'}, VALUE => $self->id);
+    
+    return $coll;
+}
+
 # sub {{{ ReadableAttributes
 
 =head2 ReadableAttributes
@@ -1228,6 +1254,10 @@
     foreach $key ( keys %attribs ) {
         my $method = "Validate$key";
 
+        if ( $self->_Accessible( $key, 'record-write') ) {
+            $attribs{$key} = $attribs{$key}->id if UNIVERSAL::isa($attribs{$key}, 'DBIx::SearchBuilder::Record');
+        }
+
             #Truncate things that are too long for their datatypes
         $attribs{$key} = $self->TruncateValue ($key => $attribs{$key});
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Record/Cachable.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Record/Cachable.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Record/Cachable.pm	Fri Jun  3 18:09:09 2005
@@ -228,7 +228,7 @@
     return (
         {
             values  => $self->{'values'},
-            table   => $self->{'table'},
+            table   => $self->Table,
             fetched => $self->{'fetched'}
         }
     );

Modified: DBIx-SearchBuilder/trunk/t/11schema_records.t
==============================================================================
--- DBIx-SearchBuilder/trunk/t/11schema_records.t	(original)
+++ DBIx-SearchBuilder/trunk/t/11schema_records.t	Fri Jun  3 18:09:09 2005
@@ -9,7 +9,7 @@
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 17;
+use constant TESTS_PER_DRIVER => 63;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -35,6 +35,15 @@
 	ok($e_id, "Got an id for the new employee: $e_id");
 	$emp->Load($e_id);
 	is($emp->id, $e_id);
+	
+	my $phone_collection = $emp->Phones;
+	isa_ok($phone_collection, 'TestApp::PhoneCollection');
+	
+	{
+	    my $ph = $phone_collection->Next;
+	    is($ph, undef, "No phones yet");
+	}
+	
 	my $phone = TestApp::Phone->new($handle);
 	isa_ok( $phone, 'TestApp::Phone');
 	my $p_id = $phone->Create( Employee => $e_id, Phone => '+7(903)264-03-51');
@@ -47,6 +56,15 @@
 	isa_ok( $obj, 'TestApp::Employee');
 	is($obj->id, $e_id);
 	is($obj->Name, 'RUZ');
+	
+	{
+	    $phone_collection->RedoSearch;
+	    my $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p_id, 'found first phone');
+	    is($ph->Phone, '+7(903)264-03-51');
+	    is($phone_collection->Next, undef);
+	}
 
 	# tests for no object mapping
 	my $val = $phone->Phone;
@@ -56,18 +74,113 @@
 	isa_ok($emp2, 'TestApp::Employee');
 	my $e2_id = $emp2->Create( Name => 'Dave' );
 	ok($e2_id, "Got an id for the new employee: $e2_id");
+	$emp2->Load($e2_id);
+	is($emp2->id, $e2_id);
+
+	my $phone2_collection = $emp2->Phones;
+	isa_ok($phone2_collection, 'TestApp::PhoneCollection');
+
+	{
+	    my $ph = $phone2_collection->Next;
+	    is($ph, undef, "new emp has no phones");
+	}
+	
+	{
+	    $phone_collection->RedoSearch;
+	    my $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p_id, 'first emp still has phone');
+	    is($ph->Phone, '+7(903)264-03-51');
+	    is($phone_collection->Next, undef);
+	}
 
 	$phone->SetEmployee($e2_id);
 	
+		
 	my $emp3 = $phone->Employee;
 	isa_ok($emp3, 'TestApp::Employee');
 	is($emp3->Name, 'Dave', 'changed employees by ID');
+	is($emp3->id, $emp2->id);
+
+	{
+	    $phone_collection->RedoSearch;
+	    is($phone_collection->Next, undef, "first emp lost phone");
+	}
+
+	{
+	    $phone2_collection->RedoSearch;
+	    my $ph = $phone2_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p_id, 'new emp stole the phone');
+	    is($ph->Phone, '+7(903)264-03-51');
+	    is($phone2_collection->Next, undef);
+	}
+
 
 	$phone->SetEmployee($emp);
 
 	my $emp4 = $phone->Employee;
 	isa_ok($emp4, 'TestApp::Employee');
 	is($emp4->Name, 'RUZ', 'changed employees by obj');
+	is($emp4->id, $emp->id);
+
+	{
+	    $phone2_collection->RedoSearch;
+	    is($phone2_collection->Next, undef, "second emp lost phone");
+	}
+
+	{
+	    $phone_collection->RedoSearch;
+	    my $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p_id, 'first emp stole the phone');
+	    is($ph->Phone, '+7(903)264-03-51');
+	    is($phone_collection->Next, undef);
+	}
+	
+	my $phone2 = TestApp::Phone->new($handle);
+	isa_ok( $phone2, 'TestApp::Phone');
+	my $p2_id = $phone2->Create( Employee => $e_id, Phone => '123456');
+	ok($p2_id, "Loaded phone $p2_id");
+	$phone2->Load( $p2_id );
+	
+	{
+	    $phone_collection->RedoSearch;
+	    my $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p_id, 'still has this phone');
+	    is($ph->Phone, '+7(903)264-03-51');
+	    $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p2_id, 'now has that phone');
+	    is($ph->Phone, '123456');
+	    is($phone_collection->Next, undef);
+	}
+	
+	# Test Create with obj as argument
+	my $phone3 = TestApp::Phone->new($handle);
+	isa_ok( $phone3, 'TestApp::Phone');
+	my $p3_id = $phone3->Create( Employee => $emp, Phone => '7890');
+	ok($p3_id, "Loaded phone $p3_id");
+	$phone3->Load( $p3_id );
+	
+	{
+	    $phone_collection->RedoSearch;
+	    my $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p_id, 'still has this phone');
+	    is($ph->Phone, '+7(903)264-03-51');
+	    $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p2_id, 'still has that phone');
+	    is($ph->Phone, '123456');
+	    $ph = $phone_collection->Next;
+	    isa_ok($ph, 'TestApp::Phone');
+	    is($ph->id, $p3_id, 'even has this other phone');
+	    is($ph->Phone, '7890');
+	    is($phone_collection->Next, undef);
+	}
+	
 	
 
 	cleanup_schema( 'TestApp', $handle );
@@ -132,9 +245,17 @@
 sub Schema {
     return {
         Name => { TYPE => 'varchar' },
+        Phones => { REFERENCES => 'TestApp::PhoneCollection', KEY => 'Employee' }
     };
 }
 
+sub _Value  {
+  my $self = shift;
+  my $x =  ($self->__Value(@_));
+  return $x;
+}
+
+
 1;
 
 package TestApp::Phone;
@@ -150,5 +271,22 @@
     }
 }
 
+package TestApp::PhoneCollection;
+
+use base qw/DBIx::SearchBuilder/;
+
+sub Table {
+    my $self = shift;
+    my $tab = $self->NewItem->Table();
+    return $tab;
+}
+
+sub NewItem {
+    my $self = shift;
+    my $class = 'TestApp::Phone';
+    return $class->new( $self->_Handle );
+
+}
+
 
 1;


More information about the Rt-commit mailing list