
    Ng35                       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 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
j                  Z G d de
j                  Z G d de          Z G d dee          Z  G d de          Z! G d d ee!          Z" G d! d"          Z# G d# d$e          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/

Description
-----------

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, with the exception that oracledb supports two phase transactions.

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

Two Phase Transactions Supported
--------------------------------

Two phase transactions are fully supported under oracledb. Starting with
oracledb 2.3 two phase transactions are supported also in thin mode.    APIs
for two phase transactions are provided at the Core level via
:meth:`_engine.Connection.begin_twophase` and :paramref:`_orm.Session.twophase`
for transparent ORM use.

.. versionchanged:: 2.0.32 added support for two phase transactions

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

    )annotationsN)Any)TYPE_CHECKING   )	cx_oracle   )exc)pool)AsyncAdapt_dbapi_connection)AsyncAdapt_dbapi_cursor)AsyncAdapt_dbapi_ss_cursor)#AsyncAdaptFallback_dbapi_connection)default)asbool)await_fallback)
await_only)AsyncConnection)AsyncCursorc                      e Zd ZdS )OracleExecutionContext_oracledbN)__name__
__module____qualname__     _/var/www/html/ai-engine/env/lib/python3.11/site-packages/sqlalchemy/dialects/oracle/oracledb.pyr   r   r   s         	Dr   r   c                       e Zd ZdZeZdZdZ	 	 	 	 	 d fd	Ze	d             Z
e	d             Ze	d             Zd	 Zd
 Zd Z	 ddZ	 ddZd Z xZS )OracleDialect_oracledbToracledb)r   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 )Nr   )super__init__dbapi
isinstancedictinit_oracle_client)	selfauto_convert_lobscoerce_to_decimal	arraysizeencoding_errors
thick_modekwargskw	__class__s	           r   r"   zOracleDialect_oracledb.__init__   s     			
 	

 	
 	
 	
 :! "$Z66 "  **d;;CB)DJ)//B/////	 "!!!r   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)r3   r7   s     r   is_thin_modez#OracleDialect_oracledb.is_thin_mode   s    $5::r   c                    t           S r6   )OracleDialectAsync_oracledb)r3   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 r6   )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_modulerH   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   c                v     |j         j        | }|j                             |           ||j         j        d<   d S )Noracledb_xid)r7   xid	tpc_begininfo)r'   r7   rS   conn_xiss       r   do_begin_twophasez(OracleDialect_oracledb.do_begin_twophase   sA    ,:(,c2''1115=
">222r   c                L    |j                                         }||j        d<   d S )Noracledb_should_commit)r7   tpc_preparerU   )r'   r7   rS   should_commits       r   do_prepare_twophasez*OracleDialect_oracledb.do_prepare_twophase   s)    "-99;;4A
0111r   Fc                b    |r |j         j        | }nd }|j                             |           d S r6   )r7   rS   tpc_rollback)r'   r7   rS   is_preparedrecoverconn_xids         r   do_rollback_twophasez+OracleDialect_oracledb.do_rollback_twophase   sB      	0z,0#6HHH**844444r   c                    d }|s|j                                         }n!|r |j         j        | }d}n|j        d         }|r|j                             |           d S d S )NTrY   )r7   rZ   rS   rU   
tpc_commit)r'   r7   rS   r_   r`   ra   r[   s          r   do_commit_twophasez)OracleDialect_oracledb.do_commit_twophase   s      	F&1==??MM 	F0z,0#6H MM&O,DEM 	7!,,X66666	7 	7r   c                H    d |j                                         D             S )Nc                    g | ]]\  }}}|t          |t                    r|                                n|t          |t                    r|                                n|f^S r   )r$   bytesdecode)rB   figtibqs       r   
<listcomp>z>OracleDialect_oracledb.do_recover_twophase.<locals>.<listcomp>   sq     
 
 
 C	  *3 6 6?

C)"e44<		"
 
 
r   )r7   tpc_recoverr'   r7   s     r   do_recover_twophasez*OracleDialect_oracledb.do_recover_twophase   s4    
 
  *4@@BB
 
 
 	
