SQLAlchemy 2.0.47__cp313-cp313t-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. sqlalchemy/__init__.py +283 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +184 -0
  4. sqlalchemy/connectors/asyncio.py +429 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/cyextension/__init__.py +6 -0
  7. sqlalchemy/cyextension/collections.cp313t-win_amd64.pyd +0 -0
  8. sqlalchemy/cyextension/collections.pyx +409 -0
  9. sqlalchemy/cyextension/immutabledict.cp313t-win_amd64.pyd +0 -0
  10. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  11. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  12. sqlalchemy/cyextension/processors.cp313t-win_amd64.pyd +0 -0
  13. sqlalchemy/cyextension/processors.pyx +68 -0
  14. sqlalchemy/cyextension/resultproxy.cp313t-win_amd64.pyd +0 -0
  15. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  16. sqlalchemy/cyextension/util.cp313t-win_amd64.pyd +0 -0
  17. sqlalchemy/cyextension/util.pyx +90 -0
  18. sqlalchemy/dialects/__init__.py +62 -0
  19. sqlalchemy/dialects/_typing.py +30 -0
  20. sqlalchemy/dialects/mssql/__init__.py +88 -0
  21. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  22. sqlalchemy/dialects/mssql/base.py +4093 -0
  23. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  24. sqlalchemy/dialects/mssql/json.py +129 -0
  25. sqlalchemy/dialects/mssql/provision.py +185 -0
  26. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  27. sqlalchemy/dialects/mssql/pyodbc.py +760 -0
  28. sqlalchemy/dialects/mysql/__init__.py +104 -0
  29. sqlalchemy/dialects/mysql/aiomysql.py +250 -0
  30. sqlalchemy/dialects/mysql/asyncmy.py +231 -0
  31. sqlalchemy/dialects/mysql/base.py +3949 -0
  32. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  33. sqlalchemy/dialects/mysql/dml.py +225 -0
  34. sqlalchemy/dialects/mysql/enumerated.py +282 -0
  35. sqlalchemy/dialects/mysql/expression.py +146 -0
  36. sqlalchemy/dialects/mysql/json.py +91 -0
  37. sqlalchemy/dialects/mysql/mariadb.py +72 -0
  38. sqlalchemy/dialects/mysql/mariadbconnector.py +322 -0
  39. sqlalchemy/dialects/mysql/mysqlconnector.py +302 -0
  40. sqlalchemy/dialects/mysql/mysqldb.py +314 -0
  41. sqlalchemy/dialects/mysql/provision.py +153 -0
  42. sqlalchemy/dialects/mysql/pymysql.py +158 -0
  43. sqlalchemy/dialects/mysql/pyodbc.py +157 -0
  44. sqlalchemy/dialects/mysql/reflection.py +727 -0
  45. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  46. sqlalchemy/dialects/mysql/types.py +835 -0
  47. sqlalchemy/dialects/oracle/__init__.py +81 -0
  48. sqlalchemy/dialects/oracle/base.py +3802 -0
  49. sqlalchemy/dialects/oracle/cx_oracle.py +1555 -0
  50. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  51. sqlalchemy/dialects/oracle/oracledb.py +941 -0
  52. sqlalchemy/dialects/oracle/provision.py +297 -0
  53. sqlalchemy/dialects/oracle/types.py +316 -0
  54. sqlalchemy/dialects/oracle/vector.py +365 -0
  55. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  56. sqlalchemy/dialects/postgresql/_psycopg_common.py +189 -0
  57. sqlalchemy/dialects/postgresql/array.py +519 -0
  58. sqlalchemy/dialects/postgresql/asyncpg.py +1284 -0
  59. sqlalchemy/dialects/postgresql/base.py +5378 -0
  60. sqlalchemy/dialects/postgresql/dml.py +339 -0
  61. sqlalchemy/dialects/postgresql/ext.py +540 -0
  62. sqlalchemy/dialects/postgresql/hstore.py +406 -0
  63. sqlalchemy/dialects/postgresql/json.py +404 -0
  64. sqlalchemy/dialects/postgresql/named_types.py +524 -0
  65. sqlalchemy/dialects/postgresql/operators.py +129 -0
  66. sqlalchemy/dialects/postgresql/pg8000.py +669 -0
  67. sqlalchemy/dialects/postgresql/pg_catalog.py +326 -0
  68. sqlalchemy/dialects/postgresql/provision.py +183 -0
  69. sqlalchemy/dialects/postgresql/psycopg.py +862 -0
  70. sqlalchemy/dialects/postgresql/psycopg2.py +892 -0
  71. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  72. sqlalchemy/dialects/postgresql/ranges.py +1031 -0
  73. sqlalchemy/dialects/postgresql/types.py +313 -0
  74. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  75. sqlalchemy/dialects/sqlite/aiosqlite.py +482 -0
  76. sqlalchemy/dialects/sqlite/base.py +3056 -0
  77. sqlalchemy/dialects/sqlite/dml.py +263 -0
  78. sqlalchemy/dialects/sqlite/json.py +92 -0
  79. sqlalchemy/dialects/sqlite/provision.py +229 -0
  80. sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
  81. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  82. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  83. sqlalchemy/engine/__init__.py +62 -0
  84. sqlalchemy/engine/_py_processors.py +136 -0
  85. sqlalchemy/engine/_py_row.py +128 -0
  86. sqlalchemy/engine/_py_util.py +74 -0
  87. sqlalchemy/engine/base.py +3390 -0
  88. sqlalchemy/engine/characteristics.py +155 -0
  89. sqlalchemy/engine/create.py +893 -0
  90. sqlalchemy/engine/cursor.py +2298 -0
  91. sqlalchemy/engine/default.py +2394 -0
  92. sqlalchemy/engine/events.py +965 -0
  93. sqlalchemy/engine/interfaces.py +3471 -0
  94. sqlalchemy/engine/mock.py +134 -0
  95. sqlalchemy/engine/processors.py +61 -0
  96. sqlalchemy/engine/reflection.py +2102 -0
  97. sqlalchemy/engine/result.py +2399 -0
  98. sqlalchemy/engine/row.py +400 -0
  99. sqlalchemy/engine/strategies.py +16 -0
  100. sqlalchemy/engine/url.py +924 -0
  101. sqlalchemy/engine/util.py +167 -0
  102. sqlalchemy/event/__init__.py +26 -0
  103. sqlalchemy/event/api.py +220 -0
  104. sqlalchemy/event/attr.py +676 -0
  105. sqlalchemy/event/base.py +472 -0
  106. sqlalchemy/event/legacy.py +258 -0
  107. sqlalchemy/event/registry.py +390 -0
  108. sqlalchemy/events.py +17 -0
  109. sqlalchemy/exc.py +832 -0
  110. sqlalchemy/ext/__init__.py +11 -0
  111. sqlalchemy/ext/associationproxy.py +2027 -0
  112. sqlalchemy/ext/asyncio/__init__.py +25 -0
  113. sqlalchemy/ext/asyncio/base.py +281 -0
  114. sqlalchemy/ext/asyncio/engine.py +1471 -0
  115. sqlalchemy/ext/asyncio/exc.py +21 -0
  116. sqlalchemy/ext/asyncio/result.py +965 -0
  117. sqlalchemy/ext/asyncio/scoping.py +1599 -0
  118. sqlalchemy/ext/asyncio/session.py +1947 -0
  119. sqlalchemy/ext/automap.py +1701 -0
  120. sqlalchemy/ext/baked.py +570 -0
  121. sqlalchemy/ext/compiler.py +600 -0
  122. sqlalchemy/ext/declarative/__init__.py +65 -0
  123. sqlalchemy/ext/declarative/extensions.py +564 -0
  124. sqlalchemy/ext/horizontal_shard.py +478 -0
  125. sqlalchemy/ext/hybrid.py +1535 -0
  126. sqlalchemy/ext/indexable.py +364 -0
  127. sqlalchemy/ext/instrumentation.py +450 -0
  128. sqlalchemy/ext/mutable.py +1085 -0
  129. sqlalchemy/ext/mypy/__init__.py +6 -0
  130. sqlalchemy/ext/mypy/apply.py +324 -0
  131. sqlalchemy/ext/mypy/decl_class.py +515 -0
  132. sqlalchemy/ext/mypy/infer.py +590 -0
  133. sqlalchemy/ext/mypy/names.py +335 -0
  134. sqlalchemy/ext/mypy/plugin.py +303 -0
  135. sqlalchemy/ext/mypy/util.py +357 -0
  136. sqlalchemy/ext/orderinglist.py +439 -0
  137. sqlalchemy/ext/serializer.py +185 -0
  138. sqlalchemy/future/__init__.py +16 -0
  139. sqlalchemy/future/engine.py +15 -0
  140. sqlalchemy/inspection.py +174 -0
  141. sqlalchemy/log.py +288 -0
  142. sqlalchemy/orm/__init__.py +171 -0
  143. sqlalchemy/orm/_orm_constructors.py +2661 -0
  144. sqlalchemy/orm/_typing.py +179 -0
  145. sqlalchemy/orm/attributes.py +2845 -0
  146. sqlalchemy/orm/base.py +971 -0
  147. sqlalchemy/orm/bulk_persistence.py +2135 -0
  148. sqlalchemy/orm/clsregistry.py +571 -0
  149. sqlalchemy/orm/collections.py +1627 -0
  150. sqlalchemy/orm/context.py +3334 -0
  151. sqlalchemy/orm/decl_api.py +2004 -0
  152. sqlalchemy/orm/decl_base.py +2192 -0
  153. sqlalchemy/orm/dependency.py +1302 -0
  154. sqlalchemy/orm/descriptor_props.py +1092 -0
  155. sqlalchemy/orm/dynamic.py +300 -0
  156. sqlalchemy/orm/evaluator.py +379 -0
  157. sqlalchemy/orm/events.py +3252 -0
  158. sqlalchemy/orm/exc.py +237 -0
  159. sqlalchemy/orm/identity.py +302 -0
  160. sqlalchemy/orm/instrumentation.py +754 -0
  161. sqlalchemy/orm/interfaces.py +1496 -0
  162. sqlalchemy/orm/loading.py +1686 -0
  163. sqlalchemy/orm/mapped_collection.py +557 -0
  164. sqlalchemy/orm/mapper.py +4444 -0
  165. sqlalchemy/orm/path_registry.py +809 -0
  166. sqlalchemy/orm/persistence.py +1788 -0
  167. sqlalchemy/orm/properties.py +935 -0
  168. sqlalchemy/orm/query.py +3459 -0
  169. sqlalchemy/orm/relationships.py +3508 -0
  170. sqlalchemy/orm/scoping.py +2148 -0
  171. sqlalchemy/orm/session.py +5280 -0
  172. sqlalchemy/orm/state.py +1168 -0
  173. sqlalchemy/orm/state_changes.py +196 -0
  174. sqlalchemy/orm/strategies.py +3470 -0
  175. sqlalchemy/orm/strategy_options.py +2568 -0
  176. sqlalchemy/orm/sync.py +164 -0
  177. sqlalchemy/orm/unitofwork.py +796 -0
  178. sqlalchemy/orm/util.py +2403 -0
  179. sqlalchemy/orm/writeonly.py +674 -0
  180. sqlalchemy/pool/__init__.py +44 -0
  181. sqlalchemy/pool/base.py +1524 -0
  182. sqlalchemy/pool/events.py +375 -0
  183. sqlalchemy/pool/impl.py +588 -0
  184. sqlalchemy/py.typed +0 -0
  185. sqlalchemy/schema.py +69 -0
  186. sqlalchemy/sql/__init__.py +145 -0
  187. sqlalchemy/sql/_dml_constructors.py +132 -0
  188. sqlalchemy/sql/_elements_constructors.py +1872 -0
  189. sqlalchemy/sql/_orm_types.py +20 -0
  190. sqlalchemy/sql/_py_util.py +75 -0
  191. sqlalchemy/sql/_selectable_constructors.py +763 -0
  192. sqlalchemy/sql/_typing.py +482 -0
  193. sqlalchemy/sql/annotation.py +587 -0
  194. sqlalchemy/sql/base.py +2293 -0
  195. sqlalchemy/sql/cache_key.py +1057 -0
  196. sqlalchemy/sql/coercions.py +1404 -0
  197. sqlalchemy/sql/compiler.py +8081 -0
  198. sqlalchemy/sql/crud.py +1752 -0
  199. sqlalchemy/sql/ddl.py +1444 -0
  200. sqlalchemy/sql/default_comparator.py +551 -0
  201. sqlalchemy/sql/dml.py +1850 -0
  202. sqlalchemy/sql/elements.py +5589 -0
  203. sqlalchemy/sql/events.py +458 -0
  204. sqlalchemy/sql/expression.py +159 -0
  205. sqlalchemy/sql/functions.py +2158 -0
  206. sqlalchemy/sql/lambdas.py +1442 -0
  207. sqlalchemy/sql/naming.py +209 -0
  208. sqlalchemy/sql/operators.py +2623 -0
  209. sqlalchemy/sql/roles.py +323 -0
  210. sqlalchemy/sql/schema.py +6222 -0
  211. sqlalchemy/sql/selectable.py +7265 -0
  212. sqlalchemy/sql/sqltypes.py +3930 -0
  213. sqlalchemy/sql/traversals.py +1024 -0
  214. sqlalchemy/sql/type_api.py +2368 -0
  215. sqlalchemy/sql/util.py +1485 -0
  216. sqlalchemy/sql/visitors.py +1164 -0
  217. sqlalchemy/testing/__init__.py +96 -0
  218. sqlalchemy/testing/assertions.py +994 -0
  219. sqlalchemy/testing/assertsql.py +520 -0
  220. sqlalchemy/testing/asyncio.py +135 -0
  221. sqlalchemy/testing/config.py +434 -0
  222. sqlalchemy/testing/engines.py +483 -0
  223. sqlalchemy/testing/entities.py +117 -0
  224. sqlalchemy/testing/exclusions.py +476 -0
  225. sqlalchemy/testing/fixtures/__init__.py +28 -0
  226. sqlalchemy/testing/fixtures/base.py +384 -0
  227. sqlalchemy/testing/fixtures/mypy.py +332 -0
  228. sqlalchemy/testing/fixtures/orm.py +227 -0
  229. sqlalchemy/testing/fixtures/sql.py +482 -0
  230. sqlalchemy/testing/pickleable.py +155 -0
  231. sqlalchemy/testing/plugin/__init__.py +6 -0
  232. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  233. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  234. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  235. sqlalchemy/testing/profiling.py +329 -0
  236. sqlalchemy/testing/provision.py +603 -0
  237. sqlalchemy/testing/requirements.py +1945 -0
  238. sqlalchemy/testing/schema.py +198 -0
  239. sqlalchemy/testing/suite/__init__.py +19 -0
  240. sqlalchemy/testing/suite/test_cte.py +237 -0
  241. sqlalchemy/testing/suite/test_ddl.py +389 -0
  242. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  243. sqlalchemy/testing/suite/test_dialect.py +776 -0
  244. sqlalchemy/testing/suite/test_insert.py +630 -0
  245. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  246. sqlalchemy/testing/suite/test_results.py +504 -0
  247. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  248. sqlalchemy/testing/suite/test_select.py +2010 -0
  249. sqlalchemy/testing/suite/test_sequence.py +317 -0
  250. sqlalchemy/testing/suite/test_types.py +2147 -0
  251. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  252. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  253. sqlalchemy/testing/util.py +535 -0
  254. sqlalchemy/testing/warnings.py +52 -0
  255. sqlalchemy/types.py +74 -0
  256. sqlalchemy/util/__init__.py +162 -0
  257. sqlalchemy/util/_collections.py +712 -0
  258. sqlalchemy/util/_concurrency_py3k.py +288 -0
  259. sqlalchemy/util/_has_cy.py +40 -0
  260. sqlalchemy/util/_py_collections.py +541 -0
  261. sqlalchemy/util/compat.py +421 -0
  262. sqlalchemy/util/concurrency.py +110 -0
  263. sqlalchemy/util/deprecations.py +401 -0
  264. sqlalchemy/util/langhelpers.py +2203 -0
  265. sqlalchemy/util/preloaded.py +150 -0
  266. sqlalchemy/util/queue.py +322 -0
  267. sqlalchemy/util/tool_support.py +201 -0
  268. sqlalchemy/util/topological.py +120 -0
  269. sqlalchemy/util/typing.py +734 -0
  270. sqlalchemy-2.0.47.dist-info/METADATA +243 -0
  271. sqlalchemy-2.0.47.dist-info/RECORD +274 -0
  272. sqlalchemy-2.0.47.dist-info/WHEEL +5 -0
  273. sqlalchemy-2.0.47.dist-info/licenses/LICENSE +19 -0
  274. sqlalchemy-2.0.47.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1555 @@
