summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/pg8000.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2009-11-15 19:20:22 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2009-11-15 19:20:22 +0000
commit5f6ed1a3f8bb0b2a724c7f07b98936433a3ef053 (patch)
tree8c0c389fc315127d1942db0cb566d118ddc54629 /lib/sqlalchemy/dialects/postgresql/pg8000.py
parentb14d53aba109cc33b245135ac490dc896d56100f (diff)
downloadsqlalchemy-5f6ed1a3f8bb0b2a724c7f07b98936433a3ef053.tar.gz
- pg8000 + postgresql dialects now check for float/numeric return
types to more intelligently determine float() vs. Decimal(), [ticket:1567] - since result processing is a hot issue of late, the DBAPI type returned from cursor.description is certainly useful in cases like these to determine an efficient result processor. There's likely other result processors that can make use of it. But, backwards incompat change to result_processor(). Happy major version number..
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/pg8000.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/pg8000.py49
1 files changed, 35 insertions, 14 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py
index 17fe86be6..e90bebb6b 100644
--- a/lib/sqlalchemy/dialects/postgresql/pg8000.py
+++ b/lib/sqlalchemy/dialects/postgresql/pg8000.py
@@ -21,25 +21,47 @@ Passing data from/to the Interval type is not supported as of yet.
"""
from sqlalchemy.engine import default
import decimal
-from sqlalchemy import util
+from sqlalchemy import util, exc
from sqlalchemy import types as sqltypes
-from sqlalchemy.dialects.postgresql.base import PGDialect, PGCompiler, PGIdentifierPreparer, PGExecutionContext
+from sqlalchemy.dialects.postgresql.base import PGDialect, \
+ PGCompiler, PGIdentifierPreparer, PGExecutionContext
class _PGNumeric(sqltypes.Numeric):
def bind_processor(self, dialect):
- return None
-
- def result_processor(self, dialect):
+ def process(value):
+ if value is not None:
+ return float(value)
+ else:
+ return value
+ return process
+
+ def result_processor(self, dialect, coltype):
if self.asdecimal:
- return None
+ if coltype in (700, 701):
+ def process(value):
+ if value is not None:
+ return decimal.Decimal(str(value))
+ else:
+ return value
+ return process
+ elif coltype == 1700:
+ # pg8000 returns Decimal natively for 1700
+ return None
+ else:
+ raise exc.InvalidRequestError("Unknown PG numeric type: %d" % coltype)
else:
- def process(value):
- if isinstance(value, decimal.Decimal):
- return float(value)
- else:
- return value
- return process
-
+ if coltype in (700, 701):
+ # pg8000 returns float natively for 701
+ return None
+ elif coltype == 1700:
+ def process(value):
+ if value is not None:
+ return float(value)
+ else:
+ return value
+ return process
+ else:
+ raise exc.InvalidRequestError("Unknown PG numeric type: %d" % coltype)
class PostgreSQL_pg8000ExecutionContext(PGExecutionContext):
pass
@@ -79,7 +101,6 @@ class PostgreSQL_pg8000(PGDialect):
PGDialect.colspecs,
{
sqltypes.Numeric : _PGNumeric,
- sqltypes.Float: sqltypes.Float, # prevents _PGNumeric from being used
}
)