commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r7326 - trunk/gnue-common/src/datasources/drivers/DBSIG2


From: reinhard
Subject: [gnue] r7326 - trunk/gnue-common/src/datasources/drivers/DBSIG2
Date: Fri, 8 Apr 2005 17:43:06 -0500 (CDT)

Author: reinhard
Date: 2005-04-08 17:43:05 -0500 (Fri, 08 Apr 2005)
New Revision: 7326

Modified:
   trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py
   trunk/gnue-common/src/datasources/drivers/DBSIG2/ResultSet.py
Log:
*Experimental*
Use new _query/_count/_fetch schema for DBSIG2 driver. This temporarly breaks
Query by detail.


Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py      
2005-04-08 22:02:10 UTC (rev 7325)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/DataObject.py      
2005-04-08 22:43:05 UTC (rev 7326)
@@ -154,33 +154,6 @@
                                        "%s'" % self._escapeSingleQuote)
 
 
-  def _createResultSet(self, conditions={}, readOnly=False,
-                       masterRecordSet=None,sql=""):
-
-    try:
-      query = self._buildQuery(conditions, additionalSQL = sql)
-      cursor = self._connection.makecursor(query)
-      cursor.arraysize = self.cache
-
-    except self._DatabaseError:
-      raise Exceptions.ConnectionError, errors.getException () [2]
-
-    rs = self._resultSetClass(self, cursor=cursor, 
masterRecordSet=masterRecordSet,
-       fieldOrder=self._fieldOrder)
-
-    if self._connection._broken_rowcount:
-      rs._recordCount = self._getQueryCount(conditions,sql)
-
-    if readOnly:
-      rs._readonly = readOnly
-
-    return rs
-
-  def _getQueryCount(self,conditions={},sql=""):
-    query = self._buildQueryCount (conditions, additionalSQL = sql)
-    return self._connection.sql1 (query)
-
-
   # Used to convert a condition tree to an sql where clause
   def _conditionToSQL (self, condition):
     if condition == {} or condition == None:
@@ -326,23 +299,6 @@
 
     return q
 
-  def _buildQueryCount(self, conditions={}, additionalSQL=""):
-    whereClause = self._conditionToSQL(conditions)
-    if additionalSQL:
-      if len(whereClause):
-        whereClause += ' and %s' % (additionalSQL)
-      else:
-        whereClause = ' WHERE %s' % (additionalSQL)
-
-
-    q = "SELECT count(*) FROM %s%s" % (self.table, whereClause)
-
-    gDebug (8, q)
-
-    return q
-
-
-
 ######################################################################
 #
 #

Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/ResultSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/ResultSet.py       
2005-04-08 22:02:10 UTC (rev 7325)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/ResultSet.py       
2005-04-08 22:43:05 UTC (rev 7326)
@@ -1,6 +1,9 @@
+# GNU Enterprise Common Library - DBSIG2 DB Driver - ResultSet
 #
-# This file is part of GNU Enterprise.
+# Copyright 2001-2005 Free Software Foundation
 #
+# This file is part of GNU Enterprise
+#
 # GNU Enterprise is free software; you can redistribute it
 # and/or modify it under the terms of the GNU General Public
 # License as published by the Free Software Foundation; either
@@ -16,106 +19,130 @@
 # write to the Free Software Foundation, Inc., 59 Temple Place
 # - Suite 330, Boston, MA 02111-1307, USA.
 #
-# Copyright 2000-2005 Free Software Foundation
-#
-# FILE:
-# _dbsig/DBdriver.py
-#
-# DESCRIPTION:
-# Generic implementation of dbdriver using Python DB-SIG v2
-# specification.
-#
-# NOTES:
-# The classes below are meant to be extended
-#
-# HISTORY:
-#
+# $Id$
 
 __all__ = ['ResultSet']
 
-from gnue.common.datasources import GConditions, Exceptions
-from gnue.common.datasources.drivers.Base import ResultSet as BaseResultSet
-from gnue.common.apps import errors
 import string
 import types
 
-class ResultSet(BaseResultSet):
+from gnue.common.datasources.drivers import Base
 
-  def __init__(self, dataObject, cursor = None, defaultValues = {},
-               readonly = False, masterRecordSet = None, fieldOrder = []):
+class ResultSet (Base.ResultSet):
+  """
+  Implementation of the ResultSet object for DBSIG2 based drivers.
+  """
 
-    BaseResultSet.__init__(self, dataObject, defaultValues = defaultValues,
-                           readonly = readonly,
-                           masterRecordSet = masterRecordSet)
+  # ---------------------------------------------------------------------------
+  # Build the query string
+  # ---------------------------------------------------------------------------
 
-    self._cursor = cursor
-    self._fieldOrder = fieldOrder
+  def __buildQuery (self, table, fieldnames, where, sortorder):
 
-    self._fieldNames = []
+    if self._dataObject.distinct:
+      distinct = 'DISTINCT '
+    else:
+      distinct = ''
 
-    if self._cursor:
-      for t in self._cursor.description:
-        name = t[0].lower ()
+    query = 'SELECT %s%s' % (distinct, string.join (fieldnames, ', '))
 
