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