[Rt-commit] r5117 - in DBIx-SearchBuilder/trunk: SearchBuilder SearchBuilder/Handle

ruz at bestpractical.com ruz at bestpractical.com
Tue Apr 25 21:02:54 EDT 2006


Author: ruz
Date: Tue Apr 25 21:02:53 2006
New Revision: 5117

Added:
   DBIx-SearchBuilder/trunk/t/03versions.t
Modified:
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm

Log:
* backport DatabaseVersion enhancements from JDBI
* add tests for this functionality
** I've tested it on SQLite, mysql and Pg
** PLEASE test it with other backends

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	Tue Apr 25 21:02:53 2006
@@ -587,23 +587,48 @@
 
 
 
-=head2 DatabaseVersion
+=head2 DatabaseVersion [Short => 1]
 
-Returns the database's version. The base implementation uses a "SELECT VERSION"
+Returns the database's version.
+
+If argument C<Short> is true returns short variant, in other
+case returns whatever database handle/driver returns. By default
+returns short version, e.g. '4.1.23' or '8.0-rc4'.
+
+Returns empty string on error or if database couldn't return version.
+
+The base implementation uses a C<SELECT VERSION()>
 
 =cut
 
 sub DatabaseVersion {
     my $self = shift;
+    my %args = ( Short => 1, @_ );
+
+    unless ( defined $self->{'database_version'} ) {
+
+        # turn off error handling, store old values to restore later
+        my $re = $self->RaiseError;
+        $self->RaiseError(0);
+        my $pe = $self->PrintError;
+        $self->PrintError(0);
 
-    unless ($self->{'database_version'}) {
-        my $statement  = "SELECT VERSION()";
-        my $sth = $self->SimpleQuery($statement);
-        my @vals = $sth->fetchrow();
-        $self->{'database_version'}= $vals[0];
+        my $statement = "SELECT VERSION()";
+        my $sth       = $self->SimpleQuery($statement);
+
+        my $ver = '';
+        $ver = ( $sth->fetchrow_arrayref->[0] || '' ) if $sth;
+        $ver =~ /(\d+(?:\.\d+)*(?:-[a-z0-9]+)?)/i;
+        $self->{'database_version'}       = $ver;
+        $self->{'database_version_short'} = $1 || $ver;
+
+        $self->RaiseError($re);
+        $self->PrintError($pe);
     }
-}
 
+    return $self->{'database_version_short'} if $args{'Short'};
+    return $self->{'database_version'};
+}
 
 =head2 CaseSensitive
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/SQLite.pm	Tue Apr 25 21:02:53 2006
@@ -20,8 +20,18 @@
 
 =head1 METHODS
 
+=head2 DatabaseVersion
+
+Returns the version of the SQLite library which is used, e.g., "2.8.0".
+SQLite can only return short variant.
+
 =cut
 
+sub DatabaseVersion {
+    my $self = shift;
+    return '' unless $self->dbh;
+    return $self->dbh->{sqlite_version} || '';
+}
 
 =head2 Insert
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm	Tue Apr 25 21:02:53 2006
@@ -21,9 +21,6 @@
 
 =head1 METHODS
 
-=cut
-
-
 =head2 Insert
 
 Takes a table name as the first argument and assumes that the rest of the arguments are an array of key-value pairs to be inserted.

Added: DBIx-SearchBuilder/trunk/t/03versions.t
==============================================================================
--- (empty file)
+++ DBIx-SearchBuilder/trunk/t/03versions.t	Tue Apr 25 21:02:53 2006
@@ -0,0 +1,41 @@
+#!/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 => 6;
+
+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 = get_handle( $d );
+	ok($handle, "Made a handle");
+    isa_ok($handle, 'DBIx::SearchBuilder::Handle');
+	connect_handle( $handle );
+	isa_ok($handle->dbh, 'DBI::db');
+
+    my $full_version = $handle->DatabaseVersion( Short => 0 );
+diag("Full version is '$full_version'") if defined $full_version && $ENV{'TEST_VERBOSE'};
+    ok($full_version, "returns full version");
+
+    my $short_version = $handle->DatabaseVersion;
+diag("Short version is '$short_version'") if defined $short_version && $ENV{'TEST_VERBOSE'};
+    ok($short_version, "returns short version");
+
+    like($short_version, qr{^[-\w\.]+$}, "short version has only \\w.-");
+
+}} # SKIP, foreach blocks
+
+1;


More information about the Rt-commit mailing list