ZPsycopgDA now uses connection-level type-casters.

This commit is contained in:
Federico Di Gregorio 2007-02-23 15:23:06 +00:00
parent 6598a279e2
commit 2ae0c32031
3 changed files with 19 additions and 17 deletions

View File

@ -125,30 +125,23 @@ class Connection(Shared.DC.ZRDB.Connection.Connection):
raise ImportError("psycopg version mismatch (imported %s)" %
psycopg2.__version__)
self.set_type_casts()
self._v_connected = ''
dbf = self.factory()
# TODO: let the psycopg exception propagate, or not?
self._v_database_connection = dbf(
self.connection_string, self.tilevel, self.encoding)
self.connection_string, self.tilevel, self.get_type_casts(), self.encoding)
self._v_database_connection.open()
self._v_connected = DateTime()
return self
def set_type_casts(self):
def get_type_casts(self):
# note that in both cases order *is* important
if self.zdatetime:
# use zope internal datetime routines
register_type(ZDATETIME)
register_type(ZDATE)
register_type(ZTIME)
return ZDATETIME, ZDATE, ZTIME
else:
# use the standard
register_type(DATETIME)
register_type(DATE)
register_type(TIME)
return DATETIME, DATE, TIME
## browsing and table/column management ##

View File

@ -27,6 +27,7 @@ import pool
import psycopg2
from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE, TIME
from psycopg2.extensions import register_type
from psycopg2 import NUMBER, STRING, ROWID, DATETIME
@ -36,9 +37,10 @@ class DB(TM, dbi_db.DB):
_p_oid = _p_changed = _registered = None
def __init__(self, dsn, tilevel, enc='utf-8'):
def __init__(self, dsn, tilevel, typecasts, enc='utf-8'):
self.dsn = dsn
self.tilevel = tilevel
self.typecasts = typecasts
self.encoding = enc
self.failures = 0
self.calls = 0
@ -47,6 +49,9 @@ class DB(TM, dbi_db.DB):
def getconn(self, create=True):
conn = pool.getconn(self.dsn)
conn.set_isolation_level(int(self.tilevel))
conn.set_client_encoding(self.encoding)
for tc in self.typecasts:
register_type(tc, conn)
return conn
def putconn(self, close=False):
@ -159,9 +164,6 @@ class DB(TM, dbi_db.DB):
try:
for qs in [x for x in query_string.split('\0') if x]:
if type(qs) == unicode:
if self.encoding:
qs = qs.encode(self.encoding)
try:
if query_data:
c.execute(qs, query_data)

View File

@ -236,6 +236,9 @@ conn_switch_isolation_level(connectionObject *self, int level)
{
int res = 0;
/* if the current isolation level is equal to the requested one don't switch */
if (self->isolation_level == level) return 0;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&self->lock);
@ -264,6 +267,10 @@ conn_set_client_encoding(connectionObject *self, char *enc)
char query[48];
int res = 0;
/* If the current encoding is equal to the requested one we don't
issue any query to the backend */
if (strcmp(self->encoding, enc) == 0) return 0;
/* TODO: check for async query here and raise error if necessary */
Py_BEGIN_ALLOW_THREADS;