r   )TTNNN)TF)r   r   r   supports_statement_cacher   execution_ctx_clsdriverrL   r"   classmethodr4   r:   r>   rP   rW   r\   rb   re   rp   __classcell__)r/   s   @r   r   r   x   s&       #7FL 0 0 0 0 0 0.   [
 ; ; [; + + [+  "> > >
B B B
 :?5 5 5 5 :?7 7 7 7	
 	
 	
 	
 	
 	
 	
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 r6   rx   outputtypehandlerr'   s    r   r{   z,AsyncAdapt_oracledb_cursor.outputtypehandler   s    |--r   c                    || j         _        d S r6   rz   r'   values     r   r{   z,AsyncAdapt_oracledb_cursor.outputtypehandler   s    ).&&&r   c                &     | j         j        |i |S r6   )rx   varr'   argsr-   s      r   r   zAsyncAdapt_oracledb_cursor.var   s    t|0000r   c                j    | j                                          | j                                         d S r6   )_rowsclearrx   closer|   s    r   r   z AsyncAdapt_oracledb_cursor.close   s0    
r   r   r   r-   returnc                &     | j         j        |i |S r6   )rx   setinputsizesr   s      r   r   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r6   )	__enter__	Exception_adapt_connection_handle_exception)r'   r   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 r6   )rx   executedescriptionserver_sidecollectionsdequefetchallr   )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 r6   )rx   executemany)r'   r   seq_of_parameterss      r   _executemany_asyncz-AsyncAdapt_oracledb_cursor._executemany_async	  s3       \--i9JKKKKKKKKKr   c                    | S r6   r   r|   s    r   r   z$AsyncAdapt_oracledb_cursor.__enter__  s    r   type_r   	tracebackNonec                .    |                                   d S r6   )r   )r'   r   r   r   s       r   __exit__z#AsyncAdapt_oracledb_cursor.__exit__  s    

