summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain <syt@logilab.fr>2006-10-31 19:04:06 +0100
committerSylvain <syt@logilab.fr>2006-10-31 19:04:06 +0100
commit16ee6e31edb28e80fd4e5545a8b164f2c0472069 (patch)
tree0177e0c828006d61e616b3a86a4faa9dd0eacf3c
parentda69367843a46f5fe0e418b252f4c4479fa3362b (diff)
downloadlogilab-common-16ee6e31edb28e80fd4e5545a8b164f2c0472069.tar.gz
default postgres module is now psycopg2, which has been customized
to return mx.Datetime objects for date/time related types
-rw-r--r--ChangeLog4
-rw-r--r--db.py46
2 files changed, 45 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 8163833..a8eb52f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,7 +5,9 @@ ChangeLog for logilab.common
* umessage:
- new message_from_string function
- fixed get_payload encoding bug
-
+ * db: default postgres module is now psycopg2, which has been customized
+ to return mx.Datetime objects for date/time related types
+
2006-10-27 -- 0.20.0
* db:
- fixed date handling
diff --git a/db.py b/db.py
index 77230ae..7b66fc8 100644
--- a/db.py
+++ b/db.py
@@ -206,7 +206,47 @@ class _Psycopg2Adapter(_PsycopgAdapter):
"""Simple Psycopg2 Adapter to DBAPI (cnx_string differs from classical ones)
"""
BOOLEAN = 16 # XXX
-
+ def __init__(self, native_module, pywrap=False):
+ DBAPIAdapter.__init__(self, native_module, pywrap)
+ self._init_psycopg2()
+
+ def _init_psycopg2(self):
+ """initialize psycopg2 to use mx.DateTime for date and timestamps
+ instead for datetime.datetime"""
+ psycopg2 = self._native_module
+ if hasattr(psycopg2, '_mx_initialized'):
+ return
+ from psycopg2 import extensions
+ from mx.DateTime import strptime
+
+ def cast_date(value, cursor):
+ if value:
+ return strptime(value, '%Y-%m-%d')
+
+ DATE = extensions.new_type(extensions.DATE.values, "DATE", cast_date)
+ for v in DATE.values:
+ extensions.string_types[v] = DATE
+ extensions.DATE = DATE
+
+ def cast_datetime(value, cursor):
+ if value:
+ return strptime(value, '%Y-%m-%d %H:%M:%S')
+
+ DATETIME = extensions.new_type(psycopg2.DATETIME.values, "DATETIME", cast_datetime)
+ for v in DATETIME.values:
+ extensions.string_types[v] = DATETIME
+ psycopg2.DATETIME = DATETIME
+
+ def cast_time(value, cursor):
+ if value:
+ return strptime(value, '%H:%M:%S')
+ TIME = extensions.new_type(extensions.TIME.values, "TIME", cast_time)
+ for v in TIME.values:
+ extensions.string_types[v] = TIME
+ extensions.TIME = TIME
+
+ psycopg2._mx_initialized = 1
+
class _PgsqlAdapter(DBAPIAdapter):
"""Simple pyPgSQL Adapter to DBAPI
@@ -236,7 +276,6 @@ class _PySqlite2Adapter(DBAPIAdapter):
"""
def __init__(self, native_module, pywrap=False):
DBAPIAdapter.__init__(self, native_module, pywrap)
- from pysqlite2 import _sqlite
self._init_pysqlite2()
# no type code in pysqlite2
self.BINARY = 'XXX'
@@ -296,7 +335,6 @@ class _PySqlite2Adapter(DBAPIAdapter):
return str(bval).upper()
sqlite.register_adapter(bool, adapt_boolean)
- # import pysqlite2.dbapi2 as sqlite
sqlite._mx_initialized = 1
@@ -510,7 +548,7 @@ class _SqliteAdvFuncHelper(_GenericAdvFuncHelper):
PREFERED_DRIVERS = {
- "postgres" : [ 'psycopg', 'psycopg2', 'pgdb', 'pyPgSQL.PgSQL', ],
+ "postgres" : [ 'psycopg2', 'psycopg', 'pgdb', 'pyPgSQL.PgSQL', ],
"mysql" : [ 'MySQLdb', ], # 'pyMySQL.MySQL, ],
"sqlite" : [ 'pysqlite2.dbapi2', 'sqlite', ],
}