[Rt-commit] r2931 - in DBIx-SearchBuilder/trunk: . SearchBuilder t

glasser at bestpractical.com glasser at bestpractical.com
Mon May 23 00:12:42 EDT 2005


Author: glasser
Date: Mon May 23 00:12:42 2005
New Revision: 2931

Added:
   DBIx-SearchBuilder/trunk/README
   DBIx-SearchBuilder/trunk/t/03rebless.t
Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/SearchBuilder.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
   DBIx-SearchBuilder/trunk/t/utils.pl
Log:
 r32866 at net-95869:  glasser | 2005-05-22 23:36:27 -0400
 New feature: $h = DBIx::SearchBuilder::Handle->new;
              $h->Connect( Driver => 'Pg', ... );
 	     # now $h isa DBIx::SearchBuilder::Handle::Pg
 r32867 at net-95869:  glasser | 2005-05-23 00:10:09 -0400
 Document the new 'make test' stuff, and add a README. (And update copyright to 2005.)


Added: DBIx-SearchBuilder/trunk/README
==============================================================================
--- (empty file)
+++ DBIx-SearchBuilder/trunk/README	Mon May 23 00:12:42 2005
@@ -0,0 +1,37 @@
+NAME
+    DBIx::SearchBuilder - Encapsulate SQL queries and rows in simple perl
+    objects
+
+DESCRIPTION
+    This module provides an object-oriented mechanism for retrieving and
+    updating data in a DBI-accesible database.
+
+INSTALLATION
+    $ perl Makefile.PL
+    $ make
+    $ make test   # but see below for how to actually test against a test database
+    # make install
+
+TESTING
+    In order to test most of the features of "DBIx::SearchBuilder", you need
+    to provide "make test" with a test database. For each DBI driver that
+    you would like to test, set the environment variables "SB_TEST_FOO",
+    "SB_TEST_FOO_USER", and "SB_TEST_FOO_PASS" to a database name, database
+    username, and database password, where "FOO" is the driver name in all
+    uppercase. You can test as many drivers as you like. (The appropriate
+    "DBD::" module needs to be installed in order for the test to work.)
+    Note that the "SQLite" driver will automatically be tested if
+    "DBD::Sqlite" is installed, using a temporary file as the database. For
+    example:
+
+      SB_TEST_MYSQL=test SB_TEST_MYSQL_USER=root SB_TEST_MYSQL_PASS=foo \
+        SB_TEST_PG=test SB_TEST_PG_USER=postgres  make test
+
+AUTHOR
+    Copyright (c) 2001-2005 Jesse Vincent, jesse at fsck.com.
+
+    All rights reserved.
+
+    This library is free software; you can redistribute it and/or modify it
+    under the same terms as Perl itself.
+

Modified: DBIx-SearchBuilder/trunk/SearchBuilder.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder.pm	Mon May 23 00:12:42 2005
@@ -1630,10 +1630,24 @@
 # {{{ POD
 
 
+=head1 TESTING
+
+In order to test most of the features of C<DBIx::SearchBuilder>, you need
+to provide C<make test> with a test database.  For each DBI driver that you
+would like to test, set the environment variables C<SB_TEST_FOO>, C<SB_TEST_FOO_USER>,
+and C<SB_TEST_FOO_PASS> to a database name, database username, and database password,
+where "FOO" is the driver name in all uppercase.  You can test as many drivers
+as you like.  (The appropriate C<DBD::> module needs to be installed in order for
+the test to work.)  Note that the C<SQLite> driver will automatically be tested if C<DBD::Sqlite>
+is installed, using a temporary file as the database.  For example:
+
+  SB_TEST_MYSQL=test SB_TEST_MYSQL_USER=root SB_TEST_MYSQL_PASS=foo \
+    SB_TEST_PG=test SB_TEST_PG_USER=postgres  make test
+
 
 =head1 AUTHOR
 
