Hacked By AnonymousFox
�
�܋f�? � � � d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlm Z ddlm
Z
d d
lmZ d dlm
Z d dlmZ d d
lmZ G d� de� � Z G d� deej � � Z G d� deej � � Z G d� de� � Z G d� de� � Z G d� dee
� � Z G d� dee� � Z G d� de � � Z G d� dee� � ZeZdS ) a�
.. dialect:: mssql+pyodbc
:name: PyODBC
:dbapi: pyodbc
:connectstring: mssql+pyodbc://<username>:<password>@<dsnname>
:url: http://pypi.python.org/pypi/pyodbc/
Connecting to PyODBC
--------------------
The URL here is to be translated to PyODBC connection strings, as
detailed in `ConnectionStrings <https://code.google.com/p/pyodbc/wiki/ConnectionStrings>`_.
DSN Connections
^^^^^^^^^^^^^^^
A DSN connection in ODBC means that a pre-existing ODBC datasource is
configured on the client machine. The application then specifies the name
of this datasource, which encompasses details such as the specific ODBC driver
in use as well as the network address of the database. Assuming a datasource
is configured on the client, a basic DSN-based connection looks like::
engine = create_engine("mssql+pyodbc://scott:tiger@some_dsn")
Which above, will pass the following connection string to PyODBC::
dsn=mydsn;UID=user;PWD=pass
If the username and password are omitted, the DSN form will also add
the ``Trusted_Connection=yes`` directive to the ODBC string.
Hostname Connections
^^^^^^^^^^^^^^^^^^^^
Hostname-based connections are also supported by pyodbc. These are often
easier to use than a DSN and have the additional advantage that the specific
database name to connect towards may be specified locally in the URL, rather
than it being fixed as part of a datasource configuration.
When using a hostname connection, the driver name must also be specified in the
query parameters of the URL. As these names usually have spaces in them, the
name must be URL encoded which means using plus signs for spaces::
engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")
Other keywords interpreted by the Pyodbc dialect to be passed to
``pyodbc.connect()`` in both the DSN and hostname cases include:
``odbc_autotranslate``, ``ansi``, ``unicode_results``, ``autocommit``,
``authentication`` (e.g., ``authentication=ActiveDirectoryIntegrated``).
Note that in order for the dialect to recognize these keywords
(including the ``driver`` keyword above) they must be all lowercase.
Pass through exact Pyodbc string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A PyODBC connection string can also be sent in pyodbc's format directly, as
specified in `ConnectionStrings
<https://code.google.com/p/pyodbc/wiki/ConnectionStrings>`_ into the driver
using the parameter ``odbc_connect``. The delimeters must be URL encoded, as
illustrated below using ``urllib.parse.quote_plus``::
import urllib
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Pyodbc Pooling / connection close behavior
------------------------------------------
PyODBC uses internal `pooling
<https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling>`_ by
default, which means connections will be longer lived than they are within
SQLAlchemy itself. As SQLAlchemy has its own pooling behavior, it is often
preferable to disable this behavior. This behavior can only be disabled
globally at the PyODBC module level, **before** any connections are made::
import pyodbc
pyodbc.pooling = False
# don't use the engine before pooling is set to False
engine = create_engine("mssql+pyodbc://user:pass@dsn")
If this variable is left at its default value of ``True``, **the application
will continue to maintain active database connections**, even when the
SQLAlchemy engine itself fully discards a connection or if the engine is
disposed.
.. seealso::
`pooling <https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling>`_ -
in the PyODBC documentation.
Driver / Unicode Support
-------------------------
PyODBC works best with Microsoft ODBC drivers, particularly in the area
of Unicode support on both Python 2 and Python 3.
Using the FreeTDS ODBC drivers on Linux or OSX with PyODBC is **not**
recommended; there have been historically many Unicode-related issues
in this area, including before Microsoft offered ODBC drivers for Linux
and OSX. Now that Microsoft offers drivers for all platforms, for
PyODBC support these are recommended. FreeTDS remains relevant for
non-ODBC drivers such as pymssql where it works very well.
Rowcount Support
----------------
Pyodbc only has partial support for rowcount. See the notes at
:ref:`mssql_rowcount_versioning` for important notes when using ORM
versioning.
.. _mssql_pyodbc_fastexecutemany:
Fast Executemany Mode
---------------------
The Pyodbc driver has added support for a "fast executemany" mode of execution
which greatly reduces round trips for a DBAPI ``executemany()`` call when using
Microsoft ODBC drivers, for **limited size batches that fit in memory**. The
feature is enabled by setting the flag ``.fast_executemany`` on the DBAPI
cursor when an executemany call is to be used. The SQLAlchemy pyodbc SQL
Server dialect supports setting this flag automatically when the
``.fast_executemany`` flag is passed to
:func:`_sa.create_engine` ; note that the ODBC driver must be the Microsoft
driver in order to use this flag::
engine = create_engine(
"mssql+pyodbc://scott:tiger@mssql2017:1433/test?driver=ODBC+Driver+13+for+SQL+Server",
fast_executemany=True)
.. warning:: The pyodbc fast_executemany mode **buffers all rows in memory** and is
not compatible with very large batches of data. A future version of SQLAlchemy
may support this flag as a per-execution option instead.
.. versionadded:: 1.3
.. seealso::
`fast executemany <https://github.com/mkleehammer/pyodbc/wiki/Features-beyond-the-DB-API#fast_executemany>`_
- on github
� N� )�BINARY)�DATETIMEOFFSET)� MSDialect)�MSExecutionContext)� VARBINARY� )�exc)�types)�util)�PyODBCConnectorc �. � � e Zd ZdZ� fd�Zd� Zd� Z� xZS )�_ms_numeric_pyodbcz�Turns Decimals with adjusted() < 0 or > 7 into strings.
The routines here are needed for older pyodbc versions
as well as current mxODBC versions.
c �z �� �� t t � � � � |� � �|j s�S � �fd�}|S )Nc � �� �j rdt | t j � � rJ| � � � }|dk r�� | � � S |dk r�� | � � S �r �| � � S | S )Nr � )� asdecimal�
isinstance�decimal�Decimal�adjusted�_small_dec_to_string�_large_dec_to_string)�valuer �self�
super_processs ���s/builddir/build/BUILD/cloudlinux-venv-1.0.6/venv/lib64/python3.11/site-packages/sqlalchemy/dialects/mssql/pyodbc.py�processz2_ms_numeric_pyodbc.bind_processor.<locals>.process� s� �� ��~�
<�*�U�G�O�"D�"D�
<� �>�>�+�+���a�<�<��4�4�U�;�;�;���\�\��4�4�U�;�;�;��
�$�}�U�+�+�+��� )�superr �bind_processor�_need_decimal_fix)r �dialectr r � __class__s ` @�r r! z!_ms_numeric_pyodbc.bind_processor� sY ���� ��0�$�7�7�F�F�w�O�O�
��(� !� � � � � � � � � �r c �� � |dk rdpd�ddt |� � � � � dz
z �d� d� |� � � d D � � � � ��S )Nr �-� z0.�0r c �, � g | ]}t |� � ��S � ��str)�.0�nints r �
<listcomp>z;_ms_numeric_pyodbc._small_dec_to_string.<locals>.<listcomp>� s � �?�?�?�4�S��Y�Y�?�?�?r )�absr �join�as_tuple)r r s r r z'_ms_numeric_pyodbc._small_dec_to_string� su � �
�Q�Y�
�3�
$�"�
$�
$��3�u�~�~�'�'�(�(�1�,�-�-��G�G�?�?�5�>�>�+;�+;�A�+>�?�?�?�@�@�@�
�
r c �� � |� � � d }dt |� � v rV|dk rdpd�d� d� |D � � � � �d|� � � t |� � dz
z
z ��}n�t |� � dz
|� � � k r�|dk rdpd�d� d� |D � � d|� � � dz � � � �d d� d
� |D � � |� � � dz d � � � ��}nH|dk rdpd�d� d� |D � � d|� � � dz � � � ��}|S )Nr �Er r&