DBD-Pg.diff

text/plain

Filename: DBD-Pg.diff
Type: text/plain
Part: 0
Message: Re: DBD::Pg BYTEA Character Escaping

Patch

Format: unified
File+
bin/Pg.pm 17 37
--- /usr/local/lib/perl5/site_perl/5.6.1/ppc-linux/DBD/Pg.pm	Wed Jun 27 10:54:58 2001
+++ bin/Pg.pm	Sun Nov 18 13:20:36 2001
@@ -104,7 +104,12 @@
 
 {   package DBD::Pg::db; # ====== DATABASE ======
     use strict;
-    use POSIX(qw(isprint)); # necessary for quote()
+
+    # Characters that need to be escaped by quote().
+    my %esc = ( "'"  => '\\047', # '\\' . sprintf("%03o", ord("'")), # ISO SQL 2
+                '\\' => '\\134', # '\\' . sprintf("%03o", ord("\\")),
+                "\0" => '\\000'  # '\\' . sprintf("%03o", ord("\0")),
+              );
 
     sub prepare {
         my($dbh, $statement, @attribs)= @_;
@@ -341,48 +346,23 @@
 
     sub quote {
         my ($dbh, $str, $data_type) = @_;
-
         return "NULL" unless defined $str;
 
-        unless ($data_type) {
-            $str =~ s/'/''/g;           # ISO SQL2
-            # In addition to the DBI method it doubles also the
-            # backslash, because PostgreSQL treats a backslash as an
-            # escape character.
-            $str =~ s/\\/\\\\/g;
+        if (! defined $data_type
+	    || $data_type != DBI::SQL_INTEGER
+	    || $data_type != DBI::SQL_SMALLINT
+	    || $data_type != DBI::SQL_DECIMAL
+	    || $data_type != DBI::SQL_FLOAT
+	    || $data_type != DBI::SQL_REAL
+	    || $data_type != DBI::SQL_DOUBLE
+	    || $data_type != DBI::SQL_NUMERIC)
+	{
+            $str =~ s/(['\\\0])/$esc{$&}/g;
             return "'$str'";
         }
-
-        # Optimise for standard numerics which need no quotes
-        return $str if $data_type == DBI::SQL_INTEGER
-                    || $data_type == DBI::SQL_SMALLINT
-                    || $data_type == DBI::SQL_DECIMAL
-                    || $data_type == DBI::SQL_FLOAT
-                    || $data_type == DBI::SQL_REAL
-                    || $data_type == DBI::SQL_DOUBLE
-                    || $data_type == DBI::SQL_NUMERIC;
-        my $ti = $dbh->type_info($data_type);
-        # XXX needs checking
-        my $lp = $ti ? $ti->{LITERAL_PREFIX} || "" : "'";
-        my $ls = $ti ? $ti->{LITERAL_SUFFIX} || "" : "'";
-        # XXX don't know what the standard says about escaping
-        # in the 'general case' (where $lp != "'").
-        # So we just do this and hope:
-        $str =~ s/$lp/$lp$lp/g  
-            if $lp && $lp eq $ls && ($lp eq "'" || $lp eq '"');
-        # also, escape the backslashes, always
-        $str =~ s/\\/\\\\/g;
-        # if the type is SQL_BINARY, escape the non-printable chars
-        if ($data_type == DBI::SQL_BINARY ||
-            $data_type == DBI::SQL_VARBINARY ||
-            $data_type == DBI::SQL_LONGVARBINARY) {
-            $str=join("", map { isprint($_)?$_:'\\'.sprintf("%03o",ord($_)) } 
-                          split //, $str);
-        }
-        return "$lp$str$ls";
+	return $str;
     }
 }
-
 
 {   package DBD::Pg::st; # ====== STATEMENT ======