1
+ # dialects/oracle/cx_oracle.py
2
+ # Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
3
+ # <see AUTHORS file>
4
+ #
5
+ # This module is part of SQLAlchemy and is released under
6
+ # the MIT License: https://www.opensource.org/licenses/mit-license.php
7
+ # mypy: ignore-errors
8
+
9
+
10
+ r""".. dialect:: oracle+cx_oracle
11
+ :name: cx-Oracle
12
+ :dbapi: cx_oracle
13
+ :connectstring: oracle+cx_oracle://user:pass@hostname:port[/dbname][?service_name=<service>[&key=value&key=value...]]
14
+ :url: https://oracle.github.io/python-cx_Oracle/
15
+
16
+ Description
17
+ -----------
18
+
19
+ cx_Oracle was the original driver for Oracle Database. It was superseded by
20
+ python-oracledb which should be used instead.
21
+
22
+ DSN vs. Hostname connections
23
+ -----------------------------
24
+
25
+ cx_Oracle provides several methods of indicating the target database. The
26
+ dialect translates from a series of different URL forms.
27
+
28
+ Hostname Connections with Easy Connect Syntax
29
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30
+
31
+ Given a hostname, port and service name of the target database, for example
32
+ from Oracle Database's Easy Connect syntax then connect in SQLAlchemy using the
33
+ ``service_name`` query string parameter::
34
+
35
+ engine = create_engine(
36
+ "oracle+cx_oracle://scott:tiger@hostname:port?service_name=myservice&encoding=UTF-8&nencoding=UTF-8"
37
+ )
38
+
39
+ Note that the default driver value for encoding and nencoding was changed to
40
+ “UTF-8” in cx_Oracle 8.0 so these parameters can be omitted when using that
41
+ version, or later.
42
+
43
+ To use a full Easy Connect string, pass it as the ``dsn`` key value in a
44
+ :paramref:`_sa.create_engine.connect_args` dictionary::
45
+
46
+ import cx_Oracle
47
+
48
+ e = create_engine(
49
+ "oracle+cx_oracle://@",
50
+ connect_args={
51
+ "user": "scott",
52
+ "password": "tiger",
53
+ "dsn": "hostname:port/myservice?transport_connect_timeout=30&expire_time=60",
54
+ },
55
+ )
56
+
57
+ Connections with tnsnames.ora or to Oracle Autonomous Database
58
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59
+
60
+ Alternatively, if no port, database name, or service name is provided, the
61
+ dialect will use an Oracle Database DSN "connection string". This takes the
62
+ "hostname" portion of the URL as the data source name. For example, if the
63
+ ``tnsnames.ora`` file contains a TNS Alias of ``myalias`` as below:
64
+
65
+ .. sourcecode:: text
66
+
67
+ myalias =
68
+ (DESCRIPTION =
69
+ (ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.example.com)(PORT = 1521))
70
+ (CONNECT_DATA =
71
+ (SERVER = DEDICATED)
72
+ (SERVICE_NAME = orclpdb1)
73
+ )
74
+ )
75
+
76
+ The cx_Oracle dialect connects to this database service when ``myalias`` is the
77
+ hostname portion of the URL, without specifying a port, database name or
78
+ ``service_name``::
79
+
80
+ engine = create_engine("oracle+cx_oracle://scott:tiger@myalias")
81
+
82
+ Users of Oracle Autonomous Database should use this syntax. If the database is
83
+ configured for mutural TLS ("mTLS"), then you must also configure the cloud
84
+ wallet as shown in cx_Oracle documentation `Connecting to Autononmous Databases
85
+ <https://cx-oracle.readthedocs.io/en/latest/user_guide/connection_handling.html#autonomousdb>`_.
86
+
87
+ SID Connections
88
+ ^^^^^^^^^^^^^^^
89
+
90
+ To use Oracle Database's obsolete System Identifier connection syntax, the SID
91
+ can be passed in a "database name" portion of the URL::
92
+
93
+ engine = create_engine(
94
+ "oracle+cx_oracle://scott:tiger@hostname:port/dbname"
95
+ )
96
+
97
+ Above, the DSN passed to cx_Oracle is created by ``cx_Oracle.makedsn()`` as
98
+ follows::
99
+
100
+ >>> import cx_Oracle
101
+ >>> cx_Oracle.makedsn("hostname", 1521, sid="dbname")
102
+ '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521))(CONNECT_DATA=(SID=dbname)))'
103
+
104
+ Note that although the SQLAlchemy syntax ``hostname:port/dbname`` looks like
105
+ Oracle's Easy Connect syntax it is different. It uses a SID in place of the
106
+ service name required by Easy Connect. The Easy Connect syntax does not
107
+ support SIDs.
108
+
109
+ Passing cx_Oracle connect arguments
110
+ -----------------------------------
111
+
112
+ Additional connection arguments can usually be passed via the URL query string;
113
+ particular symbols like ``SYSDBA`` are intercepted and converted to the correct
114
+ symbol::
115
+
116
+ e = create_engine(
117
+ "oracle+cx_oracle://user:pass@dsn?encoding=UTF-8&nencoding=UTF-8&mode=SYSDBA&events=true"
118
+ )
119
+
120
+ .. versionchanged:: 1.3 the cx_Oracle dialect now accepts all argument names
121
+ within the URL string itself, to be passed to the cx_Oracle DBAPI. As
122
+ was the case earlier but not correctly documented, the
123
+ :paramref:`_sa.create_engine.connect_args` parameter also accepts all
124
+ cx_Oracle DBAPI connect arguments.
125
+
126
+ To pass arguments directly to ``.connect()`` without using the query
127
+ string, use the :paramref:`_sa.create_engine.connect_args` dictionary.
128
+ Any cx_Oracle parameter value and/or constant may be passed, such as::
129
+
130
+ import cx_Oracle
131
+
132
+ e = create_engine(
133
+ "oracle+cx_oracle://user:pass@dsn",
134
+ connect_args={
135
+ "encoding": "UTF-8",
136
+ "nencoding": "UTF-8",
137
+ "mode": cx_Oracle.SYSDBA,
138
+ "events": True,
139
+ },
140
+ )
141
+
142
+ Note that the default driver value for ``encoding`` and ``nencoding`` was
143
+ changed to "UTF-8" in cx_Oracle 8.0 so these parameters can be omitted when
144
+ using that version, or later.
145
+
146
+ Options consumed by the SQLAlchemy cx_Oracle dialect outside of the driver
147
+ --------------------------------------------------------------------------
148
+
149
+ There are also options that are consumed by the SQLAlchemy cx_oracle dialect
150
+ itself. These options are always passed directly to :func:`_sa.create_engine`
151
+ , such as::
152
+
153
+ e = create_engine(
154
+ "oracle+cx_oracle://user:pass@dsn", coerce_to_decimal=False
155
+ )
156
+
157
+ The parameters accepted by the cx_oracle dialect are as follows:
158
+
159
+ * ``arraysize`` - set the cx_oracle.arraysize value on cursors; defaults
160
+ to ``None``, indicating that the driver default should be used (typically
161
+ the value is 100). This setting controls how many rows are buffered when
162
+ fetching rows, and can have a significant effect on performance when
163
+ modified.
164
+
165
+ .. versionchanged:: 2.0.26 - changed the default value from 50 to None,
166
+ to use the default value of the driver itself.
167
+
168
+ * ``auto_convert_lobs`` - defaults to True; See :ref:`cx_oracle_lob`.
169
+
170
+ * ``coerce_to_decimal`` - see :ref:`cx_oracle_numeric` for detail.
171
+
172
+ * ``encoding_errors`` - see :ref:`cx_oracle_unicode_encoding_errors` for detail.
173
+
174
+ .. _cx_oracle_sessionpool:
175
+
176
+ Using cx_Oracle SessionPool
177
+ ---------------------------
178
+
179
+ The cx_Oracle driver provides its own connection pool implementation that may
180
+ be used in place of SQLAlchemy's pooling functionality. The driver pool
181
+ supports Oracle Database features such dead connection detection, connection
182
+ draining for planned database downtime, support for Oracle Application
183
+ Continuity and Transparent Application Continuity, and gives support for
184
+ Database Resident Connection Pooling (DRCP).
185
+
186
+ Using the driver pool can be achieved by using the
187
+ :paramref:`_sa.create_engine.creator` parameter to provide a function that
188
+ returns a new connection, along with setting
189
+ :paramref:`_sa.create_engine.pool_class` to ``NullPool`` to disable
190
+ SQLAlchemy's pooling::
191
+
192
+ import cx_Oracle
193
+ from sqlalchemy import create_engine
194
+ from sqlalchemy.pool import NullPool
195
+
196
+ pool = cx_Oracle.SessionPool(
197
+ user="scott",
198
+ password="tiger",
199
+ dsn="orclpdb",
200
+ min=1,
201
+ max=4,
202
+ increment=1,
203
+ threaded=True,
204
+ encoding="UTF-8",
205
+ nencoding="UTF-8",
206
+ )
207
+
208
+ engine = create_engine(
209
+ "oracle+cx_oracle://", creator=pool.acquire, poolclass=NullPool
210
+ )
211
+
212
+ The above engine may then be used normally where cx_Oracle's pool handles
213
+ connection pooling::
214
+
215
+ with engine.connect() as conn:
216
+ print(conn.scalar("select 1 from dual"))
217
+
218
+ As well as providing a scalable solution for multi-user applications, the
219
+ cx_Oracle session pool supports some Oracle features such as DRCP and
220
+ `Application Continuity
221
+ <https://cx-oracle.readthedocs.io/en/latest/user_guide/ha.html#application-continuity-ac>`_.
222
+
223
+ Note that the pool creation parameters ``threaded``, ``encoding`` and
224
+ ``nencoding`` were deprecated in later cx_Oracle releases.
225
+
226
+ Using Oracle Database Resident Connection Pooling (DRCP)
227
+ --------------------------------------------------------
228
+
229
+ When using Oracle Database's DRCP, the best practice is to pass a connection
230
+ class and "purity" when acquiring a connection from the SessionPool. Refer to
231
+ the `cx_Oracle DRCP documentation
232
+ <https://cx-oracle.readthedocs.io/en/latest/user_guide/connection_handling.html#database-resident-connection-pooling-drcp>`_.
233
+
234
+ This can be achieved by wrapping ``pool.acquire()``::
235
+
236
+ import cx_Oracle
237
+ from sqlalchemy import create_engine
238
+ from sqlalchemy.pool import NullPool
239
+
240
+ pool = cx_Oracle.SessionPool(
241
+ user="scott",
242
+ password="tiger",
243
+ dsn="orclpdb",
244
+ min=2,
245
+ max=5,
246
+ increment=1,
247
+ threaded=True,
248
+ encoding="UTF-8",
249
+ nencoding="UTF-8",
250
+ )
251
+
252
+
253
+ def creator():
254
+ return pool.acquire(
255
+ cclass="MYCLASS", purity=cx_Oracle.ATTR_PURITY_SELF
256
+ )
257
+
258
+
259
+ engine = create_engine(
260
+ "oracle+cx_oracle://", creator=creator, poolclass=NullPool
261
+ )
262
+
263
+ The above engine may then be used normally where cx_Oracle handles session
264
+ pooling and Oracle Database additionally uses DRCP::
265
+
266
+ with engine.connect() as conn:
267
+ print(conn.scalar("select 1 from dual"))
268
+
269
+ .. _cx_oracle_unicode:
270
+
271
+ Unicode
272
+ -------
273
+
274
+ As is the case for all DBAPIs under Python 3, all strings are inherently
275
+ Unicode strings. In all cases however, the driver requires an explicit
276
+ encoding configuration.
277
+
278
+ Ensuring the Correct Client Encoding
279
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
280
+
281
+ The long accepted standard for establishing client encoding for nearly all
282
+ Oracle Database related software is via the `NLS_LANG
283
+ <https://www.oracle.com/database/technologies/faq-nls-lang.html>`_ environment
284
+ variable. Older versions of cx_Oracle use this environment variable as the
285
+ source of its encoding configuration. The format of this variable is
286
+ Territory_Country.CharacterSet; a typical value would be
287
+ ``AMERICAN_AMERICA.AL32UTF8``. cx_Oracle version 8 and later use the character
288
+ set "UTF-8" by default, and ignore the character set component of NLS_LANG.
289
+
290
+ The cx_Oracle driver also supported a programmatic alternative which is to pass
291
+ the ``encoding`` and ``nencoding`` parameters directly to its ``.connect()``
292
+ function. These can be present in the URL as follows::
293
+
294
+ engine = create_engine(
295
+ "oracle+cx_oracle://scott:tiger@tnsalias?encoding=UTF-8&nencoding=UTF-8"
296
+ )
297
+
298
+ For the meaning of the ``encoding`` and ``nencoding`` parameters, please
299
+ consult
300
+ `Characters Sets and National Language Support (NLS) <https://cx-oracle.readthedocs.io/en/latest/user_guide/globalization.html#globalization>`_.
301
+
302
+ .. seealso::
303
+
304
+ `Characters Sets and National Language Support (NLS) <https://cx-oracle.readthedocs.io/en/latest/user_guide/globalization.html#globalization>`_
305
+ - in the cx_Oracle documentation.
306
+
307
+
308
+ Unicode-specific Column datatypes
309
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
310
+
311
+ The Core expression language handles unicode data by use of the
312
+ :class:`.Unicode` and :class:`.UnicodeText` datatypes. These types correspond
313
+ to the VARCHAR2 and CLOB Oracle Database datatypes by default. When using
314
+ these datatypes with Unicode data, it is expected that the database is
315
+ configured with a Unicode-aware character set, as well as that the ``NLS_LANG``
316
+ environment variable is set appropriately (this applies to older versions of
317
+ cx_Oracle), so that the VARCHAR2 and CLOB datatypes can accommodate the data.
318
+
319
+ In the case that Oracle Database is not configured with a Unicode character
320
+ set, the two options are to use the :class:`_types.NCHAR` and
321
+ :class:`_oracle.NCLOB` datatypes explicitly, or to pass the flag
322
+ ``use_nchar_for_unicode=True`` to :func:`_sa.create_engine`, which will cause
323
+ the SQLAlchemy dialect to use NCHAR/NCLOB for the :class:`.Unicode` /
324
+ :class:`.UnicodeText` datatypes instead of VARCHAR/CLOB.
325
+
326
+ .. versionchanged:: 1.3 The :class:`.Unicode` and :class:`.UnicodeText`
327
+ datatypes now correspond to the ``VARCHAR2`` and ``CLOB`` Oracle Database
328
+ datatypes unless the ``use_nchar_for_unicode=True`` is passed to the dialect
329
+ when :func:`_sa.create_engine` is called.
330
+
331
+
332
+ .. _cx_oracle_unicode_encoding_errors:
333
+
334
+ Encoding Errors
335
+ ^^^^^^^^^^^^^^^
336
+
337
+ For the unusual case that data in Oracle Database is present with a broken
338
+ encoding, the dialect accepts a parameter ``encoding_errors`` which will be
339
+ passed to Unicode decoding functions in order to affect how decoding errors are
340
+ handled. The value is ultimately consumed by the Python `decode
341
+ <https://docs.python.org/3/library/stdtypes.html#bytes.decode>`_ function, and
342
+ is passed both via cx_Oracle's ``encodingErrors`` parameter consumed by
343
+ ``Cursor.var()``, as well as SQLAlchemy's own decoding function, as the
344
+ cx_Oracle dialect makes use of both under different circumstances.
345
+
346
+ .. versionadded:: 1.3.11
347
+
348
+
349
+ .. _cx_oracle_setinputsizes:
350
+
351
+ Fine grained control over cx_Oracle data binding performance with setinputsizes
352
+ -------------------------------------------------------------------------------
353
+
354
+ The cx_Oracle DBAPI has a deep and fundamental reliance upon the usage of the
355
+ DBAPI ``setinputsizes()`` call. The purpose of this call is to establish the
356
+ datatypes that are bound to a SQL statement for Python values being passed as
357
+ parameters. While virtually no other DBAPI assigns any use to the
358
+ ``setinputsizes()`` call, the cx_Oracle DBAPI relies upon it heavily in its
359
+ interactions with the Oracle Database client interface, and in some scenarios
360
+ it is not possible for SQLAlchemy to know exactly how data should be bound, as
361
+ some settings can cause profoundly different performance characteristics, while
362
+ altering the type coercion behavior at the same time.
363
+
364
+ Users of the cx_Oracle dialect are **strongly encouraged** to read through
365
+ cx_Oracle's list of built-in datatype symbols at
366
+ https://cx-oracle.readthedocs.io/en/latest/api_manual/module.html#database-types.
367
+ Note that in some cases, significant performance degradation can occur when
368
+ using these types vs. not, in particular when specifying ``cx_Oracle.CLOB``.
369
+
370
+ On the SQLAlchemy side, the :meth:`.DialectEvents.do_setinputsizes` event can
371
+ be used both for runtime visibility (e.g. logging) of the setinputsizes step as
372
+ well as to fully control how ``setinputsizes()`` is used on a per-statement
373
+ basis.
374
+
375
+ .. versionadded:: 1.2.9 Added :meth:`.DialectEvents.setinputsizes`
376
+
377
+
378
+ Example 1 - logging all setinputsizes calls
379
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
380
+
381
+ The following example illustrates how to log the intermediary values from a
382
+ SQLAlchemy perspective before they are converted to the raw ``setinputsizes()``
383
+ parameter dictionary. The keys of the dictionary are :class:`.BindParameter`
384
+ objects which have a ``.key`` and a ``.type`` attribute::
385
+
386
+ from sqlalchemy import create_engine, event
387
+
388
+ engine = create_engine("oracle+cx_oracle://scott:tiger@host/xe")
389
+
390
+
391
+ @event.listens_for(engine, "do_setinputsizes")
392
+ def _log_setinputsizes(inputsizes, cursor, statement, parameters, context):
393
+ for bindparam, dbapitype in inputsizes.items():
394
+ log.info(
395
+ "Bound parameter name: %s SQLAlchemy type: %r DBAPI object: %s",
396
+ bindparam.key,
397
+ bindparam.type,
398
+ dbapitype,
399
+ )
400
+
401
+ Example 2 - remove all bindings to CLOB
402
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
403
+
404
+ The ``CLOB`` datatype in cx_Oracle incurs a significant performance overhead,
405
+ however is set by default for the ``Text`` type within the SQLAlchemy 1.2
406
+ series. This setting can be modified as follows::
407
+
408
+ from sqlalchemy import create_engine, event
409
+ from cx_Oracle import CLOB
410
+
411
+ engine = create_engine("oracle+cx_oracle://scott:tiger@host/xe")
412
+
413
+
414
+ @event.listens_for(engine, "do_setinputsizes")
415
+ def _remove_clob(inputsizes, cursor, statement, parameters, context):
416
+ for bindparam, dbapitype in list(inputsizes.items()):
417
+ if dbapitype is CLOB:
418
+ del inputsizes[bindparam]
419
+
420
+ .. _cx_oracle_lob:
421
+
422
+ LOB Datatypes
423
+ --------------
424
+
425
+ LOB datatypes refer to the "large object" datatypes such as CLOB, NCLOB and
426
+ BLOB. Modern versions of cx_Oracle is optimized for these datatypes to be
427
+ delivered as a single buffer. As such, SQLAlchemy makes use of these newer type
428
+ handlers by default.
429
+
430
+ To disable the use of newer type handlers and deliver LOB objects as classic
431
+ buffered objects with a ``read()`` method, the parameter
432
+ ``auto_convert_lobs=False`` may be passed to :func:`_sa.create_engine`,
433
+ which takes place only engine-wide.
434
+
435
+ .. _cx_oracle_returning:
436
+
437
+ RETURNING Support
438
+ -----------------
439
+
440
+ The cx_Oracle dialect implements RETURNING using OUT parameters.
441
+ The dialect supports RETURNING fully.
442
+
443
+ Two Phase Transactions Not Supported
444
+ ------------------------------------
445
+
446
+ Two phase transactions are **not supported** under cx_Oracle due to poor driver
447
+ support. The newer :ref:`oracledb` dialect however **does** support two phase
448
+ transactions.
449
+
450
+ .. _cx_oracle_numeric:
451
+
452
+ Precision Numerics
453
+ ------------------
454
+
455
+ SQLAlchemy's numeric types can handle receiving and returning values as Python
456
+ ``Decimal`` objects or float objects. When a :class:`.Numeric` object, or a
457
+ subclass such as :class:`.Float`, :class:`_oracle.DOUBLE_PRECISION` etc. is in
458
+ use, the :paramref:`.Numeric.asdecimal` flag determines if values should be
459
+ coerced to ``Decimal`` upon return, or returned as float objects. To make
460
+ matters more complicated under Oracle Database, the ``NUMBER`` type can also
461
+ represent integer values if the "scale" is zero, so the Oracle
462
+ Database-specific :class:`_oracle.NUMBER` type takes this into account as well.
463
+
464
+ The cx_Oracle dialect makes extensive use of connection- and cursor-level
465
+ "outputtypehandler" callables in order to coerce numeric values as requested.
466
+ These callables are specific to the specific flavor of :class:`.Numeric` in
467
+ use, as well as if no SQLAlchemy typing objects are present. There are
468
+ observed scenarios where Oracle Database may send incomplete or ambiguous
469
+ information about the numeric types being returned, such as a query where the
470
+ numeric types are buried under multiple levels of subquery. The type handlers
471
+ do their best to make the right decision in all cases, deferring to the
472
+ underlying cx_Oracle DBAPI for all those cases where the driver can make the
473
+ best decision.
474
+
475
+ When no typing objects are present, as when executing plain SQL strings, a
476
+ default "outputtypehandler" is present which will generally return numeric
477
+ values which specify precision and scale as Python ``Decimal`` objects. To
478
+ disable this coercion to decimal for performance reasons, pass the flag
479
+ ``coerce_to_decimal=False`` to :func:`_sa.create_engine`::
480
+
481
+ engine = create_engine("oracle+cx_oracle://dsn", coerce_to_decimal=False)
482
+
483
+ The ``coerce_to_decimal`` flag only impacts the results of plain string
484
+ SQL statements that are not otherwise associated with a :class:`.Numeric`
485
+ SQLAlchemy type (or a subclass of such).
486
+
487
+ .. versionchanged:: 1.2 The numeric handling system for cx_Oracle has been
488
+ reworked to take advantage of newer cx_Oracle features as well
489
+ as better integration of outputtypehandlers.
490
+
491
+ """ # noqa
492
+ from __future__ import annotations
493
+
494
+ import decimal
495
+ import random
496
+ import re
497
+
498
+ from . import base as oracle
499
+ from .base import OracleCompiler
500
+ from .base import OracleDialect
501
+ from .base import OracleExecutionContext
502
+ from .types import _OracleDateLiteralRender
503
+ from ... import exc
504
+ from ... import util
505
+ from ...engine import cursor as _cursor
506
+ from ...engine import interfaces
507
+ from ...engine import processors
508
+ from ...sql import sqltypes
509
+ from ...sql._typing import is_sql_compiler
510
+
511
+ # source:
512
+ # https://github.com/oracle/python-cx_Oracle/issues/596#issuecomment-999243649
513
+ _CX_ORACLE_MAGIC_LOB_SIZE = 131072
514
+
515
+
516
+ class _OracleInteger(sqltypes.Integer):
517
+ def get_dbapi_type(self, dbapi):
518
+ # see https://github.com/oracle/python-cx_Oracle/issues/
519
+ # 208#issuecomment-409715955
520
+ return int
521
+
522
+ def _cx_oracle_var(self, dialect, cursor, arraysize=None):
523
+ cx_Oracle = dialect.dbapi
524
+ return cursor.var(
525
+ cx_Oracle.STRING,
526
+ 255,
527
+ arraysize=arraysize if arraysize is not None else cursor.arraysize,
528
+ outconverter=int,
529
+ )
530
+
531
+ def _cx_oracle_outputtypehandler(self, dialect):
532
+ def handler(cursor, name, default_type, size, precision, scale):
533
+ return self._cx_oracle_var(dialect, cursor)
534
+
535
+ return handler
536
+
537
+
538
+ class _OracleNumeric(sqltypes.Numeric):
539
+ is_number = False
540
+
541
+ def bind_processor(self, dialect):
542
+ if self.scale == 0:
543
+ return None
544
+ elif self.asdecimal:
545
+ processor = processors.to_decimal_processor_factory(
546
+ decimal.Decimal, self._effective_decimal_return_scale
547
+ )
548
+
549
+ def process(value):
550
+ if isinstance(value, (int, float)):
551
+ return processor(value)
552
+ elif value is not None and value.is_infinite():
553
+ return float(value)
554
+ else:
555
+ return value
556
+
557
+ return process
558
+ else:
559
+ return processors.to_float
560
+
561
+ def result_processor(self, dialect, coltype):
562
+ return None
563
+
564
+ def _cx_oracle_outputtypehandler(self, dialect):
565
+ cx_Oracle = dialect.dbapi
566
+
567
+ def handler(cursor, name, default_type, size, precision, scale):
568
+ outconverter = None
569
+
570
+ if precision:
571
+ if self.asdecimal:
572
+ if default_type == cx_Oracle.NATIVE_FLOAT:
573
+ # receiving float and doing Decimal after the fact
574
+ # allows for float("inf") to be handled
575
+ type_ = default_type
576
+ outconverter = decimal.Decimal
577
+ else:
578
+ type_ = decimal.Decimal
579
+ else:
580
+ if self.is_number and scale == 0:
581
+ # integer. cx_Oracle is observed to handle the widest
582
+ # variety of ints when no directives are passed,
583
+ # from 5.2 to 7.0. See [ticket:4457]
584
+ return None
585
+ else:
586
+ type_ = cx_Oracle.NATIVE_FLOAT
587
+
588
+ else:
589
+ if self.asdecimal:
590
+ if default_type == cx_Oracle.NATIVE_FLOAT:
591
+ type_ = default_type
592
+ outconverter = decimal.Decimal
593
+ else:
594
+ type_ = decimal.Decimal
595
+ else:
596
+ if self.is_number and scale == 0:
597
+ # integer. cx_Oracle is observed to handle the widest
598
+ # variety of ints when no directives are passed,
599
+ # from 5.2 to 7.0. See [ticket:4457]
600
+ return None
601
+ else:
602
+ type_ = cx_Oracle.NATIVE_FLOAT
603
+
604
+ return cursor.var(
605
+ type_,
606
+ 255,
607
+ arraysize=cursor.arraysize,
608
+ outconverter=outconverter,
609
+ )
610
+
611
+ return handler
612
+
613
+
614
+ class _OracleUUID(sqltypes.Uuid):
615
+ def get_dbapi_type(self, dbapi):
616
+ return dbapi.STRING
617
+
618
+
619
+ class _OracleBinaryFloat(_OracleNumeric):
620
+ def get_dbapi_type(self, dbapi):
621
+ return dbapi.NATIVE_FLOAT
622
+
623
+
624
+ class _OracleBINARY_FLOAT(_OracleBinaryFloat, oracle.BINARY_FLOAT):
625
+ pass
626
+
627
+
628
+ class _OracleBINARY_DOUBLE(_OracleBinaryFloat, oracle.BINARY_DOUBLE):
629
+ pass
630
+
631
+
632
+ class _OracleNUMBER(_OracleNumeric):
633
+ is_number = True
634
+
635
+
636
+ class _CXOracleDate(oracle._OracleDate):
637
+ def bind_processor(self, dialect):
638
+ return None
639
+
640
+ def result_processor(self, dialect, coltype):
641
+ def process(value):
642
+ if value is not None:
643
+ return value.date()
644
+ else:
645
+ return value
646
+
647
+ return process
648
+
649
+
650
+ class _CXOracleTIMESTAMP(_OracleDateLiteralRender, sqltypes.TIMESTAMP):
651
+ def literal_processor(self, dialect):
652
+ return self._literal_processor_datetime(dialect)
653
+
654
+
655
+ class _LOBDataType:
656
+ pass
657
+
658
+
659
+ # TODO: the names used across CHAR / VARCHAR / NCHAR / NVARCHAR
660
+ # here are inconsistent and not very good
661
+ class _OracleChar(sqltypes.CHAR):
662
+ def get_dbapi_type(self, dbapi):
663
+ return dbapi.FIXED_CHAR
664
+
665
+
666
+ class _OracleNChar(sqltypes.NCHAR):
667
+ def get_dbapi_type(self, dbapi):
668
+ return dbapi.FIXED_NCHAR
669
+
670
+
671
+ class _OracleUnicodeStringNCHAR(oracle.NVARCHAR2):
672
+ def get_dbapi_type(self, dbapi):
673
+ return dbapi.NCHAR
674
+
675
+
676
+ class _OracleUnicodeStringCHAR(sqltypes.Unicode):
677
+ def get_dbapi_type(self, dbapi):
678
+ return dbapi.LONG_STRING
679
+
680
+
681
+ class _OracleUnicodeTextNCLOB(_LOBDataType, oracle.NCLOB):
682
+ def get_dbapi_type(self, dbapi):
683
+ # previously, this was dbapi.NCLOB.
684
+ # DB_TYPE_NVARCHAR will instead be passed to setinputsizes()
685
+ # when this datatype is used.
686
+ return dbapi.DB_TYPE_NVARCHAR
687
+
688
+
689
+ class _OracleUnicodeTextCLOB(_LOBDataType, sqltypes.UnicodeText):
690
+ def get_dbapi_type(self, dbapi):
691
+ # previously, this was dbapi.CLOB.
692
+ # DB_TYPE_NVARCHAR will instead be passed to setinputsizes()
693
+ # when this datatype is used.
694
+ return dbapi.DB_TYPE_NVARCHAR
695
+
696
+
697
+ class _OracleText(_LOBDataType, sqltypes.Text):
698
+ def get_dbapi_type(self, dbapi):
699
+ # previously, this was dbapi.CLOB.
700
+ # DB_TYPE_NVARCHAR will instead be passed to setinputsizes()
701
+ # when this datatype is used.
702
+ return dbapi.DB_TYPE_NVARCHAR
703
+
704
+
705
+ class _OracleLong(_LOBDataType, oracle.LONG):
706
+ def get_dbapi_type(self, dbapi):
707
+ return dbapi.LONG_STRING
708
+
709
+
710
+ class _OracleString(sqltypes.String):
711
+ pass
712
+
713
+
714
+ class _OracleEnum(sqltypes.Enum):
715
+ def bind_processor(self, dialect):
716
+ enum_proc = sqltypes.Enum.bind_processor(self, dialect)
717
+
718
+ def process(value):
719
+ raw_str = enum_proc(value)
720
+ return raw_str
721
+
722
+ return process
723
+
724
+
725
+ class _OracleBinary(_LOBDataType, sqltypes.LargeBinary):
726
+ def get_dbapi_type(self, dbapi):
727
+ # previously, this was dbapi.BLOB.
728
+ # DB_TYPE_RAW will instead be passed to setinputsizes()
729
+ # when this datatype is used.
730
+ return dbapi.DB_TYPE_RAW
731
+
732
+ def bind_processor(self, dialect):
733
+ return None
734
+
735
+ def result_processor(self, dialect, coltype):
736
+ if not dialect.auto_convert_lobs:
737
+ return None
738
+ else:
739
+ return super().result_processor(dialect, coltype)
740
+
741
+
742
+ class _OracleInterval(oracle.INTERVAL):
743
+ def get_dbapi_type(self, dbapi):
744
+ return dbapi.INTERVAL
745
+
746
+
747
+ class _OracleRaw(oracle.RAW):
748
+ pass
749
+
750
+
751
+ class _OracleRowid(oracle.ROWID):
752
+ def get_dbapi_type(self, dbapi):
753
+ return dbapi.ROWID
754
+
755
+
756
+ class OracleCompiler_cx_oracle(OracleCompiler):
757
+ _oracle_cx_sql_compiler = True
758
+
759
+ _oracle_returning = False
760
+
761
+ # Oracle bind names can't start with digits or underscores.
762
+ # currently we rely upon Oracle-specific quoting of bind names in most
763
+ # cases. however for expanding params, the escape chars are used.
764
+ # see #8708
765
+ bindname_escape_characters = util.immutabledict(
766
+ {
767
+ "%": "P",
768
+ "(": "A",
769
+ ")": "Z",
770
+ ":": "C",
771
+ ".": "C",
772
+ "[": "C",
773
+ "]": "C",
774
+ " ": "C",
775
+ "\\": "C",
776
+ "/": "C",
777
+ "?": "C",
778
+ }
779
+ )
780
+
781
+ def bindparam_string(self, name, **kw):
782
+ quote = getattr(name, "quote", None)
783
+ if (
784
+ quote is True
785
+ or quote is not False
786
+ and self.preparer._bindparam_requires_quotes(name)
787
+ # bind param quoting for Oracle doesn't work with post_compile
788
+ # params. For those, the default bindparam_string will escape
789
+ # special chars, and the appending of a number "_1" etc. will
790
+ # take care of reserved words
791
+ and not kw.get("post_compile", False)
792
+ ):
793
+ # interesting to note about expanding parameters - since the
794
+ # new parameters take the form <paramname>_<int>, at least if
795
+ # they are originally formed from reserved words, they no longer
796
+ # need quoting :). names that include illegal characters
797
+ # won't work however.
798
+ quoted_name = '"%s"' % name
799
+ kw["escaped_from"] = name
800
+ name = quoted_name
801
+ return OracleCompiler.bindparam_string(self, name, **kw)
802
+
803
+ # TODO: we could likely do away with quoting altogether for
804
+ # Oracle parameters and use the custom escaping here
805
+ escaped_from = kw.get("escaped_from", None)
806
+ if not escaped_from:
807
+ if self._bind_translate_re.search(name):
808
+ # not quite the translate use case as we want to
809
+ # also get a quick boolean if we even found
810
+ # unusual characters in the name
811
+ new_name = self._bind_translate_re.sub(
812
+ lambda m: self._bind_translate_chars[m.group(0)],
813
+ name,
814
+ )
815
+ if new_name[0].isdigit() or new_name[0] == "_":
816
+ new_name = "D" + new_name
817
+ kw["escaped_from"] = name
818
+ name = new_name
819
+ elif name[0].isdigit() or name[0] == "_":
820
+ new_name = "D" + name
821
+ kw["escaped_from"] = name
822
+ name = new_name
823
+
824
+ return OracleCompiler.bindparam_string(self, name, **kw)
825
+
826
+
827
+ class OracleExecutionContext_cx_oracle(OracleExecutionContext):
828
+ out_parameters = None
829
+
830
+ def _generate_out_parameter_vars(self):
831
+ # check for has_out_parameters or RETURNING, create cx_Oracle.var
832
+ # objects if so
833
+ if self.compiled.has_out_parameters or self.compiled._oracle_returning:
834
+ out_parameters = self.out_parameters
835
+ assert out_parameters is not None
836
+
837
+ len_params = len(self.parameters)
838
+
839
+ quoted_bind_names = self.compiled.escaped_bind_names
840
+ for bindparam in self.compiled.binds.values():
841
+ if bindparam.isoutparam:
842
+ name = self.compiled.bind_names[bindparam]
843
+ type_impl = bindparam.type.dialect_impl(self.dialect)
844
+
845
+ if hasattr(type_impl, "_cx_oracle_var"):
846
+ out_parameters[name] = type_impl._cx_oracle_var(
847
+ self.dialect, self.cursor, arraysize=len_params
848
+ )
849
+ else:
850
+ dbtype = type_impl.get_dbapi_type(self.dialect.dbapi)
851
+
852
+ cx_Oracle = self.dialect.dbapi
853
+
854
+ assert cx_Oracle is not None
855
+
856
+ if dbtype is None:
857
+ raise exc.InvalidRequestError(
858
+ "Cannot create out parameter for "
859
+ "parameter "
860
+ "%r - its type %r is not supported by"
861
+ " cx_oracle" % (bindparam.key, bindparam.type)
862
+ )
863
+
864
+ # note this is an OUT parameter. Using
865
+ # non-LOB datavalues with large unicode-holding
866
+ # values causes the failure (both cx_Oracle and
867
+ # oracledb):
868
+ # ORA-22835: Buffer too small for CLOB to CHAR or
869
+ # BLOB to RAW conversion (actual: 16507,
870
+ # maximum: 4000)
871
+ # [SQL: INSERT INTO long_text (x, y, z) VALUES
872
+ # (:x, :y, :z) RETURNING long_text.x, long_text.y,
873
+ # long_text.z INTO :ret_0, :ret_1, :ret_2]
874
+ # so even for DB_TYPE_NVARCHAR we convert to a LOB
875
+
876
+ if isinstance(type_impl, _LOBDataType):
877
+ if dbtype == cx_Oracle.DB_TYPE_NVARCHAR:
878
+ dbtype = cx_Oracle.NCLOB
879
+ elif dbtype == cx_Oracle.DB_TYPE_RAW:
880
+ dbtype = cx_Oracle.BLOB
881
+ # other LOB types go in directly
882
+
883
+ out_parameters[name] = self.cursor.var(
884
+ dbtype,
885
+ # this is fine also in oracledb_async since
886
+ # the driver will await the read coroutine
887
+ outconverter=lambda value: value.read(),
888
+ arraysize=len_params,
889
+ )
890
+ elif (
891
+ isinstance(type_impl, _OracleNumeric)
892
+ and type_impl.asdecimal
893
+ ):
894
+ out_parameters[name] = self.cursor.var(
895
+ decimal.Decimal,
896
+ arraysize=len_params,
897
+ )
898
+
899
+ else:
900
+ out_parameters[name] = self.cursor.var(
901
+ dbtype, arraysize=len_params
902
+ )
903
+
904
+ for param in self.parameters:
905
+ param[quoted_bind_names.get(name, name)] = (
906
+ out_parameters[name]
907
+ )
908
+
909
+ def _generate_cursor_outputtype_handler(self):
910
+ output_handlers = {}
911
+
912
+ for keyname, name, objects, type_ in self.compiled._result_columns:
913
+ handler = type_._cached_custom_processor(
914
+ self.dialect,
915
+ "cx_oracle_outputtypehandler",
916
+ self._get_cx_oracle_type_handler,
917
+ )
918
+
919
+ if handler:
920
+ denormalized_name = self.dialect.denormalize_name(keyname)
921
+ output_handlers[denormalized_name] = handler
922
+
923
+ if output_handlers:
924
+ default_handler = self._dbapi_connection.outputtypehandler
925
+
926
+ def output_type_handler(
927
+ cursor, name, default_type, size, precision, scale
928
+ ):
929
+ if name in output_handlers:
930
+ return output_handlers[name](
931
+ cursor, name, default_type, size, precision, scale
932
+ )
933
+ else:
934
+ return default_handler(
935
+ cursor, name, default_type, size, precision, scale
936
+ )
937
+
938
+ self.cursor.outputtypehandler = output_type_handler
939
+
940
+ def _get_cx_oracle_type_handler(self, impl):
941
+ if hasattr(impl, "_cx_oracle_outputtypehandler"):
942
+ return impl._cx_oracle_outputtypehandler(self.dialect)
943
+ else:
944
+ return None
945
+
946
+ def pre_exec(self):
947
+ super().pre_exec()
948
+ if not getattr(self.compiled, "_oracle_cx_sql_compiler", False):
949
+ return
950
+
951
+ self.out_parameters = {}
952
+
953
+ self._generate_out_parameter_vars()
954
+
955
+ self._generate_cursor_outputtype_handler()
956
+
957
+ def post_exec(self):
958
+ if (
959
+ self.compiled
960
+ and is_sql_compiler(self.compiled)
961
+ and self.compiled._oracle_returning
962
+ ):
963
+ initial_buffer = self.fetchall_for_returning(
964
+ self.cursor, _internal=True
965
+ )
966
+
967
+ fetch_strategy = _cursor.FullyBufferedCursorFetchStrategy(
968
+ self.cursor,
969
+ [
970
+ (entry.keyname, None)
971
+ for entry in self.compiled._result_columns
972
+ ],
973
+ initial_buffer=initial_buffer,
974
+ )
975
+
976
+ self.cursor_fetch_strategy = fetch_strategy
977
+
978
+ def create_cursor(self):
979
+ c = self._dbapi_connection.cursor()
980
+ if self.dialect.arraysize:
981
+ c.arraysize = self.dialect.arraysize
982
+
983
+ return c
984
+
985
+ def fetchall_for_returning(self, cursor, *, _internal=False):
986
+ compiled = self.compiled
987
+ if (
988
+ not _internal
989
+ and compiled is None
990
+ or not is_sql_compiler(compiled)
991
+ or not compiled._oracle_returning
992
+ ):
993
+ raise NotImplementedError(
994
+ "execution context was not prepared for Oracle RETURNING"
995
+ )
996
+
997
+ # create a fake cursor result from the out parameters. unlike
998
+ # get_out_parameter_values(), the result-row handlers here will be
999
+ # applied at the Result level
1000
+
1001
+ numcols = len(self.out_parameters)
1002
+
1003
+ # [stmt_result for stmt_result in outparam.values] == each
1004
+ # statement in executemany
1005
+ # [val for val in stmt_result] == each row for a particular
1006
+ # statement
1007
+ return list(
1008
+ zip(
1009
+ *[
1010
+ [
1011
+ val
1012
+ for stmt_result in self.out_parameters[
1013
+ f"ret_{j}"
1014
+ ].values
1015
+ for val in (stmt_result or ())
1016
+ ]
1017
+ for j in range(numcols)
1018
+ ]
1019
+ )
1020
+ )
1021
+
1022
+ def get_out_parameter_values(self, out_param_names):
1023
+ # this method should not be called when the compiler has
1024
+ # RETURNING as we've turned the has_out_parameters flag set to
1025
+ # False.
1026
+ assert not self.compiled.returning
1027
+
1028
+ return [
1029
+ self.dialect._paramval(self.out_parameters[name])
1030
+ for name in out_param_names
1031
+ ]
1032
+
1033
+
1034
+ class OracleDialect_cx_oracle(OracleDialect):
1035
+ supports_statement_cache = True
1036
+ execution_ctx_cls = OracleExecutionContext_cx_oracle
1037
+ statement_compiler = OracleCompiler_cx_oracle
1038
+
1039
+ supports_sane_rowcount = True
1040
+ supports_sane_multi_rowcount = True
1041
+
1042
+ insert_executemany_returning = True
1043
+ insert_executemany_returning_sort_by_parameter_order = True
1044
+ update_executemany_returning = True
1045
+ delete_executemany_returning = True
1046
+
1047
+ bind_typing = interfaces.BindTyping.SETINPUTSIZES
1048
+
1049
+ driver = "cx_oracle"
1050
+
1051
+ colspecs = util.update_copy(
1052
+ OracleDialect.colspecs,
1053
+ {
1054
+ sqltypes.TIMESTAMP: _CXOracleTIMESTAMP,
1055
+ sqltypes.Numeric: _OracleNumeric,
1056
+ sqltypes.Float: _OracleNumeric,
1057
+ oracle.BINARY_FLOAT: _OracleBINARY_FLOAT,
1058
+ oracle.BINARY_DOUBLE: _OracleBINARY_DOUBLE,
1059
+ sqltypes.Integer: _OracleInteger,
1060
+ oracle.NUMBER: _OracleNUMBER,
1061
+ sqltypes.Date: _CXOracleDate,
1062
+ sqltypes.LargeBinary: _OracleBinary,
1063
+ sqltypes.Boolean: oracle._OracleBoolean,
1064
+ sqltypes.Interval: _OracleInterval,
1065
+ oracle.INTERVAL: _OracleInterval,
1066
+ sqltypes.Text: _OracleText,
1067
+ sqltypes.String: _OracleString,
1068
+ sqltypes.UnicodeText: _OracleUnicodeTextCLOB,
1069
+ sqltypes.CHAR: _OracleChar,
1070
+ sqltypes.NCHAR: _OracleNChar,
1071
+ sqltypes.Enum: _OracleEnum,
1072
+ oracle.LONG: _OracleLong,
1073
+ oracle.RAW: _OracleRaw,
1074
+ sqltypes.Unicode: _OracleUnicodeStringCHAR,
1075
+ sqltypes.NVARCHAR: _OracleUnicodeStringNCHAR,
1076
+ sqltypes.Uuid: _OracleUUID,
1077
+ oracle.NCLOB: _OracleUnicodeTextNCLOB,
1078
+ oracle.ROWID: _OracleRowid,
1079
+ },
1080
+ )
1081
+
1082
+ execute_sequence_format = list
1083
+
1084
+ _cx_oracle_threaded = None
1085
+
1086
+ _cursor_var_unicode_kwargs = util.immutabledict()
1087
+
1088
+ @util.deprecated_params(
1089
+ threaded=(
1090
+ "1.3",
1091
+ "The 'threaded' parameter to the cx_oracle/oracledb dialect "
1092
+ "is deprecated as a dialect-level argument, and will be removed "
1093
+ "in a future release. As of version 1.3, it defaults to False "
1094
+ "rather than True. The 'threaded' option can be passed to "
1095
+ "cx_Oracle directly in the URL query string passed to "
1096
+ ":func:`_sa.create_engine`.",
1097
+ )
1098
+ )
1099
+ def __init__(
1100
+ self,
1101
+ auto_convert_lobs=True,
1102
+ coerce_to_decimal=True,
1103
+ arraysize=None,
1104
+ encoding_errors=None,
1105
+ threaded=None,
1106
+ **kwargs,
1107
+ ):
1108
+ OracleDialect.__init__(self, **kwargs)
1109
+ self.arraysize = arraysize
1110
+ self.encoding_errors = encoding_errors
1111
+ if encoding_errors:
1112
+ self._cursor_var_unicode_kwargs = {
1113
+ "encodingErrors": encoding_errors
1114
+ }
1115
+ if threaded is not None:
1116
+ self._cx_oracle_threaded = threaded
1117
+ self.auto_convert_lobs = auto_convert_lobs
1118
+ self.coerce_to_decimal = coerce_to_decimal
1119
+ if self._use_nchar_for_unicode:
1120
+ self.colspecs = self.colspecs.copy()
1121
+ self.colspecs[sqltypes.Unicode] = _OracleUnicodeStringNCHAR
1122
+ self.colspecs[sqltypes.UnicodeText] = _OracleUnicodeTextNCLOB
1123
+
1124
+ dbapi_module = self.dbapi
1125
+ self._load_version(dbapi_module)
1126
+
1127
+ if dbapi_module is not None:
1128
+ # these constants will first be seen in SQLAlchemy datatypes
1129
+ # coming from the get_dbapi_type() method. We then
1130
+ # will place the following types into setinputsizes() calls
1131
+ # on each statement. Oracle constants that are not in this
1132
+ # list will not be put into setinputsizes().
1133
+ self.include_set_input_sizes = {
1134
+ dbapi_module.DATETIME,
1135
+ dbapi_module.DB_TYPE_NVARCHAR, # used for CLOB, NCLOB
1136
+ dbapi_module.DB_TYPE_RAW, # used for BLOB
1137
+ dbapi_module.NCLOB, # not currently used except for OUT param
1138
+ dbapi_module.CLOB, # not currently used except for OUT param
1139
+ dbapi_module.LOB, # not currently used
1140
+ dbapi_module.BLOB, # not currently used except for OUT param
1141
+ dbapi_module.NCHAR,
1142
+ dbapi_module.FIXED_NCHAR,
1143
+ dbapi_module.FIXED_CHAR,
1144
+ dbapi_module.TIMESTAMP,
1145
+ int, # _OracleInteger,
1146
+ # _OracleBINARY_FLOAT, _OracleBINARY_DOUBLE,
1147
+ dbapi_module.NATIVE_FLOAT,
1148
+ }
1149
+
1150
+ self._paramval = lambda value: value.getvalue()
1151
+
1152
+ def _load_version(self, dbapi_module):
1153
+ version = (0, 0, 0)
1154
+ if dbapi_module is not None:
1155
+ m = re.match(r"(\d+)\.(\d+)(?:\.(\d+))?", dbapi_module.version)
1156
+ if m:
1157
+ version = tuple(
1158
+ int(x) for x in m.group(1, 2, 3) if x is not None
1159
+ )
1160
+ self.cx_oracle_ver = version
1161
+ if self.cx_oracle_ver < (8,) and self.cx_oracle_ver > (0, 0, 0):
1162
+ raise exc.InvalidRequestError(
1163
+ "cx_Oracle version 8 and above are supported"
1164
+ )
1165
+
1166
+ @classmethod
1167
+ def import_dbapi(cls):
1168
+ import cx_Oracle
1169
+
1170
+ return cx_Oracle
1171
+
1172
+ def initialize(self, connection):
1173
+ super().initialize(connection)
1174
+ self._detect_decimal_char(connection)
1175
+
1176
+ def get_isolation_level(self, dbapi_connection):
1177
+ # sources:
1178
+
1179
+ # general idea of transaction id, have to start one, etc.
1180
+ # https://stackoverflow.com/questions/10711204/how-to-check-isoloation-level
1181
+
1182
+ # how to decode xid cols from v$transaction to match
1183
+ # https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:9532779900346079444
1184
+
1185
+ # Oracle tuple comparison without using IN:
1186
+ # https://www.sql-workbench.eu/comparison/tuple_comparison.html
1187
+
1188
+ with dbapi_connection.cursor() as cursor:
1189
+ # this is the only way to ensure a transaction is started without
1190
+ # actually running DML. There's no way to see the configured
1191
+ # isolation level without getting it from v$transaction which
1192
+ # means transaction has to be started.
1193
+ outval = cursor.var(str)
1194
+ cursor.execute(
1195
+ """
1196
+ begin
1197
+ :trans_id := dbms_transaction.local_transaction_id( TRUE );
1198
+ end;
1199
+ """,
1200
+ {"trans_id": outval},
1201
+ )
1202
+ trans_id = outval.getvalue()
1203
+ xidusn, xidslot, xidsqn = trans_id.split(".", 2)
1204
+
1205
+ cursor.execute(
1206
+ "SELECT CASE BITAND(t.flag, POWER(2, 28)) "
1207
+ "WHEN 0 THEN 'READ COMMITTED' "
1208
+ "ELSE 'SERIALIZABLE' END AS isolation_level "
1209
+ "FROM v$transaction t WHERE "
1210
+ "(t.xidusn, t.xidslot, t.xidsqn) = "
1211
+ "((:xidusn, :xidslot, :xidsqn))",
1212
+ {"xidusn": xidusn, "xidslot": xidslot, "xidsqn": xidsqn},
1213
+ )
1214
+ row = cursor.fetchone()
1215
+ if row is None:
1216
+ raise exc.InvalidRequestError(
1217
+ "could not retrieve isolation level"
1218
+ )
1219
+ result = row[0]
1220
+
1221
+ return result
1222
+
1223
+ def get_isolation_level_values(self, dbapi_connection):
1224
+ return super().get_isolation_level_values(dbapi_connection) + [
1225
+ "AUTOCOMMIT"
1226
+ ]
1227
+
1228
+ def set_isolation_level(self, dbapi_connection, level):
1229
+ if level == "AUTOCOMMIT":
1230
+ dbapi_connection.autocommit = True
1231
+ else:
1232
+ dbapi_connection.autocommit = False
1233
+ dbapi_connection.rollback()
1234
+ with dbapi_connection.cursor() as cursor:
1235
+ cursor.execute(f"ALTER SESSION SET ISOLATION_LEVEL={level}")
1236
+
1237
+ def detect_autocommit_setting(self, dbapi_conn) -> bool:
1238
+ return bool(dbapi_conn.autocommit)
1239
+
1240
+ def _detect_decimal_char(self, connection):
1241
+ # we have the option to change this setting upon connect,
1242
+ # or just look at what it is upon connect and convert.
1243
+ # to minimize the chance of interference with changes to
1244
+ # NLS_TERRITORY or formatting behavior of the DB, we opt
1245
+ # to just look at it
1246
+
1247
+ dbapi_connection = connection.connection
1248
+
1249
+ with dbapi_connection.cursor() as cursor:
1250
+ # issue #8744
1251
+ # nls_session_parameters is not available in some Oracle
1252
+ # modes like "mount mode". But then, v$nls_parameters is not
1253
+ # available if the connection doesn't have SYSDBA priv.
1254
+ #
1255
+ # simplify the whole thing and just use the method that we were
1256
+ # doing in the test suite already, selecting a number
1257
+
1258
+ def output_type_handler(
1259
+ cursor, name, defaultType, size, precision, scale
1260
+ ):
1261
+ return cursor.var(
1262
+ self.dbapi.STRING, 255, arraysize=cursor.arraysize
1263
+ )
1264
+
1265
+ cursor.outputtypehandler = output_type_handler
1266
+ cursor.execute("SELECT 1.1 FROM DUAL")
1267
+ value = cursor.fetchone()[0]
1268
+
1269
+ decimal_char = value.lstrip("0")[1]
1270
+ assert not decimal_char[0].isdigit()
1271
+
1272
+ self._decimal_char = decimal_char
1273
+
1274
+ if self._decimal_char != ".":
1275
+ _detect_decimal = self._detect_decimal
1276
+ _to_decimal = self._to_decimal
1277
+
1278
+ self._detect_decimal = lambda value: _detect_decimal(
1279
+ value.replace(self._decimal_char, ".")
1280
+ )
1281
+ self._to_decimal = lambda value: _to_decimal(
1282
+ value.replace(self._decimal_char, ".")
1283
+ )
1284
+
1285
+ def _detect_decimal(self, value):
1286
+ if "." in value:
1287
+ return self._to_decimal(value)
1288
+ else:
1289
+ return int(value)
1290
+
1291
+ _to_decimal = decimal.Decimal
1292
+
1293
+ def _generate_connection_outputtype_handler(self):
1294
+ """establish the default outputtypehandler established at the
1295
+ connection level.
1296
+
1297
+ """
1298
+
1299
+ dialect = self
1300
+ cx_Oracle = dialect.dbapi
1301
+
1302
+ number_handler = _OracleNUMBER(
1303
+ asdecimal=True
1304
+ )._cx_oracle_outputtypehandler(dialect)
1305
+ float_handler = _OracleNUMBER(
1306
+ asdecimal=False
1307
+ )._cx_oracle_outputtypehandler(dialect)
1308
+
1309
+ def output_type_handler(
1310
+ cursor, name, default_type, size, precision, scale
1311
+ ):
1312
+ if (
1313
+ default_type == cx_Oracle.NUMBER
1314
+ and default_type is not cx_Oracle.NATIVE_FLOAT
1315
+ ):
1316
+ if not dialect.coerce_to_decimal:
1317
+ return None
1318
+ elif precision == 0 and scale in (0, -127):
1319
+ # ambiguous type, this occurs when selecting
1320
+ # numbers from deep subqueries
1321
+ return cursor.var(
1322
+ cx_Oracle.STRING,
1323
+ 255,
1324
+ outconverter=dialect._detect_decimal,
1325
+ arraysize=cursor.arraysize,
1326
+ )
1327
+ elif precision and scale > 0:
1328
+ return number_handler(
1329
+ cursor, name, default_type, size, precision, scale
1330
+ )
1331
+ else:
1332
+ return float_handler(
1333
+ cursor, name, default_type, size, precision, scale
1334
+ )
1335
+
1336
+ # if unicode options were specified, add a decoder, otherwise
1337
+ # cx_Oracle should return Unicode
1338
+ elif (
1339
+ dialect._cursor_var_unicode_kwargs
1340
+ and default_type
1341
+ in (
1342
+ cx_Oracle.STRING,
1343
+ cx_Oracle.FIXED_CHAR,
1344
+ )
1345
+ and default_type is not cx_Oracle.CLOB
1346
+ and default_type is not cx_Oracle.NCLOB
1347
+ ):
1348
+ return cursor.var(
1349
+ str,
1350
+ size,
1351
+ cursor.arraysize,
1352
+ **dialect._cursor_var_unicode_kwargs,
1353
+ )
1354
+
1355
+ elif dialect.auto_convert_lobs and default_type in (
1356
+ cx_Oracle.CLOB,
1357
+ cx_Oracle.NCLOB,
1358
+ ):
1359
+ typ = (
1360
+ cx_Oracle.DB_TYPE_VARCHAR
1361
+ if default_type is cx_Oracle.CLOB
1362
+ else cx_Oracle.DB_TYPE_NVARCHAR
1363
+ )
1364
+ return cursor.var(
1365
+ typ,
1366
+ _CX_ORACLE_MAGIC_LOB_SIZE,
1367
+ cursor.arraysize,
1368
+ **dialect._cursor_var_unicode_kwargs,
1369
+ )
1370
+
1371
+ elif dialect.auto_convert_lobs and default_type in (
1372
+ cx_Oracle.BLOB,
1373
+ ):
1374
+ return cursor.var(
1375
+ cx_Oracle.DB_TYPE_RAW,
1376
+ _CX_ORACLE_MAGIC_LOB_SIZE,
1377
+ cursor.arraysize,
1378
+ )
1379
+
1380
+ return output_type_handler
1381
+
1382
+ def on_connect(self):
1383
+ output_type_handler = self._generate_connection_outputtype_handler()
1384
+
1385
+ def on_connect(conn):
1386
+ conn.outputtypehandler = output_type_handler
1387
+
1388
+ return on_connect
1389
+
1390
+ def create_connect_args(self, url):
1391
+ opts = dict(url.query)
1392
+
1393
+ for opt in ("use_ansi", "auto_convert_lobs"):
1394
+ if opt in opts:
1395
+ util.warn_deprecated(
1396
+ f"{self.driver} dialect option {opt!r} should only be "
1397
+ "passed to create_engine directly, not within the URL "
1398
+ "string",
1399
+ version="1.3",
1400
+ )
1401
+ util.coerce_kw_type(opts, opt, bool)
1402
+ setattr(self, opt, opts.pop(opt))
1403
+
1404
+ database = url.database
1405
+ service_name = opts.pop("service_name", None)
1406
+ if database or service_name:
1407
+ # if we have a database, then we have a remote host
1408
+ port = url.port
1409
+ if port:
1410
+ port = int(port)
1411
+ else:
1412
+ port = 1521
1413
+
1414
+ if database and service_name:
1415
+ raise exc.InvalidRequestError(
1416
+ '"service_name" option shouldn\'t '
1417
+ 'be used with a "database" part of the url'
1418
+ )
1419
+ if database:
1420
+ makedsn_kwargs = {"sid": database}
1421
+ if service_name:
1422
+ makedsn_kwargs = {"service_name": service_name}
1423
+
1424
+ dsn = self.dbapi.makedsn(url.host, port, **makedsn_kwargs)
1425
+ else:
1426
+ # we have a local tnsname
1427
+ dsn = url.host
1428
+
1429
+ if dsn is not None:
1430
+ opts["dsn"] = dsn
1431
+ if url.password is not None:
1432
+ opts["password"] = url.password
1433
+ if url.username is not None:
1434
+ opts["user"] = url.username
1435
+
1436
+ if self._cx_oracle_threaded is not None:
1437
+ opts.setdefault("threaded", self._cx_oracle_threaded)
1438
+
1439
+ def convert_cx_oracle_constant(value):
1440
+ if isinstance(value, str):
1441
+ try:
1442
+ int_val = int(value)
1443
+ except ValueError:
1444
+ value = value.upper()
1445
+ return getattr(self.dbapi, value)
1446
+ else:
1447
+ return int_val
1448
+ else:
1449
+ return value
1450
+
1451
+ util.coerce_kw_type(opts, "mode", convert_cx_oracle_constant)
1452
+ util.coerce_kw_type(opts, "threaded", bool)
1453
+ util.coerce_kw_type(opts, "events", bool)
1454
+ util.coerce_kw_type(opts, "purity", convert_cx_oracle_constant)
1455
+ return ([], opts)
1456
+
1457
+ def _get_server_version_info(self, connection):
1458
+ return tuple(int(x) for x in connection.connection.version.split("."))
1459
+
1460
+ def is_disconnect(self, e, connection, cursor):
1461
+ (error,) = e.args
1462
+ if isinstance(
1463
+ e, (self.dbapi.InterfaceError, self.dbapi.DatabaseError)
1464
+ ) and "not connected" in str(e):
1465
+ return True
1466
+
1467
+ if hasattr(error, "code") and error.code in {
1468
+ 28,
1469
+ 3114,
1470
+ 3113,
1471
+ 3135,
1472
+ 1033,
1473
+ 2396,
1474
+ }:
1475
+ # ORA-00028: your session has been killed
1476
+ # ORA-03114: not connected to ORACLE
1477
+ # ORA-03113: end-of-file on communication channel
1478
+ # ORA-03135: connection lost contact
1479
+ # ORA-01033: ORACLE initialization or shutdown in progress
1480
+ # ORA-02396: exceeded maximum idle time, please connect again
1481
+ # TODO: Others ?
1482
+ return True
1483
+
1484
+ if re.match(r"^(?:DPI-1010|DPI-1080|DPY-1001|DPY-4011)", str(e)):
1485
+ # DPI-1010: not connected
1486
+ # DPI-1080: connection was closed by ORA-3113
1487
+ # python-oracledb's DPY-1001: not connected to database
1488
+ # python-oracledb's DPY-4011: the database or network closed the
1489
+ # connection
1490
+ # TODO: others?
1491
+ return True
1492
+
1493
+ return False
1494
+
1495
+ def create_xid(self):
1496
+ id_ = random.randint(0, 2**128)
1497
+ return (0x1234, "%032x" % id_, "%032x" % 9)
1498
+
1499
+ def do_executemany(self, cursor, statement, parameters, context=None):
1500
+ if isinstance(parameters, tuple):
1501
+ parameters = list(parameters)
1502
+ cursor.executemany(statement, parameters)
1503
+
1504
+ def do_begin_twophase(self, connection, xid):
1505
+ connection.connection.begin(*xid)
1506
+ connection.connection.info["cx_oracle_xid"] = xid
1507
+
1508
+ def do_prepare_twophase(self, connection, xid):
1509
+ result = connection.connection.prepare()
1510
+ connection.info["cx_oracle_prepared"] = result
1511
+
1512
+ def do_rollback_twophase(
1513
+ self, connection, xid, is_prepared=True, recover=False
1514
+ ):
1515
+ self.do_rollback(connection.connection)
1516
+ # TODO: need to end XA state here
1517
+
1518
+ def do_commit_twophase(
1519
+ self, connection, xid, is_prepared=True, recover=False
1520
+ ):
1521
+ if not is_prepared:
1522
+ self.do_commit(connection.connection)
1523
+ else:
1524
+ if recover:
1525
+ raise NotImplementedError(
1526
+ "2pc recovery not implemented for cx_Oracle"
1527
+ )
1528
+ oci_prepared = connection.info["cx_oracle_prepared"]
1529
+ if oci_prepared:
1530
+ self.do_commit(connection.connection)
1531
+ # TODO: need to end XA state here
1532
+
1533
+ def do_set_input_sizes(self, cursor, list_of_tuples, context):
1534
+ if self.positional:
1535
+ # not usually used, here to support if someone is modifying
1536
+ # the dialect to use positional style
1537
+ cursor.setinputsizes(
1538
+ *[dbtype for key, dbtype, sqltype in list_of_tuples]
1539
+ )
1540
+ else:
1541
+ collection = (
1542
+ (key, dbtype)
1543
+ for key, dbtype, sqltype in list_of_tuples
1544
+ if dbtype
1545
+ )
1546
+
1547
+ cursor.setinputsizes(**{key: dbtype for key, dbtype in collection})
1548
+
1549
+ def do_recover_twophase(self, connection):
1550
+ raise NotImplementedError(
1551
+ "recover two phase query for cx_Oracle not implemented"
1552
+ )
1553
+
1554
+
1555
+ dialect = OracleDialect_cx_oracle