~psycopg/psycopg/2.0.x

« back to all changes in this revision

Viewing changes to lib/extras.py

  • Committer: Federico Di Gregorio
  • Date: 2004-11-19 15:50:57 UTC
  • Revision ID: fog-f158b25706fb1159b0aa686499aa8f9df5b4e696
DictCursor fixes again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    __query_executed = 0
21
21
    
22
22
    def execute(self, query, vars=None, async=0):
23
 
        self.tuple_factory = DictRow
 
23
        self.row_factory = DictRow
24
24
        self.index = {}
25
25
        self.__query_executed = 1
26
26
        return _cursor.execute(self, query, vars, async)
27
27
 
28
28
    def _build_index(self):
29
 
        if self.description:
 
29
        if self.__query_executed == 1 and self.description:
30
30
            for i in range(len(self.description)):
31
31
                self.index[self.description[i][0]] = i
32
 
                
 
32
            self.__query_executed = 0
 
33
            
33
34
    def fetchone(self):
 
35
        res = _cursor.fetchone(self)
34
36
        if self.__query_executed:
35
37
            self._build_index()
36
 
        return _cursor.fetchone(self)
 
38
        return res
37
39
 
38
40
    def fetchmany(self, size=None):
 
41
        res = _cursor.fetchmany(self, size)
39
42
        if self.__query_executed:
40
43
            self._build_index()
41
 
        return _cursor.fetchmany(self, size)
 
44
        return res
42
45
 
43
46
    def fetchall(self):
 
47
        res = _cursor.fetchall(self)
44
48
        if self.__query_executed:
45
49
            self._build_index()
46
 
        return _cursor.fetchall(self)
 
50
        return res
47
51
        
48
52
class DictRow(list):
49
53
    """A row object that allow by-colun-name access to data."""
51
55
    def __init__(self, cursor):
52
56
        self._cursor = cursor
53
57
        self[:] = [None] * len(cursor.description)
54
 
        print cursor, self
55
58
 
56
59
    def __getitem__(self, x):
57
60
        if type(x) != int: