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

Ruslan U. Zakirov Ruslan.Zakirov at miet.ru
Tue May 24 09:10:51 EDT 2005


	Hello, David and all.

Nice feature, but a little buggy implementation.
Pg and Oracle has own Connect methods, other back-ends may have own 
BuildDSN methods.
So I attach patch that fixes this issue. If it would be approved, then 
I'll write tests which covers corner cases.

--
Regards, Ruslan.

glasser at bestpractical.com wrote:
> 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;
> _______________________________________________
> Rt-commit mailing list
> Rt-commit at lists.bestpractical.com
> http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-commit
> 

-------------- next part --------------
=== META.yml
==================================================================
--- META.yml  (revision 1675)
+++ META.yml  (local)
@@ -1,5 +1,5 @@
 name: DBIx-SearchBuilder
-version: 1.30_01
+version: 1.30_02
 license: perl
 distribution_type: module
 build_requires:
=== SearchBuilder/Handle.pm
==================================================================
--- SearchBuilder/Handle.pm  (revision 1675)
+++ SearchBuilder/Handle.pm  (local)
@@ -92,6 +92,13 @@
            DisconnectHandleOnDestroy => undef,
 	       @_);
 
+    if( $args{'Driver'} && !$self->isa( 'DBIx::SearchBuilder::Handle::'. $args{'Driver'} ) ) {
+	unless( $self->_UpgradeHandle($args{Driver}) ) {
+	    return (undef);
+	}
+	return ($self->Connect( %args ));
+    }
+
     my $dsn = $self->DSN || '';
 
     # Setting this actually breaks old RT versions in subtle ways. So we need to explicitly call it
@@ -111,9 +118,7 @@
   #Set the handle 
   $self->dbh($handle);
   
-  $self->_UpgradeHandle($args{Driver}) if ref($self) eq 'DBIx::SearchBuilder::Handle';
-  
-  return (1); 
+  return (1);
     }
 
     return(undef);
@@ -132,7 +137,7 @@
 
 sub _UpgradeHandle {
     my $self = shift;
-    return unless ref($self) eq 'DBIx::SearchBuilder::Handle';
+#    return unless ref($self) eq 'DBIx::SearchBuilder::Handle';
     
     my $driver = shift;
     my $class = 'DBIx::SearchBuilder::Handle::' . $driver;
@@ -140,7 +145,7 @@
     return if $@;
     
     bless $self, $class;
-    return;
+    return 1;
 }
 
 # }}}


More information about the Rt-devel mailing list