o
    Df%                     @  s,  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm	Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ er`ddlmZ ddlmZ G dd de
ZG dd deZG dd deZG dd deeZG dd dZG dd deZeZeZ dS )a
  
.. dialect:: oracle+oracledb
    :name: python-oracledb
    :dbapi: oracledb
    :connectstring: oracle+oracledb://user:pass@hostname:port[/dbname][?service_name=<service>[&key=value&key=value...]]
    :url: https://oracle.github.io/python-oracledb/

python-oracledb is released by Oracle to supersede the cx_Oracle driver.
It is fully compatible with cx_Oracle and features both a "thin" client
mode that requires no dependencies, as well as a "thick" mode that uses
the Oracle Client Interface in the same way as cx_Oracle.

.. seealso::

    :ref:`cx_oracle` - all of cx_Oracle's notes apply to the oracledb driver
    as well.

The SQLAlchemy ``oracledb`` dialect provides both a sync and an async
implementation under the same dialect name. The proper version is
selected depending on how the engine is created:

* calling :func:`_sa.create_engine` with ``oracle+oracledb://...`` will
  automatically select the sync version, e.g.::

    from sqlalchemy import create_engine
    sync_engine = create_engine("oracle+oracledb://scott:tiger@localhost/?service_name=XEPDB1")

* calling :func:`_asyncio.create_async_engine` with
  ``oracle+oracledb://...`` will automatically select the async version,
  e.g.::

    from sqlalchemy.ext.asyncio import create_async_engine
    asyncio_engine = create_async_engine("oracle+oracledb://scott:tiger@localhost/?service_name=XEPDB1")

The asyncio version of the dialect may also be specified explicitly using the
``oracledb_async`` suffix, as::

    from sqlalchemy.ext.asyncio import create_async_engine
    asyncio_engine = create_async_engine("oracle+oracledb_async://scott:tiger@localhost/?service_name=XEPDB1")

.. versionadded:: 2.0.25 added support for the async version of oracledb.

Thick mode support
------------------

By default the ``python-oracledb`` is started in thin mode, that does not
require oracle client libraries to be installed in the system. The
``python-oracledb`` driver also support a "thick" mode, that behaves
similarly to ``cx_oracle`` and requires that Oracle Client Interface (OCI)
is installed.

To enable this mode, the user may call ``oracledb.init_oracle_client``
manually, or by passing the parameter ``thick_mode=True`` to
:func:`_sa.create_engine`. To pass custom arguments to ``init_oracle_client``,
like the ``lib_dir`` path, a dict may be passed to this parameter, as in::

    engine = sa.create_engine("oracle+oracledb://...", thick_mode={
        "lib_dir": "/path/to/oracle/client/lib", "driver_name": "my-app"
    })

.. seealso::

    https://python-oracledb.readthedocs.io/en/latest/api_manual/module.html#oracledb.init_oracle_client


.. versionadded:: 2.0.0 added support for oracledb driver.

    )annotationsN)Any)TYPE_CHECKING   )OracleDialect_cx_oracle   )exc)pool)AsyncAdapt_dbapi_connection)AsyncAdapt_dbapi_cursor)#AsyncAdaptFallback_dbapi_connection)asbool)await_fallback)
await_only)AsyncConnection)AsyncCursorc                      s`   e Zd ZdZdZdZ					d fdd	Zedd Zed	d
 Z	edd Z
dd Z  ZS )OracleDialect_oracledbToracledb)r   Nc                   sb   t  j||||fi | | jd ur-|st|tr/t|tr |ni }| jjdi | d S d S d S )N )super__init__dbapi
isinstancedictinit_oracle_client)selfauto_convert_lobscoerce_to_decimal	arraysizeencoding_errors
thick_modekwargskw	__class__r   \/home/ubuntu/webapp/venv/lib/python3.10/site-packages/sqlalchemy/dialects/oracle/oracledb.pyr   h   s    	
zOracleDialect_oracledb.__init__c                 C  s   dd l }|S Nr   )r   clsr   r   r   r%   import_dbapi   s   z#OracleDialect_oracledb.import_dbapic                 C  s
   |j jjS N)
connectiondbapi_connectionthin)r(   r+   r   r   r%   is_thin_mode   s   
z#OracleDialect_oracledb.is_thin_modec                 C  s   t S r*   )OracleDialectAsync_oracledb)r(   urlr   r   r%   get_async_dialect_cls   s   z,OracleDialect_oracledb.get_async_dialect_clsc                 C  sr   d}|d urt d|j}|rtdd |dddD }|| _| jdkr5| j| jk r7td| j d	d S d S )
N)r   r   r   z(\d+)\.(\d+)(?:\.(\d+))?c                 s  s     | ]}|d urt |V  qd S r*   )int).0xr   r   r%   	<genexpr>   s    z7OracleDialect_oracledb._load_version.<locals>.<genexpr>r      r   zoracledb version z and above are supported)	rematchversiontuplegrouporacledb_ver_min_versionr   InvalidRequestError)r   dbapi_moduler9   mr   r   r%   _load_version   s   
z$OracleDialect_oracledb._load_version)TTNNN)__name__
__module____qualname__supports_statement_cachedriverr=   r   classmethodr)   r.   r1   rA   __classcell__r   r   r#   r%   r   c   s"    


r   c                   @  sz   e Zd ZU ded< dZedd Zejdd Zdd Zd	d
 Z	d!ddZ
