U a@sdZddlZddlZddlZddlmZddlmZddlmZddlm Z dd l m Z dd l m Z dd l m Z dd l mZdd l mZerddlmZeZnddZddZGdddeZGdddejZGdddeZd2ddZe dZGddde ZGdddeZGd d!d!eeZGd"d#d#eZGd$d%d%eZGd&d'd'eeZ Gd(d)d)eZ!Gd*d+d+eZ"d,d-Z#Gd.d/d/e"Z$Gd0d1d1e"Z%dS)3z%Define generic result set constructs.N)_baserow_usecext)Row)exc)util) _generative) HasMemoized)InPlaceGenerative)collections_abc)py2k) tuplegettercs*tj|t|dkrSfddSdS)Nrcs |fSNrowitrYC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\engine\result.py&ztuplegetter..)operator itemgetterlenindexesrrrr s  r csdd|DfddS)NcSsg|]}td|qS)Z_get_by_int_impl)r methodcaller).0indexrrr ,sz!_row_as_tuple..cstfddDS)Ncsg|] }|qSrr)rgetterrecrrr0sz3_row_as_tuple....)tupler!Zgettersr!rr0rz_row_as_tuple..rrrr$r _row_as_tuple(sr%c@seZdZdZdZdZdZdZeddZ ddZ dd Z dd d Z d dZ ddZddZddZddZdddZddZdS)ResultMetaDataz$Base for metadata about result rows.rNcCst|Sr) RMKeyViewselfrrrkeys<szResultMetaData.keyscCs tdSrNotImplementedErrorr)keyrrr_has_key@szResultMetaData._has_keycCs tdSrr+r(rrr _for_freezeCszResultMetaData._for_freezeTcCs|sttjt||ddS)N)Zreplace_context)AssertionErrorrZraise_KeyError)r)r.errraiseerrrrr _key_fallbackFszResultMetaData._key_fallbackcCstddS)NzRetrieving row members using strings or other non-integers is deprecated; use row._mapping for a dictionary interface to the row)rZwarn_deprecated_20r-rrr_warn_for_nonintJszResultMetaData._warn_for_nonintcCstdt|jdS)Nz;TypeError: tuple indices must be integers or slices, not %s) TypeErrortype__name__r-rrr_raise_for_nonintQs z ResultMetaData._raise_for_nonintcCs tdSrr+)r)r*r4rrr_index_for_keyWszResultMetaData._index_for_keycCs tdSrr+r-rrr_metadata_for_keysZsz!ResultMetaData._metadata_for_keyscCs tdSrr+r)r*rrr_reduce]szResultMetaData._reducecCs&|||}|dk rt|SdSdSr)r;rr)r)r.r4rrrr_getter`s  zResultMetaData._gettercCs||}t|Sr)_indexes_for_keysr%)r)r*rrrr_row_as_tuple_getteris z#ResultMetaData._row_as_tuple_getter)T)T)r9 __module__ __qualname____doc__ __slots__ _tuplefilter_translated_indexes_unique_filterspropertyr*r/r0r5r6r:r;r<r>r?rArrrrr&3s"   r&c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)r'_parent_keyscCs||_dd|jD|_dS)NcSsg|]}|dk r|qSrr)rkrrrrssz&RMKeyView.__init__..rJ)r)parentrrr__init__qszRMKeyView.__init__cCs t|jSr)rrLr(rrr__len__uszRMKeyView.__len__cCs d|S)Nz#{0.__class__.__name__}({0._keys!r}))formatr(rrr__repr__xszRMKeyView.__repr__cCs t|jSr)iterrLr(rrr__iter__{szRMKeyView.__iter__cCstst|trdS|j|S)NF)r isinstanceintrKr/)r)itemrrr __contains__~szRMKeyView.__contains__cCst|t|kSrlistr)otherrrr__eq__szRMKeyView.__eq__cCst|t|kSrrYr[rrr__ne__szRMKeyView.__ne__N) r9rBrCrErOrPrRrTrXr]r^rrrrr'nsr'c@sheZdZdZdZdddZddZdd Zd d Zd d Z ddZ dddZ ddZ ddZ ddZdS)SimpleResultMetaDataz*result metadata for in-memory collections.)rL_keymap _processorsrFrGrHNcCsjt||_||_||_||_|r.cSs g|]\}}|f||dffqS)rr)rrrbrrrrscSs i|]\}}|D] }||qqSrr)rr*r"r.rrr s z1SimpleResultMetaData.__init__..) rZrLrFrGrH enumeratezipr`ra)r)r*extrararFrGrHZ recs_namesrrrrOs zSimpleResultMetaData.__init__cCs ||jkSrr`r-rrrr/szSimpleResultMetaData._has_keycs:j}|rjr|}tjfddjD|dS)Ncsg|]}j|dqS)rrhrr.r(rrrsz4SimpleResultMetaData._for_freeze..)rgrH)rHrFr_rL)r)Zunique_filtersrr(rr0s  z SimpleResultMetaData._for_freezecCs|j|jdS)NrLrGrjr(rrr __getstate__sz!SimpleResultMetaData.__getstate__cCs:|dr|d}t|}nd}}|j|d||ddS)NrGrL)rGrF)r rO)r)staterGrFrrr __setstate__s z!SimpleResultMetaData.__setstate__cCs ||jkSr)_data)r)valuerrrr _containsszSimpleResultMetaData._containsTc Cs^t|jjkr|j|}z|j|}Wn0tk rT}z||||}W5d}~XYnX|dSNrrV __class____mro__rLr`r2r5)r)r.r4r"kerrrr;s   z#SimpleResultMetaData._index_for_keycsfdd|DS)Ncsg|]}j|dqSrrhrir(rrrsz:SimpleResultMetaData._indexes_for_keys..rr=rr(rr@sz&SimpleResultMetaData._indexes_for_keysc csj|D]`}t|jjkr|j|}z|j|}Wn0tk r\}z|||d}W5d}~XYnX|VqdS)NTrr)r)r*r.r"rurrrr<s   z'SimpleResultMetaData._metadata_for_keysc szfdd|D}Wn6tk rL}z|jd|dW5d}~XYnXt|\}}}jrtfdd|D}t|}t||||jjd}|S)Ncs,g|]$}jt|jjkr"j|n|qSr)r`rVrsrtrLrir(rrrsz0SimpleResultMetaData._reduce..rTcsg|]}j|qSr)rG)ridxr(rrrs)rgrFrGrarH) r2r5argsrfrGr r_rarH) r)r*Zmetadata_for_keysrurZnew_keysrgtupZ new_metadatarr(rr>s&  & zSimpleResultMetaData._reduce)NNNNN)T)r9rBrCrDrErOr/r0rkrmrpr;r@r<r>rrrrr_s"      r_cCs"t||}tt||j|jtjSr)r_ functoolspartialrrar`_default_key_style)fieldsrgrNrrr result_tuples r~ZNO_ROWc@seZdZdZdZdZdZejddZ ejddZ ddZ d d Z ejd d Z ejd dZddZddZddZeddZejddZdS)ResultInternalNTcs|jr |jn|}|jr4|js dS|jfdd}n|j}|jj}|j}|j}|j}|jr|js|rp|}t |||||fdd}nt |||||}d|j r|j fndr|fdd}|S)Ncs|||||fSrr)metadata processorskeymap key_styleZ scalar_obj)_procrr process_row2sz/ResultInternal._row_getter..process_rowcs |Srrr)_make_row_origtfrrmake_rowKsz,ResultInternal._row_getter..make_rowrcs|}D] }||}q |Srr)rfn) _make_rowfnsrrr]s ) _real_result_source_supports_scalars_generate_rows _process_rowr| _metadatar`rarFrzr{_row_logging_fn)r) real_resultrrrrrrr)rrrrrr _row_getter(sL  zResultInternal._row_gettercsB|j|j|jr0|j\fdd}nfdd}|S)Nc3sX|D]J}r|n|}r(|n|}|kr6q|rL|}|VqdSr)_fetchiter_impladd)r)robjhashedrpost_creational_filterstrategyuniquesrriterrowsos  z1ResultInternal._iterator_getter..iterrowsc3s4|D]&}r|n|}r(|}|VqdSr)rr)rrrrrr|s  r_post_creational_filter_unique_filter_state_unique_strategy)r)rrrr_iterator_getteres  zResultInternal._iterator_gettercs |j|}fdd|DS)Ncsg|] }|qSrrrrrrrrsz0ResultInternal._raw_all_rows..)r_fetchall_impl)r)rowsrrr _raw_all_rowsszResultInternal._raw_all_rowscs|j|j|}r,fdd|D}n|}|jrb|j\fddfdd|DD}n|}r|fdd|D}|S)Ncsg|] }|qSrrrrrrrsz+ResultInternal._allrows..cs&g|]\}}|kr|s|qSrrrmade_rowZsig_rowrrrrs cs g|]}|r|n|fqSrrrrrrrrscsg|] }|qSrrrrrrrs)rrrrr)r)r made_rowsrrr_allrowss"    zResultInternal._allrowscsB|j|j|jr0|j\fdd}nfdd}|S)Ncsd|j}|}|dkrtSr$|n|}r4|n|}|krDqn |rZ|}|SqdSr)_fetchone_impl_NO_ROWr)r)Z_onerowrrrrrronerows z-ResultInternal._onerow_getter..onerowcs8|}|dkrtSr |n|}r0|}|SdSr)rrrrrrrsr)r)rrrr_onerow_getters  zResultInternal._onerow_gettercsL|j|j|jr:|j\ddfdd}nfdd}|S)NcsNrfdd|D}r.fdd|D}ndd|D}fdd|DS)Ncsg|] }|qSrrrrrrrszFResultInternal._manyrow_getter..filterrows..c3s|]}||fVqdSrrrrrr szEResultInternal._manyrow_getter..filterrows..css|]}||fVqdSrrrrrrrscs&g|]\}}|kr|s|qSrrrrrrrs r)rrrrrr)rrrr filterrowss  z2ResultInternal._manyrow_getter..filterrowscsg}|j}|dkrf|jr|jn|}|jr4|j}}qj||}t|}|||t|}n|}|r||}|s|q|||t|}qjrfdd|D}|S)Ncsg|] }|qSrrrrrrrsDResultInternal._manyrow_getter..manyrows..)_fetchmany_implr _yield_perrextend)r)numZcollectZ _manyrowsrZ num_requiredrrrrrrrrmanyrowss2   z0ResultInternal._manyrow_getter..manyrowscsX|dkr|jr|jn|}|j}||}r>fdd|D}rTfdd|D}|S)Ncsg|] }|qSrrrrrrrsrcsg|] }|qSrrrrrrr!s)rrr)r)rrrrrrrs r)r)rrrr_manyrow_getters *zResultInternal._manyrow_getterc Cs|j}|dd}|dkr,|r(tdndS|rB|jrBd|_d}n|j}z|rV||n|}Wn|jddYnX|rZ|jr|j\}}|r||n|} |dd} | dkrt } q.z<|r|| n| } |r| || krWqn || krWqWq.Wq|jddYqXqn|dd} | dkr.t } | t k rj|jddt |rRdndnt } |jdd|s|j } | r| |}|r|r|dS|SdS) NT hard_closez&No row was found when one was requiredFhardz6Multiple rows were found when exactly one was requiredz6Multiple rows were found when one or none was requiredr) rrZ NoResultFoundrrr _soft_closerrrZMultipleResultsFoundr) r)raise_for_second_rowraise_for_nonescalarrrrrrZexisting_row_hashZnext_rowrrrr _only_one_row&sn               zResultInternal._only_one_rowcCs ||Sr)rr(rrr _iter_impl{szResultInternal._iter_implcCs"||}|tkrtn|SdSr)rr StopIterationrrrr _next_impl~s zResultInternal._next_implcCsB|jr |jn|}|jr*t|dkr*d|_nd|_|j||_dS)NrFT)rrrrrr>)r)rrrrr_column_slicess zResultInternal._column_slicescCst|j\}}|jdk r|jn|}|sl|jjrl|jrD|jsD|jjd}n(|jj}|jjr`|j|}td|}||fS)NrZ_filter_on_values) rrrrHrrrFrr)r)rrrfiltersrrrrs    zResultInternal._unique_strategy)r9rBrCrrrrr Zmemoized_attributerrrrrrrrrrrrrrrrr"s* < ! ' RU rc@seZdZddZdS) _WithKeyscCs|jjS)adReturn an iterable view which yields the string keys that would be represented by each :class:`.Row`. The keys can represent the labels of the columns returned by a core statement or the names of the orm classes returned by an orm execution. The view also can be tested for key containment using the Python ``in`` operator, which will test both for the string keys represented in the view, as well as for alternate keys such as column objects. .. versionchanged:: 1.4 a key view object is returned rather than a plain list. )rr*r(rrrr*sz_WithKeys.keysN)r9rBrCr*rrrrrsrc@s4eZdZdZeZdZdZdZe Z ddZ dBddZ edd ZedCd d Zd d ZdDddZdEddZddZddZddZddZdFddZddZdGd d!Zd"d#Zd$d%Zerd&d'ZdHd(d)Zd*d+Z d,d-Z!dId.d/Z"d0d1Z#d2d3Z$d4d5Z%d6d7Z&d8d9Z'd:d;Z(dd?Z*d@dAZ+dS)JResulta9Represent a set of database results. .. versionadded:: 1.4 The :class:`.Result` object provides a completely updated usage model and calling facade for SQLAlchemy Core and SQLAlchemy ORM. In Core, it forms the basis of the :class:`.CursorResult` object which replaces the previous :class:`.ResultProxy` interface. When using the ORM, a higher level object called :class:`.ChunkedIteratorResult` is normally used. .. note:: In SQLAlchemy 1.4 and above, this object is used for ORM results returned by :meth:`_orm.Session.execute`, which can yield instances of ORM mapped objects either individually or within tuple-like rows. Note that the :class:`_result.Result` object does not deduplicate instances or rows automatically as is the case with the legacy :class:`_orm.Query` object. For in-Python de-duplication of instances or rows, use the :meth:`_result.Result.unique` modifier method. .. seealso:: :ref:`tutorial_fetching_rows` - in the :doc:`/tutorial/index` NFcCs ||_dSr)r)r)cursor_metadatarrrrOszResult.__init__cCs tdSrr+r)rrrrrszResult._soft_closecCs ||_dS)aqConfigure the row-fetching strategy to fetch num rows at a time. This impacts the underlying behavior of the result when iterating over the result object, or otherwise making use of methods such as :meth:`_engine.Result.fetchone` that return one row at a time. Data from the underlying cursor or other data source will be buffered up to this many rows in memory, and the buffered collection will then be yielded out one row at at time or as many rows are requested. Each time the buffer clears, it will be refreshed to this many rows or as many rows remain if fewer remain. The :meth:`_engine.Result.yield_per` method is generally used in conjunction with the :paramref:`_engine.Connection.execution_options.stream_results` execution option, which will allow the database dialect in use to make use of a server side cursor, if the DBAPI supports it. Most DBAPIs do not use server side cursors by default, which means all rows will be fetched upfront from the database regardless of the :meth:`_engine.Result.yield_per` setting. However, :meth:`_engine.Result.yield_per` may still be useful in that it batches the SQLAlchemy-side processing of the raw data from the database, and additionally when used for ORM scenarios will batch the conversion of database rows into ORM entity rows. .. versionadded:: 1.4 :param num: number of rows to fetch each time the buffer is refilled. If set to a value below 1, fetches all rows for the next buffer. N)rr)rrrr yield_pers"zResult.yield_percCst|f|_dS)aApply unique filtering to the objects returned by this :class:`_engine.Result`. When this filter is applied with no arguments, the rows or objects returned will filtered such that each row is returned uniquely. The algorithm used to determine this uniqueness is by default the Python hashing identity of the whole tuple. In some cases a specialized per-entity hashing scheme may be used, such as when using the ORM, a scheme is applied which works against the primary key identity of returned objects. The unique filter is applied **after all other filters**, which means if the columns returned have been refined using a method such as the :meth:`_engine.Result.columns` or :meth:`_engine.Result.scalars` method, the uniquing is applied to **only the column or columns returned**. This occurs regardless of the order in which these methods have been called upon the :class:`_engine.Result` object. The unique filter also changes the calculus used for methods like :meth:`_engine.Result.fetchmany` and :meth:`_engine.Result.partitions`. When using :meth:`_engine.Result.unique`, these methods will continue to yield the number of rows or objects requested, after uniquing has been applied. However, this necessarily impacts the buffering behavior of the underlying cursor or datasource, such that multiple underlying calls to ``cursor.fetchmany()`` may be necessary in order to accumulate enough objects in order to provide a unique collection of the requested size. :param strategy: a callable that will be applied to rows or objects being iterated, which should return an object that represents the unique value of the row. A Python ``set()`` is used to store these identities. If not passed, a default uniqueness strategy is used which may have been assembled by the source of this :class:`_engine.Result` object. Nsetrr)rrrrunique s&z Result.uniquecGs ||S)aEstablish the columns that should be returned in each row. This method may be used to limit the columns returned as well as to reorder them. The given list of expressions are normally a series of integers or string key names. They may also be appropriate :class:`.ColumnElement` objects which correspond to a given statement construct. E.g.:: statement = select(table.c.x, table.c.y, table.c.z) result = connection.execute(statement) for z, y in result.columns('z', 'y'): # ... Example of using the column objects from the statement itself:: for z, y in result.columns( statement.selected_columns.c.z, statement.selected_columns.c.y ): # ... .. versionadded:: 1.4 :param \*col_expressions: indicates columns to be returned. Elements may be integer row indexes, string column names, or appropriate :class:`.ColumnElement` objects corresponding to a select construct. :return: this :class:`_engine.Result` object with the modifications given. rr)Zcol_expressionsrrrcolumns2s$zResult.columnsrcCs t||S)a@Return a :class:`_result.ScalarResult` filtering object which will return single elements rather than :class:`_row.Row` objects. E.g.:: >>> result = conn.execute(text("select int_id from table")) >>> result.scalars().all() [1, 2, 3] When results are fetched from the :class:`_result.ScalarResult` filtering object, the single column-row that would be returned by the :class:`_result.Result` is instead returned as the column's value. .. versionadded:: 1.4 :param index: integer or row key indicating the column to be fetched from each row, defaults to ``0`` indicating the first column. :return: a new :class:`_result.ScalarResult` filtering object referring to this :class:`_result.Result` object. ) ScalarResult)r)rrrrscalarsXszResult.scalarsTcCs|jrtd|j||S)zZreturn a callable that will retrieve the given key from a :class:`.Row`. .can't use this function in 'only scalars' mode)rr,rr?)r)r.r4rrrr?qs zResult._gettercCs|jrtd|j|S)z[return a callable that will retrieve the given keys from a :class:`.Row`. r)rr,rrAr=rrr _tuple_getter|s zResult._tuple_gettercCst|S)aApply a mappings filter to returned rows, returning an instance of :class:`_result.MappingResult`. When this filter is applied, fetching rows will return :class:`.RowMapping` objects instead of :class:`.Row` objects. .. versionadded:: 1.4 :return: a new :class:`_result.MappingResult` filtering object referring to this :class:`_result.Result` object. ) MappingResultr(rrrmappingsszResult.mappingscCs tdS)zReturn a safe iterator that yields raw row data. This is used by the :meth:`._engine.Result.merge` method to merge multiple compatible results together. Nr+r(rrr_raw_row_iteratorszResult._raw_row_iteratorcCs tdSrr+r(rrrrszResult._fetchiter_implcCs tdSrr+r)rrrrrszResult._fetchone_implcCs tdSrr+r(rrrrszResult._fetchall_implcCs tdSrr+r)sizerrrrszResult._fetchmany_implcCs|Srrr(rrrrTszResult.__iter__cCs|Srrr(rrr__next__szResult.__next__cCs|Srrr(rrrnextsz Result.nextccs$|j}|||}|r |Vqq qdS)aFIterate through sub-lists of rows of the size given. Each list will be of the size given, excluding the last list to be yielded, which may have a small number of rows. No empty lists will be yielded. The result object is automatically closed when the iterator is fully consumed. Note that the backend driver will usually buffer the entire result ahead of time unless the :paramref:`.Connection.execution_options.stream_results` execution option is used indicating that the driver should not pre-buffer results, if possible. Not all drivers support this option and the option is silently ignored for those who do not. .. versionadded:: 1.4 :param size: indicate the maximum number of rows to be present in each list yielded. If None, makes use of the value set by :meth:`_engine.Result.yield_per`, if present, otherwise uses the :meth:`_engine.Result.fetchmany` default which may be backend specific. :return: iterator of lists Nrr)rr partitionrrr partitionss  zResult.partitionscCs|S)z4A synonym for the :meth:`_engine.Result.all` method.rr(rrrfetchallszResult.fetchallcCs||}|tkrdS|SdS)aFetch one row. When all rows are exhausted, returns None. This method is provided for backwards compatibility with SQLAlchemy 1.x.x. To fetch the first row of a result only, use the :meth:`_engine.Result.first` method. To iterate through all rows, iterate the :class:`_engine.Result` object directly. :return: a :class:`.Row` object if no filters are applied, or None if no rows remain. Nrrrrrrfetchones zResult.fetchonecCs |||S)aCFetch many rows. When all rows are exhausted, returns an empty list. This method is provided for backwards compatibility with SQLAlchemy 1.x.x. To fetch rows in groups, use the :meth:`._result.Result.partitions` method. :return: a list of :class:`.Row` objects. rrrrr fetchmanyszResult.fetchmanycCs|S)zReturn all rows in a list. Closes the result set after invocation. Subsequent invocations will return an empty list. .. versionadded:: 1.4 :return: a list of :class:`.Row` objects. rr(rrrall s z Result.allcCs|jddddS)aXFetch the first row or None if no row is present. Closes the result set and discards remaining rows. .. note:: This method returns one **row**, e.g. tuple, by default. To return exactly one single scalar value, that is, the first column of the first row, use the :meth:`.Result.scalar` method, or combine :meth:`.Result.scalars` and :meth:`.Result.first`. :return: a :class:`.Row` object, or None if no rows remain. .. seealso:: :meth:`_result.Result.scalar` :meth:`_result.Result.one` Frrrrr(rrrfirsts z Result.firstcCs|jddddS)aReturn at most one result or raise an exception. Returns ``None`` if the result has no rows. Raises :class:`.MultipleResultsFound` if multiple rows are returned. .. versionadded:: 1.4 :return: The first :class:`.Row` or None if no row is available. :raises: :class:`.MultipleResultsFound` .. seealso:: :meth:`_result.Result.first` :meth:`_result.Result.one` TFrrr(rrr one_or_none0s zResult.one_or_nonecCs|jddddS)aReturn exactly one scalar result or raise an exception. This is equivalent to calling :meth:`.Result.scalars` and then :meth:`.Result.one`. .. seealso:: :meth:`.Result.one` :meth:`.Result.scalars` Trrr(rrr scalar_oneHs zResult.scalar_onecCs|jddddS)aReturn exactly one or no scalar result. This is equivalent to calling :meth:`.Result.scalars` and then :meth:`.Result.one_or_none`. .. seealso:: :meth:`.Result.one_or_none` :meth:`.Result.scalars` TFrrr(rrrscalar_one_or_noneYs zResult.scalar_one_or_nonecCs|jddddS)a3Return exactly one row or raise an exception. Raises :class:`.NoResultFound` if the result returns no rows, or :class:`.MultipleResultsFound` if multiple rows would be returned. .. note:: This method returns one **row**, e.g. tuple, by default. To return exactly one single scalar value, that is, the first column of the first row, use the :meth:`.Result.scalar_one` method, or combine :meth:`.Result.scalars` and :meth:`.Result.one`. .. versionadded:: 1.4 :return: The first :class:`.Row`. :raises: :class:`.MultipleResultsFound`, :class:`.NoResultFound` .. seealso:: :meth:`_result.Result.first` :meth:`_result.Result.one_or_none` :meth:`_result.Result.scalar_one` TFrrr(rrronejs z Result.onecCs|jddddS)aFetch the first column of the first row, and close the result set. Returns None if there are no rows to fetch. No validation is performed to test if additional rows remain. After calling this method, the object is fully closed, e.g. the :meth:`_engine.CursorResult.close` method will have been called. :return: a Python scalar value , or None if no rows remain. FTrrr(rrrrs z Result.scalarcCst|S)aReturn a callable object that will produce copies of this :class:`.Result` when invoked. The callable object returned is an instance of :class:`_engine.FrozenResult`. This is used for result set caching. The method must be called on the result when it has been unconsumed, and calling the method will consume the result fully. When the :class:`_engine.FrozenResult` is retrieved from a cache, it can be called any number of times where it will produce a new :class:`_engine.Result` object each time against its stored set of rows. .. seealso:: :ref:`do_orm_execute_re_executing` - example usage within the ORM to implement a result-set cache. ) FrozenResultr(rrrfreezesz Result.freezecGst|j|f|S)aMerge this :class:`.Result` with other compatible result objects. The object returned is an instance of :class:`_engine.MergedResult`, which will be composed of iterators from the given result objects. The new result will use the metadata from this result object. The subsequent result objects must be against an identical set of result / cursor metadata, otherwise the behavior is undefined. ) MergedResultr)r)Zothersrrrmergesz Result.merge)F)N)r)T)F)N)N)N),r9rBrCrDrrrrrrZ immutabledict _attributesrOrrrrrrr?rrrrrrrrTrr rrrrrrrrrrrrrrrrrrrsN  # '&       & rc@sNeZdZdZdZdddZeddZdd Zdd d Z d d Z dddZ dS) FilterResultzA wrapper for a :class:`_engine.Result` that returns objects other than :class:`_result.Row` objects, such as dictionaries or scalar objects. NFcCs|jj|ddSNr)rrrrrrrszFilterResult._soft_closecCs|jjSr)rrr(rrrrszFilterResult._attributescCs |jSr)rrr(rrrrszFilterResult._fetchiter_implcCs|jj|dS)Nr)rrrrrrrszFilterResult._fetchone_implcCs |jSr)rrr(rrrrszFilterResult._fetchall_implcCs|jj|dSN)r)rrrrrrrszFilterResult._fetchmany_impl)F)F)N) r9rBrCrDrrrIrrrrrrrrrrs   rc@s~eZdZdZdZddZdddZddd Zd d Zdd d Z ddZ ddZ ddZ e rbddZddZddZddZdS)raA wrapper for a :class:`_result.Result` that returns scalar values rather than :class:`_row.Row` values. The :class:`_result.ScalarResult` object is acquired by calling the :meth:`_result.Result.scalars` method. A special limitation of :class:`_result.ScalarResult` is that it has no ``fetchone()`` method; since the semantics of ``fetchone()`` are that the ``None`` value indicates no more results, this is not compatible with :class:`_result.ScalarResult` since there is no way to distinguish between ``None`` as a row value versus ``None`` as an indicator. Use ``next(result)`` to receive values individually. FcCsD||_|jr|j|_d|_n|j|g|_td|_|j|_dSrq)rrrrr>rrr)r)rrrrrrOs zScalarResult.__init__NcCst|f|_|S)zApply unique filtering to the objects returned by this :class:`_engine.ScalarResult`. See :meth:`_engine.Result.unique` for usage details. rrrrrrs zScalarResult.uniqueccs$|j}|||}|r |Vqq qdS)zIterate through sub-lists of elements of the size given. Equivalent to :meth:`_result.Result.partitions` except that scalar values, rather than :class:`_result.Row` objects, are returned. Nrrrrrrs  zScalarResult.partitionscCs|S)z:A synonym for the :meth:`_engine.ScalarResult.all` method.rr(rrrrszScalarResult.fetchallcCs |||S)zFetch many objects. Equivalent to :meth:`_result.Result.fetchmany` except that scalar values, rather than :class:`_result.Row` objects, are returned. rrrrrrszScalarResult.fetchmanycCs|S)zReturn all scalar values in a list. Equivalent to :meth:`_result.Result.all` except that scalar values, rather than :class:`_result.Row` objects, are returned. rr(rrrr(szScalarResult.allcCs|Srrr(rrrrT2szScalarResult.__iter__cCs|Srrr(rrrr5szScalarResult.__next__cCs|Srrr(rrrr:szScalarResult.nextcCs|jddddS)zFetch the first object or None if no object is present. Equivalent to :meth:`_result.Result.first` except that scalar values, rather than :class:`_result.Row` objects, are returned. Frrr(rrrr=s zScalarResult.firstcCs|jddddS)zReturn at most one object or raise an exception. Equivalent to :meth:`_result.Result.one_or_none` except that scalar values, rather than :class:`_result.Row` objects, are returned. TFrrr(rrrrJs zScalarResult.one_or_nonecCs|jddddS)zReturn exactly one object or raise an exception. Equivalent to :meth:`_result.Result.one` except that scalar values, rather than :class:`_result.Row` objects, are returned. TFrrr(rrrrVs zScalarResult.one)N)N)N)r9rBrCrDrrOrrrrrrTrr rrrrrrrrrs     rc@seZdZdZdZedZddZd!ddZ d d Z d"d d Z d dZ ddZ d#ddZddZddZddZer|ddZddZddZdd ZdS)$rzA wrapper for a :class:`_engine.Result` that returns dictionary values rather than :class:`_engine.Row` values. The :class:`_engine.MappingResult` object is acquired by calling the :meth:`_engine.Result.mappings` method. T_mappingcCs0||_|j|_|j|_|jr,|jdg|_dSrq)rrrrr>r)resultrrrrOps zMappingResult.__init__NcCst|f|_|S)zApply unique filtering to the objects returned by this :class:`_engine.MappingResult`. See :meth:`_engine.Result.unique` for usage details. rrrrrrws zMappingResult.uniquecGs ||S)z:Establish the columns that should be returned in each row.rrrrrrszMappingResult.columnsccs$|j}|||}|r |Vqq qdS)zIterate through sub-lists of elements of the size given. Equivalent to :meth:`_result.Result.partitions` except that mapping values, rather than :class:`_result.Row` objects, are returned. Nrrrrrrs  zMappingResult.partitionscCs|S)z;A synonym for the :meth:`_engine.MappingResult.all` method.rr(rrrrszMappingResult.fetchallcCs||}|tkrdS|SdS)zFetch one object. Equivalent to :meth:`_result.Result.fetchone` except that mapping values, rather than :class:`_result.Row` objects, are returned. Nrrrrrrs zMappingResult.fetchonecCs |||S)zFetch many objects. Equivalent to :meth:`_result.Result.fetchmany` except that mapping values, rather than :class:`_result.Row` objects, are returned. rrrrrrs zMappingResult.fetchmanycCs|S)zReturn all scalar values in a list. Equivalent to :meth:`_result.Result.all` except that mapping values, rather than :class:`_result.Row` objects, are returned. rr(rrrrs zMappingResult.allcCs|Srrr(rrrrTszMappingResult.__iter__cCs|Srrr(rrrrszMappingResult.__next__cCs|Srrr(rrrrszMappingResult.nextcCs|jddddS)zFetch the first object or None if no object is present. Equivalent to :meth:`_result.Result.first` except that mapping values, rather than :class:`_result.Row` objects, are returned. Frrr(rrrrs zMappingResult.firstcCs|jddddS)zReturn at most one object or raise an exception. Equivalent to :meth:`_result.Result.one_or_none` except that mapping values, rather than :class:`_result.Row` objects, are returned. TFrrr(rrrrs zMappingResult.one_or_nonecCs|jddddS)zReturn exactly one object or raise an exception. Equivalent to :meth:`_result.Result.one` except that mapping values, rather than :class:`_result.Row` objects, are returned. TFrrr(rrrrs zMappingResult.one)N)N)N)r9rBrCrDrr attrgetterrrOrrrrrrrrTrr rrrrrrrrrcs$       rc@s0eZdZdZddZddZddZdd Zd S) raRepresents a :class:`.Result` object in a "frozen" state suitable for caching. The :class:`_engine.FrozenResult` object is returned from the :meth:`_engine.Result.freeze` method of any :class:`_engine.Result` object. A new iterable :class:`.Result` object is generated from a fixed set of data each time the :class:`.FrozenResult` is invoked as a callable:: result = connection.execute(query) frozen = result.freeze() unfrozen_result_one = frozen() for row in unfrozen_result_one: print(row) unfrozen_result_two = frozen() rows = unfrozen_result_two.all() # ... etc .. versionadded:: 1.4 .. seealso:: :ref:`do_orm_execute_re_executing` - example usage within the ORM to implement a result-set cache. :func:`_orm.loading.merge_frozen_result` - ORM function to merge a frozen result back into a :class:`_orm.Session`. cCs@|j|_|j|_|j|_|jr2t||_n ||_dSr) rr0rrrrZrdatarrrrrrOs  zFrozenResult.__init__cCs*|jrdd|jDSdd|jDSdS)NcSsg|] }|gqSrr)relemrrrr%sz-FrozenResult.rewrite_rows..cSsg|] }t|qSrrYrrrrr's)rrr(rrr rewrite_rows#szFrozenResult.rewrite_rowscCsDtt}|j|_|j|_|j|_|jr:dd|D|_n||_|S)NcSsg|] }|dqSrvr)rdrrrr0sz.FrozenResult.with_new_rows..)r__new__rrrr)r)Z tuple_datafrrrr with_new_rows)s zFrozenResult.with_new_rowscCs&t|jt|j}|j|_|j|_|Sr)IteratorResultrrSrrrrrrr__call__5szFrozenResult.__call__N)r9rBrCrDrOrr rrrrrrs &  rc@sNeZdZdZdddZddZdd Zd d Zdd d ZddZ dddZ dS)r zwA :class:`.Result` that gets data from a Python iterator of :class:`.Row` objects. .. versionadded:: 1.4 NFcCs||_||_||_||_dSr)riteratorrawr)r)rrrrrrrrODszIteratorResult.__init__cKstg|_dSr)rSr)r)kwrrrrPszIteratorResult._soft_closecCs|jSrrr(rrrrSsz IteratorResult._raw_row_iteratorcCs|jSrrr(rrrrVszIteratorResult._fetchiter_implcCs,t|jt}|tkr$|j|ddS|SdSr)rrrr)r)rrrrrrYs   zIteratorResult._fetchone_implcCszt|jWS|XdSr)rrZrr(rrrraszIteratorResult._fetchall_implcCstt|jd|Srq)rZ itertoolsislicerrrrrrgszIteratorResult._fetchmany_impl)NF)F)N) r9rBrCrDrOrrrrrrrrrrr <s   r cCsttgtgSr)r r_rSrrrr null_resultksrcs8eZdZdZd ddZeddZd fdd ZZS) ChunkedIteratorResultaAn :class:`.IteratorResult` that works from an iterator-producing callable. The given ``chunks`` argument is a function that is given a number of rows to return in each chunk, or ``None`` for all rows. The function should then return an un-consumed iterator of lists, each list of the requested size. The function can be called at any time again, in which case it should continue from the same result set but adjust the chunk size as given. .. versionadded:: 1.4 FNcCs6||_||_||_||_tj|d|_||_dSr) rchunksrrrchain from_iterablerdynamic_yield_per)r)rrZsource_supports_scalarsrrrrrrO~s zChunkedIteratorResult.__init__cCs||_tj|||_dSr)rrrrrrrrrrrszChunkedIteratorResult.yield_percs,|jrtj|||_tt|j|dSr) rrrrrrsuperrrrrsrrrsz%ChunkedIteratorResult._fetchmany_impl)FNF)N) r9rBrCrDrOrrr __classcell__rrrrros  rcs6eZdZdZdZfddZddZd ddZZS) rzA :class:`_engine.Result` that is merged from any number of :class:`_engine.Result` objects. Returned by the :meth:`_engine.Result.merge` method. .. versionadded:: 1.4 Fcsj||_tt||tjdd|D|dj|_|dj|_|dj |_ |j j dd|D|_ dS)Ncss|]}|VqdSr)rrrrrrrsz(MergedResult.__init__..rcSsg|] }|jqSr)rrrrrrsz)MergedResult.__init__..) _resultsrrrOrrrrrrrZ merge_with)r)rresultsrrrrOs      zMergedResult.__init__cCs|jdddS)NTr)rr(rrrcloseszMergedResult.closecCs&|jD]}|j|dq|r"d|_dS)NrT)r rclosed)r)rrrrrrs zMergedResult._soft_close)F) r9rBrCrDr#rOr"rrrrrrrs   r)N)&rDrzrrrrrrrZsql.baserr r r r Zsqlalchemy.cresultproxyr r%objectr&KeysViewr'r_r~symbolrrrrrrrrr rrrrrrrsV           ;  J//