A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python-oracledb.readthedocs.io/en/latest/api_manual/async_cursor.html below:

Website Navigation


AsyncCursor Objects — python-oracledb 3.4.0b1 documentation

19. API: AsyncCursor Objects 19.1. AsyncCursor Class
class oracledb.AsyncCursor(connection: Connection, scrollable: bool = False)

An AsyncCursor object should be created with AsyncConnection.cursor().

This object is an extension to the DB API definition.

Added in version 2.0.0.

Note

AsyncCursor objects are only supported in the python-oracledb Thin mode.

19.2. AsyncCursor Methods
AsyncCursor.__aiter__()

Returns the cursor itself to be used as an asynchronous iterator.

async AsyncCursor.__aenter__()

The entry point for the cursor as a context manager. It returns itself.

async AsyncCursor.__aexit__(*exc_info)

The exit point for the cursor as a context manager. It closes the cursor.

AsyncCursor.arrayvar(typ: DbType | DbObjectType | type, value: list | int, size: int = 0) Var

Creates an array variable associated with the cursor of the given type and size and returns a variable object. The value is either an integer specifying the number of elements to allocate or it is a list and the number of elements allocated is drawn from the size of the list. If the value is a list, the variable is also set with the contents of the list. If the size is not specified and the type is a string or binary, 4000 bytes is allocated. This is needed for passing arrays to PL/SQL (in cases where the list might be empty and the type cannot be determined automatically) or returning arrays from PL/SQL.

AsyncCursor.bindnames() list

Returns the list of bind variable names bound to the statement. Note that a statement must have been prepared first.

async AsyncCursor.callfunc(name: str, return_type: Any, parameters: list | tuple | None = None, keyword_parameters: dict | None = None) Any

Calls a PL/SQL function with the given name and returns its value.

The return_type parameter is expected to be a Python type, one of the oracledb types or an Object Type.

The sequence of parameters must contain one entry for each parameter that the PL/SQL function expects. Any keyword parameters will be included after the positional parameters.

Use var() to define any OUT or IN OUT parameters, if necessary.

See PL/SQL Stored Functions for examples.

async AsyncCursor.callproc(name: str, parameters: list | tuple | None = None, keyword_parameters: dict | None = None) list

Calls a PL/SQL procedure with the given name.

The sequence of parameters must contain one entry for each parameter that the procedure expects. The result of the call is a modified copy of the input sequence. Input parameters are left untouched; output and input/output parameters are replaced with possibly new values. Keyword parameters will be included after the positional parameters and are not returned as part of the output sequence.

Use var() to define any OUT or IN OUT parameters if necessary.

No query result set is returned by callproc(). Instead, use REF CURSOR parameters or Implicit Results.

See PL/SQL Stored Procedures for an example.

AsyncCursor.close() None

Closes the cursor now, rather than whenever __del__ is called. The cursor will be unusable from this point forward; an Error exception will be raised if any operation is attempted with the cursor.

Note

Asynchronous cursors are not automatically closed at the end of scope. This is different to synchronous cursor behavior. Asynchronous cursors should either be explicitly closed, or have been initially created via a context manager with block.

async AsyncCursor.execute(statement: str | None, parameters: list | tuple | dict | None = None, *, suspend_on_success: bool = False, fetch_lobs: bool | None = None, fetch_decimals: bool | None = None, **keyword_parameters: Any) None

Executes a statement against the database. See Executing SQL.

Parameters may be passed as a dictionary or sequence or as keyword parameters. If the parameters are a dictionary, the values will be bound by name and if the parameters are a sequence the values will be bound by position. Note that if the values are bound by position, the order of the variables is from left to right as they are encountered in the statement and SQL statements are processed differently than PL/SQL statements. For this reason, it is generally recommended to bind parameters by name instead of by position.

Parameters passed as a dictionary are name and value pairs. The name maps to the bind variable name used by the statement and the value maps to the Python value you wish bound to that bind variable.

A reference to the statement will be retained by the cursor. If None or the same string object is passed in again, the cursor will execute that statement again without performing a prepare or rebinding and redefining. This is most effective for algorithms where the same statement is used, but different parameters are bound to it (many times). Note that parameters that are not passed in during subsequent executions will retain the value passed in during the last execution that contained them.

The suspend_on_success parameter is specific to sessionless transactions. When set to True, the active sessionless transaction will be suspended when execute() completes successfully. See Suspending Sessionless Transactions.

The fetch_lobs parameter specifies whether to return LOB locators or str/bytes values when fetching LOB columns. The default value is oracledb.defaults.fetch_lobs.

