commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5285 - in trunk/gnue-common/src/datasources/drivers: DBSIG2 mysql/mysql


From: reinhard
Subject: r5285 - in trunk/gnue-common/src/datasources/drivers: DBSIG2 mysql/mysql postgresql/Base postgresql/pygresql
Date: Tue, 9 Mar 2004 17:18:21 -0600 (CST)

Author: reinhard
Date: 2004-03-09 17:18:21 -0600 (Tue, 09 Mar 2004)
New Revision: 5285

Modified:
   trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py
   trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py
   trunk/gnue-common/src/datasources/drivers/DBSIG2/RecordSet.py
   trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py
   trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py
   trunk/gnue-common/src/datasources/drivers/postgresql/pygresql/Driver.py
Log:
Fixed DBSIG2 framework and postgresql and mysql drivers to be *really* unicode
safe (wrt queries).


Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py      
2004-03-09 23:13:21 UTC (rev 5284)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py      
2004-03-09 23:18:21 UTC (rev 5285)
@@ -33,11 +33,12 @@
 
 __all__ = ['Connection']
 
+from types import *
+
 from gnue.common.datasources import Exceptions
 from gnue.common.datasources.drivers.Base.Connection import Connection as 
BaseConnection
 from gnue.common.apps import GDebug
 import string
-import types
 
 class Connection(BaseConnection):
 
@@ -85,17 +86,50 @@
   # Extensions
   #
 
-  # Run the SQL statement 'statement'
-  def sql(self, statement):
-    cursor = self.native.cursor()
+  # Create a new DBSIG2 cursor object and execute the given SQL statement
+  # Statement can be a normal string or a unicode string
+  def makecursor (self, statement):
+    if isinstance (statement, UnicodeType):
+      s = statement.encode (self._encoding)
+    else:
+      s = statement
     try:
-      cursor.execute(statement)
+      cursor = self.native.cursor ()
+      cursor.execute (s)
     except:
-      cursor.close()
+      cursor.close ()
       raise
+    return cursor
+
+  # Execute the given SQL statement and return the result matrix
+  # Statement can be a normal string or a unicode string
+  def sql (self, statement):
+    result = None
+    cursor = self.makecursor (statement)
     try:
-      rs = cursor.fetchall()
-      cursor.close()
-      return rs
+      # This generates an exception if the statement didn't generate a
+      # resultset
+      result = cursor.fetchall ()
     except:
+      pass
+    cursor.close ()
+
+    if result:
+      return result
+    else:
       return []
+
+  # Execute the given SQL statement that is expected to return a single value
+  # If the query returns nothing, None is returned
+  # Statement can be a normal string or a unicode string
+  def sql1 (self, statement):
+    cursor = self.makecursor (statement)
+    try:
+      result = cursor.fetchone ()
+    finally:
+      cursor.close ()
+
+    if result:
+      return result [0]
+    else:
+      return None

Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py      
2004-03-09 23:13:21 UTC (rev 5284)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py      
2004-03-09 23:18:21 UTC (rev 5285)
@@ -141,7 +141,7 @@
                                        "%s'" % self._escapeSingleQuote)      
 
       elif type(value) == types.UnicodeType:
