[Bps-public-commit] dbix-searchbuilder branch, pg-client-encoding, created. 1.67-2-gf4e3564

Alex Vandiver alexmv at bestpractical.com
Sun Aug 14 02:50:20 EDT 2016


The branch, pg-client-encoding has been created
        at  f4e3564bb14b0250ff9ac0ba3b82121c797a5c40 (commit)

- Log -----------------------------------------------------------------
commit f4e3564bb14b0250ff9ac0ba3b82121c797a5c40
Author: Alex Vandiver <alex at chmrr.net>
Date:   Sat Aug 13 22:44:54 2016 -0700

    On Postgres, force the `client_encoding` to `UTF8`
    
    The DBD::Pg driver is only able to reliably pass non-ASCII characters
    back and forth to the database if the `client_encoding` of the
    connection is set to `UTF8`.  The default is to use the server's
    encoding, which is UTF8; however, some installations may have
    explicitly set the `client_encoding` to `SQL_ASCII` or other values.
    This causes test failures, and applications fail to be able to insert
    non-ASCII characters into Postgres `text` columns.
    
    Force the `client_encoding` to `UTF8` at connection time.  This is
    done via a DSN parameter; it can also be done using `SET
    CLIENT_ENCODING=UTF8` after connecting, but that would also require
    re-setting L<DBD::Pg/pg_enable_utf8-integer> to a magic value of `-1`
    for the change to take effect.  Altering the DSN prior seems a less
    "magic" solution.

diff --git a/lib/DBIx/SearchBuilder/Handle/Pg.pm b/lib/DBIx/SearchBuilder/Handle/Pg.pm
index 3cda494..d7c48f2 100755
--- a/lib/DBIx/SearchBuilder/Handle/Pg.pm
+++ b/lib/DBIx/SearchBuilder/Handle/Pg.pm
@@ -45,6 +45,21 @@ sub Connect {
     return ($rv); 
 }
 
+=head2 BuildDSN
+
+Extend L<DBIx::SearchBuilder::Handle/BuildDNS> to force
+C<client_encoding> to be UTF-8, so that character strings can be
+safely passed to, and retrieved from, the database.  See
+L<DBD::Pg/Encoding>.
+
+=cut
+
+sub BuildDSN {
+    my $self = shift;
+    $self->SUPER::BuildDSN(@_);
+    $self->{'dsn'} .= ';client_encoding=UTF8';
+    return $self->{'dsn'};
+}
 
 =head2 Insert
 

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


More information about the Bps-public-commit mailing list