The fetch_decimals parameter specifies whether to return decimal.Decimal values when fetching columns of type NUMBER. The default value is oracledb.defaults.fetch_decimals.

For maximum efficiency when reusing a statement, it is best to use the setinputsizes() method to specify the parameter types and sizes ahead of time; in particular, None is assumed to be a string of length 1 so any values that are later bound as numbers or dates will raise a TypeError exception.

If the statement is a SELECT query, the cursor is returned as a convenience to the caller (so it can be used directly as an iterator over the rows in the cursor); otherwise, None is returned.

Changed in version 3.4.0: The fetch_lobs and fetch_decimals parameters were added.

Changed in version 3.3.0: The suspend_on_success parameter was added.

async AsyncCursor.executemany(statement: str | None, parameters: Any, batcherrors: bool = False, arraydmlrowcounts: bool = False, suspend_on_success: bool = False) None

Executes a SQL statement once using all bind value mappings or sequences found in the sequence parameters. This can be used to insert, update, or delete multiple rows in a table with a single python-oracledb call. It can also invoke a PL/SQL procedure multiple times. See Batch Statement and Bulk Copy Operations.

The statement parameter is managed in the same way as the execute() method manages it.

The parameters parameter can be a list of tuples, where each tuple item maps to one bind variable placeholder in statement. It can also be a list of dictionaries, where the keys match the bind variable placeholder names in statement. If there are no bind values, or values have previously been bound, the parameters value can be an integer specifying the number of iterations. The parameters parameter can also be a DataFrame, or a third-party data frame that supports the Apache Arrow PyCapsule Interface.

In python-oracledb Thick mode, if the size of the buffers allocated for any of the parameters exceeds 2 GB, you will receive the error DPI-1015: array size of <n> is too large. If you receive this error, decrease the number of rows being inserted.

When True, the batcherrors parameter enables batch error support within Oracle and ensures that the call succeeds even if an exception takes place in one or more of the sequence of parameters. The errors can then be retrieved using getbatcherrors().

When True, the arraydmlrowcounts parameter enables DML row counts to be retrieved from Oracle after the method has completed. The row counts can then be retrieved using getarraydmlrowcounts().

Both the batcherrors parameter and the arraydmlrowcounts parameter can only be True when executing an insert, update, delete, or merge statement. In all other cases, an error will be raised.

The suspend_on_success parameter is specific to sessionless transactions. When set to True, the active sessionless transaction will be suspended when executemany() completes successfully. See Suspending Sessionless Transactions.

For maximum efficiency, it is best to use the setinputsizes() method to specify the parameter types and sizes ahead of time. In particular, the value None is assumed to be a string of length 1 so any values that are later bound as numbers or dates will raise a TypeError exception.

Changed in version 3.3.0: Added support for passing data frames in the parameters parameter. The suspend_on_success parameter was added.

async AsyncCursor.fetchall() list

Fetches all (remaining) rows of a SELECT query result, returning them as a list of tuples. An empty list is returned if no more rows are available. An exception is raised if the previous call to execute() did not produce any result set or no call was issued yet.

Note that the cursor’s arraysize attribute can affect the performance of this operation, as internally data is fetched in batches of that size from the database.

async AsyncCursor.fetchmany(size: int | None = None) list

Fetches the next set of rows of a SELECT query result, returning a list of tuples. An empty list is returned if no more rows are available. Note that the cursor’s arraysize attribute can affect the performance of this operation.

The number of rows to fetch is specified by the parameter. If it is not given, the cursor’s arraysize attribute determines the number of rows to be fetched. If the number of rows available to be fetched is fewer than the amount requested, fewer rows will be returned.

An exception is raised if the previous call to execute() did not produce any result set or no call was issued yet.

async AsyncCursor.fetchone() Any

Fetches the next row of a SELECT query result set, returning a single tuple or None when no more data is available. An exception is raised if the previous call to execute() did not produce any result set or no call was issued yet.

When fetchone() is used to iterate over a result set, the cursor’s arraysize attribute can affect performance, as internally data is fetched in batches of that size from Oracle Database.

AsyncCursor.getarraydmlrowcounts() list

Retrieves the DML row counts after a call to executemany() with arraydmlrowcounts enabled. This will return a list of integers corresponding to the number of rows affected by the DML statement for each element of the array passed to executemany().

This method is only available for Oracle Database 12.1 and later.

AsyncCursor.getbatcherrors() list

Retrieves the exceptions that took place after a call to executemany() with batcherrors enabled. This will return a list of Error objects, one error for each iteration that failed. The offset can be determined by looking at the offset attribute of the error object.

