_dbi_oracle - Private database interface for Oracle
- Purpose:
This module contains the library’s Oracle database methods and attribute accessors; which are a specialised version of the
_dbi_base._DBIBaseclass methods.- Platform:
Linux/Windows | Python 3.10+
- Developer:
J Berendt
- Email:
- Comments:
n/a
- Example:
For class-specific usage examples, please refer to the docstring for the following classes:
- class _DBIOracle(connstr: str)[source]
Bases:
_DBIBaseThis private class holds the methods and properties which are used for accessing Oracle databases.
Note
This class is not designed to be interacted with directly.
Rather, please use the
database.DBInterfaceclass instead, as the proper interface class has an automatic switch for database interfaces, based on thesqlalchemy.Engineobject which is created from the connection string.- Parameters:
connstr (str) – The database-specific SQLAlchemy connection string.
- Example Use:
This low-level generalised class is designed to be inherited by the calling/wrapping class as:
>>> from dbilib.database import DBInterface class MyDB(DBInterface): def __init__(self, connstr: str): super().__init__(connstr=('oracle+cx_oracle://' '<user>:<pwd>' '@(DESCRIPTION=(ADDRESS=' '(PROTOCOL=TCP)' '(HOST=<host>)' '(PORT=<port>))' '(CONNECT_DATA=' '(SERVICE_NAME=<svcname>)))'))
- call_procedure(proc: str, params: list | tuple = None, return_status: bool = False) DataFrame | tuple[DataFrame | bool][source]
Call a stored procedure, and return as a DataFrame.
- Parameters:
proc (str) – Name of the stored procedure to call.
params (list | tuple, optional) – A list (or tuple) of parameters to pass into the procedure. Defaults to None.
return_status (bool, optional) – Return the method’s success status. Defaults to False.
- Returns:
If the
return_statusargument is True, a tuple of the data and the method’s return status is returned as:(df, status)
Otherwise, only the data is returned, as a pd.DataFrame.
- Return type:
pd.DataFrame | tuple[pd.DataFrame | bool]
- call_procedure_update()[source]
Call an update or insert stored procedure.
Warning
Due to the restrictions on the Oracle development environment, database UPDATE-like methods are not allowed. Therefore, this method is currently not implemented.
- Raises:
NotImplementedError – Warn the user that this method is
currently not implemented. –
- call_procedure_update_many()[source]
Call an update or insert stored procedure for an iterable.
Warning
Due to the restrictions on the Oracle development environment, database UPDATE-like methods are not allowed. Therefore, this method is currently not implemented.
- Raises:
NotImplementedError – Warn the user that this method is
currently not implemented. –
- call_procedure_update_raw()[source]
Call an update or insert stored procedure, without error handling.
Warning
Due to the restrictions on the Oracle development environment, database UPDATE-like methods are not allowed. Therefore, this method is currently not implemented.
- Raises:
NotImplementedError – Warn the user that this method is
currently not implemented. –
- table_exists(table_name: str, verbose: bool = False) bool[source]
Using the
engineobject, test if the given table exists.- Parameters:
table_name (str) – Name of the table to test.
verbose (bool, optional) – Print a message if the table does not exist. Defaults to False.
Note
As most Oracle objects are UPPERCASE, the table name is converted to upper case before being passed into the query.
- Returns:
True if the given table exists, otherwise False.
- Return type:
bool
- _report_cxo_error(msg: str, error: cx_Oracle.DatabaseError)[source]
Report cx_Oracle error to the terminal.
- Parameters:
msg (str) – Additional error to be displayed. This message will be automatically prefixed with ‘[DatabaseError]: ‘
error (cx_Oracle.DatabaseError) – Caught error object from the try/except block.
- static _result_to_df__refcursor(refcur: cx_Oracle.Cursor) DataFrame[source]
Convert a
cx_Oracle.Cursorobject to a DataFrame.If the cursor did not return results, an empty DataFrame containing the column names only, is returned.
- Parameters:
refcur (cx_Oracle.Cursor) – Object to be converted.
- Returns:
A
pandas.DataFrameobject containing the cursor’s data.- Return type:
pd.DataFrame
- _create_engine() sqlalchemy.engine.base.Engine
Create a database engine using the provided environment.
- Returns:
A sqlalchemy database engine object.
- Return type:
sqlalchemy.engine.base.Engine
- static _is_dangerous(stmt: str) bool
Perform a dirty security check for injection attempts.
- Parameters:
stmt (str) – SQL statement to be potentially executed.
- Raises:
SecurityWarning – If there are multiple semi-colons (
;) in the statement, or any comment delimiters (--).- Returns:
False if the checks pass.
- Return type:
bool
- _report_sa_error(msg: str, error: sqlalchemy.exc.SQLAlchemyError)
Report SQLAlchemy error to the terminal.
- Parameters:
msg (str) – Additional error to be displayed. This message will be automatically prefixed with ‘[DatabaseError]: ‘
error (sqlalchemy.exc.SQLAlchemyError) – Caught error object from the try/except block.
- static _result_to_df__cursor(result: sqlalchemy.engine.cursor.CursorResult) DataFrame
Convert a
CursorResultobject to a DataFrame.If the cursor did not return results, an empty DataFrame containing the column names only, is returned.
- Parameters:
result (sqlalchemy.engine.cursor.CursorResult) – Object to be converted.
- Returns:
A
pandas.DataFrameobject containing the cursor’s data.- Return type:
pd.DataFrame
- static _result_to_df__stored(result: object) DataFrame
Convert a
MySQLCursor.stored_resultsobject to a DataFrame.- Parameters:
result (object) – The
cursor.stored_results()object fromcall. (a sqlalchemy or mysql.connector procedure)
- Returns:
A DataFrame containing the results from the procedure call.
- Return type:
pd.DataFrame
- property engine
Accessor to the
sqlalchemy.engine.base.Engineobject.
- execute_query(stmt: str, params: dict = None, *, raw: bool = True, flat: bool = False, commit: bool = True, ignore_unsafe: bool = False) list | DataFrame | None
Execute a query statement.
Important
The following are not allowed to be executed by this method:
Statements containing multiple semi-colons (
;).Statements containing a comment delimiter (
--).
If found, a
SecurityWarningwill be raised by the_is_dangerous()method.- Parameters:
stmt (str) – Statement to be executed. The parameter bindings are to be written in colon format.
params (dict, optional) – Parameter key/value bindings as a dictionary, if applicable. Defaults to None.
raw (bool, optional) – Return the data in ‘raw’ (tuple) format rather than as a formatted DataFrame. Defaults to True for efficiency.
flat (bool, optional) – Flatten the response. This is useful if the expected response is a collection of single elements. If True, this will return a flattened tuple of elements, rather than a list of tuples, as is the default behaviour. Note (1): If true, and the return is a list of multi-value tuples, only the first element in each tuple will be returned. Note (2): This argument should only be used if
raw=True. Defaults to False.commit (bool, optional) – Call COMMIT after the transaction is complete. Defaults to True (for backwards compatibility).
ignore_unsafe (bool, optional) –
Bypass the ‘is dangerous’ check and the run query anyway. This may be required if a script contains multiple statements. Defaults to False.
WARNING: HC SVNT DRACONES
If the query did not return results and the
rawargument is False, an empty DataFrame containing the column names only, is returned.Note
In the SQL query, the bind parameters are specified by name, using the format
:bind_name. Theparamsdictionary argument must contain the associated parameter name/value bindings.Warning
Generally, whatever statement is passed into this method will be executed, and may have destructive implications.
This method contains a
commitcall, and the option to disable the COMMIT.
If a statement is passed into this method, and the user has the appropriate permissions - the change will be committed.
… HC SVNT DRACONES.
- Returns:
If the
rawparameter is True, a list of tuples containing values is returned. Otherwise, apandas.DataFrameobject containing the returned data is returned.If this method is called with a script which does not return results, for example a CREATE script, None is returned; regardless of the value passed to the
rawparameter.- Return type:
list | pd.DataFrame | None