-        return "'%s'" % 
string.replace(value.encode(self._connection._encoding),
+        return "'%s'" % string.replace(value,
                                        "'",
                                        "%s'" % self._escapeSingleQuote)
 
@@ -173,14 +173,9 @@
     if not self._primaryIdChecked: self._checkForPrimaryId()
 
     try:
-      cursor = self._connection.native.cursor()
-
+      query = self._buildQuery (conditions, additionalSQL = sql)
+      cursor = self._connection.makecursor (query)
       cursor.arraysize = self.cache
-      
-      try:
-        cursor.execute(self._buildQuery(conditions, additionalSQL=sql))
-      except TypeError:
-        cursor.execute(str(self._buildQuery(conditions, additionalSQL=sql)))
 
       # pull a record count
       if self._strictQueryCount:
@@ -200,17 +195,11 @@
     return rs
 
   def _getQueryCount(self,conditions={},sql=""):
-    cursor = self._connection.native.cursor()
+    query = self._buildQueryCount (conditions, additionalSQL = sql)
+    rs = self._connection.sql1 (query)
+    return int (rs[0])
 
-    try:
-      cursor.execute(self._buildQueryCount(conditions, additionalSQL=sql))
-    except TypeError:
-      cursor.execute(str(self._buildQueryCount(conditions, additionalSQL=sql)))
 
-    rs = cursor.fetchone()
-    return int(rs[0])
-
-
   # Used to convert a condition tree to an sql where clause
   def _conditionToSQL (self, condition):
     if condition == {} or condition == None:

Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/RecordSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/RecordSet.py       
2004-03-09 23:13:21 UTC (rev 5284)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/RecordSet.py       
2004-03-09 23:18:21 UTC (rev 5285)
@@ -56,13 +56,7 @@
     GDebug.printMesg(5, "_postChanges: statement=%s" % statement)
 
     try:
-      try:
-        self._parent._update_cursor.execute(statement)
-        
-      # catch errors, raised, if unicode type isn't accepted
-      except TypeError:
-        # TODO: move this conversion into statement creation
-        self._parent._update_cursor.execute(str(statement))
+      self._parent._dataObject._connection.sql (statement)
 
       # Set _initialData to be the just-now posted values
       if not self._deleteFlag:

Modified: trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py 
2004-03-09 23:13:21 UTC (rev 5284)
+++ trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py 
2004-03-09 23:18:21 UTC (rev 5285)
@@ -103,27 +103,8 @@
 
   # Return the current date, according to database
   def getTimeStamp(self):
-    return self.__singleQuery("select current_timestamp")
+    return self.sql1 ("select current_timestamp")
 
   # Return a sequence number from sequence 'name'
   #def getSequence(self, name):
   #  raise "Not supported"
-
-  # Used internally
-  def __singleQuery(self, statement):
-    cursor = self.native.cursor()
-    try:
-      cursor.execute(statement)
-      rv = cursor.fetchone()
-      cursor.close()
-    except:
-      print "DBdriver.py", "You've got your bar in my foo! And you've got your 
foo on my bar!  Two great reams that ream well together!"
-      GDebug.printMesg(1,"**** Unable to execute extension query")
-      GDebug.printMesg(1,"**** %s" % sys.exc_info()[1])
-      cursor.close()
-      raise
-
-    try:
-      return rv[0]
-    except:
-      return None

Modified: 
trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py     
2004-03-09 23:13:21 UTC (rev 5284)
+++ trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py     
2004-03-09 23:18:21 UTC (rev 5285)
@@ -85,26 +85,17 @@
       raise Exceptions.LoginError, _("The PostgreSQL driver returned the 
following error:\n\t%s") % value
 
     try:
-      try:
-        self._pg_encoding = pg_encTable[self._encoding]
-      except KeyError:
-        GDebug.printMesg(1,_("Encoding '%s' is not supported by postgresql 
dbdriver.") % \
-                         self._encoding + _('Using default encoding.'))
-        self._pg_encoding = ''
+      self._pg_encoding = pg_encTable[self._encoding]
+    except KeyError:
+      GDebug.printMesg(1,_("Encoding '%s' is not supported by postgresql 
dbdriver.") % \
+                       self._encoding + _('Using default encoding.'))
+      self._pg_encoding = ''
 
-      if self._pg_encoding not in ("",'DEFAULT'):
-        GDebug.printMesg(1,'Setting postgresql client_encoding to %s (%s)' % 
(self._pg_encoding,
+    if self._pg_encoding not in ("",'DEFAULT'):
+      GDebug.printMesg(1,'Setting postgresql client_encoding to %s (%s)' % 
(self._pg_encoding,
                                                                               
self._encoding))
-        cursor = self.native.cursor()
-        cursor.execute("SET CLIENT_ENCODING TO '%s'" % self._pg_encoding)
-        cursor.close()
+      self.sql ("SET CLIENT_ENCODING TO '%s'" % self._pg_encoding)
 
-    except self._DatabaseError:
-      try:
-        cursor.close()
-      except:
-        pass
-
     if connectData.has_key('datetimeformat'):
       self._dateTimeFormat = "'%s'" % connectData['datetimeformat']
     else:
@@ -118,31 +109,12 @@
 
   # Return the current date, according to database
   def getTimeStamp(self):
-    return self.__singleQuery("select current_timestamp")
+    return self.sql1 ("select current_timestamp")
 
   # Return a sequence number from sequence 'name'
   def getSequence(self, name):
-    return self.__singleQuery("select nextval('%s')" % name)
+    return self.sql1 ("select nextval('%s')" % name)
 
-  # Used internally
-  def __singleQuery(self, statement):
-    cursor = self.native.cursor()
-    try:
-      cursor.execute(statement)
-      rv = cursor.fetchone()
-      cursor.close()
-    except:
-      print "DBdriver.py", "You've got your bar in my foo! And you've got your 
foo on my bar!  Two great reams that ream well together!"
-      GDebug.printMesg(1,"**** Unable to execute extension query")
-      GDebug.printMesg(1,"**** %s" % sys.exc_info()[1])
-      cursor.close()
-      raise
-
-    try:
-      return rv[0]
-    except:
-      return None
-
 pg_encTable =  {'ascii'     :  'SQL_ASCII',     # ASCII
                 ''          :  'EUC_JP',        # Japanese EUC
                 ''          :  'EUC_CN',       # Chinese EUC

Modified: 
trunk/gnue-common/src/datasources/drivers/postgresql/pygresql/Driver.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/postgresql/pygresql/Driver.py     
2004-03-09 23:13:21 UTC (rev 5284)
+++ trunk/gnue-common/src/datasources/drivers/postgresql/pygresql/Driver.py     
2004-03-09 23:18:21 UTC (rev 5285)
@@ -58,17 +58,7 @@
     except self._DatabaseError, value:
       raise GDataObjects.LoginError, value
 
-    try:
+    if connectData.has_key ('encoding'):
       encoding = connectData['encoding']
       GDebug.printMesg(1,'Setting postgresql client_encoding to %s' % encoding)
-      cursor = self.native.cursor()
-      cursor.execute("SET CLIENT_ENCODING TO '%s'" % encoding)
-      cursor.close()
-    except KeyError:
-      pass
-    except self._DatabaseError:
-      try:
-        cursor.close()
-      except:
-        pass
-
+      self.sql ("SET CLIENT_ENCODING TO '%s'" % encoding)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]