AsyncCursor.getimplicitresults() list

Returns a list of cursors which correspond to implicit results made available from a PL/SQL block or procedure without the use of OUT ref cursor parameters. The PL/SQL block or procedure opens the cursors and marks them for return to the client using the procedure dbms_sql.return_result. In python-oracledb Thick mode, closing the parent cursor will result in the automatic closure of the implicit result set cursors. See Implicit Results.

This method is only available for Oracle Database 12.1 (or later). For python-oracledb Thick mode, Oracle Client 12.1 (or later) is additionally required.

Note

It is most like the DB API method nextset(), but unlike that method (which requires that the next result set overwrite the current result set), this method returns cursors which can be fetched independently of each other.

async AsyncCursor.parse(statement: str) None

This can be used to parse a statement without actually executing it (parsing step is done automatically by Oracle when a statement is executed).

Note

You can parse any DML or DDL statement. DDL statements are executed immediately and an implied commit takes place.

AsyncCursor.prepare(statement: str, tag: str | None = None, cache_statement: bool = True) None

This can be used before a call to execute() or executemany() to define the statement that will be executed. When this is done, the prepare phase will not be performed when the call to execute() or executemany() is made with None or the same string object as the statement.

If the tag parameter is specified and the cache_statement parameter is True, the statement will be returned to the statement cache with the given tag.

If the cache_statement parameter is False, the statement will be removed from the statement cache (if it was found there) or will simply not be cached.

See Statement Caching for more information.

AsyncCursor.setinputsizes(*args: Any, **kwargs: Any) list | dict

This can be used before calls to execute() or executemany() to predefine memory areas used for bind variables. Each parameter should be a type object corresponding to the data that will be used for a bind variable placeholder in the SQL or PL/SQL statement. Alternatively, it can be an integer specifying the maximum length of a string bind variable value.

Use keyword parameters when binding by name. Use positional parameters when binding by position. The parameter value can be None to indicate that python-oracledb should determine the required space from the data value provided.

The parameters or keyword names correspond to the bind variable placeholders used in the SQL or PL/SQL statement. Note this means that for use with executemany() it does not correspond to the number of bind value mappings or sequences being passed.

When repeated calls to execute() or executemany() are made binding different string data lengths, using setinputsizes() can help reduce the database’s SQL “version count” for the statement. See Reducing the SQL Version Count.

async AsyncCursor.scroll(value: int = 0, mode: str = 'relative') None

Scrolls the cursor in the result set to a new position according to the mode.

If mode is relative (the default value), the value is taken as an offset to the current position in the result set. If set to absolute, value states an absolute target position. If set to first, the cursor is positioned at the first row and if set to last, the cursor is set to the last row in the result set.

An error is raised if the mode is relative or absolute and the scroll operation would position the cursor outside of the result set.

AsyncCursor.setoutputsize(size: int, column: int = 0) None

This method does nothing and is retained solely for compatibility with the DB API. Python-oracledb automatically allocates as much space as needed to fetch LONG and LONG RAW columns, and also to fetch CLOB as string and BLOB as bytes.

AsyncCursor.var(typ: DbType | DbObjectType | type, size: int = 0, arraysize: int = 1, inconverter: Callable | None = None, outconverter: Callable | None = None, typename: str | None = None, encoding_errors: str | None = None, bypass_decode: bool = False, convert_nulls: bool = False, *, encodingErrors: str | None = None) Var

Creates a variable object with the specified characteristics. This method can be used for binding to PL/SQL IN and OUT parameters where the length or type cannot be determined automatically from the Python variable being bound. It can also be used in input and output type handlers.

The typ parameter specifies the type of data that should be stored in the variable. This should be one of the database type constants, DB API constants, an object type returned from the method Connection.gettype() or one of the following Python types:

The size parameter specifies the length of string and raw variables and is ignored in all other cases. If not specified for string and raw variables, the value 4000 is used.

The arraysize parameter specifies the number of elements the variable will have. If not specified the bind array size (usually 1) is used. When a variable is created in an output type handler this parameter should be set to the cursor’s array size.

The inconverter and outconverter parameters specify methods used for converting values to/from the database. More information can be found in the section on variable objects.

The typename parameter specifies the name of a SQL object type and must be specified when using type oracledb.OBJECT unless the type object was passed directly as the first parameter.

The encoding_errors parameter specifies what should happen when decoding byte strings fetched from the database into strings. It should be one of the values noted in the builtin decode function.

