]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added client_encoding parameter to create_engine()
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 15 Oct 2011 19:05:51 +0000 (15:05 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 15 Oct 2011 19:05:51 +0000 (15:05 -0400)
when the postgresql+psycopg2 dialect is used;
calls the psycopg2 set_client_encoding() method
with the value upon connect.  [ticket:1839]

CHANGES
lib/sqlalchemy/dialects/postgresql/psycopg2.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index cddd86566f52d64e89de1f60d56037e3b5e2d971..f18854abdf66d35fef4187a9c78255ea39ef752c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -246,6 +246,11 @@ CHANGES
     PG.  [ticket:2290].  Thanks to Ryan P. Kelly for
     the patch.
 
+  - Added client_encoding parameter to create_engine()
+    when the postgresql+psycopg2 dialect is used;
+    calls the psycopg2 set_client_encoding() method
+    with the value upon connect.  [ticket:1839]
+
   - Fixed bug related to [ticket:2141] whereby the 
     same modified index behavior in PG 9 affected
     primary key reflection on a renamed column.
index 22487a3590cca17b6b3d604bdc3014ad42f3630c..4ae7e43d3095c5b2c9a96fb7706b1905f7fe26a7 100644 (file)
@@ -76,6 +76,21 @@ Transactions
 
 The psycopg2 dialect fully supports SAVEPOINT and two-phase commit operations.
 
+Client Encoding
+---------------
+
+The psycopg2 dialect accepts a parameter ``client_encoding`` via :func:`.create_engine`
+which will call the psycopg2 ``set_client_encoding()`` method for each new 
+connection::
+
+    engine = create_engine("postgresql://user:pass@host/dbname", client_encoding='utf8')
+
+This overrides the encoding specified in the Postgresql client configuration.
+
+See: http://initd.org/psycopg/docs/connection.html#connection.set_client_encoding
+
+New in 0.7.3.
+
 Transaction Isolation Level
 ---------------------------
 
@@ -247,11 +262,13 @@ class PGDialect_psycopg2(PGDialect):
         }
     )
 
-    def __init__(self, server_side_cursors=False, use_native_unicode=True, **kwargs):
+    def __init__(self, server_side_cursors=False, use_native_unicode=True, 
+                        client_encoding=None, **kwargs):
         PGDialect.__init__(self, **kwargs)
         self.server_side_cursors = server_side_cursors
         self.use_native_unicode = use_native_unicode
         self.supports_unicode_binds = use_native_unicode
+        self.client_encoding = client_encoding
         if self.dbapi and hasattr(self.dbapi, '__version__'):
             m = re.match(r'(\d+)\.(\d+)(?:\.(\d+))?', 
                                 self.dbapi.__version__)
@@ -289,21 +306,30 @@ class PGDialect_psycopg2(PGDialect):
         connection.set_isolation_level(level)
 
     def on_connect(self):
+        fns = []
+        if self.client_encoding is not None:
+            def on_connect(conn):
+                conn.set_client_encoding(self.client_encoding)
+            fns.append(on_connect)
+
         if self.isolation_level is not None:
-            def base_on_connect(conn):
+            def on_connect(conn):
                 self.set_isolation_level(conn, self.isolation_level)
-        else:
-            base_on_connect = None
+            fns.append(on_connect)
 
         if self.dbapi and self.use_native_unicode:
             extensions = __import__('psycopg2.extensions').extensions
-            def connect(conn):
+            def on_connect(conn):
                 extensions.register_type(extensions.UNICODE, conn)
-                if base_on_connect:
-                    base_on_connect(conn)
-            return connect
+            fns.append(on_connect)
+
+        if fns:
+            def on_connect(conn):
+                for fn in fns:
+                    fn(conn)
+            return on_connect
         else:
-            return base_on_connect
+            return None
 
     def create_connect_args(self, url):
         opts = url.translate_connect_args(username='user')
index b37344b279e23e3e0945308dd8812dfeb485202d..487139102dfc93a6af63cf247fb78a886987abd0 100644 (file)
@@ -1622,6 +1622,25 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
         assert 'will create implicit sequence' in msgs
         assert 'will create implicit index' in msgs
 
+    @testing.only_on('postgresql+psycopg2', 'psycopg2-specific feature')
+    @engines.close_open_connections
+    def test_client_encoding(self):
+        c = testing.db.connect()
+        current_encoding = c.connection.connection.encoding
+        c.close()
+
+        # attempt to use an encoding that's not 
+        # already set
+        if current_encoding == 'UTF8':
+            test_encoding = 'LATIN1'
+        else:
+            test_encoding = 'UTF8'
+
+        e = engines.testing_engine(
+                        options={'client_encoding':test_encoding}
+                    )
+        c = e.connect()
+        eq_(c.connection.connection.encoding, test_encoding)
 
     @testing.fails_on('+zxjdbc',
                       "Can't infer the SQL type to use for an instance "