-        if not isinstance (t[0], types.UnicodeType):
-          name = unicode (name, self._dataObject._connection._encoding)
+    query += ' FROM ' + table
 
-        gDebug (8, "Field from Cursordescription: %s new %s" % (t[0], name))
+    if where:
+      query += ' WHERE ' + where
 
-        self._fieldNames.append (name)
-        self._dataObject._fieldReferences [name] = ""
+    if sortorder:
+      order = []
+      for item in sortorder:
+        field      = item ['name']
+        descending = item.get ('descending') or False
+        ignorecase = item.get ('ignorecase') or False
 
-      gDebug (8, "DBSIG2::Fields from cursor set to %s" % self._fieldNames)
+        fmt = ignorecase and "LOWER (%s)%s" or "%s%s"
+        order.append (fmt % (field, descending and ' DESC' or ''))
 
-      self._recordCount = self._cursor.rowcount or 0
+      query += ' ORDER BY ' + string.join (order, ', ')
 
-    gDebug (8, 'ResultSet created')
+    return query
 
-  def _loadNextRecord(self):
-    if self._cursor:
-      rs = None
 
-      if self._dataObject._connection._broken_fetchmany:
+  # ---------------------------------------------------------------------------
+  # Build the query string for the count
+  # ---------------------------------------------------------------------------
+
+  def __buildQueryCount (self, table, fieldnames, where):
+
+    if self._dataObject.distinct:
+      distinct = 'DISTINCT '
+    else:
+      distinct = ''
+
+    query = 'SELECT COUNT (%s%s)' % (distinct, string.join (fieldnames, ', '))
+
+    query += ' FROM ' + table
+
+    if where:
+      query += ' WHERE ' + where
+
+    return query
+
+
+  # ---------------------------------------------------------------------------
+  # Implementation of virtual methods
+  # ---------------------------------------------------------------------------
+
+  def _query_object (self, connection, table, fieldnames, condition, 
sortorder):
+    params = {}
+    where = condition.asSQL (params)
+    query = self.__buildQuery (table, fieldnames, where, sortorder)
+    self.__cursor = connection.makecursor (query, params)
+    if connection._broken_rowcount:
+      query = self.__buildQueryCount (table, fieldnames, where)
+      self.__count = connection.sql1 (query, params)
+    else:
+      self.__count = self.__cursor.rowcount
+    self.__connection = connection
+    self.__fieldnames = fieldnames
+
+  # ---------------------------------------------------------------------------
+
+  def _query_sql (self, connection, sql):
+    self.__cursor = connection.makecursor (sql)
+    if connection._broken_rowcount:
+      self.__count = 0                  # No chance to find it out
+    else:
+      self.__count = self.__cursor.rowcount
+    self.__connection = connection
+    self.__fieldnames = [(unicode (d [0], connection._encoding)).lower () \
+                         for d in self.__cursor.description]
+
+  # ---------------------------------------------------------------------------
+
+  def _count (self):
+    return self.__count
+
+  # ---------------------------------------------------------------------------
+
+  def _fetch (self, cachesize):
+    while True:
+      if self.__connection._broken_fetchmany:
         try:
-          rsets = self._cursor.fetchmany()
+          rows = self.__cursor.fetchmany (cachesize)
         except:
-          rsets = None
+          break
       else:
-        try:
-          # Pass arraysize because of mysql fetchmany bug in MySQLdb < 0.9.2
-          rsets = self._cursor.fetchmany (self._cursor.arraysize)
-        except self._dataObject._connection._DatabaseError, err:
-          raise Exceptions.ConnectionError, errors.getException()[2]
+        rows = self.__cursor.fetchmany (cachesize)
+      if not rows:
+        break
+      for row in rows:
+        result = {}
+        for (fieldname, value) in zip (self.__fieldnames, row):
+          if isinstance (value, types.StringType):
+            value = unicode (value, self.__connection._encoding)
+          result [fieldname] = value
+        yield result
 
-      if rsets and len(rsets):
-        for rs in rsets:
-          if rs:
-            i = 0
-            dict = {}
-            for f in (rs):
-              if self._dataObject._unicodeMode and type(f)==types.StringType:
-                f = unicode(f,self._dataObject._connection._encoding)
+  # ---------------------------------------------------------------------------
 
-              try:
-                name = self._fieldOrder[i]
-                if not name:
-                  raise IndexError
-              except IndexError:
-                  name = string.lower(self._fieldNames[i])
-              dict[name] = f
-              i += 1
-            self._cachedRecords.append (self._recordSetClass (parent = self,
-                initialData = dict,
-                rowidField = self._dataObject._dataSource._rowidField,
-                primarykeyFields = 
self._dataObject._dataSource._primarykeyFields))
-          else:
-            return False
-        return True
-      else:
-        # We no longer need the cursor if we are read only
-        if self._readonly:
-          self._cursor.close()
-        return False
-    else:
-      return False
-
+  def _close (self):
+    self.__cursor.close ()





reply via email to

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