class documentation

class BaseCursorResult(object):

Known subclasses: sqlalchemy.engine.cursor.CursorResult

View In Hierarchy

Base class for database result objects.
Method __init__ Undocumented
Method ​_init​_metadata Undocumented
Method ​_soft​_close Soft close this _engine.CursorResult.
Method last​_inserted​_params Return the collection of inserted parameters from this execution.
Method last​_updated​_params Return the collection of updated parameters from this execution.
Method lastrow​_has​_defaults Return lastrow_has_defaults() from the underlying .ExecutionContext.
Method postfetch​_cols Return postfetch_cols() from the underlying .ExecutionContext.
Method prefetch​_cols Return prefetch_cols() from the underlying .ExecutionContext.
Method supports​_sane​_multi​_rowcount Return supports_sane_multi_rowcount from the dialect.
Method supports​_sane​_rowcount Return supports_sane_rowcount from the dialect.
Class Variable out​_parameters Undocumented
Instance Variable ​_echo Undocumented
Instance Variable ​_metadata Undocumented
Instance Variable ​_row​_logging​_fn Undocumented
Instance Variable ​_soft​_closed Undocumented
Instance Variable closed Undocumented
Instance Variable connection Undocumented
Instance Variable context Undocumented
Instance Variable cursor Undocumented
Instance Variable cursor​_strategy Undocumented
Instance Variable dialect Undocumented
Property inserted​_primary​_key Return the primary key for the row just inserted.
Property inserted​_primary​_key​_rows Return the value of _engine.CursorResult.inserted_primary_key as a row contained within a list; some dialects may support a multiple row form as well.
Property is​_insert True if this _engine.CursorResult is the result of a executing an expression language compiled _expression.insert construct.
Property lastrowid Return the 'lastrowid' accessor on the DBAPI cursor.
Property returned​_defaults Return the values of default columns that were fetched using the .ValuesBase.return_defaults feature.
Property returned​_defaults​_rows Return a list of rows each containing the values of default columns that were fetched using the .ValuesBase.return_defaults feature.
Property returns​_rows True if this _engine.CursorResult returns zero or more rows.
Property rowcount Return the 'rowcount' for this result.
def __init__(self, context, cursor_strategy, cursor_description):

Undocumented

def _init_metadata(self, context, cursor_description):

Undocumented

def _soft_close(self, hard=False):

Soft close this _engine.CursorResult.

This releases all DBAPI cursor resources, but leaves the CursorResult "open" from a semantic perspective, meaning the fetchXXX() methods will continue to return empty results.

This method is called automatically when:

  • all result rows are exhausted using the fetchXXX() methods.
  • cursor.description is None.

This method is not public, but is documented in order to clarify the "autoclose" process used.

New in version 1.0.0.

See Also

_engine.CursorResult.close

def last_inserted_params(self):

Return the collection of inserted parameters from this execution.

Raises ~sqlalchemy.exc.InvalidRequestError if the executed statement is not a compiled expression construct or is not an insert() construct.

def last_updated_params(self):

Return the collection of updated parameters from this execution.

Raises ~sqlalchemy.exc.InvalidRequestError if the executed statement is not a compiled expression construct or is not an update() construct.

def lastrow_has_defaults(self):

Return lastrow_has_defaults() from the underlying .ExecutionContext.

See .ExecutionContext for details.

def postfetch_cols(self):

Return postfetch_cols() from the underlying .ExecutionContext.

See .ExecutionContext for details.

Raises ~sqlalchemy.exc.InvalidRequestError if the executed statement is not a compiled expression construct or is not an insert() or update() construct.

def prefetch_cols(self):

Return prefetch_cols() from the underlying .ExecutionContext.

See .ExecutionContext for details.

Raises ~sqlalchemy.exc.InvalidRequestError if the executed statement is not a compiled expression construct or is not an insert() or update() construct.

def supports_sane_multi_rowcount(self):

Return supports_sane_multi_rowcount from the dialect.

See _engine.CursorResult.rowcount for background.

def supports_sane_rowcount(self):

Return supports_sane_rowcount from the dialect.

See _engine.CursorResult.rowcount for background.

out_parameters =

Undocumented

_echo =

Undocumented

_metadata =

Undocumented

_row_logging_fn =

Undocumented

_soft_closed: bool =

Undocumented

closed: bool =

Undocumented

connection =

Undocumented

context =

Undocumented

cursor =

Undocumented

cursor_strategy =

Undocumented

dialect =

Undocumented

@property
inserted_primary_key =

Return the primary key for the row just inserted.

The return value is a _result.Row object representing a named tuple of primary key values in the order in which the primary key columns are configured in the source _schema.Table.

Changed in version 1.4.8: - the _engine.CursorResult.inserted_primary_key value is now a named tuple via the _result.Row class, rather than a plain tuple.

This accessor only applies to single row _expression.insert constructs which did not explicitly specify _expression.Insert.returning. Support for multirow inserts, while not yet available for most backends, would be accessed using the _engine.CursorResult.inserted_primary_key_rows accessor.

