~sidnei/storm/mssql-support

« back to all changes in this revision

Viewing changes to storm/database.py

  • Committer: Gustavo Niemeyer
  • Date: 2007-08-07 23:15:09 UTC
  • mfrom: (156.1.8 preset-primary-key)
  • Revision ID: gustavo@niemeyer.net-20070807231509-iw58uiygzuef5wth
Merged preset-primary-key branch [r=radix,jkakar] [f=126080]

This branch introduces a new hook which allows backends to modify
variables which are part of the primary key of an object before
they are inserted in the database.  Also, lazy values may now be
used in the primary key, which means that it's possible to set these
primary variables to SQL expressions if needed.

Other minor changes include an optimization in Result: it will
preset the raw cursor's arraysize to a better value if it's found
to be 1.  Result.__iter__() was also fixed to parse rows through
the from_database hook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    def __init__(self, connection, raw_cursor):
40
40
        self._connection = connection # Ensures deallocation order.
41
41
        self._raw_cursor = raw_cursor
 
42
        if raw_cursor.arraysize == 1:
 
43
            # Default of 1 is silly.
 
44
            self._raw_cursor.arraysize = 10
42
45
 
43
46
    def __del__(self):
44
47
        try:
65
68
        return result
66
69
 
67
70
    def __iter__(self):
68
 
        if self._raw_cursor.arraysize == 1:
69
 
            while True:
70
 
                result = self._raw_cursor.fetchone()
71
 
                if not result:
72
 
                    break
73
 
                yield result
74
 
        else:
75
 
            while True:
76
 
                results = self._raw_cursor.fetchmany()
77
 
                if not results:
78
 
                    break
79
 
                for result in results:
80
 
                    yield result
 
71
        while True:
 
72
            results = self._raw_cursor.fetchmany()
 
73
            if not results:
 
74
                break
 
75
            for result in results:
 
76
                yield tuple(self._from_database(result))
81
77
 
82
78
    def get_insert_identity(self, primary_columns, primary_variables):
83
79
        raise NotImplementedError
161
157
            else:
162
158
                yield param
163
159
 
 
160
    def preset_primary_key(self, primary_columns, primary_variables):
 
161
        """Process primary variables before an insert happens.
 
162
 
 
163
        This method may be overwritten by backends to implement custom
 
164
        changes in primary variables before an insert happens.
 
165
        """
 
166
 
164
167
 
165
168
class Database(object):
166
169