~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/datasources/drivers/DBSIG2/ResultSet.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2000-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# _dbsig/DBdriver.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Generic implementation of dbdriver using Python DB-SIG v2
 
26
# specification.
 
27
#
 
28
# NOTES:
 
29
# The classes below are meant to be extended
 
30
#
 
31
# HISTORY:
 
32
#
 
33
 
 
34
__all__ = ['ResultSet']
 
35
 
 
36
from gnue.common.datasources import GConditions, Exceptions
 
37
from gnue.common.datasources.drivers.Base import ResultSet as BaseResultSet
 
38
from gnue.common.apps import errors
 
39
import string
 
40
import types
 
41
 
 
42
from RecordSet import RecordSet
 
43
 
 
44
class ResultSet(BaseResultSet):
 
45
  _recordSetClass = RecordSet
 
46
  def __init__(self, *args, **parms):
 
47
    try:
 
48
      self._fieldOrder = parms['fieldOrder']
 
49
      del parms['fieldOrder']
 
50
    except KeyError:
 
51
      self._fieldOrder = []
 
52
    BaseResultSet.__init__(self, *args, **parms)
 
53
 
 
54
    self._fieldNames = []
 
55
 
 
56
    if self._cursor:
 
57
      for t in self._cursor.description:
 
58
        name = t[0].lower ()
 
59
 
 
60
        if not isinstance (t[0], types.UnicodeType):
 
61
          name = unicode (name, self._dataObject._connection._encoding)
 
62
 
 
63
        gDebug (8, "Field from Cursordescription: %s new %s" % (t[0], name))
 
64
 
 
65
        self._fieldNames.append (name)
 
66
        self._dataObject._fieldReferences [name] = ""
 
67
 
 
68
      gDebug (8, "DBSIG2::Fields from cursor set to %s" % self._fieldNames)
 
69
 
 
70
    self._recordCount = self._cursor.rowcount or 0
 
71
 
 
72
    gDebug (8, 'ResultSet created')
 
73
 
 
74
  def _loadNextRecord(self):
 
75
    if self._cursor:
 
76
      rs = None
 
77
 
 
78
      if self._dataObject._connection._broken_fetchmany:
 
79
        try:
 
80
          rsets = self._cursor.fetchmany()
 
81
        except:
 
82
          rsets = None
 
83
      else:
 
84
        try:
 
85
          # Pass arraysize because of mysql fetchmany bug in MySQLdb < 0.9.2
 
86
          rsets = self._cursor.fetchmany (self._cursor.arraysize)
 
87
        except self._dataObject._connection._DatabaseError, err:
 
88
          raise Exceptions.ConnectionError, errors.getException()[2]
 
89
 
 
90
      if rsets and len(rsets):
 
91
        for rs in rsets:
 
92
          if rs:
 
93
            i = 0
 
94
            dict = {}
 
95
            for f in (rs):
 
96
              if self._dataObject._unicodeMode and type(f)==types.StringType:
 
97
                f = unicode(f,self._dataObject._connection._encoding)
 
98
 
 
99
              try:
 
100
                name = self._fieldOrder[i]
 
101
                if not name:
 
102
                  raise IndexError
 
103
              except IndexError:
 
104
                  name = string.lower(self._fieldNames[i])
 
105
              dict[name] = f
 
106
              i += 1
 
107
            self._cachedRecords.append (self._recordSetClass(parent=self,
 
108
                                                             initialData=dict))
 
109
          else:
 
110
            return False
 
111
        return True
 
112
      else:
 
113
        # We no longer need the cursor if we are read only
 
114
        if self._readonly:
 
115
          self._cursor.close()
 
116
        return False
 
117
    else:
 
118
      return False
 
119