o
     Zh%                     @  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__Zdbapi
isinstancedictZinit_oracle_client)selfZauto_convert_lobsZcoerce_to_decimalZ	arraysizeZencoding_errorsZ
thick_modekwargskw	__class__r   Z/var/www/html/lang_env/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)
connectionZ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groupZoracledb_ver_min_versionr   ZInvalidRequestError)r   Zdbapi_moduler1   mr   r   r   _load_version   s   
z$OracleDialect_oracledb._load_version)TTNNN)__name__
__module____qualname__supports_statement_cacheZdriverr4   r   classmethodr"   r&   r)   r6   __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#   r>   outputtypehandlerr   r   r   r   rA         z,AsyncAdapt_oracledb_cursor.outputtypehandlerc                 C     || j _d S r#   r@   r   valuer   r   r   rA         c                 O     | j j|i |S r#   )r>   varr   argsr   r   r   r   rI         zAsyncAdapt_oracledb_cursor.varc                 C  s   | j   | j  d S r#   )_rowsclearr>   closerB   r   r   r   rO      s   
z AsyncAdapt_oracledb_cursor.closerK   r   r   returnc                 O  rH   r#   )r>   setinputsizesrJ   r   r   r   rQ      rL   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__	ExceptionZ_adapt_connectionZ_handle_exception)r   rR   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#   )r>   executedescriptionserver_sidecollectionsdequeZfetchallrM   )r   	operation
parametersresultr   r   r   _execute_async   s   z)AsyncAdapt_oracledb_cursor._execute_asyncc                   s   | j ||I d H S r#   )r>   Zexecutemany)r   r\   Zseq_of_parametersr   r   r   _executemany_async   s   z-AsyncAdapt_oracledb_cursor._executemany_asyncc                 C  s   | S r#   r   rB   r   r   r   rS      s   z$AsyncAdapt_oracledb_cursor.__enter__type_rF   	tracebackNonec                 C  s   |    d S r#   )rO   )r   ra   rF   rb   r   r   r   __exit__   s   z#AsyncAdapt_oracledb_cursor.__exit__N)rK   r   r   r   rP   r   )rR   r   rP   r   )ra   r   rF   r   rb   r   rP   rc   )r7   r8   r9   __annotations__	__slots__propertyrA   setterrI   rO   rQ   rV   r_   r`   rS   rd   r   r   r   r   r=      s   
 



r=   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  r?   r#   rj   
autocommitrB   r   r   r   rl      rC   z)AsyncAdapt_oracledb_connection.autocommitc                 C  rD   r#   rk   rE   r   r   r   rl      rG   c                 C  r?   r#   rj   rA   rB   r   r   r   rA      rC   z0AsyncAdapt_oracledb_connection.outputtypehandlerc                 C  rD   r#   rm   rE   r   r   r   rA      rG   c                 C  r?   r#   )rj   r1   rB   r   r   r   r1      rC   z&AsyncAdapt_oracledb_connection.versionc                 C  r?   r#   rj   stmtcachesizerB   r   r   r   ro      rC   z,AsyncAdapt_oracledb_connection.stmtcachesizec                 C  rD   r#   rn   rE   r   r   r   ro      rG   c                 C  s   t | S r#   )r=   rB   r   r   r   rR      s   z%AsyncAdapt_oracledb_connection.cursor)r7   r8   r9   re   rf   r%   r=   Z_cursor_clsZ_ss_cursor_clsrg   rl   rh   rA   r1   ro   rR   r   r   r   r   ri      s*   
 






ri   c                   @  s   e Zd ZdZdS )&AsyncAdaptFallback_oracledb_connectionr   N)r7   r8   r9   rf   r   r   r   r   rp      s    rp   c                   @  s   e Zd ZdddZdd ZdS )	OracledbAdaptDBAPIrP   rc   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_fallbackFZasync_creator_fn)popr   Zconnect_asyncr   rp   r   ri   r   )r   argr   rw   Z
creator_fnr   r   r   rr     s   zOracledbAdaptDBAPI.connectN)rP   rc   )r7   r8   r9   r   rr   r   r   r   r   rq     s    
rq   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)r.   c                 C  s   dd l }t|S r   )r   rq   r    r   r   r   r"   #  s   z(OracleDialectAsync_oracledb.import_dbapic                 C  s"   |j dd}t|rtjS tjS )Nrw   F)querygetr   r	   ZFallbackAsyncAdaptedQueuePoolZAsyncAdaptedQueuePool)r!   r(   rw   r   r   r   get_pool_class)  s   z*OracleDialectAsync_oracledb.get_pool_classc                 C  s   |j S r#   )rj   )r   r$   r   r   r   get_driver_connection2  s   z1OracleDialectAsync_oracledb.get_driver_connectionN)
r7   r8   r9   is_asyncr:   r4   r;   r"   r|   r}   r   r   r   r   r'     s    

r'   )!__doc__
__future__r   rZ   r/   typingr   r   Z	cx_oracler   Z_OracleDialect_cx_oracle r   r	   Zconnectors.asyncior
   r   r   utilr   r   r   r   r   r   r   r=   ri   rp   rq   r'   dialectZdialect_asyncr   r   r   r   <module>   s8   D<7
)