[Bps-public-commit] dbix-searchbuilder branch driver-mariadb updated. 1.79-5-g1620c2a

BPS Git Server git at git.bestpractical.com
Fri Jan 12 22:04:36 UTC 2024


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "dbix-searchbuilder".

The branch, driver-mariadb has been updated
       via  1620c2af222ee7f5d38d577ac5dd7c93362767dd (commit)
      from  f31670918dce989dc535fdeff7a9177ba0370493 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 1620c2af222ee7f5d38d577ac5dd7c93362767dd
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri Jan 12 17:04:15 2024 -0500

    Support BLOBs with DBD::MariaDB

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 694f7c9..2a60a6a 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -4,7 +4,7 @@ package DBIx::SearchBuilder;
 use strict;
 use warnings;
 
-our $VERSION = "1.80";
+our $VERSION = "1.81";
 
 use Clone qw();
 use Encode qw();
diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index b9f9deb..6a2f474 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -602,13 +602,25 @@ sub SimpleQuery {
         }
     }
 
-    # Check @bind_values for HASH refs
+    # Check @bind_values for HASH refs. These can be sent as type settings
+    # or hits for the DBD module for dealing with certain data types,
+    # usually BLOBS.
     for ( my $bind_idx = 0 ; $bind_idx < scalar @bind_values ; $bind_idx++ ) {
         if ( ref( $bind_values[$bind_idx] ) eq "HASH" ) {
             my $bhash = $bind_values[$bind_idx];
             $bind_values[$bind_idx] = $bhash->{'value'};
             delete $bhash->{'value'};
-            $sth->bind_param( $bind_idx + 1, undef, $bhash );
+
+            # DBD::MariaDB only works using the string version of setting type,
+            # so convert the hash to the string for this case.
+            if ( $self->isa('DBIx::SearchBuilder::Handle::MariaDB')
+                 && $bhash->{'TYPE'}
+                 && $bhash->{'TYPE'} eq 'SQL_BLOB' ) {
+                $sth->bind_param( $bind_idx + 1, undef, DBI::SQL_BLOB );
+            }
+            else {
+                $sth->bind_param( $bind_idx + 1, undef, $bhash );
+            }
         }
     }
 
