§ w<ˆj¶?ãó´—dZddlZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z d d l m Z d d l m Zd d l mZd d lmZGd„de¦«ZGd„deej¦«ZGd„deej¦«ZGd„de¦«ZGd„de¦«ZGd„dee ¦«ZGd„dee¦«ZGd„de ¦«ZGd„dee¦«ZeZdS) a« .. dialect:: mssql+pyodbc :name: PyODBC :dbapi: pyodbc :connectstring: mssql+pyodbc://:@ :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 `_. 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 `_ 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 `_ 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 `_ - 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 `_ - on github éNé)ÚBINARY)ÚDATETIMEOFFSET)Ú MSDialect)ÚMSExecutionContext)Ú VARBINARYé)Úexc)Útypes)Úutil)ÚPyODBCConnectorcó.‡—eZdZdZˆ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•‡‡—tt‰¦« |¦«Š|js‰Sˆˆfd„}|S)Ncóø•—‰jrdt|tj¦«rJ| ¦«}|dkr‰ |¦«S|dkr‰ |¦«S‰r ‰|¦«S|S)Nré)Ú asdecimalÚ isinstanceÚdecimalÚDecimalÚadjustedÚ_small_dec_to_stringÚ_large_dec_to_string)ÚvaluerÚselfÚ super_processs €€út/builddir/build/BUILD/cloudlinux-venv-1.0.12/venv/lib64/python3.11/site-packages/sqlalchemy/dialects/mssql/pyodbc.pyÚprocessz2_ms_numeric_pyodbc.bind_processor..process»s†ø€ØŒ~ð <¥*¨UµG´OÑ"DÔ"Dð <Ø Ÿ>š>Ñ+Ô+�ؘa’<�<Ø×4Ò4°UÑ;Ô;Ð;Ø ’\�\Ø×4Ò4°UÑ;Ô;Ð;àð Ø$�} UÑ+Ô+Ð+à� ó)ÚsuperrÚbind_processorÚ_need_decimal_fix)rÚdialectrrÚ __class__s` @€rr!z!_ms_numeric_pyodbc.bind_processor´sYøøø€åÕ0°$Ñ7Ô7×FÒFÀwÑOÔOˆ àÔ(ð !Ø Ð ð ð ð ð ð ð ðˆrcóØ—|dkrdpd›ddt| ¦«¦«dz z›d d„| ¦«dD¦«¦«›�S)Nrú-Úz0.Ú0rcó,—g|]}t|¦«‘ŒS©©Ústr)Ú.0Únints rú z;_ms_numeric_pyodbc._small_dec_to_string..Ñs€Ð?Ð?Ð? 4•S˜‘Y”YÐ?Ð?Ð?r)ÚabsrÚjoinÚas_tuple)rrs rrz'_ms_numeric_pyodbc._small_dec_to_stringÍsu€à �QŠYÐ ˜3Ð $ "Ð $Ð $Ø •3�u—~’~Ñ'Ô'Ñ(Ô(¨1Ñ,Ñ -Ð -Ø �GŠGÐ?Ð?¨5¯>ª>Ñ+;Ô+;¸AÔ+>Ð?Ñ?Ô?Ñ @Ô @Ð @ð ð rcóô—| ¦«d}dt|¦«vrV|dkrdpd›d d„|D¦«¦«›d| ¦«t |¦«dz z z›�}nöt |¦«dz | ¦«kr†|dkrdpd›d d„|D¦«d| ¦«dz…¦«›d d d „|D¦«| ¦«dzd…¦«›�}nH|dkrdpd›d d „|D¦«d| ¦«dz…¦«›�}|S) NrÚErr&r'có,—g|]}t|¦«‘ŒSr*r+©r-Úss rr/z;_ms_numeric_pyodbc._large_dec_to_string..Ùs€Ð.Ð.Ð. A�˜Q™œÐ.Ð.Ð.rr(có,—g|]}t|¦«‘ŒSr*r+r6s rr/z;_ms_numeric_pyodbc._large_dec_to_string..àó€Ð2Ð2Ð2¨�S ™VœVÐ2Ð2Ð2rú.có,—g|]}t|¦«‘ŒSr*r+r6s rr/z;_ms_numeric_pyodbc._large_dec_to_string..ár9rcó,—g|]}t|¦«‘ŒSr*r+r6s rr/z;_ms_numeric_pyodbc._large_dec_to_string..ær9r)r2r,r1rÚlen)rrÚ_intÚresults rrz'_ms_numeric_pyodbc._large_dec_to_stringÔs�€Ø�~Š~ÑÔ Ô"ˆØ •#�e‘*”*Ð Ð à˜’Ð"˜sÐ( bÐ(Ø—’Ð.Ð.¨Ð.Ñ.Ô.Ñ/Ô/Ð/Ø�u—~’~Ñ'Ô'­3¨t©9¬9°q©=Ñ9Ñ:Ð:ðˆFˆFõ �D‘ ” ˜A‘  §¢Ñ!1Ô!1Ò1Ð1à˜Q’YÐ& 3Ð,¨"Ð,Ø—G’GÐ2Ð2¨TÐ2Ñ2Ô2°1°u·~²~Ñ7GÔ7GÈ!Ñ7KÐ3KÔLÑMÔMÐMÐMØ—G’GÐ2Ð2¨TÐ2Ñ2Ô2°5·>²>Ñ3CÔ3CÀaÑ3GÐ3IÐ3IÔJÑKÔKÐKð��ð˜Q’YÐ& 3Ð,¨"Ð,Ø—G’GÐ2Ð2¨TÐ2Ñ2Ô2°1°u·~²~Ñ7GÔ7GÈ!Ñ7KÐ3KÔLÑMÔMÐMð�ðˆ r)Ú__name__Ú __module__Ú __qualname__Ú__doc__r!rrÚ __classcell__©r$s@rrr«s`ø€€€€€ðððððððð2 ð ð ðððððððrrcó—eZdZdS)Ú_MSNumeric_pyodbcN©r@rArBr*rrrGrGëó€€€€€Ø€DrrGcó—eZdZdS)Ú_MSFloat_pyodbcNrHr*rrrKrKïrIrrKcó—eZdZdZd„ZdS)Ú_ms_binary_pyodbczÿWraps binary values in dialect-specific Binary wrapper. If the value is null, return a pyodbc-specific BinaryNull object to prevent pyODBC [and FreeTDS] from defaulting binary NULL types to SQLWCHAR and causing implicit conversion errors. có@‡‡—‰j€dS‰jjŠˆˆfd„}|S)Ncó6•—|� ‰|¦«S‰jjS©N)ÚdbapiÚ BinaryNull)rÚ DBAPIBinaryr#s €€rrz1_ms_binary_pyodbc.bind_processor..processs&ø€ØÐ Ø"�{ 5Ñ)Ô)Ð)ð”}Ô/Ð/r)rQÚBinary)rr#rrSs ` @rr!z _ms_binary_pyodbc.bind_processorús@øø€Ø Œ=Ð Ø�4à”mÔ*ˆ ð 0ð 0ð 0ð 0ð 0ð 0ðˆrN)r@rArBrCr!r*rrrMrMós-€€€€€ððð ð ð ð ð rrMcó—eZdZd„ZdS)Ú_ODBCDateTimeOffsetcó —d„}|S)Ncóœ—|€dSt|tj¦«r|S| d¦«}t jdd|¦«}|S)Nz%Y-%m-%d %H:%M:%S.%f %zz([\+\-]\d{2})([\d\.]+)$z\1:\2)rr Ú string_typesÚstrftimeÚreÚsub)rÚ dto_strings rrz3_ODBCDateTimeOffset.bind_processor..process s\€Øˆ}Ø�tݘE¥4Ô#4Ñ5Ô5ð "à� ð#Ÿ^š^Ð,EÑFÔF� õ œVØ.°¸*ñô� ð"Ð!rr*)rr#rs rr!z"_ODBCDateTimeOffset.bind_processor s€ð "ð "ð "ð"ˆrN)r@rArBr!r*rrrVrV s#€€€€€ðððððrrVcó—eZdZdS)Ú_VARBINARY_pyodbcNrHr*rrr_r_ rIrr_có—eZdZdS)Ú_BINARY_pyodbcNrHr*rrrara$rIrracó,‡—eZdZdZˆfd„Zˆfd„ZˆxZS)ÚMSExecutionContext_pyodbcFcóê•—tt|¦« ¦«|jr?|jjr5t |jd¦«rd|_|xj dz c_ dSdSdSdS)aºwhere appropriate, issue "select scope_identity()" in the same statement. Background on why "scope_identity()" is preferable to "@@identity": http://msdn.microsoft.com/en-us/library/ms190315.aspx Background on why we attempt to embed "scope_identity()" into the same statement as the INSERT: http://code.google.com/p/pyodbc/wiki/FAQs#How_do_I_retrieve_autogenerated/identity_values? rTz; select scope_identity()N) r rcÚpre_execÚ_select_lastrowidr#Úuse_scope_identityr=Ú parametersÚ_embedded_scope_identityÚ statement)rr$s €rrez"MSExecutionContext_pyodbc.pre_exec+s’ø€õ Õ'¨Ñ.Ô.×7Ò7Ñ9Ô9Ð9ð Ô "ð :à” Ô/ð :õ�D”O AÔ&Ñ'Ô'ð :ð -1ˆDÔ )à ˆNŒNÐ9Ñ 9ˆNŒNˆNˆNð :ð :ð :ð :ð :ð :rcóH•—|jrr |j ¦«d}n4#|jjj$r|j ¦«YnwxYwŒUt|d¦«|_dStt|¦«  ¦«dS)NTr) riÚcursorÚfetchallr#rQÚErrorÚnextsetÚintÚ _lastrowidr rcÚ post_exec)rÚrowr$s €rrrz#MSExecutionContext_pyodbc.post_execEs³ø€Ø Ô (ð ?ð *ð*ðœ+×.Ò.Ñ0Ô0°Ô3�CØøØ”|Ô)Ô/ð*ð*ð*ð”K×'Ò'Ñ)Ô)Ð)Ð)Ð)ð*øøøð *õ" # a¤&™kœkˆDŒOˆOˆOå Õ+¨TÑ 2Ô 2× <Ò <Ñ >Ô >Ð >Ð >Ð >s‹+«-AÁA)r@rArBrirerrrDrEs@rrcrc(sWø€€€€€Ø$Ðð:ð:ð:ð:ð:ð4?ð?ð?ð?ð?ð?ð?ð?ð?rrccóʇ—eZdZdZeZejej e j e e j eeeeeeee jee jei¦«Z d ˆfd„ Zˆfd„Zˆfd„Zd„Zd ˆfd„ Zˆfd„ZˆxZS) ÚMSDialect_pyodbcFNc ó4•—d|vr| d¦«|_tt|¦«jdi|¤Ž|jo |jot|jjd¦«|_|jo|  ¦«dk|_ ||_ dS)NÚdescription_encodingro)érér*) Úpoprwr ruÚ__init__rgrQÚhasattrÚCursorÚ_dbapi_versionr"Úfast_executemany)rrwrÚparamsr$s €rr{zMSDialect_pyodbc.__init__qs²ø€ð " VÐ +Ð +Ø(.¯ ª Ð3IÑ(JÔ(JˆDÔ %Ø.�Õ Ñ%Ô%Ô.Ð8Ð8°Ð8Ð8Ð8à Ô #ð 6Ø” ð 6嘜 Ô)¨9Ñ5Ô5ð Ôð "&¤ð" °×0CÒ0CÑ0EÔ0EðI ò1 ˆÔð !1ˆÔÐÐrcóŽ•— | d¦«}g}tjd¦«}| |¦«D]5} | t |¦«¦«Œ&#t $rYŒ2wxYwt|¦«S#tj $r-tt|¦«  |d¬¦«cYSwxYw)Nz8SELECT CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR)z[.\-]F)Ú allow_chars) Úscalarr[ÚcompileÚsplitÚappendrpÚ ValueErrorÚtupler Ú DBAPIErrorr ruÚ_get_server_version_info)rÚ connectionÚrawÚversionÚrÚnr$s €rrŠz)MSDialect_pyodbc._get_server_version_infoƒsöø€ð "ð×#Ò#ØJñôˆCðˆGÝ” ˜8Ñ$Ô$ˆAØ—W’W˜S‘\”\ð ð �ðØ—N’N¥3 q¡6¤6Ñ*Ô*Ð*Ð*øÝ!ðððØ�Dðøøøå˜‘>”>Ð !øõŒ~ð ð ð õÕ)¨4Ñ0Ô0×IÒIبðJñôð ð ð ð  øøøs#ƒBÁ"A)Á) A6Á5A6Â9CÃCcóf•‡‡—tt‰¦« ¦«Šˆˆfd„}|S)NcóL•—‰� ‰|¦«‰ |¦«dSrP)Ú_setup_timestampoffset_type)ÚconnrÚsuper_s €€rÚ on_connectz/MSDialect_pyodbc.on_connect..on_connectžs0ø€ØÐ!Ø��t‘ ” � à × ,Ò ,¨TÑ 2Ô 2Ð 2Ð 2Ð 2r)r rur•)rr•r”r$s` @€rr•zMSDialect_pyodbc.on_connect›sGøøø€ÝÕ'¨Ñ.Ô.×9Ò9Ñ;Ô;ˆð 3ð 3ð 3ð 3ð 3ð 3ð Ðrcó<—d„}d}| ||¦«dS)Ncó —tjd|¦«}tj|d|d|d|d|d|d|dd ztjtj|d |d ¬ ¦«¦«¦«S) Nz<6hI2hrrrxr éééièrry)ÚhoursÚminutes)ÚstructÚunpackÚdatetimer ÚtimezoneÚ timedelta)Ú dto_valueÚtups rÚ_handle_datetimeoffsetzLMSDialect_pyodbc._setup_timestampoffset_type.._handle_datetimeoffset¨s‡€Ý”- ¨)Ñ4Ô4ˆCÝÔ$Ø�A”Ø�A”Ø�A”Ø�A”Ø�A”Ø�A”Ø�A”˜$‘Ý” ÝÔ&¨S°¬V¸SÀ¼VÐDÑDÔDñôñ ô ð rieÿÿÿ)Úadd_output_converter)rr‹r¤Úodbc_SQL_SS_TIMESTAMPOFFSETs rr’z,MSDialect_pyodbc._setup_timestampoffset_type¦sA€ð ð ð ð'+Ð#Ø×'Ò'Ø 'Ð)?ñ ô ð ð ð rcó|•—|jrd|_tt|¦« ||||¬¦«dS)NT)Úcontext)rr ruÚdo_executemany)rrlrjrhr¨r$s €rr©zMSDialect_pyodbc.do_executemany¼sQø€Ø Ô ð +Ø&*ˆFÔ #Ý Õ Ñ%Ô%×4Ò4Ø �I˜z°7ð 5ñ ô ð ð ð rc󲕗t||jj¦«r|jd}|dvrdSt t |¦« |||¦«S)Nr> Ú01000Ú01002Ú08001Ú08003Ú08007Ú08S01Ú08S02Ú10054ÚHY010ÚHYT00T)rrQrnÚargsr ruÚ is_disconnect)rÚer‹rlÚcoder$s €rr¶zMSDialect_pyodbc.is_disconnectÃshø€Ý �a˜œÔ)Ñ *Ô *ð Ø”6˜!”9ˆDØð ð ð ð�tÝÕ% tÑ,Ô,×:Ò:Ø ˆz˜6ñ ô ð r)NFrP)r@rArBÚ supports_sane_rowcount_returningrcÚexecution_ctx_clsr Ú update_copyrÚcolspecsÚsqltypesÚNumericrGÚFloatrKrrarrVrr_Ú LargeBinaryr{rŠr•r’r©r¶rDrEs@rruruZsø€€€€€ð(-Ð$à1ÐàˆtÔØÔà Ô Ð/Ø ŒN˜OØ �NØ Ð/ð Ð(Ø Ô Ð 1Ø Ô Ð"3ð ñô€Hð";@ð1ð1ð1ð1ð1ð1ð$"ð"ð"ð"ð"ð0 ð ð ð ð ð ð ð ð, ð ð ð ð ð ð ð ð ð ð ð ð ð ð rru)rCrŸrr[r�Úbaserrrrrr'r r r½r Úconnectors.pyodbcr Úobjectrr¾rGr¿rKrMrVr_rarcrur#r*rrúrÄs~ððQðQðf€€€Ø€€€Ø € € € Ø € € € àÐÐÐÐÐØ Ð Ð Ð Ð Ð ØÐÐÐÐÐØ$Ð$Ð$Ð$Ð$Ð$ØÐÐÐÐÐØÐÐÐÐÐØ!Ð!Ð!Ð!Ð!Ð!ØÐÐÐÐÐØ0Ð0Ð0Ð0Ð0Ð0ð=ð=ð=ð=ð=˜ñ=ô=ð=ð@ ð ð ð ð Ð*¨HÔ,<ñ ô ð ð ð ð ð ð Ð(¨(¬.ñ ô ð ððððð˜ñôðð.ðððð˜.ñôðð, ð ð ð ð Ð)¨9ñ ô ð ð ð ð ð ð Ð&¨ñ ô ð ð/?ð/?ð/?ð/?ð/?Ð 2ñ/?ô/?ð/?ðd{ ð{ ð{ ð{ ð{ �¨ ñ{ ô{ ð{ ð| €€€r