From 4fc439e4a16f1556e98277b993f4da18fe6cc19a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 31 May 2009 22:47:50 +0000 Subject: [PATCH] initial 3.0-only driver for py-postgresql. tests are maybe 60/40 at the moment. --- lib/sqlalchemy/dialects/postgres/base.py | 4 +- .../dialects/postgres/pypostgresql.py | 80 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 lib/sqlalchemy/dialects/postgres/pypostgresql.py diff --git a/lib/sqlalchemy/dialects/postgres/base.py b/lib/sqlalchemy/dialects/postgres/base.py index 90af08867b..30a1877807 100644 --- a/lib/sqlalchemy/dialects/postgres/base.py +++ b/lib/sqlalchemy/dialects/postgres/base.py @@ -514,7 +514,7 @@ class PGDialect(default.DefaultDialect): sql.bindparam('schema', unicode(schema), type_=sqltypes.Unicode)] ) ) - return bool( cursor.rowcount ) + return bool(cursor.fetchone()) def has_sequence(self, connection, sequence_name): cursor = connection.execute( @@ -523,7 +523,7 @@ class PGDialect(default.DefaultDialect): "AND nspname != 'information_schema' AND relname = :seqname)", bindparams=[sql.bindparam('seqname', unicode(sequence_name), type_=sqltypes.Unicode)] )) - return bool(cursor.rowcount) + return bool(cursor.fetchone()) def table_names(self, connection, schema): result = connection.execute( diff --git a/lib/sqlalchemy/dialects/postgres/pypostgresql.py b/lib/sqlalchemy/dialects/postgres/pypostgresql.py new file mode 100644 index 0000000000..b032aa6a6a --- /dev/null +++ b/lib/sqlalchemy/dialects/postgres/pypostgresql.py @@ -0,0 +1,80 @@ +"""Support for the PostgreSQL database via py-postgresql. + +Connecting +---------- + +URLs are of the form `postgres+pypostgresql://user@password@host:port/dbname[?key=value&key=value...]`. + + +""" +from sqlalchemy.engine import default +import decimal +from sqlalchemy import util +from sqlalchemy import types as sqltypes +from sqlalchemy.dialects.postgres.base import PGDialect, PGDefaultRunner + +class PGNumeric(sqltypes.Numeric): + def bind_processor(self, dialect): + return None + + def result_processor(self, dialect): + if self.asdecimal: + return None + else: + def process(value): + if isinstance(value, decimal.Decimal): + return float(value) + else: + return value + return process + +class Postgres_pypostgresqlExecutionContext(default.DefaultExecutionContext): + pass + +class Postgres_pypostgresqlDefaultRunner(PGDefaultRunner): + def execute_string(self, stmt, params=None): + return PGDefaultRunner.execute_string(self, stmt, params or ()) + +class Postgres_pypostgresql(PGDialect): + driver = 'pypostgresql' + + supports_unicode_statements = True + + supports_unicode_binds = True + description_encoding = None + + defaultrunner = Postgres_pypostgresqlDefaultRunner + + default_paramstyle = 'format' + + supports_sane_rowcount = False # alas....posting a bug now + + supports_sane_multi_rowcount = False + + execution_ctx_cls = Postgres_pypostgresqlExecutionContext + colspecs = util.update_copy( + PGDialect.colspecs, + { + sqltypes.Numeric : PGNumeric, + sqltypes.Float: sqltypes.Float, # prevents PGNumeric from being used + } + ) + + @classmethod + def dbapi(cls): + from postgresql.driver import dbapi20 + return dbapi20 + + def create_connect_args(self, url): + opts = url.translate_connect_args(username='user') + if 'port' in opts: + opts['port'] = int(opts['port']) + else: + opts['port'] = 5432 + opts.update(url.query) + return ([], opts) + + def is_disconnect(self, e): + return "connection is closed" in str(e) + +dialect = Postgres_pypostgresql -- 2.47.3