
    e%                    v   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e
          Z G d de          Z G d de          Z G d dee          Z G d d          Z G 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                  ~     e Zd ZdZdZdZ	 	 	 	 	 d fd	Zed             Zed             Z	ed	             Z
d
 Z xZS )OracleDialect_oracledbToracledb)r   2   Nc                     t                      j        ||||fi | | j        D|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__s	           Clib/python3.11/site-packages/sqlalchemy/dialects/oracle/oracledb.pyr   zOracleDialect_oracledb.__init__h   s     			
 	

 	
 	
 	
 :! "$Z66 "  **d;;CB)DJ)//B/////	 "!!!    c                    dd l }|S Nr   )r   clsr   s     r(   import_dbapiz#OracleDialect_oracledb.import_dbapi   s    r)   c                $    |j         j        j        S N)
connectiondbapi_connectionthin)r-   r1   s     r(   is_thin_modez#OracleDialect_oracledb.is_thin_mode   s    $5::r)   c                    t           S r0   )OracleDialectAsync_oracledb)r-   urls     r(   get_async_dialect_clsz,OracleDialect_oracledb.get_async_dialect_cls   s    **r)   c                $   d}|Jt          j        d|j                  }|r.t          d |                    ddd          D                       }|| _        | j        dk    r-| j        | j        k     rt          j        d| j         d          d S d S )	N)r   r   r   z(\d+)\.(\d+)(?:\.(\d+))?c              3  8   K   | ]}|t          |          V  d S r0   )int).0xs     r(   	<genexpr>z7OracleDialect_oracledb._load_version.<locals>.<genexpr>   s1           CFF   r)   r      r   zoracledb version z and above are supported)	rematchversiontuplegrouporacledb_ver_min_versionr	   InvalidRequestError)r   dbapi_modulerB   ms       r(   _load_versionz$OracleDialect_oracledb._load_version   s    #4l6JKKA     $%GGAq!$4$4        $	))!D$555)OD$5OOO   *)55r)   )TTr   NN)__name__
__module____qualname__supports_statement_cachedriverrF   r   classmethodr.   r4   r8   rJ   __classcell__)r'   s   @r(   r   r   c   s        #FL 0 0 0 0 0 0.   [
 ; ; [; + + [+      r)   r   c                      e Zd ZU ded<   dZed             Zej        d             Zd Zd Z	ddZ
ddZd Zd Zd ZddZdS )AsyncAdapt_oracledb_cursorr   _cursorr   c                    | j         j        S r0   rT   outputtypehandlerr   s    r(   rW   z,AsyncAdapt_oracledb_cursor.outputtypehandler   s    |--r)   c                    || j         _        d S r0   rV   r   values     r(   rW   z,AsyncAdapt_oracledb_cursor.outputtypehandler   s    ).&&&r)   c                &     | j         j        |i |S r0   )rT   varr   argsr%   s      r(   r]   zAsyncAdapt_oracledb_cursor.var   s    t|0000r)   c                j    | j                                          | j                                         d S r0   )_rowsclearrT   closerX   s    r(   rc   z AsyncAdapt_oracledb_cursor.close   s0    
r)   r_   r   r%   returnc                &     | j         j        |i |S r0   )rT   setinputsizesr^   s      r(   rf   z(AsyncAdapt_oracledb_cursor.setinputsizes   s    )t|)4:6:::r)   cursorc                    	 |                                 S # t          $ r%}| j                            |           Y d }~d S d }~ww xY wr0   )	__enter__	Exception_adapt_connection_handle_exception)r   rg   errors      r(   _aenter_cursorz)AsyncAdapt_oracledb_cursor._aenter_cursor   sa    	<##%%% 	< 	< 	<"44U;;;;;;;;;	<s    
AA  Ac                $  K   |!| j                             |           d {V }n!| j                             ||           d {V }| j         j        r=| j        s6t	          j        | j                                          d {V           | _        |S r0   )rT   executedescriptionserver_sidecollectionsdequefetchallra   )r   	operation
parametersresults       r(   _execute_asyncz)AsyncAdapt_oracledb_cursor._execute_async   s       <//	::::::::FF<//	:FFFFFFFFF<# 	JD,< 	J$*1F1F1H1H+H+H+H+H+H+HIIDJr)   c                H   K   | j                             ||           d {V S r0   )rT   executemany)r   rv   seq_of_parameterss      r(   _executemany_asyncz-AsyncAdapt_oracledb_cursor._executemany_async   s3       \--i9JKKKKKKKKKr)   c                    | S r0   r   rX   s    r(   ri   z$AsyncAdapt_oracledb_cursor.__enter__   s    r)   type_r[   	tracebackNonec                .    |                                   d S r0   )rc   )r   r   r[   r   s       r(   __exit__z#AsyncAdapt_oracledb_cursor.__exit__   s    