Note that primary key columns which specify a server_default clause, or otherwise do not qualify as "autoincrement" columns (see the notes at _schema.Column), and were generated using the database-side default, will appear in this list as None unless the backend supports "returning" and the insert statement executed with the "implicit returning" enabled.

Raises ~sqlalchemy.exc.InvalidRequestError if the executed statement is not a compiled expression construct or is not an insert() construct.

@property
inserted_primary_key_rows =

Return the value of _engine.CursorResult.inserted_primary_key as a row contained within a list; some dialects may support a multiple row form as well.

Note

As indicated below, in current SQLAlchemy versions this accessor is only useful beyond what's already supplied by _engine.CursorResult.inserted_primary_key when using the :ref:`postgresql_psycopg2` dialect. Future versions hope to generalize this feature to more dialects.

This accessor is added to support dialects that offer the feature that is currently implemented by the :ref:`psycopg2_executemany_mode` feature, currently only the psycopg2 dialect, which provides for many rows to be INSERTed at once while still retaining the behavior of being able to return server-generated primary key values.

  • When using the psycopg2 dialect, or other dialects that may support "fast executemany" style inserts in upcoming releases : When invoking an INSERT statement while passing a list of rows as the second argument to _engine.Connection.execute, this accessor will then provide a list of rows, where each row contains the primary key value for each row that was INSERTed.
  • When using all other dialects / backends that don't yet support this feature: This accessor is only useful for single row INSERT statements, and returns the same information as that of the _engine.CursorResult.inserted_primary_key within a single-element list. When an INSERT statement is executed in conjunction with a list of rows to be INSERTed, the list will contain one row per row inserted in the statement, however it will contain None for any server-generated values.

Future releases of SQLAlchemy will further generalize the "fast execution helper" feature of psycopg2 to suit other dialects, thus allowing this accessor to be of more general use.

New in version 1.4.

See Also

_engine.CursorResult.inserted_primary_key

@property
is_insert =

True if this _engine.CursorResult is the result of a executing an expression language compiled _expression.insert construct.

When True, this implies that the inserted_primary_key attribute is accessible, assuming the statement did not include a user defined "returning" construct.

@property
lastrowid =

Return the 'lastrowid' accessor on the DBAPI cursor.

This is a DBAPI specific method and is only functional for those backends which support it, for statements where it is appropriate. It's behavior is not consistent across backends.

Usage of this method is normally unnecessary when using insert() expression constructs; the ~CursorResult.inserted_primary_key attribute provides a tuple of primary key values for a newly inserted row, regardless of database backend.

@property
returned_defaults =

Return the values of default columns that were fetched using the .ValuesBase.return_defaults feature.

The value is an instance of .Row, or None if .ValuesBase.return_defaults was not used or if the backend does not support RETURNING.

New in version 0.9.0.

See Also

.ValuesBase.return_defaults

@property
returned_defaults_rows =

Return a list of rows each containing the values of default columns that were fetched using the .ValuesBase.return_defaults feature.

The return value is a list of .Row objects.

New in version 1.4.
@property
returns_rows =

True if this _engine.CursorResult returns zero or more rows.

I.e. if it is legal to call the methods _engine.CursorResult.fetchone, _engine.CursorResult.fetchmany _engine.CursorResult.fetchall.

Overall, the value of _engine.CursorResult.returns_rows should always be synonymous with whether or not the DBAPI cursor had a .description attribute, indicating the presence of result columns, noting that a cursor that returns zero rows still has a .description if a row-returning statement was emitted.

This attribute should be True for all results that are against SELECT statements, as well as for DML statements INSERT/UPDATE/DELETE that use RETURNING. For INSERT/UPDATE/DELETE statements that were not using RETURNING, the value will usually be False, however there are some dialect-specific exceptions to this, such as when using the MSSQL / pyodbc dialect a SELECT is emitted inline in order to retrieve an inserted primary key value.

Return the 'rowcount' for this result.

The 'rowcount' reports the number of rows matched by the WHERE criterion of an UPDATE or DELETE statement.

Note

Notes regarding _engine.CursorResult.rowcount:

  • This attribute returns the number of rows matched, which is not necessarily the same as the number of rows that were actually modified - an UPDATE statement, for example, may have no net change on a given row if the SET values given are the same as those present in the row already. Such a row would be matched but not modified. On backends that feature both styles, such as MySQL, rowcount is configured by default to return the match count in all cases.
  • _engine.CursorResult.rowcount is only useful in conjunction with an UPDATE or DELETE statement. Contrary to what the Python DBAPI says, it does not return the number of rows available from the results of a SELECT statement as DBAPIs cannot support this functionality when rows are unbuffered.
  • _engine.CursorResult.rowcount may not be fully implemented by all dialects. In particular, most DBAPIs do not support an aggregate rowcount result from an executemany call. The _engine.CursorResult.supports_sane_rowcount and _engine.CursorResult.supports_sane_multi_rowcount methods will report from the dialect if each usage is known to be supported.
  • Statements that use RETURNING may not return a correct rowcount.