d"ddZdd Zdd Zdd Zd#ddZd S )$AsyncAdapt_oracledb_cursorr   _cursorr   c                 C     | j jS r*   rJ   outputtypehandlerr   r   r   r%   rM         z,AsyncAdapt_oracledb_cursor.outputtypehandlerc                 C     || j _d S r*   rL   r   valuer   r   r%   rM         c                 O     | j j|i |S r*   )rJ   varr   argsr!   r   r   r%   rU         zAsyncAdapt_oracledb_cursor.varc                 C  s   | j   | j  d S r*   )_rowsclearrJ   closerN   r   r   r%   r[      s   
z AsyncAdapt_oracledb_cursor.closerW   r   r!   returnc                 O  rT   r*   )rJ   setinputsizesrV   r   r   r%   r]      rX   z(AsyncAdapt_oracledb_cursor.setinputsizescursorc              
   C  s>   z|  W S  ty } z| j| W Y d }~d S d }~ww r*   )	__enter__	Exception_adapt_connection_handle_exception)r   r^   errorr   r   r%   _aenter_cursor   s   
z)AsyncAdapt_oracledb_cursor._aenter_cursorc                   s\   |d u r| j |I d H }n
| j ||I d H }| j jr,| js,t| j  I d H | _|S r*   )rJ   executedescriptionserver_sidecollectionsdequefetchallrY   )r   	operation
parametersresultr   r   r%   _execute_async   s   z)AsyncAdapt_oracledb_cursor._execute_asyncc                   s   | j ||I d H S r*   )rJ   executemany)r   rk   seq_of_parametersr   r   r%   _executemany_async   s   z-AsyncAdapt_oracledb_cursor._executemany_asyncc                 C  s   | S r*   r   rN   r   r   r%   r_      s   z$AsyncAdapt_oracledb_cursor.__enter__type_rR   	tracebackNonec                 C  s   |    d S r*   )r[   )r   rr   rR   rs   r   r   r%   __exit__   s   z#AsyncAdapt_oracledb_cursor.__exit__N)rW   r   r!   r   r\   r   )r^   r   r\   r   )rr   r   rR   r   rs   r   r\   rt   )rB   rC   rD   __annotations__	__slots__propertyrM   setterrU   r[   r]   rd   rn   rq   r_   ru   r   r   r   r%   rI      s   
 



rI   c                   @  s   e Zd ZU ded< dZdZeZdZe	dd Z
e
jdd Z
e	d	d
 Zejdd
 Ze	dd Ze	dd Zejdd Zdd ZdS )AsyncAdapt_oracledb_connectionr   _connectionr   TNc                 C  rK   r*   r{   
autocommitrN   r   r   r%   r}      rO   z)AsyncAdapt_oracledb_connection.autocommitc                 C  rP   r*   r|   rQ   r   r   r%   r}      rS   c                 C  rK   r*   r{   rM   rN   r   r   r%   rM      rO   z0AsyncAdapt_oracledb_connection.outputtypehandlerc                 C  rP   r*   r~   rQ   r   r   r%   rM      rS   c                 C  rK   r*   )r{   r9   rN   r   r   r%   r9      rO   z&AsyncAdapt_oracledb_connection.versionc                 C  rK   r*   r{   stmtcachesizerN   r   r   r%   r      rO   z,AsyncAdapt_oracledb_connection.stmtcachesizec                 C  rP   r*   r   rQ   r   r   r%   r      rS   c                 C  s   t | S r*   )rI   rN   r   r   r%   r^      s   z%AsyncAdapt_oracledb_connection.cursor)rB   rC   rD   rv   rw   r-   rI   _cursor_cls_ss_cursor_clsrx   r}   ry   rM   r9   r   r^   r   r   r   r%   rz      s*   
 






rz   c                   @  s   e Zd ZdZdS )&AsyncAdaptFallback_oracledb_connectionr   N)rB   rC   rD   rw   r   r   r   r%   r      s    r   c                   @  s   e Zd ZdddZdd ZdS )	OracledbAdaptDBAPIr\   rt   c                 C  s2   || _ | j j D ]\}}|dkr|| j|< q	d S )Nconnect)r   __dict__items)r   r   kvr   r   r%   r     s   
zOracledbAdaptDBAPI.__init__c                 O  sT   | dd}| d| jj}t|rt| t||i |S t| t||i |S )Nasync_fallbackFasync_creator_fn)popr   connect_asyncr   r   r   rz   r   )r   argr"   r   
creator_fnr   r   r%   r     s   zOracledbAdaptDBAPI.connectN)r\   rt   )rB   rC   rD   r   r   r   r   r   r%   r     s    
r   c                   @  s8   e Zd ZdZdZdZedd Zedd Zdd Z	d	S )
r/   T)r6   c                 C  s   dd l }t|S r&   )r   r   r'   r   r   r%   r)   #  s   z(OracleDialectAsync_oracledb.import_dbapic                 C  s"   |j dd}t|rtjS tjS )Nr   F)querygetr   r	   FallbackAsyncAdaptedQueuePoolAsyncAdaptedQueuePool)r(   r0   r   r   r   r%   get_pool_class)  s   z*OracleDialectAsync_oracledb.get_pool_classc                 C  s   |j S r*   )r{   )r   r+   r   r   r%   get_driver_connection2  s   z1OracleDialectAsync_oracledb.get_driver_connectionN)
rB   rC   rD   is_asyncrE   r=   rG   r)   r   r   r   r   r   r%   r/     s    

r/   )!__doc__
__future__r   rh   r7   typingr   r   	cx_oracler   _OracleDialect_cx_oracle r   r	   connectors.asyncior
   r   r   utilr   r   r   r   r   r   r   rI   rz   r   r   r/   dialectdialect_asyncr   r   r   r%   <module>   s8   D<7
)