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,909 @@
1
+ # dialects/oracle/oracledb.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
+ r""".. dialect:: oracle+oracledb
10
+ :name: python-oracledb
11
+ :dbapi: oracledb
12
+ :connectstring: oracle+oracledb://user:pass@hostname:port[/dbname][?service_name=<service>[&key=value&key=value...]]
13
+ :url: https://oracle.github.io/python-oracledb/
14
+
15
+ Description
16
+ -----------
17
+
18
+ Python-oracledb is the Oracle Database driver for Python. It features a default
19
+ "thin" client mode that requires no dependencies, and an optional "thick" mode
20
+ that uses Oracle Client libraries. It supports SQLAlchemy features including
21
+ two phase transactions and Asyncio.
22
+
23
+ Python-oracle is the renamed, updated cx_Oracle driver. Oracle is no longer
24
+ doing any releases in the cx_Oracle namespace.
25
+
26
+ The SQLAlchemy ``oracledb`` dialect provides both a sync and an async
27
+ implementation under the same dialect name. The proper version is
28
+ selected depending on how the engine is created:
29
+
30
+ * calling :func:`_sa.create_engine` with ``oracle+oracledb://...`` will
31
+ automatically select the sync version::
32
+
33
+ from sqlalchemy import create_engine
34
+
35
+ sync_engine = create_engine(
36
+ "oracle+oracledb://scott:tiger@localhost?service_name=FREEPDB1"
37
+ )
38
+
39
+ * calling :func:`_asyncio.create_async_engine` with ``oracle+oracledb://...``
40
+ will automatically select the async version::
41
+
42
+ from sqlalchemy.ext.asyncio import create_async_engine
43
+
44
+ asyncio_engine = create_async_engine(
45
+ "oracle+oracledb://scott:tiger@localhost?service_name=FREEPDB1"
46
+ )
47
+
48
+ The asyncio version of the dialect may also be specified explicitly using the
49
+ ``oracledb_async`` suffix::
50
+
51
+ from sqlalchemy.ext.asyncio import create_async_engine
52
+
53
+ asyncio_engine = create_async_engine(
54
+ "oracle+oracledb_async://scott:tiger@localhost?service_name=FREEPDB1"
55
+ )
56
+
57
+ .. versionadded:: 2.0.25 added support for the async version of oracledb.
58
+
59
+ Thick mode support
60
+ ------------------
61
+
62
+ By default, the python-oracledb driver runs in a "thin" mode that does not
63
+ require Oracle Client libraries to be installed. The driver also supports a
64
+ "thick" mode that uses Oracle Client libraries to get functionality such as
65
+ Oracle Application Continuity.
66
+
67
+ To enable thick mode, call `oracledb.init_oracle_client()
68
+ <https://python-oracledb.readthedocs.io/en/latest/api_manual/module.html#oracledb.init_oracle_client>`_
69
+ explicitly, or pass the parameter ``thick_mode=True`` to
70
+ :func:`_sa.create_engine`. To pass custom arguments to
71
+ ``init_oracle_client()``, like the ``lib_dir`` path, a dict may be passed, for
72
+ example::
73
+
74
+ engine = sa.create_engine(
75
+ "oracle+oracledb://...",
76
+ thick_mode={
77
+ "lib_dir": "/path/to/oracle/client/lib",
78
+ "config_dir": "/path/to/network_config_file_directory",
79
+ "driver_name": "my-app : 1.0.0",
80
+ },
81
+ )
82
+
83
+ Note that passing a ``lib_dir`` path should only be done on macOS or
84
+ Windows. On Linux it does not behave as you might expect.
85
+
86
+ .. seealso::
87
+
88
+ python-oracledb documentation `Enabling python-oracledb Thick mode
89
+ <https://python-oracledb.readthedocs.io/en/latest/user_guide/initialization.html#enabling-python-oracledb-thick-mode>`_
90
+
91
+ Connecting to Oracle Database
92
+ -----------------------------
93
+
94
+ python-oracledb provides several methods of indicating the target database.
95
+ The dialect translates from a series of different URL forms.
96
+
97
+ Given the hostname, port and service name of the target database, you can
98
+ connect in SQLAlchemy using the ``service_name`` query string parameter::
99
+
100
+ engine = create_engine(
101
+ "oracle+oracledb://scott:tiger@hostname:port?service_name=myservice"
102
+ )
103
+
104
+ Connecting with Easy Connect strings
105
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106
+
107
+ You can pass any valid python-oracledb connection string as the ``dsn`` key
108
+ value in a :paramref:`_sa.create_engine.connect_args` dictionary. See
109
+ python-oracledb documentation `Oracle Net Services Connection Strings
110
+ <https://python-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html#oracle-net-services-connection-strings>`_.
111
+
112
+ For example to use an `Easy Connect string
113
+ <https://download.oracle.com/ocomdocs/global/Oracle-Net-Easy-Connect-Plus.pdf>`_
114
+ with a timeout to prevent connection establishment from hanging if the network
115
+ transport to the database cannot be established in 30 seconds, and also setting
116
+ a keep-alive time of 60 seconds to stop idle network connections from being
117
+ terminated by a firewall::
118
+
119
+ e = create_engine(
120
+ "oracle+oracledb://@",
121
+ connect_args={
122
+ "user": "scott",
123
+ "password": "tiger",
124
+ "dsn": "hostname:port/myservice?transport_connect_timeout=30&expire_time=60",
125
+ },
126
+ )
127
+
128
+ The Easy Connect syntax has been enhanced during the life of Oracle Database.
129
+ Review the documentation for your database version. The current documentation
130
+ is at `Understanding the Easy Connect Naming Method
131
+ <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-B0437826-43C1-49EC-A94D-B650B6A4A6EE>`_.
132
+
133
+ The general syntax is similar to:
134
+
135
+ .. sourcecode:: text
136
+
137
+ [[protocol:]//]host[:port][/[service_name]][?parameter_name=value{&parameter_name=value}]
138
+
139
+ Note that although the SQLAlchemy URL syntax ``hostname:port/dbname`` looks
140
+ like Oracle's Easy Connect syntax, it is different. SQLAlchemy's URL requires a
141
+ system identifier (SID) for the ``dbname`` component::
142
+
143
+ engine = create_engine("oracle+oracledb://scott:tiger@hostname:port/sid")
144
+
145
+ Easy Connect syntax does not support SIDs. It uses services names, which are
146
+ the preferred choice for connecting to Oracle Database.
147
+
148
+ Passing python-oracledb connect arguments
149
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
150
+
151
+ Other python-oracledb driver `connection options
152
+ <https://python-oracledb.readthedocs.io/en/latest/api_manual/module.html#oracledb.connect>`_
153
+ can be passed in ``connect_args``. For example::
154
+
155
+ e = create_engine(
156
+ "oracle+oracledb://@",
157
+ connect_args={
158
+ "user": "scott",
159
+ "password": "tiger",
160
+ "dsn": "hostname:port/myservice",
161
+ "events": True,
162
+ "mode": oracledb.AUTH_MODE_SYSDBA,
163
+ },
164
+ )
165
+
166
+ Connecting with tnsnames.ora TNS aliases
167
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168
+
169
+ If no port, database name, or service name is provided, the dialect will use an
170
+ Oracle Database DSN "connection string". This takes the "hostname" portion of
171
+ the URL as the data source name. For example, if the ``tnsnames.ora`` file
172
+ contains a `TNS Alias
173
+ <https://python-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html#tns-aliases-for-connection-strings>`_
174
+ of ``myalias`` as below:
175
+
176
+ .. sourcecode:: text
177
+
178
+ myalias =
179
+ (DESCRIPTION =
180
+ (ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.example.com)(PORT = 1521))
181
+ (CONNECT_DATA =
182
+ (SERVER = DEDICATED)
183
+ (SERVICE_NAME = orclpdb1)
184
+ )
185
+ )
186
+
187
+ The python-oracledb dialect connects to this database service when ``myalias`` is the
188
+ hostname portion of the URL, without specifying a port, database name or
189
+ ``service_name``::
190
+
191
+ engine = create_engine("oracle+oracledb://scott:tiger@myalias")
192
+
193
+ Connecting to Oracle Autonomous Database
194
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
195
+
196
+ Users of Oracle Autonomous Database should use either use the TNS Alias URL
197
+ shown above, or pass the TNS Alias as the ``dsn`` key value in a
198
+ :paramref:`_sa.create_engine.connect_args` dictionary.
199
+
200
+ If Oracle Autonomous Database is configured for mutual TLS ("mTLS")
201
+ connections, then additional configuration is required as shown in `Connecting
202
+ to Oracle Cloud Autonomous Databases
203
+ <https://python-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html#connecting-to-oracle-cloud-autonomous-databases>`_. In
204
+ summary, Thick mode users should configure file locations and set the wallet
205
+ path in ``sqlnet.ora`` appropriately::
206
+
207
+ e = create_engine(
208
+ "oracle+oracledb://@",
209
+ thick_mode={
210
+ # directory containing tnsnames.ora and cwallet.so
211
+ "config_dir": "/opt/oracle/wallet_dir",
212
+ },
213
+ connect_args={
214
+ "user": "scott",
215
+ "password": "tiger",
216
+ "dsn": "mydb_high",
217
+ },
218
+ )
219
+
220
+ Thin mode users of mTLS should pass the appropriate directories and PEM wallet
221
+ password when creating the engine, similar to::
222
+
223
+ e = create_engine(
224
+ "oracle+oracledb://@",
225
+ connect_args={
226
+ "user": "scott",
227
+ "password": "tiger",
228
+ "dsn": "mydb_high",
229
+ "config_dir": "/opt/oracle/wallet_dir", # directory containing tnsnames.ora
230
+ "wallet_location": "/opt/oracle/wallet_dir", # directory containing ewallet.pem
231
+ "wallet_password": "top secret", # password for the PEM file
232
+ },
233
+ )
234
+
235
+ Typically ``config_dir`` and ``wallet_location`` are the same directory, which
236
+ is where the Oracle Autonomous Database wallet zip file was extracted. Note
237
+ this directory should be protected.
238
+
239
+ Using python-oracledb Connection Pooling
240
+ ----------------------------------------
241
+
242
+ The python-oracledb driver provides its own connection pool implementation that
243
+ may be used in place of SQLAlchemy's pooling functionality. The driver pool
244
+ gives support for high availability features such as dead connection detection,
245
+ connection draining for planned database downtime, support for Oracle
246
+ Application Continuity and Transparent Application Continuity, and gives
247
+ support for `Database Resident Connection Pooling (DRCP)
248
+ <https://python-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html#database-resident-connection-pooling-drcp>`_.
249
+
250
+ To take advantage of python-oracledb's pool, use the
251
+ :paramref:`_sa.create_engine.creator` parameter to provide a function that
252
+ returns a new connection, along with setting
253
+ :paramref:`_sa.create_engine.pool_class` to ``NullPool`` to disable
254
+ SQLAlchemy's pooling::
255
+
256
+ import oracledb
257
+ from sqlalchemy import create_engine
258
+ from sqlalchemy import text
259
+ from sqlalchemy.pool import NullPool
260
+
261
+ # Uncomment to use the optional python-oracledb Thick mode.
262
+ # Review the python-oracledb doc for the appropriate parameters
263
+ # oracledb.init_oracle_client(<your parameters>)
264
+
265
+ pool = oracledb.create_pool(
266
+ user="scott",
267
+ password="tiger",
268
+ dsn="localhost:1521/freepdb1",
269
+ min=1,
270
+ max=4,
271
+ increment=1,
272
+ )
273
+ engine = create_engine(
274
+ "oracle+oracledb://", creator=pool.acquire, poolclass=NullPool
275
+ )
276
+
277
+ The above engine may then be used normally. Internally, python-oracledb handles
278
+ connection pooling::
279
+
280
+ with engine.connect() as conn:
281
+ print(conn.scalar(text("select 1 from dual")))
282
+
283
+ Refer to the python-oracledb documentation for `oracledb.create_pool()
284
+ <https://python-oracledb.readthedocs.io/en/latest/api_manual/module.html#oracledb.create_pool>`_
285
+ for the arguments that can be used when creating a connection pool.
286
+
287
+ .. _drcp:
288
+
289
+ Using Oracle Database Resident Connection Pooling (DRCP)
290
+ --------------------------------------------------------
291
+
292
+ When using Oracle Database's Database Resident Connection Pooling (DRCP), the
293
+ best practice is to specify a connection class and "purity". Refer to the
294
+ `python-oracledb documentation on DRCP
295
+ <https://python-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html#database-resident-connection-pooling-drcp>`_.
296
+ For example::
297
+
298
+ import oracledb
299
+ from sqlalchemy import create_engine
300
+ from sqlalchemy import text
301
+ from sqlalchemy.pool import NullPool
302
+
303
+ # Uncomment to use the optional python-oracledb Thick mode.
304
+ # Review the python-oracledb doc for the appropriate parameters
305
+ # oracledb.init_oracle_client(<your parameters>)
306
+
307
+ pool = oracledb.create_pool(
308
+ user="scott",
309
+ password="tiger",
310
+ dsn="localhost:1521/freepdb1",
311
+ min=1,
312
+ max=4,
313
+ increment=1,
314
+ cclass="MYCLASS",
315
+ purity=oracledb.PURITY_SELF,
316
+ )
317
+ engine = create_engine(
318
+ "oracle+oracledb://", creator=pool.acquire, poolclass=NullPool
319
+ )
320
+
321
+ The above engine may then be used normally where python-oracledb handles
322
+ application connection pooling and Oracle Database additionally uses DRCP::
323
+
324
+ with engine.connect() as conn:
325
+ print(conn.scalar(text("select 1 from dual")))
326
+
327
+ If you wish to use different connection classes or purities for different
328
+ connections, then wrap ``pool.acquire()``::
329
+
330
+ import oracledb
331
+ from sqlalchemy import create_engine
332
+ from sqlalchemy import text
333
+ from sqlalchemy.pool import NullPool
334
+
335
+ # Uncomment to use python-oracledb Thick mode.
336
+ # Review the python-oracledb doc for the appropriate parameters
337
+ # oracledb.init_oracle_client(<your parameters>)
338
+
339
+ pool = oracledb.create_pool(
340
+ user="scott",
341
+ password="tiger",
342
+ dsn="localhost:1521/freepdb1",
343
+ min=1,
344
+ max=4,
345
+ increment=1,
346
+ cclass="MYCLASS",
347
+ purity=oracledb.PURITY_SELF,
348
+ )
349
+
350
+
351
+ def creator():
352
+ return pool.acquire(cclass="MYOTHERCLASS", purity=oracledb.PURITY_NEW)
353
+
354
+
355
+ engine = create_engine(
356
+ "oracle+oracledb://", creator=creator, poolclass=NullPool
357
+ )
358
+
359
+ Engine Options consumed by the SQLAlchemy oracledb dialect outside of the driver
360
+ --------------------------------------------------------------------------------
361
+
362
+ There are also options that are consumed by the SQLAlchemy oracledb dialect
363
+ itself. These options are always passed directly to :func:`_sa.create_engine`,
364
+ such as::
365
+
366
+ e = create_engine("oracle+oracledb://user:pass@tnsalias", arraysize=500)
367
+
368
+ The parameters accepted by the oracledb dialect are as follows:
369
+
370
+ * ``arraysize`` - set the driver cursor.arraysize value. It defaults to
371
+ ``None``, indicating that the driver default value of 100 should be used.
372
+ This setting controls how many rows are buffered when fetching rows, and can
373
+ have a significant effect on performance if increased for queries that return
374
+ large numbers of rows.
375
+
376
+ .. versionchanged:: 2.0.26 - changed the default value from 50 to None,
377
+ to use the default value of the driver itself.
378
+
379
+ * ``auto_convert_lobs`` - defaults to True; See :ref:`oracledb_lob`.
380
+
381
+ * ``coerce_to_decimal`` - see :ref:`oracledb_numeric` for detail.
382
+
383
+ * ``encoding_errors`` - see :ref:`oracledb_unicode_encoding_errors` for detail.
384
+
385
+ .. _oracledb_unicode:
386
+
387
+ Unicode
388
+ -------
389
+
390
+ As is the case for all DBAPIs under Python 3, all strings are inherently
391
+ Unicode strings.
392
+
393
+ Ensuring the Correct Client Encoding
394
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395
+
396
+ In python-oracledb, the encoding used for all character data is "UTF-8".
397
+
398
+ Unicode-specific Column datatypes
399
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
400
+
401
+ The Core expression language handles unicode data by use of the
402
+ :class:`.Unicode` and :class:`.UnicodeText` datatypes. These types correspond
403
+ to the VARCHAR2 and CLOB Oracle Database datatypes by default. When using
404
+ these datatypes with Unicode data, it is expected that the database is
405
+ configured with a Unicode-aware character set so that the VARCHAR2 and CLOB
406
+ datatypes can accommodate the data.
407
+
408
+ In the case that Oracle Database is not configured with a Unicode character
409
+ set, the two options are to use the :class:`_types.NCHAR` and
410
+ :class:`_oracle.NCLOB` datatypes explicitly, or to pass the flag
411
+ ``use_nchar_for_unicode=True`` to :func:`_sa.create_engine`, which will cause
412
+ the SQLAlchemy dialect to use NCHAR/NCLOB for the :class:`.Unicode` /
413
+ :class:`.UnicodeText` datatypes instead of VARCHAR/CLOB.
414
+
415
+ .. _oracledb_unicode_encoding_errors:
416
+
417
+ Encoding Errors
418
+ ^^^^^^^^^^^^^^^
419
+
420
+ For the unusual case that data in Oracle Database is present with a broken
421
+ encoding, the dialect accepts a parameter ``encoding_errors`` which will be
422
+ passed to Unicode decoding functions in order to affect how decoding errors are
423
+ handled. The value is ultimately consumed by the Python `decode
424
+ <https://docs.python.org/3/library/stdtypes.html#bytes.decode>`_ function, and
425
+ is passed both via python-oracledb's ``encodingErrors`` parameter consumed by
426
+ ``Cursor.var()``, as well as SQLAlchemy's own decoding function, as the
427
+ python-oracledb dialect makes use of both under different circumstances.
428
+
429
+ .. _oracledb_setinputsizes:
430
+
431
+ Fine grained control over python-oracledb data binding with setinputsizes
432
+ -------------------------------------------------------------------------
433
+
434
+ The python-oracle DBAPI has a deep and fundamental reliance upon the usage of
435
+ the DBAPI ``setinputsizes()`` call. The purpose of this call is to establish
436
+ the datatypes that are bound to a SQL statement for Python values being passed
437
+ as parameters. While virtually no other DBAPI assigns any use to the
438
+ ``setinputsizes()`` call, the python-oracledb DBAPI relies upon it heavily in
439
+ its interactions with the Oracle Database, and in some scenarios it is not
440
+ possible for SQLAlchemy to know exactly how data should be bound, as some
441
+ settings can cause profoundly different performance characteristics, while
442
+ altering the type coercion behavior at the same time.
443
+
444
+ Users of the oracledb dialect are **strongly encouraged** to read through
445
+ python-oracledb's list of built-in datatype symbols at `Database Types
446
+ <https://python-oracledb.readthedocs.io/en/latest/api_manual/module.html#database-types>`_
447
+ Note that in some cases, significant performance degradation can occur when
448
+ using these types vs. not.
449
+
450
+ On the SQLAlchemy side, the :meth:`.DialectEvents.do_setinputsizes` event can
451
+ be used both for runtime visibility (e.g. logging) of the setinputsizes step as
452
+ well as to fully control how ``setinputsizes()`` is used on a per-statement
453
+ basis.
454
+
455
+ Example 1 - logging all setinputsizes calls
456
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
457
+
458
+ The following example illustrates how to log the intermediary values from a
459
+ SQLAlchemy perspective before they are converted to the raw ``setinputsizes()``
460
+ parameter dictionary. The keys of the dictionary are :class:`.BindParameter`
461
+ objects which have a ``.key`` and a ``.type`` attribute::
462
+
463
+ from sqlalchemy import create_engine, event
464
+
465
+ engine = create_engine(
466
+ "oracle+oracledb://scott:tiger@localhost:1521?service_name=freepdb1"
467
+ )
468
+
469
+
470
+ @event.listens_for(engine, "do_setinputsizes")
471
+ def _log_setinputsizes(inputsizes, cursor, statement, parameters, context):
472
+ for bindparam, dbapitype in inputsizes.items():
473
+ log.info(
474
+ "Bound parameter name: %s SQLAlchemy type: %r DBAPI object: %s",
475
+ bindparam.key,
476
+ bindparam.type,
477
+ dbapitype,
478
+ )
479
+
480
+ Example 2 - remove all bindings to CLOB
481
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
482
+
483
+ For performance, fetching LOB datatypes from Oracle Database is set by default
484
+ for the ``Text`` type within SQLAlchemy. This setting can be modified as
485
+ follows::
486
+
487
+
488
+ from sqlalchemy import create_engine, event
489
+ from oracledb import CLOB
490
+
491
+ engine = create_engine(
492
+ "oracle+oracledb://scott:tiger@localhost:1521?service_name=freepdb1"
493
+ )
494
+
495
+
496
+ @event.listens_for(engine, "do_setinputsizes")
497
+ def _remove_clob(inputsizes, cursor, statement, parameters, context):
498
+ for bindparam, dbapitype in list(inputsizes.items()):
499
+ if dbapitype is CLOB:
500
+ del inputsizes[bindparam]
501
+
502
+ .. _oracledb_lob:
503
+
504
+ LOB Datatypes
505
+ --------------
506
+
507
+ LOB datatypes refer to the "large object" datatypes such as CLOB, NCLOB and
508
+ BLOB. Oracle Database can efficiently return these datatypes as a single
509
+ buffer. SQLAlchemy makes use of type handlers to do this by default.
510
+
511
+ To disable the use of the type handlers and deliver LOB objects as classic
512
+ buffered objects with a ``read()`` method, the parameter
513
+ ``auto_convert_lobs=False`` may be passed to :func:`_sa.create_engine`.
514
+
515
+ .. _oracledb_returning:
516
+
517
+ RETURNING Support
518
+ -----------------
519
+
520
+ The oracledb dialect implements RETURNING using OUT parameters. The dialect
521
+ supports RETURNING fully.
522
+
523
+ Two Phase Transaction Support
524
+ -----------------------------
525
+
526
+ Two phase transactions are fully supported with python-oracledb. (Thin mode
527
+ requires python-oracledb 2.3). APIs for two phase transactions are provided at
528
+ the Core level via :meth:`_engine.Connection.begin_twophase` and
529
+ :paramref:`_orm.Session.twophase` for transparent ORM use.
530
+
531
+ .. versionchanged:: 2.0.32 added support for two phase transactions
532
+
533
+ .. _oracledb_numeric:
534
+
535
+ Precision Numerics
536
+ ------------------
537
+
538
+ SQLAlchemy's numeric types can handle receiving and returning values as Python
539
+ ``Decimal`` objects or float objects. When a :class:`.Numeric` object, or a
540
+ subclass such as :class:`.Float`, :class:`_oracle.DOUBLE_PRECISION` etc. is in
541
+ use, the :paramref:`.Numeric.asdecimal` flag determines if values should be
542
+ coerced to ``Decimal`` upon return, or returned as float objects. To make
543
+ matters more complicated under Oracle Database, the ``NUMBER`` type can also
544
+ represent integer values if the "scale" is zero, so the Oracle
545
+ Database-specific :class:`_oracle.NUMBER` type takes this into account as well.
546
+
547
+ The oracledb dialect makes extensive use of connection- and cursor-level
548
+ "outputtypehandler" callables in order to coerce numeric values as requested.
549
+ These callables are specific to the specific flavor of :class:`.Numeric` in
550
+ use, as well as if no SQLAlchemy typing objects are present. There are
551
+ observed scenarios where Oracle Database may send incomplete or ambiguous
552
+ information about the numeric types being returned, such as a query where the
553
+ numeric types are buried under multiple levels of subquery. The type handlers
554
+ do their best to make the right decision in all cases, deferring to the
555
+ underlying python-oracledb DBAPI for all those cases where the driver can make
556
+ the best decision.
557
+
558
+ When no typing objects are present, as when executing plain SQL strings, a
559
+ default "outputtypehandler" is present which will generally return numeric
560
+ values which specify precision and scale as Python ``Decimal`` objects. To
561
+ disable this coercion to decimal for performance reasons, pass the flag
562
+ ``coerce_to_decimal=False`` to :func:`_sa.create_engine`::
563
+
564
+ engine = create_engine(
565
+ "oracle+oracledb://scott:tiger@tnsalias", coerce_to_decimal=False
566
+ )
567
+
568
+ The ``coerce_to_decimal`` flag only impacts the results of plain string
569
+ SQL statements that are not otherwise associated with a :class:`.Numeric`
570
+ SQLAlchemy type (or a subclass of such).
571
+
572
+ .. versionadded:: 2.0.0 added support for the python-oracledb driver.
573
+
574
+ .. _oracledb_json:
575
+
576
+ JSON Support
577
+ ------------
578
+
579
+ Oracle Database supports a native JSON datatype as of version 21c, as well as
580
+ support for JSON functions on character and BLOB columns as of version 12c. The
581
+ SQLAlchemy :class:`_sqltypes.JSON` datatype may be used with the oracledb
582
+ backend in the same way it works with any other backend, with some slight
583
+ behavioral changes particularly when using the native JSON datatype. See
584
+ :class:`_oracle.JSON` for platform-specific notes.
585
+
586
+ .. versionadded:: 2.1 added JSON support for the Oracle backend.
587
+
588
+
589
+ """ # noqa
590
+ from __future__ import annotations
591
+
592
+ import collections
593
+ import re
594
+ from typing import Any
595
+ from typing import TYPE_CHECKING
596
+
597
+ from . import cx_oracle as _cx_oracle
598
+ from ... import exc
599
+ from ...connectors.asyncio import AsyncAdapt_dbapi_connection
600
+ from ...connectors.asyncio import AsyncAdapt_dbapi_cursor
601
+ from ...connectors.asyncio import AsyncAdapt_dbapi_module
602
+ from ...connectors.asyncio import AsyncAdapt_dbapi_ss_cursor
603
+ from ...engine import default
604
+ from ...util import await_
605
+
606
+ if TYPE_CHECKING:
607
+ from oracledb import AsyncConnection
608
+ from oracledb import AsyncCursor
609
+
610
+
611
+ class OracleExecutionContext_oracledb(
612
+ _cx_oracle.OracleExecutionContext_cx_oracle
613
+ ):
614
+ pass
615
+
616
+
617
+ class OracleDialect_oracledb(_cx_oracle.OracleDialect_cx_oracle):
618
+ supports_statement_cache = True
619
+ execution_ctx_cls = OracleExecutionContext_oracledb
620
+
621
+ driver = "oracledb"
622
+ _min_version = (1,)
623
+
624
+ def __init__(
625
+ self,
626
+ auto_convert_lobs=True,
627
+ coerce_to_decimal=True,
628
+ arraysize=None,
629
+ encoding_errors=None,
630
+ thick_mode=None,
631
+ **kwargs,
632
+ ):
633
+ super().__init__(
634
+ auto_convert_lobs,
635
+ coerce_to_decimal,
636
+ arraysize,
637
+ encoding_errors,
638
+ **kwargs,
639
+ )
640
+
641
+ if self.dbapi is not None and (
642
+ thick_mode or isinstance(thick_mode, dict)
643
+ ):
644
+ kw = thick_mode if isinstance(thick_mode, dict) else {}
645
+ self.dbapi.init_oracle_client(**kw)
646
+
647
+ @classmethod
648
+ def import_dbapi(cls):
649
+ import oracledb
650
+
651
+ return oracledb
652
+
653
+ @classmethod
654
+ def is_thin_mode(cls, connection):
655
+ return connection.connection.dbapi_connection.thin
656
+
657
+ @classmethod
658
+ def get_async_dialect_cls(cls, url):
659
+ return OracleDialectAsync_oracledb
660
+
661
+ def _load_version(self, dbapi_module):
662
+ version = (0, 0, 0)
663
+ if dbapi_module is not None:
664
+ m = re.match(r"(\d+)\.(\d+)(?:\.(\d+))?", dbapi_module.version)
665
+ if m:
666
+ version = tuple(
667
+ int(x) for x in m.group(1, 2, 3) if x is not None
668
+ )
669
+ self.oracledb_ver = version
670
+ if (
671
+ self.oracledb_ver > (0, 0, 0)
672
+ and self.oracledb_ver < self._min_version
673
+ ):
674
+ raise exc.InvalidRequestError(
675
+ f"oracledb version {self._min_version} and above are supported"
676
+ )
677
+
678
+ def do_begin_twophase(self, connection, xid):
679
+ conn_xis = connection.connection.xid(*xid)
680
+ connection.connection.tpc_begin(conn_xis)
681
+ connection.connection.info["oracledb_xid"] = conn_xis
682
+
683
+ def do_prepare_twophase(self, connection, xid):
684
+ should_commit = connection.connection.tpc_prepare()
685
+ connection.info["oracledb_should_commit"] = should_commit
686
+
687
+ def do_rollback_twophase(
688
+ self, connection, xid, is_prepared=True, recover=False
689
+ ):
690
+ if recover:
691
+ conn_xid = connection.connection.xid(*xid)
692
+ else:
693
+ conn_xid = None
694
+ connection.connection.tpc_rollback(conn_xid)
695
+
696
+ def do_commit_twophase(
697
+ self, connection, xid, is_prepared=True, recover=False
698
+ ):
699
+ conn_xid = None
700
+ if not is_prepared:
701
+ should_commit = connection.connection.tpc_prepare()
702
+ elif recover:
703
+ conn_xid = connection.connection.xid(*xid)
704
+ should_commit = True
705
+ else:
706
+ should_commit = connection.info["oracledb_should_commit"]
707
+ if should_commit:
708
+ connection.connection.tpc_commit(conn_xid)
709
+
710
+ def do_recover_twophase(self, connection):
711
+ return [
712
+ # oracledb seems to return bytes
713
+ (
714
+ fi,
715
+ gti.decode() if isinstance(gti, bytes) else gti,
716
+ bq.decode() if isinstance(bq, bytes) else bq,
717
+ )
718
+ for fi, gti, bq in connection.connection.tpc_recover()
719
+ ]
720
+
721
+ def _check_max_identifier_length(self, connection):
722
+ if self.oracledb_ver >= (2, 5):
723
+ max_len = connection.connection.max_identifier_length
724
+ if max_len is not None:
725
+ return max_len
726
+ return super()._check_max_identifier_length(connection)
727
+
728
+
729
+ class AsyncAdapt_oracledb_cursor(AsyncAdapt_dbapi_cursor):
730
+ _cursor: AsyncCursor
731
+ _awaitable_cursor_close: bool = False
732
+
733
+ __slots__ = ()
734
+
735
+ @property
736
+ def outputtypehandler(self):
737
+ return self._cursor.outputtypehandler
738
+
739
+ @outputtypehandler.setter
740
+ def outputtypehandler(self, value):
741
+ self._cursor.outputtypehandler = value
742
+
743
+ def var(self, *args, **kwargs):
744
+ return self._cursor.var(*args, **kwargs)
745
+
746
+ def setinputsizes(self, *args: Any, **kwargs: Any) -> Any:
747
+ return self._cursor.setinputsizes(*args, **kwargs)
748
+
749
+ def _aenter_cursor(self, cursor: AsyncCursor) -> AsyncCursor:
750
+ try:
751
+ return cursor.__enter__()
752
+ except Exception as error:
753
+ self._adapt_connection._handle_exception(error)
754
+
755
+ async def _execute_async(self, operation, parameters):
756
+ # override to not use mutex, oracledb already has a mutex
757
+
758
+ if parameters is None:
759
+ result = await self._cursor.execute(operation)
760
+ else:
761
+ result = await self._cursor.execute(operation, parameters)
762
+
763
+ if self._cursor.description and not self.server_side:
764
+ self._rows = collections.deque(await self._cursor.fetchall())
765
+ return result
766
+
767
+ async def _executemany_async(
768
+ self,
769
+ operation,
770
+ seq_of_parameters,
771
+ ):
772
+ # override to not use mutex, oracledb already has a mutex
773
+ return await self._cursor.executemany(operation, seq_of_parameters)
774
+
775
+
776
+ class AsyncAdapt_oracledb_ss_cursor(
777
+ AsyncAdapt_dbapi_ss_cursor, AsyncAdapt_oracledb_cursor
778
+ ):
779
+ __slots__ = ()
780
+
781
+ def close(self) -> None:
782
+ if self._cursor is not None:
783
+ self._cursor.close()
784
+ self._cursor = None # type: ignore
785
+
786
+
787
+ class AsyncAdapt_oracledb_connection(AsyncAdapt_dbapi_connection):
788
+ _connection: AsyncConnection
789
+ __slots__ = ()
790
+
791
+ thin = True
792
+
793
+ _cursor_cls = AsyncAdapt_oracledb_cursor
794
+ _ss_cursor_cls = None
795
+
796
+ @property
797
+ def autocommit(self):
798
+ return self._connection.autocommit
799
+
800
+ @autocommit.setter
801
+ def autocommit(self, value):
802
+ self._connection.autocommit = value
803
+
804
+ @property
805
+ def outputtypehandler(self):
806
+ return self._connection.outputtypehandler
807
+
808
+ @outputtypehandler.setter
809
+ def outputtypehandler(self, value):
810
+ self._connection.outputtypehandler = value
811
+
812
+ @property
813
+ def version(self):
814
+ return self._connection.version
815
+
816
+ @property
817
+ def stmtcachesize(self):
818
+ return self._connection.stmtcachesize
819
+
820
+ @stmtcachesize.setter
821
+ def stmtcachesize(self, value):
822
+ self._connection.stmtcachesize = value
823
+
824
+ @property
825
+ def max_identifier_length(self):
826
+ return self._connection.max_identifier_length
827
+
828
+ def cursor(self):
829
+ return AsyncAdapt_oracledb_cursor(self)
830
+
831
+ def ss_cursor(self):
832
+ return AsyncAdapt_oracledb_ss_cursor(self)
833
+
834
+ def xid(self, *args: Any, **kwargs: Any) -> Any:
835
+ return self._connection.xid(*args, **kwargs)
836
+
837
+ def tpc_begin(self, *args: Any, **kwargs: Any) -> Any:
838
+ return await_(self._connection.tpc_begin(*args, **kwargs))
839
+
840
+ def tpc_commit(self, *args: Any, **kwargs: Any) -> Any:
841
+ return await_(self._connection.tpc_commit(*args, **kwargs))
842
+
843
+ def tpc_prepare(self, *args: Any, **kwargs: Any) -> Any:
844
+ return await_(self._connection.tpc_prepare(*args, **kwargs))
845
+
846
+ def tpc_recover(self, *args: Any, **kwargs: Any) -> Any:
847
+ return await_(self._connection.tpc_recover(*args, **kwargs))
848
+
849
+ def tpc_rollback(self, *args: Any, **kwargs: Any) -> Any:
850
+ return await_(self._connection.tpc_rollback(*args, **kwargs))
851
+
852
+
853
+ class OracledbAdaptDBAPI(AsyncAdapt_dbapi_module):
854
+ def __init__(self, oracledb) -> None:
855
+ super().__init__(oracledb)
856
+ self.oracledb = oracledb
857
+
858
+ for k, v in self.oracledb.__dict__.items():
859
+ if k != "connect":
860
+ self.__dict__[k] = v
861
+
862
+ def connect(self, *arg, **kw):
863
+ creator_fn = kw.pop("async_creator_fn", self.oracledb.connect_async)
864
+ return await_(
865
+ AsyncAdapt_oracledb_connection.create(self, creator_fn(*arg, **kw))
866
+ )
867
+
868
+
869
+ class OracleExecutionContextAsync_oracledb(OracleExecutionContext_oracledb):
870
+ # restore default create cursor
871
+ create_cursor = default.DefaultExecutionContext.create_cursor
872
+
873
+ def create_default_cursor(self):
874
+ # copy of OracleExecutionContext_cx_oracle.create_cursor
875
+ c = self._dbapi_connection.cursor()
876
+ if self.dialect.arraysize:
877
+ c.arraysize = self.dialect.arraysize
878
+
879
+ return c
880
+
881
+ def create_server_side_cursor(self):
882
+ c = self._dbapi_connection.ss_cursor()
883
+ if self.dialect.arraysize:
884
+ c.arraysize = self.dialect.arraysize
885
+
886
+ return c
887
+
888
+
889
+ class OracleDialectAsync_oracledb(OracleDialect_oracledb):
890
+ is_async = True
891
+ supports_server_side_cursors = True
892
+ supports_statement_cache = True
893
+ execution_ctx_cls = OracleExecutionContextAsync_oracledb
894
+
895
+ _min_version = (2,)
896
+
897
+ # thick_mode mode is not supported by asyncio, oracledb will raise
898
+ @classmethod
899
+ def import_dbapi(cls):
900
+ import oracledb
901
+
902
+ return OracledbAdaptDBAPI(oracledb)
903
+
904
+ def get_driver_connection(self, connection):
905
+ return connection._connection
906
+
907
+
908
+ dialect = OracleDialect_oracledb
909
+ dialect_async = OracleDialectAsync_oracledb