diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-01-16 22:44:04 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-01-16 22:44:04 +0000 |
| commit | abccc0624228def744b0382e84f01cf95e0d3aed (patch) | |
| tree | fca7eb29b90211daa699da6d0358f81243c243d9 /lib/sqlalchemy/orm | |
| parent | 00df05061e7a0333022d02705c21270f9de4edab (diff) | |
| download | sqlalchemy-abccc0624228def744b0382e84f01cf95e0d3aed.tar.gz | |
- added "statement_options()" to Query, to so options can be
passed to the resulting statement. Currently only
Select-statements have these options, and the only option
used is "stream_results", and the only dialect which knows
"stream_results" is psycopg2.
- Query.yield_per() will set the "stream_results" statement
option automatically.
- Added "statement_options()" to Selects, which set statement
specific options. These enable e.g. dialect specific options
such as whether to enable using server side cursors, etc.
- The psycopg2 now respects the statement option
"stream_results". This option overrides the connection setting
"server_side_cursors". If true, server side cursors will be
used for the statement. If false, they will not be used, even
if "server_side_cursors" is true on the
connection. [ticket:1619]
- added a "frozendict" from http://code.activestate.com/recipes/414283/,
adding more default collections as immutable class vars on
Query, Insert, Select
Diffstat (limited to 'lib/sqlalchemy/orm')
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 7be068019..444ad37bc 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -79,14 +79,14 @@ class Query(object): _from_obj = () _filter_aliases = None _from_obj_alias = None - _joinpath = _joinpoint = {} + _joinpath = _joinpoint = util.frozendict() + _statement_options = util.frozendict() + _params = util.frozendict() + _attributes = util.frozendict() + _with_options = () def __init__(self, entities, session=None): self.session = session - - self._with_options = [] - self._params = {} - self._attributes = {} self._polymorphic_adapters = {} self._set_entities(entities) @@ -488,9 +488,17 @@ class Query(object): collections will be cleared for a new load when encountered in a subsequent result batch. + Also note that many DBAPIs do not "stream" results, pre-buffering + all rows before making them available, including mysql-python and + psycopg2. yield_per() will also set the ``stream_results`` statement + option to ``True``, which currently is only understood by psycopg2 + and causes server side cursors to be used. + """ self._yield_per = count - + self._statement_options = self._statement_options.copy() + self._statement_options['stream_results'] = True + def get(self, ident): """Return an instance of the object based on the given identifier, or None if not found. @@ -658,7 +666,7 @@ class Query(object): # most MapperOptions write to the '_attributes' dictionary, # so copy that as well self._attributes = self._attributes.copy() - opts = list(util.flatten_iterator(args)) + opts = tuple(util.flatten_iterator(args)) self._with_options = self._with_options + opts if conditional: for opt in opts: @@ -668,6 +676,21 @@ class Query(object): opt.process_query(self) @_generative() + def statement_options(self, **kwargs): + """ Set non-SQL options for the resulting statement, such as dialect-specific options. + + The only option currently understood is ``stream_results=True``, + only used by Psycopg2 to enable "server side cursors". This option + only has a useful effect if used in conjunction with :meth:`~sqlalchemy.orm.query.Query.yield_per()`, + which currently sets ``stream_results`` to ``True`` automatically. + + """ + _statement_options = self._statement_options.copy() + for key, value in kwargs.items(): + _statement_options[key] = value + self._statement_options = _statement_options + + @_generative() def with_lockmode(self, mode): """Return a new Query object with the specified locking mode.""" @@ -1915,7 +1938,7 @@ class Query(object): context.adapter = sql_util.ColumnAdapter(inner, equivs) - statement = sql.select([inner] + context.secondary_columns, for_update=for_update, use_labels=labels) + statement = sql.select([inner] + context.secondary_columns, for_update=for_update, use_labels=labels, statement_options=self._statement_options) from_clause = inner for eager_join in eager_joins: @@ -1947,6 +1970,7 @@ class Query(object): for_update=for_update, correlate=False, order_by=context.order_by, + statement_options=self._statement_options, **self._select_args ) |
