summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-12-15 21:23:01 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2013-12-15 21:23:01 -0500
commitb2223ab14988317f86fcbb6271a8b853b2924bae (patch)
tree239be6387ea75ec09c70e4ca6524834a9521e142
parent638c1c46b23273e3c50638537ecfec91e839253e (diff)
downloadsqlalchemy-b2223ab14988317f86fcbb6271a8b853b2924bae.tar.gz
- add "force_nocheck" as a way to turn on unicode=force without even
doing the isinstance() check - currently used only by psycopg2 + native enum + py2k. - didn't realize psycopg2 had UNICODEARRAY extension all this time; replace _PGArray with just using UNICODEARRAY instead. - replace unnecessary/inconsistent __import__ in _isolation_lookup.
-rw-r--r--doc/build/changelog/changelog_09.rst7
-rw-r--r--lib/sqlalchemy/dialects/postgresql/psycopg2.py28
-rw-r--r--lib/sqlalchemy/sql/sqltypes.py9
3 files changed, 23 insertions, 21 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst
index c40c4c6c8..2cfa424e8 100644
--- a/doc/build/changelog/changelog_09.rst
+++ b/doc/build/changelog/changelog_09.rst
@@ -15,6 +15,13 @@
:version: 0.9.0b2
.. change::
+ :tags: bug, postgresql
+
+ Now using psycopg2 UNICODEARRAY extension for handling unicode arrays
+ with psycopg2 + normal "native unicode" mode, in the same way the
+ UNICODE extension is used.
+
+ .. change::
:tags: bug, sql
:tickets: 2883
diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
index d7ce6eb90..c3c749523 100644
--- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py
+++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
@@ -212,23 +212,13 @@ class _PGNumeric(sqltypes.Numeric):
class _PGEnum(ENUM):
- def __init__(self, *arg, **kw):
- super(_PGEnum, self).__init__(*arg, **kw)
- if util.py2k:
- if self.convert_unicode:
- self.convert_unicode = "force"
-
-
-class _PGArray(ARRAY):
- def __init__(self, *arg, **kw):
- super(_PGArray, self).__init__(*arg, **kw)
- if util.py2k:
- # FIXME: this check won't work for setups that
- # have convert_unicode only on their create_engine().
- if isinstance(self.item_type, sqltypes.String) and \
- self.item_type.convert_unicode:
- self.item_type.convert_unicode = "force"
-
+ def result_processor(self, dialect, coltype):
+ if util.py2k and self.convert_unicode is True:
+ # we can't easily use PG's extensions here because
+ # the OID is on the fly, and we need to give it a python
+ # function anyway - not really worth it.
+ self.convert_unicode = "force_nocheck"
+ return super(_PGEnum, self).result_processor(dialect, coltype)
class _PGHStore(HSTORE):
def bind_processor(self, dialect):
@@ -334,7 +324,6 @@ class PGDialect_psycopg2(PGDialect):
sqltypes.Numeric: _PGNumeric,
ENUM: _PGEnum, # needs force_unicode
sqltypes.Enum: _PGEnum, # needs force_unicode
- ARRAY: _PGArray, # needs force_unicode
HSTORE: _PGHStore,
}
)
@@ -371,7 +360,7 @@ class PGDialect_psycopg2(PGDialect):
@util.memoized_property
def _isolation_lookup(self):
- extensions = __import__('psycopg2.extensions').extensions
+ from psycopg2 import extensions
return {
'AUTOCOMMIT': extensions.ISOLATION_LEVEL_AUTOCOMMIT,
'READ COMMITTED': extensions.ISOLATION_LEVEL_READ_COMMITTED,
@@ -409,6 +398,7 @@ class PGDialect_psycopg2(PGDialect):
if self.dbapi and self.use_native_unicode:
def on_connect(conn):
extensions.register_type(extensions.UNICODE, conn)
+ extensions.register_type(extensions.UNICODEARRAY, conn)
fns.append(on_connect)
if self.dbapi and self.use_native_hstore:
diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py
index 1c475a28b..6ed20084b 100644
--- a/lib/sqlalchemy/sql/sqltypes.py
+++ b/lib/sqlalchemy/sql/sqltypes.py
@@ -192,13 +192,18 @@ class String(Concatenable, TypeEngine):
wants_unicode = self.convert_unicode or dialect.convert_unicode
needs_convert = wants_unicode and \
(dialect.returns_unicode_strings is not True or
- self.convert_unicode == 'force')
+ self.convert_unicode in ('force', 'force_nocheck'))
+ needs_isinstance = (
+ needs_convert and
+ dialect.returns_unicode_strings and
+ self.convert_unicode != 'force_nocheck'
+ )
if needs_convert:
to_unicode = processors.to_unicode_processor_factory(
dialect.encoding, self.unicode_error)
- if dialect.returns_unicode_strings:
+ if needs_isinstance:
# we wouldn't be here unless convert_unicode='force'
# was specified, or the driver has erratic unicode-returning
# habits. since we will be getting back unicode