diff --git a/lib/DBIx/SearchBuilder/Handle/MariaDB.pm b/lib/DBIx/SearchBuilder/Handle/MariaDB.pm
index fcba526..51e9b7d 100755
--- a/lib/DBIx/SearchBuilder/Handle/MariaDB.pm
+++ b/lib/DBIx/SearchBuilder/Handle/MariaDB.pm
@@ -42,17 +42,64 @@ sub Insert  {
     #
     # https://metacpan.org/dist/DBD-MariaDB/view/lib/DBD/MariaDB.pod#mariadb_insertid
 
-    $self->{'id'}=$self->dbh->last_insert_id();
+    $self->{'id'} = $self->dbh->last_insert_id();
 
     # Docs say last_insert_id could still return undef, so keeping this code
-    unless ($self->{'id'}) {
-        $self->{'id'} =  $self->FetchResult('SELECT LAST_INSERT_ID()');
+    unless ( $self->{'id'} ) {
+        $self->{'id'} = $self->FetchResult('SELECT LAST_INSERT_ID()');
     }
     warn "$self no row id returned on row creation" unless ($self->{'id'});
 
     return( $self->{'id'}); #Add Succeded. return the id
 }
 
+=head2 KnowsBLOBs
+
+Returns 1 if the current database supports inserts of BLOBs automatically.
+Returns undef if the current database must be informed of BLOBs for inserts.
+
+=cut
+
+sub KnowsBLOBs {
+    my $self = shift;
+    return(undef);
+}
+
+=head2 BLOBParams FIELD_NAME FIELD_TYPE
+
+Returns a hash ref for the bind_param call to identify BLOB types used by
+the current database for a particular column type.
+
+=cut
+
+sub BLOBParams {
+    my $self = shift;
+    my $field = shift;
+    my $type = shift;
+
+    if ( $type =~ /^(blob|longblob)$/i ) {
+        # Don't assign to key 'value' as it is defined later.
+        return ( { TYPE => 'SQL_BLOB', } );
+    }
+    else {
+        # Normal handling for these, so no hashref
+        return;
+    }
+}
+
+=head2 BinarySafeBLOBs
+
+Return undef, as no current version of postgres supports binary-safe blobs
+
+
+sub BinarySafeBLOBs {
+    my $self = shift;
+    return(undef);
+}
+
+=cut
+
+
 =head2 SimpleUpdateFromSelect
 
 Customization of L<DBIx::SearchBuilder::Handle/SimpleUpdateFromSelect>.
diff --git a/lib/DBIx/SearchBuilder/Record.pm b/lib/DBIx/SearchBuilder/Record.pm
index 8bd4c05..e2b887b 100755
--- a/lib/DBIx/SearchBuilder/Record.pm
+++ b/lib/DBIx/SearchBuilder/Record.pm
@@ -1302,11 +1302,13 @@ sub Create {
         my $ca = $self->_ClassAccessible();
         foreach $key ( keys %attribs ) {
             my $type = $ca->{$key}->{'type'};
-            next unless $type && $type =~ /^(text|longtext|clob|blob|lob)$/i;
+            next unless $type && $type =~ /^(text|longtext|clob|blob|lob|longblob)$/i;
 
             my $bhash = $self->_Handle->BLOBParams( $key, $type );
-            $bhash->{'value'} = $attribs{$key};
-            $attribs{$key} = $bhash;
+            if ( ref($bhash) eq 'HASH' ) {
+                $bhash->{'value'} = $attribs{$key};
+                $attribs{$key} = $bhash;
+            }
         }
     }
     return ( $self->_Handle->Insert( $self->Table, %attribs ) );
diff --git a/t/01records.t b/t/01records.t
index c175267..df05db6 100644
--- a/t/01records.t
+++ b/t/01records.t
@@ -7,13 +7,14 @@ use Test::More;
 BEGIN { require "./t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 69;
+use constant TESTS_PER_DRIVER => 75;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
 
 foreach my $d ( @AvailableDrivers ) {
 SKIP: {
+    diag ("Running tests for $d");
 	unless( has_schema( 'TestApp::Address', $d ) ) {
 		skip "No schema for '$d' driver", TESTS_PER_DRIVER;
 	}
@@ -34,7 +35,7 @@ SKIP: {
 # Handle->Fields
         is_deeply(
             [$handle->Fields('Address')],
-            [qw(id name phone employeeid)],
+            [qw(id name phone employeeid content)],
             "listed all columns in the table"
         );
         is_deeply(
@@ -48,23 +49,34 @@ SKIP: {
 	is( $rec->_Accessible('id' => 'write'), undef, 'id is not accessible for write' );
 	is( $rec->_Accessible('id'), undef, "any field is not accessible in undefined mode" );
 	is( $rec->_Accessible('unexpected_field' => 'read'), undef, "field doesn't exist and can't be accessible for read" );
-	is_deeply( [sort($rec->ReadableAttributes)], [qw(EmployeeId Name Phone id)], 'readable attributes' );
-	is_deeply( [sort($rec->WritableAttributes)], [qw(EmployeeId Name Phone)], 'writable attributes' );
+	is_deeply( [sort($rec->ReadableAttributes)], [qw(Content EmployeeId Name Phone id)], 'readable attributes' );
+	is_deeply( [sort($rec->WritableAttributes)], [qw(Content EmployeeId Name Phone)], 'writable attributes' );
 
 	can_ok($rec,'Create');
 
-	my ($id) = $rec->Create( Name => 'Jesse', Phone => '617 124 567');
+	my ($id) = $rec->Create( Name => 'Jesse', Phone => '617 124 567', Content => 'Håvard');
 	ok($id,"Created record ". $id);
 	ok($rec->Load($id), "Loaded the record");
 
 
 	is($rec->id, $id, "The record has its id");
 	is ($rec->Name, 'Jesse', "The record's name is Jesse");
+    is( $rec->Content, 'Håvard', "The record's Content is Håvard");
 
 	my ($val, $msg) = $rec->SetName('Obra');
 	ok($val, $msg) ;
 	is($rec->Name, 'Obra', "We did actually change the name");
 
+    my $rec2 = TestApp::Address->new($handle);
+    isa_ok($rec2, 'DBIx::SearchBuilder::Record');
+
+    my ($id2) = $rec2->Create( Name => 'Håvard', Phone => '617 124 567', Content => 'Foo');
+    ok($id2,"Created record ". $id2);
+    ok($rec2->Load($id2), "Loaded the record");
+
+    is($rec2->id, $id2, "The record has its id");
+    is ($rec2->Name, 'Håvard', "The record's name is Håvard");
+
 # Validate immutability of the field id
 	($val, $msg) = $rec->Setid( $rec->id + 1 );
 	ok(!$val, $msg);
@@ -253,7 +265,8 @@ sub _ClassAccessible {
         {read => 1, write => 1, type => 'varchar(18)', length => 18, default => ''},
         EmployeeId => 
         {read => 1, write => 1, type => 'int(8)', default => ''},
-
+        Content =>
+        {read => 1, write => 1, sql_type => -4, length => 0,  is_blob => 1,  is_numeric => 0,  type => 'longblob', default => ''},
 }
 
 }
@@ -265,6 +278,7 @@ CREATE TEMPORARY TABLE Address (
         Name varchar(36),
         Phone varchar(18),
         EmployeeId int(8),
+        Content LONGBLOB,
   	PRIMARY KEY (id)) CHARACTER SET utf8mb4
 EOF
 
@@ -277,18 +291,22 @@ CREATE TEMPORARY TABLE Address (
         Name varchar(36),
         Phone varchar(18),
         EmployeeId int(8),
+        Content LONGBLOB,
   	PRIMARY KEY (id)) CHARACTER SET utf8mb4
 EOF
 
 }
 
+# TODO: Change this test to use bytea for Content
+
 sub schema_pg {
 <<EOF;
 CREATE TEMPORARY TABLE Address (
         id serial PRIMARY KEY,
         Name varchar,
         Phone varchar,
-        EmployeeId integer
+        EmployeeId integer,
+        Content varchar
 )
 EOF
 
@@ -301,7 +319,8 @@ CREATE TABLE Address (
         id  integer primary key,
         Name varchar(36),
         Phone varchar(18),
-        EmployeeId int(8))
+        EmployeeId int(8),
+        Content BLOB )
 EOF
 
 }
@@ -312,7 +331,8 @@ sub schema_oracle { [
         id integer CONSTRAINT Address_Key PRIMARY KEY,
         Name varchar(36),
         Phone varchar(18),
-        EmployeeId integer
+        EmployeeId integer,
+        Content CLOB
     )",
 ] }
 

-----------------------------------------------------------------------

Summary of changes:
 lib/DBIx/SearchBuilder.pm                |  2 +-
 lib/DBIx/SearchBuilder/Handle.pm         | 16 ++++++++--
 lib/DBIx/SearchBuilder/Handle/MariaDB.pm | 53 ++++++++++++++++++++++++++++++--
 lib/DBIx/SearchBuilder/Record.pm         |  8 +++--
 t/01records.t                            | 38 +++++++++++++++++------
 5 files changed, 99 insertions(+), 18 deletions(-)


hooks/post-receive
-- 
dbix-searchbuilder


More information about the Bps-public-commit mailing list