2017-04-15 18:27:12 +02:00
|
|
|
# postgresql/pypostgresql.py
|
|
|
|
# Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
|
|
|
|
# <see AUTHORS file>
|
|
|
|
#
|
|
|
|
# This module is part of SQLAlchemy and is released under
|
|
|
|
# the MIT License: http://www.opensource.org/licenses/mit-license.php
|
2010-05-07 19:33:49 +02:00
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
"""
|
|
|
|
.. dialect:: postgresql+pypostgresql
|
|
|
|
:name: py-postgresql
|
|
|
|
:dbapi: pypostgresql
|
|
|
|
:connectstring: postgresql+pypostgresql://user:password@host:port/dbname\
|
|
|
|
[?key=value&key=value...]
|
|
|
|
:url: http://python.projects.pgfoundry.org/
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
2017-04-15 18:27:12 +02:00
|
|
|
from ... import util
|
|
|
|
from ... import types as sqltypes
|
|
|
|
from .base import PGDialect, PGExecutionContext
|
|
|
|
from ... import processors
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
class PGNumeric(sqltypes.Numeric):
|
|
|
|
def bind_processor(self, dialect):
|
|
|
|
return processors.to_str
|
|
|
|
|
|
|
|
def result_processor(self, dialect, coltype):
|
|
|
|
if self.asdecimal:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return processors.to_float
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
class PGExecutionContext_pypostgresql(PGExecutionContext):
|
|
|
|
pass
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
class PGDialect_pypostgresql(PGDialect):
|
|
|
|
driver = 'pypostgresql'
|
|
|
|
|
|
|
|
supports_unicode_statements = True
|
|
|
|
supports_unicode_binds = True
|
|
|
|
description_encoding = None
|
|
|
|
default_paramstyle = 'pyformat'
|
|
|
|
|
|
|
|
# requires trunk version to support sane rowcounts
|
2017-04-15 18:27:12 +02:00
|
|
|
# TODO: use dbapi version information to set this flag appropriately
|
2010-05-07 19:33:49 +02:00
|
|
|
supports_sane_rowcount = True
|
|
|
|
supports_sane_multi_rowcount = False
|
|
|
|
|
|
|
|
execution_ctx_cls = PGExecutionContext_pypostgresql
|
|
|
|
colspecs = util.update_copy(
|
|
|
|
PGDialect.colspecs,
|
|
|
|
{
|
2017-04-15 18:27:12 +02:00
|
|
|
sqltypes.Numeric: PGNumeric,
|
|
|
|
|
|
|
|
# prevents PGNumeric from being used
|
|
|
|
sqltypes.Float: sqltypes.Float,
|
2010-05-07 19:33:49 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def dbapi(cls):
|
|
|
|
from postgresql.driver import dbapi20
|
|
|
|
return dbapi20
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
_DBAPI_ERROR_NAMES = [
|
|
|
|
"Error",
|
|
|
|
"InterfaceError", "DatabaseError", "DataError",
|
|
|
|
"OperationalError", "IntegrityError", "InternalError",
|
|
|
|
"ProgrammingError", "NotSupportedError"
|
|
|
|
]
|
|
|
|
|
|
|
|
@util.memoized_property
|
|
|
|
def dbapi_exception_translation_map(self):
|
|
|
|
if self.dbapi is None:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
(getattr(self.dbapi, name).__name__, name)
|
|
|
|
for name in self._DBAPI_ERROR_NAMES
|
|
|
|
)
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
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)
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
def is_disconnect(self, e, connection, cursor):
|
2010-05-07 19:33:49 +02:00
|
|
|
return "connection is closed" in str(e)
|
|
|
|
|
|
|
|
dialect = PGDialect_pypostgresql
|