[Bps-public-commit] dbix-searchbuilder branch, quote-table-names, updated. 1.67-14-g3316344

Dianne Skoll dianne at bestpractical.com
Fri Dec 4 09:08:08 EST 2020


The branch, quote-table-names has been updated
       via  33163442cabc042ca59462bd77350e27fc04c0a5 (commit)
      from  d70dc92d36f3cf60e4c53892f92bbf73006b23d8 (commit)

Summary of changes:
 lib/DBIx/SearchBuilder/Handle.pm       | 27 +++++++++++++++++++++++++--
 lib/DBIx/SearchBuilder/Handle/mysql.pm | 14 ++++++++++++++
 t/04mysql_identifier_quoting.t         | 18 ++++++++++++++++++
 3 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 t/04mysql_identifier_quoting.t

- Log -----------------------------------------------------------------
commit 33163442cabc042ca59462bd77350e27fc04c0a5
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Fri Dec 4 09:07:13 2020 -0500

    Refactor table-name dequoting; add a test for quoting/dequoting.

diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index b854c64..98e5026 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -1046,7 +1046,7 @@ sub Join {
             if ( $old_alias =~ /^(.*?) (\Q$args{'ALIAS2'}\E)$/ ) {
                 $args{'TABLE2'} = $1;
                 $alias = $2;
-                $args{'TABLE2'} =~ s/`//g;
+                $args{'TABLE2'} = $self->DequoteName($args{'TABLE2'}) if ($self->QuoteTableNames);
             }
             else {
                 push @new_aliases, $old_alias;
@@ -1070,7 +1070,7 @@ sub Join {
                 if ( $old_alias =~ /^(.*?) ($args{'ALIAS2'})$/ ) {
                     $args{'TABLE2'} = $1;
                     $alias = $2;
-                    $args{'TABLE2'} =~ s/`//g;
+                    $args{'TABLE2'} = $self->DequoteName($args{'TABLE2'}) if ($self->QuoteTableNames);
                 }
                 else {
                     push @new_aliases, $old_alias;
@@ -1797,6 +1797,29 @@ sub QuoteName {
     return $name;
 }
 
+=head2 DequoteName
+
+Undo the effects of QuoteName by removing quoting.
+
+=cut
+
+sub DequoteName {
+    my ($self, $name) = @_;
+    if ($self->dbh) {
+        # 29 = SQL_IDENTIFIER_QUOTE_CHAR; see "perldoc DBI"
+        my $quote_char = $self->{dbh}->get_info( 29 );
+
+        if ($quote_char) {
+            if ($name =~ /^$quote_char(.*)$quote_char$/) {
+                return $1;
+            }
+        }
+        return $name;
+    }
+    warn "DequoteName called without a db handle";
+    return $name;
+}
+
 sub _RequireQuotedTables { return 0 };
 
 =head2 DESTROY
diff --git a/lib/DBIx/SearchBuilder/Handle/mysql.pm b/lib/DBIx/SearchBuilder/Handle/mysql.pm
index 37ff3d6..37bed86 100755
--- a/lib/DBIx/SearchBuilder/Handle/mysql.pm
+++ b/lib/DBIx/SearchBuilder/Handle/mysql.pm
@@ -309,6 +309,20 @@ sub QuoteName {
     return sprintf('`%s`', $name);
 }
 
+sub DequoteName {
+    my ($self, $name) = @_;
+
+    # If we have a handle, the base class can do it for us
+    if ($self->dbh) {
+        return $self->SUPER::DequoteName($name);
+    }
+
+    if ($name =~ /^`(.*)`$/) {
+        return $1;
+    }
+    return $name;
+}
+
 sub _IsMariaDB {
     my $self = shift;
 
diff --git a/t/04mysql_identifier_quoting.t b/t/04mysql_identifier_quoting.t
new file mode 100644
index 0000000..7c68407
--- /dev/null
+++ b/t/04mysql_identifier_quoting.t
@@ -0,0 +1,18 @@
+#!/usr/bin/perl -w
+
+
+use strict;
+use warnings;
+use Test::More tests => 8;
+
+BEGIN { use_ok("DBIx::SearchBuilder::Handle"); }
+BEGIN { use_ok("DBIx::SearchBuilder::Handle::mysql"); }
+BEGIN { use_ok("DBIx::SearchBuilder::Handle::Pg"); }
+
+my $h = DBIx::SearchBuilder::Handle::mysql->new();
+
+is ($h->QuoteName('foo'), '`foo`', 'QuoteName works as expected');
+is ($h->DequoteName('`foo`'), 'foo', 'DequoteName works as expected');
+is ($h->DequoteName('`foo'), '`foo', 'DequoteName works as expected');
+is ($h->DequoteName('foo`'), 'foo`', 'DequoteName works as expected');
+is ($h->DequoteName('"foo"'), '"foo"', 'DequoteName works as expected');

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


More information about the Bps-public-commit mailing list