From 4b614b9b35cd2baddb7ca67c04bee5d70ec6a172 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 27 Apr 2013 19:53:57 -0400 Subject: - the raw 2to3 run - went through examples/ and cleaned out excess list() calls --- lib/sqlalchemy/dialects/postgresql/base.py | 61 ++++++++++++++------------ lib/sqlalchemy/dialects/postgresql/hstore.py | 4 +- lib/sqlalchemy/dialects/postgresql/psycopg2.py | 30 ++++++------- 3 files changed, 49 insertions(+), 46 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 1acdb57b9..992995434 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1419,7 +1419,7 @@ class PGDialect(default.DefaultDialect): query, bindparams=[ sql.bindparam( - 'schema', unicode(schema.lower()), + 'schema', str(schema.lower()), type_=sqltypes.Unicode)] ) ) @@ -1435,7 +1435,7 @@ class PGDialect(default.DefaultDialect): "n.oid=c.relnamespace where n.nspname=current_schema() and " "relname=:name", bindparams=[ - sql.bindparam('name', unicode(table_name), + sql.bindparam('name', str(table_name), type_=sqltypes.Unicode)] ) ) @@ -1447,9 +1447,9 @@ class PGDialect(default.DefaultDialect): "relname=:name", bindparams=[ sql.bindparam('name', - unicode(table_name), type_=sqltypes.Unicode), + str(table_name), type_=sqltypes.Unicode), sql.bindparam('schema', - unicode(schema), type_=sqltypes.Unicode)] + str(schema), type_=sqltypes.Unicode)] ) ) return bool(cursor.first()) @@ -1463,7 +1463,7 @@ class PGDialect(default.DefaultDialect): "n.nspname=current_schema() " "and relname=:name", bindparams=[ - sql.bindparam('name', unicode(sequence_name), + sql.bindparam('name', str(sequence_name), type_=sqltypes.Unicode) ] ) @@ -1475,10 +1475,10 @@ class PGDialect(default.DefaultDialect): "n.oid=c.relnamespace where relkind='S' and " "n.nspname=:schema and relname=:name", bindparams=[ - sql.bindparam('name', unicode(sequence_name), + sql.bindparam('name', str(sequence_name), type_=sqltypes.Unicode), sql.bindparam('schema', - unicode(schema), type_=sqltypes.Unicode) + str(schema), type_=sqltypes.Unicode) ] ) ) @@ -1488,9 +1488,9 @@ class PGDialect(default.DefaultDialect): def has_type(self, connection, type_name, schema=None): bindparams = [ sql.bindparam('typname', - unicode(type_name), type_=sqltypes.Unicode), + str(type_name), type_=sqltypes.Unicode), sql.bindparam('nspname', - unicode(schema), type_=sqltypes.Unicode), + str(schema), type_=sqltypes.Unicode), ] if schema is not None: query = """ @@ -1546,9 +1546,9 @@ class PGDialect(default.DefaultDialect): """ % schema_where_clause # Since we're binding to unicode, table_name and schema_name must be # unicode. - table_name = unicode(table_name) + table_name = str(table_name) if schema is not None: - schema = unicode(schema) + schema = str(schema) s = sql.text(query, bindparams=[ sql.bindparam('table_name', type_=sqltypes.Unicode), sql.bindparam('schema', type_=sqltypes.Unicode) @@ -1570,13 +1570,14 @@ class PGDialect(default.DefaultDialect): """ rp = connection.execute(s) # what about system tables? - # Py3K - #schema_names = [row[0] for row in rp \ - # if not row[0].startswith('pg_')] - # Py2K - schema_names = [row[0].decode(self.encoding) for row in rp \ +# start Py3K + schema_names = [row[0] for row in rp \ if not row[0].startswith('pg_')] - # end Py2K +# end Py3K +# start Py2K +# schema_names = [row[0].decode(self.encoding) for row in rp \ +# if not row[0].startswith('pg_')] +# end Py2K return schema_names @reflection.cache @@ -1587,7 +1588,7 @@ class PGDialect(default.DefaultDialect): current_schema = self.default_schema_name result = connection.execute( - sql.text(u"SELECT relname FROM pg_class c " + sql.text("SELECT relname FROM pg_class c " "WHERE relkind = 'r' " "AND '%s' = (select nspname from pg_namespace n " "where n.oid = c.relnamespace) " % @@ -1610,12 +1611,13 @@ class PGDialect(default.DefaultDialect): AND '%(schema)s' = (select nspname from pg_namespace n where n.oid = c.relnamespace) """ % dict(schema=current_schema) - # Py3K - #view_names = [row[0] for row in connection.execute(s)] - # Py2K - view_names = [row[0].decode(self.encoding) - for row in connection.execute(s)] - # end Py2K +# start Py3K + view_names = [row[0] for row in connection.execute(s)] +# end Py3K +# start Py2K +# view_names = [row[0].decode(self.encoding) +# for row in connection.execute(s)] +# end Py2K return view_names @reflection.cache @@ -1632,11 +1634,12 @@ class PGDialect(default.DefaultDialect): rp = connection.execute(sql.text(s), view_name=view_name, schema=current_schema) if rp: - # Py3K - #view_def = rp.scalar() - # Py2K - view_def = rp.scalar().decode(self.encoding) - # end Py2K +# start Py3K + view_def = rp.scalar() +# end Py3K +# start Py2K +# view_def = rp.scalar().decode(self.encoding) +# end Py2K return view_def @reflection.cache diff --git a/lib/sqlalchemy/dialects/postgresql/hstore.py b/lib/sqlalchemy/dialects/postgresql/hstore.py index e555a1afd..d7fd34d05 100644 --- a/lib/sqlalchemy/dialects/postgresql/hstore.py +++ b/lib/sqlalchemy/dialects/postgresql/hstore.py @@ -96,14 +96,14 @@ def _serialize_hstore(val): def esc(s, position): if position == 'value' and s is None: return 'NULL' - elif isinstance(s, basestring): + elif isinstance(s, str): return '"%s"' % s.replace('"', r'\"') else: raise ValueError("%r in %s position is not a string." % (s, position)) return ', '.join('%s=>%s' % (esc(k, 'key'), esc(v, 'value')) - for k, v in val.iteritems()) + for k, v in val.items()) class HSTORE(sqltypes.Concatenable, sqltypes.TypeEngine): diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 805fc72af..2893a3872 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -142,7 +142,7 @@ type. This replaces SQLAlchemy's pure-Python HSTORE coercion which takes effect for other DBAPIs. """ -from __future__ import absolute_import + import re import logging @@ -190,22 +190,22 @@ class _PGNumeric(sqltypes.Numeric): class _PGEnum(ENUM): def __init__(self, *arg, **kw): super(_PGEnum, self).__init__(*arg, **kw) - # Py2K - if self.convert_unicode: - self.convert_unicode = "force" - # end Py2K +# start Py2K +# if self.convert_unicode: +# self.convert_unicode = "force" +# end Py2K class _PGArray(ARRAY): def __init__(self, *arg, **kw): super(_PGArray, self).__init__(*arg, **kw) - # 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" - # end Py2K +# start 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" +# end Py2K class _PGHStore(HSTORE): @@ -294,9 +294,9 @@ class PGIdentifierPreparer_psycopg2(PGIdentifierPreparer): class PGDialect_psycopg2(PGDialect): driver = 'psycopg2' - # Py2K - supports_unicode_statements = False - # end Py2K +# start Py2K +# supports_unicode_statements = False +# end Py2K default_paramstyle = 'pyformat' supports_sane_multi_rowcount = False execution_ctx_cls = PGExecutionContext_psycopg2 -- cgit v1.2.1 From 734376130f01d17acc0d6c2d34c1e0f94a1cd1da Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 28 Apr 2013 16:27:23 -0400 Subject: postgresql dialect tests --- lib/sqlalchemy/dialects/postgresql/base.py | 25 ++++++++++++------------ lib/sqlalchemy/dialects/postgresql/psycopg2.py | 27 +++++++++++++------------- 2 files changed, 25 insertions(+), 27 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 992995434..cd5d9772d 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -188,7 +188,6 @@ underlying CREATE INDEX command, so it *must* be a valid index type for your version of PostgreSQL. """ - import re from ... import sql, schema, exc, util @@ -333,7 +332,7 @@ class UUID(sqltypes.TypeEngine): if self.as_uuid: def process(value): if value is not None: - value = str(value) + value = util.text_type(value) return value return process else: @@ -1419,7 +1418,7 @@ class PGDialect(default.DefaultDialect): query, bindparams=[ sql.bindparam( - 'schema', str(schema.lower()), + 'schema', util.text_type(schema.lower()), type_=sqltypes.Unicode)] ) ) @@ -1435,7 +1434,7 @@ class PGDialect(default.DefaultDialect): "n.oid=c.relnamespace where n.nspname=current_schema() and " "relname=:name", bindparams=[ - sql.bindparam('name', str(table_name), + sql.bindparam('name', util.text_type(table_name), type_=sqltypes.Unicode)] ) ) @@ -1447,9 +1446,9 @@ class PGDialect(default.DefaultDialect): "relname=:name", bindparams=[ sql.bindparam('name', - str(table_name), type_=sqltypes.Unicode), + util.text_type(table_name), type_=sqltypes.Unicode), sql.bindparam('schema', - str(schema), type_=sqltypes.Unicode)] + util.text_type(schema), type_=sqltypes.Unicode)] ) ) return bool(cursor.first()) @@ -1463,7 +1462,7 @@ class PGDialect(default.DefaultDialect): "n.nspname=current_schema() " "and relname=:name", bindparams=[ - sql.bindparam('name', str(sequence_name), + sql.bindparam('name', util.text_type(sequence_name), type_=sqltypes.Unicode) ] ) @@ -1475,10 +1474,10 @@ class PGDialect(default.DefaultDialect): "n.oid=c.relnamespace where relkind='S' and " "n.nspname=:schema and relname=:name", bindparams=[ - sql.bindparam('name', str(sequence_name), + sql.bindparam('name', util.text_type(sequence_name), type_=sqltypes.Unicode), sql.bindparam('schema', - str(schema), type_=sqltypes.Unicode) + util.text_type(schema), type_=sqltypes.Unicode) ] ) ) @@ -1488,9 +1487,9 @@ class PGDialect(default.DefaultDialect): def has_type(self, connection, type_name, schema=None): bindparams = [ sql.bindparam('typname', - str(type_name), type_=sqltypes.Unicode), + util.text_type(type_name), type_=sqltypes.Unicode), sql.bindparam('nspname', - str(schema), type_=sqltypes.Unicode), + util.text_type(schema), type_=sqltypes.Unicode), ] if schema is not None: query = """ @@ -1546,9 +1545,9 @@ class PGDialect(default.DefaultDialect): """ % schema_where_clause # Since we're binding to unicode, table_name and schema_name must be # unicode. - table_name = str(table_name) + table_name = util.text_type(table_name) if schema is not None: - schema = str(schema) + schema = util.text_type(schema) s = sql.text(query, bindparams=[ sql.bindparam('table_name', type_=sqltypes.Unicode), sql.bindparam('schema', type_=sqltypes.Unicode) diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 2893a3872..ce06aab05 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -142,6 +142,7 @@ type. This replaces SQLAlchemy's pure-Python HSTORE coercion which takes effect for other DBAPIs. """ +from __future__ import absolute_import import re import logging @@ -190,22 +191,20 @@ class _PGNumeric(sqltypes.Numeric): class _PGEnum(ENUM): def __init__(self, *arg, **kw): super(_PGEnum, self).__init__(*arg, **kw) -# start Py2K -# if self.convert_unicode: -# self.convert_unicode = "force" -# end Py2K + 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) -# start 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" -# end Py2K + 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" class _PGHStore(HSTORE): @@ -294,9 +293,9 @@ class PGIdentifierPreparer_psycopg2(PGIdentifierPreparer): class PGDialect_psycopg2(PGDialect): driver = 'psycopg2' -# start Py2K -# supports_unicode_statements = False -# end Py2K + if util.py2k: + supports_unicode_statements = False + default_paramstyle = 'pyformat' supports_sane_multi_rowcount = False execution_ctx_cls = PGExecutionContext_psycopg2 -- cgit v1.2.1 From b3654ee37abe7af3d83098cd8de1980369a3fcba Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 26 May 2013 16:39:50 -0400 Subject: postgresql tests --- lib/sqlalchemy/dialects/postgresql/base.py | 36 +++++++++++++--------------- lib/sqlalchemy/dialects/postgresql/hstore.py | 4 +++- 2 files changed, 19 insertions(+), 21 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index cd5d9772d..00d0acc2c 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1569,14 +1569,13 @@ class PGDialect(default.DefaultDialect): """ rp = connection.execute(s) # what about system tables? -# start Py3K - schema_names = [row[0] for row in rp \ + + if util.py2k: + schema_names = [row[0].decode(self.encoding) for row in rp \ + if not row[0].startswith('pg_')] + else: + schema_names = [row[0] for row in rp \ if not row[0].startswith('pg_')] -# end Py3K -# start Py2K -# schema_names = [row[0].decode(self.encoding) for row in rp \ -# if not row[0].startswith('pg_')] -# end Py2K return schema_names @reflection.cache @@ -1610,13 +1609,12 @@ class PGDialect(default.DefaultDialect): AND '%(schema)s' = (select nspname from pg_namespace n where n.oid = c.relnamespace) """ % dict(schema=current_schema) -# start Py3K - view_names = [row[0] for row in connection.execute(s)] -# end Py3K -# start Py2K -# view_names = [row[0].decode(self.encoding) -# for row in connection.execute(s)] -# end Py2K + + if util.py2k: + view_names = [row[0].decode(self.encoding) + for row in connection.execute(s)] + else: + view_names = [row[0] for row in connection.execute(s)] return view_names @reflection.cache @@ -1633,12 +1631,10 @@ class PGDialect(default.DefaultDialect): rp = connection.execute(sql.text(s), view_name=view_name, schema=current_schema) if rp: -# start Py3K - view_def = rp.scalar() -# end Py3K -# start Py2K -# view_def = rp.scalar().decode(self.encoding) -# end Py2K + if util.py2k: + view_def = rp.scalar().decode(self.encoding) + else: + view_def = rp.scalar() return view_def @reflection.cache diff --git a/lib/sqlalchemy/dialects/postgresql/hstore.py b/lib/sqlalchemy/dialects/postgresql/hstore.py index d7fd34d05..adfb82da7 100644 --- a/lib/sqlalchemy/dialects/postgresql/hstore.py +++ b/lib/sqlalchemy/dialects/postgresql/hstore.py @@ -10,6 +10,8 @@ from .base import ARRAY, ischema_names from ... import types as sqltypes from ...sql import functions as sqlfunc from ...sql.operators import custom_op +from ... import util + __all__ = ('HSTORE', 'hstore') @@ -96,7 +98,7 @@ def _serialize_hstore(val): def esc(s, position): if position == 'value' and s is None: return 'NULL' - elif isinstance(s, str): + elif isinstance(s, util.string_types): return '"%s"' % s.replace('"', r'\"') else: raise ValueError("%r in %s position is not a string." % -- cgit v1.2.1 From 534e05888e1ec7018c03d979df4f34074a61f12b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 29 May 2013 18:08:28 -0400 Subject: hstores are text, and in py3k they seem to be implcitly unicode. so add unicode encoding for py2k for the non-native hstore, pullreq for native psycopg2 support coming.... --- lib/sqlalchemy/dialects/postgresql/hstore.py | 36 ++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/hstore.py b/lib/sqlalchemy/dialects/postgresql/hstore.py index adfb82da7..b0b13eb0e 100644 --- a/lib/sqlalchemy/dialects/postgresql/hstore.py +++ b/lib/sqlalchemy/dialects/postgresql/hstore.py @@ -262,19 +262,35 @@ class HSTORE(sqltypes.Concatenable, sqltypes.TypeEngine): _adapt_expression(self, op, other_comparator) def bind_processor(self, dialect): - def process(value): - if isinstance(value, dict): - return _serialize_hstore(value) - else: - return value + if util.py2k: + encoding = dialect.encoding + def process(value): + if isinstance(value, dict): + return _serialize_hstore(value).encode(encoding) + else: + return value + else: + def process(value): + if isinstance(value, dict): + return _serialize_hstore(value) + else: + return value return process def result_processor(self, dialect, coltype): - def process(value): - if value is not None: - return _parse_hstore(value) - else: - return value + if util.py2k: + encoding = dialect.encoding + def process(value): + if value is not None: + return _parse_hstore(value.decode(encoding)) + else: + return value + else: + def process(value): + if value is not None: + return _parse_hstore(value) + else: + return value return process -- cgit v1.2.1 From d02d86a11452b67981e2c9637cedbfc902b237c5 Mon Sep 17 00:00:00 2001 From: Dmitry Mugtasimov Date: Tue, 28 May 2013 16:50:12 +0400 Subject: Unicode support for psycopg2 native hstore implementation --- lib/sqlalchemy/dialects/postgresql/psycopg2.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index ce06aab05..da333e8eb 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -392,7 +392,8 @@ class PGDialect_psycopg2(PGDialect): hstore_oids = self._hstore_oids(conn) if hstore_oids is not None: oid, array_oid = hstore_oids - extras.register_hstore(conn, oid=oid, array_oid=array_oid) + extras.register_hstore(conn, oid=oid, array_oid=array_oid, + unicode=True) fns.append(on_connect) if fns: -- cgit v1.2.1 From 6c4a61b07a7aec8f6043c3e82eded27dda631cf4 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 29 May 2013 18:29:12 -0400 Subject: - repair for py3k - fix test --- lib/sqlalchemy/dialects/postgresql/psycopg2.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index da333e8eb..fcc1946ff 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -392,8 +392,13 @@ class PGDialect_psycopg2(PGDialect): hstore_oids = self._hstore_oids(conn) if hstore_oids is not None: oid, array_oid = hstore_oids - extras.register_hstore(conn, oid=oid, array_oid=array_oid, + if util.py2k: + extras.register_hstore(conn, oid=oid, + array_oid=array_oid, unicode=True) + else: + extras.register_hstore(conn, oid=oid, + array_oid=array_oid) fns.append(on_connect) if fns: -- cgit v1.2.1