관리-도구
편집 파일: cx_oracle.cpython-37.pyc
B ��4]� � @ sZ d Z ddlmZ ddlZddlZddlZddlZddlmZ ddlm Z ddlmZ ddlmZ d d lm Z d dlmZ d dlmZ d d lmZ d dlmZ d dlmZ G dd� dej�ZG dd� dej�ZG dd� de�ZG dd� dee j�ZG dd� dee j�ZG dd� de�ZG dd� dej �Z!G dd� dej"�Z#G d d!� d!e j$�Z%G d"d#� d#ej&�Z'G d$d%� d%e j(�Z)G d&d'� d'ej*�Z+G d(d)� d)ej,�Z-G d*d+� d+e j.�Z/G d,d-� d-ej0�Z1G d.d/� d/ej2�Z3G d0d1� d1ej4�Z5G d2d3� d3e j6�Z7G d4d5� d5e j8�Z9G d6d7� d7e j:�Z;G d8d9� d9e �Z<G d:d;� d;e�Z=G d<d=� d=ej>�Z?G d>d?� d?e�Z@e@ZAdS )@a0 .. dialect:: oracle+cx_oracle :name: cx-Oracle :dbapi: cx_oracle :connectstring: oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...] :url: https://oracle.github.io/python-cx_Oracle/ Additional Connect Arguments ---------------------------- When connecting with the ``dbname`` URL token present, the ``hostname``, ``port``, and ``dbname`` tokens are converted to a TNS name using the ``cx_Oracle.makedsn()`` function. The URL below:: e = create_engine("oracle+cx_oracle://user:pass@hostname/dbname") Will be used to create the DSN as follows:: >>> import cx_Oracle >>> cx_Oracle.makedsn("hostname", 1521, sid="dbname") '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521))(CONNECT_DATA=(SID=dbname)))' The ``service_name`` parameter, also consumed by ``cx_Oracle.makedsn()``, may be specified in the URL query string, e.g. ``?service_name=my_service``. If ``dbname`` is not present, then the value of ``hostname`` in the URL is used directly as the DSN passed to ``cx_Oracle.connect()``. Additional connection arguments may be sent to the ``cx_Oracle.connect()`` function using the :paramref:`.create_engine.connect_args` dictionary. Any cx_Oracle parameter value and/or constant may be passed, such as:: import cx_Oracle e = create_engine( "oracle+cx_oracle://user:pass@dsn", connect_args={ "mode": cx_Oracle.SYSDBA, "events": True } ) Alternatively, most cx_Oracle DBAPI arguments can also be encoded as strings within the URL, which includes parameters such as ``mode``, ``purity``, ``events``, ``threaded``, and others:: e = create_engine( "oracle+cx_oracle://user:pass@dsn?mode=SYSDBA&events=true") .. versionchanged:: 1.3 the cx_oracle dialect now accepts all argument names within the URL string itself, to be passed to the cx_Oracle DBAPI. As was the case earlier but not correctly documented, the :paramref:`.create_engine.connect_args` parameter also accepts all cx_Oracle DBAPI connect arguments. There are also options that are consumed by the SQLAlchemy cx_oracle dialect itself. These options are always passed directly to :func:`.create_engine`, such as:: e = create_engine( "oracle+cx_oracle://user:pass@dsn", coerce_to_unicode=False) The parameters accepted by the cx_oracle dialect are as follows: * ``arraysize`` - set the cx_oracle.arraysize value on cursors, defaulted to 50. This setting is significant with cx_Oracle as the contents of LOB objects are only readable within a "live" row (e.g. within a batch of 50 rows). * ``auto_convert_lobs`` - defaults to True; See :ref:`cx_oracle_lob`. * ``coerce_to_unicode`` - see :ref:`cx_oracle_unicode` for detail. * ``coerce_to_decimal`` - see :ref:`cx_oracle_numeric` for detail. .. _cx_oracle_unicode: Unicode ------- The cx_Oracle DBAPI as of version 5 fully supports Unicode, and has the ability to return string results as Python Unicode objects natively. Explicit Unicode support is available by using the :class:`.Unicode` datatype with SQLAlchemy Core expression language, as well as the :class:`.UnicodeText` datatype. These types correspond to the VARCHAR2 and CLOB Oracle datatypes by default. When using these datatypes with Unicode data, it is expected that the Oracle database is configured with a Unicode-aware character set, as well as that the ``NLS_LANG`` environment variable is set appropriately, so that the VARCHAR2 and CLOB datatypes can accommodate the data. In the case that the Oracle database is not configured with a Unicode character set, the two options are to use the :class:`.oracle.NCHAR` and :class:`.oracle.NCLOB` datatypes explicitly, or to pass the flag ``use_nchar_for_unicode=True`` to :func:`.create_engine`, which will cause the SQLAlchemy dialect to use NCHAR/NCLOB for the :class:`.Unicode` / :class:`.UnicodeText` datatypes instead of VARCHAR/CLOB. .. versionchanged:: 1.3 The :class:`.Unicode` and :class:`.UnicodeText` datatypes now correspond to the ``VARCHAR2`` and ``CLOB`` Oracle datatypes unless the ``use_nchar_for_unicode=True`` is passed to the dialect when :func:`.create_engine` is called. When result sets are fetched that include strings, under Python 3 the cx_Oracle DBAPI returns all strings as Python Unicode objects, since Python 3 only has a Unicode string type. This occurs for data fetched from datatypes such as VARCHAR2, CHAR, CLOB, NCHAR, NCLOB, etc. In order to provide cross- compatibility under Python 2, the SQLAlchemy cx_Oracle dialect will add Unicode-conversion to string data under Python 2 as well. Historically, this made use of converters that were supplied by cx_Oracle but were found to be non-performant; SQLAlchemy's own converters are used for the string to Unicode conversion under Python 2. To disable the Python 2 Unicode conversion for VARCHAR2, CHAR, and CLOB, the flag ``coerce_to_unicode=False`` can be passed to :func:`.create_engine`. .. versionchanged:: 1.3 Unicode conversion is applied to all string values by default under python 2. The ``coerce_to_unicode`` now defaults to True and can be set to False to disable the Unicode coercion of strings that are delivered as VARCHAR2/CHAR/CLOB data. .. _cx_oracle_setinputsizes: Fine grained control over cx_Oracle data binding performance with setinputsizes ------------------------------------------------------------------------------- The cx_Oracle DBAPI has a deep and fundamental reliance upon the usage of the DBAPI ``setinputsizes()`` call. The purpose of this call is to establish the datatypes that are bound to a SQL statement for Python values being passed as parameters. While virtually no other DBAPI assigns any use to the ``setinputsizes()`` call, the cx_Oracle DBAPI relies upon it heavily in its interactions with the Oracle client interface, and in some scenarios it is not possible for SQLAlchemy to know exactly how data should be bound, as some settings can cause profoundly different performance characteristics, while altering the type coercion behavior at the same time. Users of the cx_Oracle dialect are **strongly encouraged** to read through cx_Oracle's list of built-in datatype symbols at http://cx-oracle.readthedocs.io/en/latest/module.html#types. Note that in some cases, significant performance degradation can occur when using these types vs. not, in particular when specifying ``cx_Oracle.CLOB``. On the SQLAlchemy side, the :meth:`.DialectEvents.do_setinputsizes` event can be used both for runtime visibility (e.g. logging) of the setinputsizes step as well as to fully control how ``setinputsizes()`` is used on a per-statement basis. .. versionadded:: 1.2.9 Added :meth:`.DialectEvents.setinputsizes` Example 1 - logging all setinputsizes calls ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The following example illustrates how to log the intermediary values from a SQLAlchemy perspective before they are converted to the raw ``setinputsizes()`` parameter dictionary. The keys of the dictionary are :class:`.BindParameter` objects which have a ``.key`` and a ``.type`` attribute:: from sqlalchemy import create_engine, event engine = create_engine("oracle+cx_oracle://scott:tiger@host/xe") @event.listens_for(engine, "do_setinputsizes") def _log_setinputsizes(inputsizes, cursor, statement, parameters, context): for bindparam, dbapitype in inputsizes.items(): log.info( "Bound parameter name: %s SQLAlchemy type: %r " "DBAPI object: %s", bindparam.key, bindparam.type, dbapitype) Example 2 - remove all bindings to CLOB ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The ``CLOB`` datatype in cx_Oracle incurs a significant performance overhead, however is set by default for the ``Text`` type within the SQLAlchemy 1.2 series. This setting can be modified as follows:: from sqlalchemy import create_engine, event from cx_Oracle import CLOB engine = create_engine("oracle+cx_oracle://scott:tiger@host/xe") @event.listens_for(engine, "do_setinputsizes") def _remove_clob(inputsizes, cursor, statement, parameters, context): for bindparam, dbapitype in list(inputsizes.items()): if dbapitype is CLOB: del inputsizes[bindparam] .. _cx_oracle_returning: RETURNING Support ----------------- The cx_Oracle dialect implements RETURNING using OUT parameters. The dialect supports RETURNING fully, however cx_Oracle 6 is recommended for complete support. .. _cx_oracle_lob: LOB Objects ----------- cx_oracle returns oracle LOBs using the cx_oracle.LOB object. SQLAlchemy converts these to strings so that the interface of the Binary type is consistent with that of other backends, which takes place within a cx_Oracle outputtypehandler. cx_Oracle prior to version 6 would require that LOB objects be read before a new batch of rows would be read, as determined by the ``cursor.arraysize``. As of the 6 series, this limitation has been lifted. Nevertheless, because SQLAlchemy pre-reads these LOBs up front, this issue is avoided in any case. To disable the auto "read()" feature of the dialect, the flag ``auto_convert_lobs=False`` may be passed to :func:`.create_engine`. Under the cx_Oracle 5 series, having this flag turned off means there is the chance of reading from a stale LOB object if not read as it is fetched. With cx_Oracle 6, this issue is resolved. .. versionchanged:: 1.2 the LOB handling system has been greatly simplified internally to make use of outputtypehandlers, and no longer makes use of alternate "buffered" result set objects. Two Phase Transactions Not Supported ------------------------------------- Two phase transactions are **not supported** under cx_Oracle due to poor driver support. As of cx_Oracle 6.0b1, the interface for two phase transactions has been changed to be more of a direct pass-through to the underlying OCI layer with less automation. The additional logic to support this system is not implemented in SQLAlchemy. .. _cx_oracle_numeric: Precision Numerics ------------------ SQLAlchemy's numeric types can handle receiving and returning values as Python ``Decimal`` objects or float objects. When a :class:`.Numeric` object, or a subclass such as :class:`.Float`, :class:`.oracle.DOUBLE_PRECISION` etc. is in use, the :paramref:`.Numeric.asdecimal` flag determines if values should be coerced to ``Decimal`` upon return, or returned as float objects. To make matters more complicated under Oracle, Oracle's ``NUMBER`` type can also represent integer values if the "scale" is zero, so the Oracle-specific :class:`.oracle.NUMBER` type takes this into account as well. The cx_Oracle dialect makes extensive use of connection- and cursor-level "outputtypehandler" callables in order to coerce numeric values as requested. These callables are specific to the specific flavor of :class:`.Numeric` in use, as well as if no SQLAlchemy typing objects are present. There are observed scenarios where Oracle may sends incomplete or ambiguous information about the numeric types being returned, such as a query where the numeric types are buried under multiple levels of subquery. The type handlers do their best to make the right decision in all cases, deferring to the underlying cx_Oracle DBAPI for all those cases where the driver can make the best decision. When no typing objects are present, as when executing plain SQL strings, a default "outputtypehandler" is present which will generally return numeric values which specify precision and scale as Python ``Decimal`` objects. To disable this coercion to decimal for performance reasons, pass the flag ``coerce_to_decimal=False`` to :func:`.create_engine`:: engine = create_engine("oracle+cx_oracle://dsn", coerce_to_decimal=False) The ``coerce_to_decimal`` flag only impacts the results of plain string SQL staements that are not otherwise associated with a :class:`.Numeric` SQLAlchemy type (or a subclass of such). .. versionchanged:: 1.2 The numeric handling system for cx_Oracle has been reworked to take advantage of newer cx_Oracle features as well as better integration of outputtypehandlers. � )�absolute_importN� )�base)�OracleCompiler)� OracleDialect)�OracleExecutionContext� )�exc)� processors)�types)�util)�result)�compatc @ s$ e Zd Zdd� Zdd� Zdd� ZdS )�_OracleIntegerc C s t S )N)�int)�self�dbapi� r �W/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/dialects/oracle/cx_oracle.py�get_dbapi_type+ s z_OracleInteger.get_dbapi_typec C s |j }|j|jd|jtd�S )N� )� arraysize�outconverter)r �var�STRINGr r )r �dialect�cursor� cx_Oracler r r �_cx_oracle_var0 s z_OracleInteger._cx_oracle_varc s � �fdd�}|S )Nc s �� � | �S )N)r )r �name�default_type�size� precision�scale)r r r r �handler7 s z<_OracleInteger._cx_oracle_outputtypehandler.<locals>.handlerr )r r r$ r )r r r �_cx_oracle_outputtypehandler6 s z+_OracleInteger._cx_oracle_outputtypehandlerN)�__name__� __module__�__qualname__r r r% r r r r r * s r c @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �_OracleNumericFc s>