r   Nr   r   r-   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   __annotations__	__slots__propertyr{   setterr   r   r   r   r   r   r   r   r   r   r   rw   rw      s         I. . X. / / /1 1 1  ; ; ; ;< < < <
 
 
L L L       r   rw   c                      e Zd ZdZddZdS )AsyncAdapt_oracledb_ss_cursorr   r   r   c                X    | j         "| j                                          d | _         d S d S r6   )rx   r   r|   s    r   r   z#AsyncAdapt_oracledb_ss_cursor.close  s1    <#L   DLLL $#r   Nr   r   )r   r   r   r   r   r   r   r   r   r     s/         I           r   r   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 ZddZddZddZddZddZddZdS )AsyncAdapt_oracledb_connectionr   _connectionr   TNc                    | j         j        S r6   r   
autocommitr|   s    r   r   z)AsyncAdapt_oracledb_connection.autocommit,  s    **r   c                    || j         _        d S r6   r   r~   s     r   r   z)AsyncAdapt_oracledb_connection.autocommit0  s    &+###r   c                    | j         j        S r6   r   r{   r|   s    r   r{   z0AsyncAdapt_oracledb_connection.outputtypehandler4  s    11r   c                    || j         _        d S r6   r   r~   s     r   r{   z0AsyncAdapt_oracledb_connection.outputtypehandler8  s    -2***r   c                    | j         j        S r6   )r   rH   r|   s    r   rH   z&AsyncAdapt_oracledb_connection.version<  s    ''r   c                    | j         j        S r6   r   stmtcachesizer|   s    r   r   z,AsyncAdapt_oracledb_connection.stmtcachesize@  s    --r   c                    || j         _        d S r6   r   r~   s     r   r   z,AsyncAdapt_oracledb_connection.stmtcachesizeD  s    ).&&&r   c                     t          |           S r6   )rw   r|   s    r   r   z%AsyncAdapt_oracledb_connection.cursorH  s    )$///r   c                     t          |           S r6   )r   r|   s    r   	ss_cursorz(AsyncAdapt_oracledb_connection.ss_cursorK  s    ,T222r   r   r   r-   r   c                &     | j         j        |i |S r6   )r   rS   r   s      r   rS   z"AsyncAdapt_oracledb_connection.xidN  s    #t#T4V444r   c                L    |                       | j        j        |i |          S r6   )await_r   rT   r   s      r   rT   z(AsyncAdapt_oracledb_connection.tpc_beginQ  s*    {{54+5tFvFFGGGr   c                L    |                       | j        j        |i |          S r6   )r   r   rd   r   s      r   rd   z)AsyncAdapt_oracledb_connection.tpc_commitT  s*    {{64+6GGGHHHr   c                L    |                       | j        j        |i |          S r6   )r   r   rZ   r   s      r   rZ   z*AsyncAdapt_oracledb_connection.tpc_prepareW  *    {{74+7HHHIIIr   c                L    |                       | j        j        |i |          S r6   )r   r   rn   r   s      r   rn   z*AsyncAdapt_oracledb_connection.tpc_recoverZ  r   r   c                L    |                       | j        j        |i |          S r6   )r   r   r^   r   s      r   r^   z+AsyncAdapt_oracledb_connection.tpc_rollback]  s*    {{84+8$I&IIJJJr   r   )r   r   r   r   r   r9   rw   _cursor_cls_ss_cursor_clsr   r   r   r{   rH   r   r   r   rS   rT   rd   rZ   rn   r^   r   r   r   r   r   #  s            ID,KN+ + X+ , , , 2 2 X2 3 3 3 ( ( X( . . X. / / /0 0 03 3 35 5 5 5H H H HI I I IJ J J JJ J J JK K K K K Kr   r   c                      e Zd ZdZdS )&AsyncAdaptFallback_oracledb_connectionr   N)r   r   r   r   r   r   r   r   r   a  s         IIIr   r   c                      e Zd ZddZd ZdS )OracledbAdaptDBAPIr   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__h  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.connecto  s     0%88VV.0KLL
.!! 	9nZZ%;%;%;<<  
 2jS!7B!7!788  r   Nr   )r   r   r   r"   r   r   r   r   r   r   g  s7        % % % %    r   r   c                  2    e Zd Zej        j        Zd Zd ZdS )$OracleExecutionContextAsync_oracledbc                r    | j                                         }| j        j        r| j        j        |_        |S r6   )_dbapi_connectionr   dialectr*   r'   cs     r   create_default_cursorz:OracleExecutionContextAsync_oracledb.create_default_cursor  s4    "))++<! 	1,0AKr   c                r    | j                                         }| j        j        r| j        j        |_        |S r6   )r   r   r   r*   r   s     r   create_server_side_cursorz>OracleExecutionContextAsync_oracledb.create_server_side_cursor  s4    ",,..<! 	1,0AKr   N)r   r   r   r   DefaultExecutionContextcreate_cursorr   r   r   r   r   r   r   ~  s=        3AM      r   r   c                  T    e Zd ZdZdZdZeZdZe	d             Z
e	d             Zd ZdS )r<   T)rE   c                (    dd l }t          |          S r1   )r   r   r2   s     r   r4   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)r3   r=   r   s      r   get_pool_classz*OracleDialectAsync_oracledb.get_pool_class  s:    '7??.!! 	.55--r   c                    |j         S r6   )r   ro   s     r   get_driver_connectionz1OracleDialectAsync_oracledb.get_driver_connection  s    %%r   N)r   r   r   is_asyncsupports_server_side_cursorsrq   r   rr   rL   rt   r4   r   r   r   r   r   r<   r<     su        H#' #<L , , [,
 . . [.& & & & &r   r<   )(__doc__
__future__r   r   rF   typingr   r    r   
_cx_oracler	   r
   connectors.asyncior   r   r   r   enginer   utilr   r   r   r   r   r    OracleExecutionContext_cx_oracler   OracleDialect_cx_oracler   rw   r   r   r   r   r   r<   r   dialect_asyncr   r   r   <module>r     s  P Pb # " " " " "     				                   % % % % % %             = = = = = = 9 9 9 9 9 9 < < < < < < E E E E E E             " " " " " "       %(((((($$$$$$	 	 	 	 	/	 	 	f
 f
 f
 f
 f
Z? f
 f
 f
R4 4 4 4 4!8 4 4 4n          :     ;K ;K ;K ;K ;K%@ ;K ;K ;K|    ')G         .    +J   (& & & & &"8 & & &8 !+r   