The bypass_decode parameter, if specified, should be passed as a boolean value. Passing a True value causes values of database types DB_TYPE_VARCHAR, DB_TYPE_CHAR, DB_TYPE_NVARCHAR, DB_TYPE_NCHAR and DB_TYPE_LONG to be returned as bytes instead of str, meaning that python-oracledb does not do any decoding. See Fetching raw data for more information.

The convert_nulls parameter, if specified, should be passed as a boolean value. Passing the value True causes the outconverter to be called when a null value is fetched from the database; otherwise, the outconverter is only called when non-null values are fetched from the database.

For consistency and compliance with the PEP 8 naming style, the parameter encodingErrors was renamed to encoding_errors. The old name will continue to work as a keyword parameter for a period of time.

19.3. AsyncCursor Attributes
property AsyncCursor.arraysize: int

This read-write attribute can be used to tune the number of rows internally fetched and buffered by internal calls to the database when fetching rows from SELECT statements and REF CURSORS.

The value of arraysize can drastically affect the performance of a query since it directly affects the number of network round trips between Python and the database. For methods like fetchone() and fetchall() it affects internal behavior but does not change how many rows are returned to the application. For fetchmany() it is the default number of rows to fetch.

The attribute is only used for tuning row and SODA document fetches from the database. It does not affect data inserts.

Due to the performance benefits, the default arraysize is 100 instead of the 1 that the Python DB API recommends.

See Tuning Fetch Performance for more information.

property AsyncCursor.bindvars: list

This read-only attribute provides the bind variables used for the last statement that was executed on the cursor. The value will be either a list or a dictionary, depending on whether binding was done by position or name. Care should be taken when referencing this attribute. In particular, elements should not be removed or replaced.

property AsyncCursor.connection: AsyncConnection

This read-only attribute returns a reference to the connection object on which the cursor was created.

property AsyncCursor.description: list[FetchInfo] | None

This read-only attribute contains information about the columns used in a query. It is a list of FetchInfo objects, one per column. This attribute will be None for statements that are not SELECT or WITH statements, or if the cursor has not had execute() invoked yet.

property AsyncCursor.fetchvars: list

This read-only attribute specifies the list of variables created for the last SELECT query that was executed on the cursor. Care should be taken when referencing this attribute. In particular, elements should not be removed or replaced.

property AsyncCursor.inputtypehandler: Callable

This read-write attribute specifies a method called for each value that is bound to a statement executed on the cursor and overrides the attribute with the same name on the connection if specified. The method signature is handler(cursor, value, arraysize) and the return value is expected to be a variable object or None in which case a default variable object will be created. If this attribute is None, the default behavior will take place for all values bound to the statements.

property AsyncCursor.lastrowid: str

This read-only attribute returns the rowid of the last row modified by the cursor. If no row was modified by the last operation performed on the cursor, the value None is returned.

property AsyncCursor.outputtypehandler: Callable

This read-write attribute specifies a method called for each column that is to be fetched from this cursor. The method signature is handler(cursor, metadata) and the return value is expected to be a variable object or None in which case a default variable object will be created. If this attribute is None, then the default behavior will take place for all columns fetched from this cursor.

See Changing Fetched Data Types with Output Type Handlers.

property AsyncCursor.prefetchrows: int

This read-write attribute can be used to tune the number of rows that python-oracledb initially fetches from Oracle Database when a SELECT query is executed. The value can improve performance by reducing the number of round-trips to the database. The attribute does not affect data insertion.

In python-oracledb Thin mode, prefetching can reuse the arraysize buffer. However in Thick mode, extra memory is required.

Setting this value to 0 can be useful when the timing of fetches must be explicitly controlled.

Queries that return LOB objects and similar types do not support prefetching. The prefetchrows attribute is ignored in queries that involve these types.

See Tuning Fetch Performance for more information.

property AsyncCursor.rowcount: int

This read-only attribute specifies the number of rows that have currently been fetched from the cursor (for select statements) or that have been affected by the operation (for insert, update, delete, and merge statements). For all other statements the value is always 0. If the cursor or connection is closed, the value returned is -1.

property AsyncCursor.rowfactory: Callable

This read-write attribute specifies a method to call for each row that is retrieved from the database. Ordinarily, a tuple is returned for each row but if this attribute is set, the method is called with the tuple that would normally be returned, and the result of the method is returned instead.

See Changing Query Results with Rowfactories.

property AsyncCursor.scrollable: bool

This read-write boolean attribute specifies whether the cursor can be scrolled or not. By default, cursors are not scrollable, as the server resources and response times are greater than nonscrollable cursors. This attribute is checked and the corresponding mode set in Oracle when calling the method execute().


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4