-Copyright (c) 2001-2004 Jesse Vincent, jesse at fsck.com.
+Copyright (c) 2001-2005 Jesse Vincent, jesse at fsck.com.
 
 All rights reserved.
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	Mon May 23 00:12:42 2005
@@ -29,6 +29,7 @@
                     Host => 'hostname',
                     User => 'dbuser',
                     Password => 'dbpassword');
+  # now $handle isa DBIx::SearchBuilder::Handle::mysql                    
  
 =head1 DESCRIPTION
 
@@ -69,6 +70,12 @@
      DisconnectHandleOnDestroy => 1 
 
 unless you have a legacy app like RT2 or RT 3.0.{0,1,2} that depends on the broken behaviour.
+
+If you created the handle with 
+     DBIx::SearchBuilder::Handle->new
+and there is a DBIx::SearchBuilder::Handle::(Driver) subclass for the driver you have chosen,
+the handle will be automatically "upgraded" into that subclass.
+
 =cut
 
 sub Connect  {
@@ -103,6 +110,9 @@
 
   #Set the handle 
   $self->dbh($handle);
+  
+  $self->_UpgradeHandle($args{Driver}) if ref($self) eq 'DBIx::SearchBuilder::Handle';
+  
   return (1); 
     }
 
@@ -111,6 +121,31 @@
 }
 # }}}
 
+# {{{ _UpgradeHandle
+
+=head2 _UpgradeHandle DRIVER
+
+This private internal method turns a plain DBIx::SearchBuilder::Handle into one
+of the standard driver-specific subclasses.
+
+=cut
+
+sub _UpgradeHandle {
+    my $self = shift;
+    return unless ref($self) eq 'DBIx::SearchBuilder::Handle';
+    
+    my $driver = shift;
+    my $class = 'DBIx::SearchBuilder::Handle::' . $driver;
+    eval "require $class";
+    return if $@;
+    
+    bless $self, $class;
+    return;
+}
+
+# }}}
+
+
 # {{{ BuildDSN
 
 =head2 BuildDSN PARAMHASH

Added: DBIx-SearchBuilder/trunk/t/03rebless.t
==============================================================================
--- (empty file)
+++ DBIx-SearchBuilder/trunk/t/03rebless.t	Mon May 23 00:12:42 2005
@@ -0,0 +1,35 @@
+#!/usr/bin/perl -w
+
+
+use strict;
+use warnings;
+use File::Spec;
+use Test::More;
+use DBIx::SearchBuilder::Handle;
+
+BEGIN { require "t/utils.pl" }
+our (@AvailableDrivers);
+
+use constant TESTS_PER_DRIVER => 4;
+
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+foreach my $d ( @AvailableDrivers ) {
+SKIP: {
+	unless( should_test( $d ) ) {
+		skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+	}
+
+	my $handle = DBIx::SearchBuilder::Handle->new;
+	ok($handle, "Made a generic handle");
+	
+	is(ref $handle, 'DBIx::SearchBuilder::Handle', "It's really generic");
+	
+	connect_handle_with_driver( $handle, $d );
+	isa_ok($handle->dbh, 'DBI::db');
+	
+	isa_ok($handle, "DBIx::SearchBuilder::Handle::$d", "Specialized Handle")
+}} # SKIP, foreach blocks
+
+1;

Modified: DBIx-SearchBuilder/trunk/t/utils.pl
==============================================================================
--- DBIx-SearchBuilder/trunk/t/utils.pl	(original)
+++ DBIx-SearchBuilder/trunk/t/utils.pl	Mon May 23 00:12:42 2005
@@ -76,6 +76,22 @@
 	goto &$call;
 }
 
+=head2 connect_handle_with_driver($handle, $driver)
+
+Connects C<$handle> using driver C<$driver>; can use this to test the
+magic that turns a C<DBIx::SearchBuilder::Handle> into a C<DBIx::SearchBuilder::Handle::Foo>
+on C<Connect>.
+
+=cut
+
+sub connect_handle_with_driver
+{
+	my $call = "connect_". lc $_[1];
+	return unless defined &$call;
+	@_ = $_[0];
+	goto &$call;
+}
+
 sub connect_sqlite
 {
 	my $handle = shift;


More information about the Rt-commit mailing list