r)   N)r_   r   r%   r   rd   r   )rg   r   rd   r   )r   r   r[   r   r   r   rd   r   )rK   rL   rM   __annotations__	__slots__propertyrW   setterr]   rc   rf   rn   ry   r}   ri   r   r   r)   r(   rS   rS      s         I. . X. / / /1 1 1  ; ; ; ;< < < <
 
 
L L L       r)   rS   c                      e Zd ZU ded<   dZdZeZdZe	d             Z
e
j        d             Z
e	d             Zej        d	             Ze	d
             Ze	d             Zej        d             Zd ZdS )AsyncAdapt_oracledb_connectionr   _connectionr   TNc                    | j         j        S r0   r   
autocommitrX   s    r(   r   z)AsyncAdapt_oracledb_connection.autocommit   s    **r)   c                    || j         _        d S r0   r   rZ   s     r(   r   z)AsyncAdapt_oracledb_connection.autocommit   s    &+###r)   c                    | j         j        S r0   r   rW   rX   s    r(   rW   z0AsyncAdapt_oracledb_connection.outputtypehandler   s    11r)   c                    || j         _        d S r0   r   rZ   s     r(   rW   z0AsyncAdapt_oracledb_connection.outputtypehandler   s    -2***r)   c                    | j         j        S r0   )r   rB   rX   s    r(   rB   z&AsyncAdapt_oracledb_connection.version   s    ''r)   c                    | j         j        S r0   r   stmtcachesizerX   s    r(   r   z,AsyncAdapt_oracledb_connection.stmtcachesize   s    --r)   c                    || j         _        d S r0   r   rZ   s     r(   r   z,AsyncAdapt_oracledb_connection.stmtcachesize   s    ).&&&r)   c                     t          |           S r0   )rS   rX   s    r(   rg   z%AsyncAdapt_oracledb_connection.cursor   s    )$///r)   )rK   rL   rM   r   r   r3   rS   _cursor_cls_ss_cursor_clsr   r   r   rW   rB   r   rg   r   r)   r(   r   r      s            ID,KN+ + X+ , , , 2 2 X2 3 3 3 ( ( X( . . X. / / /0 0 0 0 0r)   r   c                      e Zd ZdZdS )&AsyncAdaptFallback_oracledb_connectionr   N)rK   rL   rM   r   r   r)   r(   r   r      s         IIIr)   r   c                      e Zd ZddZd ZdS )OracledbAdaptDBAPIrd   r   c                |    || _         | j         j                                        D ]\  }}|dk    r
|| j        |<   d S )Nconnect)r   __dict__items)r   r   kvs       r(   r   zOracledbAdaptDBAPI.__init__  sM     M*0022 	% 	%DAqI~~#$a 	% 	%r)   c           
        |                     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   r   r   )r   argr&   r   
creator_fns        r(   r   zOracledbAdaptDBAPI.connect  s     0%88VV.0KLL
.!! 	9nZZ%;%;%;<<  
 2jS!7B!7!788  r)   N)rd   r   )rK   rL   rM   r   r   r   r)   r(   r   r     s7        % % % %    r)   r   c                  L    e Zd ZdZdZdZed             Zed             Zd Z	dS )r6   T)r?   c                (    dd l }t          |          S r+   )r   r   r,   s     r(   r.   z(OracleDialectAsync_oracledb.import_dbapi#  s    !(+++r)   c                    |j                             dd          }t          |          rt          j        S t          j        S )Nr   F)querygetr   r
   FallbackAsyncAdaptedQueuePoolAsyncAdaptedQueuePool)r-   r7   r   s      r(   get_pool_classz*OracleDialectAsync_oracledb.get_pool_class)  s:    '7??.!! 	.55--r)   c                    |j         S r0   )r   )r   r1   s     r(   get_driver_connectionz1OracleDialectAsync_oracledb.get_driver_connection2  s    %%r)   N)
rK   rL   rM   is_asyncrN   rF   rP   r.   r   r   r   r)   r(   r6   r6     si        H#L , , [,
 . . [.& & & & &r)   r6   )!__doc__
__future__r   rs   r@   typingr   r   	cx_oracler   _OracleDialect_cx_oracle r	   r
   connectors.asyncior   r   r   utilr   r   r   r   r   r   r   rS   r   r   r   r6   dialectdialect_asyncr   r)   r(   <module>r      sB  C CH # " " " " "     				                   J J J J J J             = = = = = = 9 9 9 9 9 9 E E E E E E       " " " " " "       %(((((($$$$$$9 9 9 9 95 9 9 9x4 4 4 4 4!8 4 4 4n&0 &0 &0 &0 &0%@ &0 &0 &0R    ')G         .& & & & &"8 & & &4 !+r)   