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,754 @@
1
+ # dialects/sqlite/pysqlite.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
+
8
+
9
+ r"""
10
+ .. dialect:: sqlite+pysqlite
11
+ :name: pysqlite
12
+ :dbapi: sqlite3
13
+ :connectstring: sqlite+pysqlite:///file_path
14
+ :url: https://docs.python.org/library/sqlite3.html
15
+
16
+ Note that ``pysqlite`` is the same driver as the ``sqlite3``
17
+ module included with the Python distribution.
18
+
19
+ Driver
20
+ ------
21
+
22
+ The ``sqlite3`` Python DBAPI is standard on all modern Python versions;
23
+ for cPython and Pypy, no additional installation is necessary.
24
+
25
+
26
+ Connect Strings
27
+ ---------------
28
+
29
+ The file specification for the SQLite database is taken as the "database"
30
+ portion of the URL. Note that the format of a SQLAlchemy url is:
31
+
32
+ .. sourcecode:: text
33
+
34
+ driver://user:pass@host/database
35
+
36
+ This means that the actual filename to be used starts with the characters to
37
+ the **right** of the third slash. So connecting to a relative filepath
38
+ looks like::
39
+
40
+ # relative path
41
+ e = create_engine("sqlite:///path/to/database.db")
42
+
43
+ An absolute path, which is denoted by starting with a slash, means you
44
+ need **four** slashes::
45
+
46
+ # absolute path
47
+ e = create_engine("sqlite:////path/to/database.db")
48
+
49
+ To use a Windows path, regular drive specifications and backslashes can be
50
+ used. Double backslashes are probably needed::
51
+
52
+ # absolute path on Windows
53
+ e = create_engine("sqlite:///C:\\path\\to\\database.db")
54
+
55
+ To use sqlite ``:memory:`` database specify it as the filename using
56
+ ``sqlite:///:memory:``. It's also the default if no filepath is
57
+ present, specifying only ``sqlite://`` and nothing else::
58
+
59
+ # in-memory database (note three slashes)
60
+ e = create_engine("sqlite:///:memory:")
61
+ # also in-memory database
62
+ e2 = create_engine("sqlite://")
63
+
64
+ .. _pysqlite_uri_connections:
65
+
66
+ URI Connections
67
+ ^^^^^^^^^^^^^^^
68
+
69
+ Modern versions of SQLite support an alternative system of connecting using a
70
+ `driver level URI <https://www.sqlite.org/uri.html>`_, which has the advantage
71
+ that additional driver-level arguments can be passed including options such as
72
+ "read only". The Python sqlite3 driver supports this mode under modern Python
73
+ 3 versions. The SQLAlchemy pysqlite driver supports this mode of use by
74
+ specifying "uri=true" in the URL query string. The SQLite-level "URI" is kept
75
+ as the "database" portion of the SQLAlchemy url (that is, following a slash)::
76
+
77
+ e = create_engine("sqlite:///file:path/to/database?mode=ro&uri=true")
78
+
79
+ .. note:: The "uri=true" parameter must appear in the **query string**
80
+ of the URL. It will not currently work as expected if it is only
81
+ present in the :paramref:`_sa.create_engine.connect_args`
82
+ parameter dictionary.
83
+
84
+ The logic reconciles the simultaneous presence of SQLAlchemy's query string and
85
+ SQLite's query string by separating out the parameters that belong to the
86
+ Python sqlite3 driver vs. those that belong to the SQLite URI. This is
87
+ achieved through the use of a fixed list of parameters known to be accepted by
88
+ the Python side of the driver. For example, to include a URL that indicates
89
+ the Python sqlite3 "timeout" and "check_same_thread" parameters, along with the
90
+ SQLite "mode" and "nolock" parameters, they can all be passed together on the
91
+ query string::
92
+
93
+ e = create_engine(
94
+ "sqlite:///file:path/to/database?"
95
+ "check_same_thread=true&timeout=10&mode=ro&nolock=1&uri=true"
96
+ )
97
+
98
+ Above, the pysqlite / sqlite3 DBAPI would be passed arguments as::
99
+
100
+ sqlite3.connect(
101
+ "file:path/to/database?mode=ro&nolock=1",
102
+ check_same_thread=True,
103
+ timeout=10,
104
+ uri=True,
105
+ )
106
+
107
+ Regarding future parameters added to either the Python or native drivers. new
108
+ parameter names added to the SQLite URI scheme should be automatically
109
+ accommodated by this scheme. New parameter names added to the Python driver
110
+ side can be accommodated by specifying them in the
111
+ :paramref:`_sa.create_engine.connect_args` dictionary,
112
+ until dialect support is
113
+ added by SQLAlchemy. For the less likely case that the native SQLite driver
114
+ adds a new parameter name that overlaps with one of the existing, known Python
115
+ driver parameters (such as "timeout" perhaps), SQLAlchemy's dialect would
116
+ require adjustment for the URL scheme to continue to support this.
117
+
118
+ As is always the case for all SQLAlchemy dialects, the entire "URL" process
119
+ can be bypassed in :func:`_sa.create_engine` through the use of the
120
+ :paramref:`_sa.create_engine.creator`
121
+ parameter which allows for a custom callable
122
+ that creates a Python sqlite3 driver level connection directly.
123
+
124
+ .. seealso::
125
+
126
+ `Uniform Resource Identifiers <https://www.sqlite.org/uri.html>`_ - in
127
+ the SQLite documentation
128
+
129
+ .. _pysqlite_regexp:
130
+
131
+ Regular Expression Support
132
+ ---------------------------
133
+
134
+ .. versionadded:: 1.4
135
+
136
+ Support for the :meth:`_sql.ColumnOperators.regexp_match` operator is provided
137
+ using Python's re.search_ function. SQLite itself does not include a working
138
+ regular expression operator; instead, it includes a non-implemented placeholder
139
+ operator ``REGEXP`` that calls a user-defined function that must be provided.
140
+
141
+ SQLAlchemy's implementation makes use of the pysqlite create_function_ hook
142
+ as follows::
143
+
144
+
145
+ def regexp(a, b):
146
+ return re.search(a, b) is not None
147
+
148
+
149
+ sqlite_connection.create_function(
150
+ "regexp",
151
+ 2,
152
+ regexp,
153
+ )
154
+
155
+ There is currently no support for regular expression flags as a separate
156
+ argument, as these are not supported by SQLite's REGEXP operator, however these
157
+ may be included inline within the regular expression string. See `Python regular expressions`_ for
158
+ details.
159
+
160
+ .. seealso::
161
+
162
+ `Python regular expressions`_: Documentation for Python's regular expression syntax.
163
+
164
+ .. _create_function: https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function
165
+
166
+ .. _re.search: https://docs.python.org/3/library/re.html#re.search
167
+
168
+ .. _Python regular expressions: https://docs.python.org/3/library/re.html#re.search
169
+
170
+
171
+
172
+ Compatibility with sqlite3 "native" date and datetime types
173
+ -----------------------------------------------------------
174
+
175
+ The pysqlite driver includes the sqlite3.PARSE_DECLTYPES and
176
+ sqlite3.PARSE_COLNAMES options, which have the effect of any column
177
+ or expression explicitly cast as "date" or "timestamp" will be converted
178
+ to a Python date or datetime object. The date and datetime types provided
179
+ with the pysqlite dialect are not currently compatible with these options,
180
+ since they render the ISO date/datetime including microseconds, which
181
+ pysqlite's driver does not. Additionally, SQLAlchemy does not at
182
+ this time automatically render the "cast" syntax required for the
183
+ freestanding functions "current_timestamp" and "current_date" to return
184
+ datetime/date types natively. Unfortunately, pysqlite
185
+ does not provide the standard DBAPI types in ``cursor.description``,
186
+ leaving SQLAlchemy with no way to detect these types on the fly
187
+ without expensive per-row type checks.
188
+
189
+ Keeping in mind that pysqlite's parsing option is not recommended,
190
+ nor should be necessary, for use with SQLAlchemy, usage of PARSE_DECLTYPES
191
+ can be forced if one configures "native_datetime=True" on create_engine()::
192
+
193
+ engine = create_engine(
194
+ "sqlite://",
195
+ connect_args={
196
+ "detect_types": sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
197
+ },
198
+ native_datetime=True,
199
+ )
200
+
201
+ With this flag enabled, the DATE and TIMESTAMP types (but note - not the
202
+ DATETIME or TIME types...confused yet ?) will not perform any bind parameter
203
+ or result processing. Execution of "func.current_date()" will return a string.
204
+ "func.current_timestamp()" is registered as returning a DATETIME type in
205
+ SQLAlchemy, so this function still receives SQLAlchemy-level result
206
+ processing.
207
+
208
+ .. _pysqlite_threading_pooling:
209
+
210
+ Threading/Pooling Behavior
211
+ ---------------------------
212
+
213
+ The ``sqlite3`` DBAPI by default prohibits the use of a particular connection
214
+ in a thread which is not the one in which it was created. As SQLite has
215
+ matured, it's behavior under multiple threads has improved, and even includes
216
+ options for memory only databases to be used in multiple threads.
217
+
218
+ The thread prohibition is known as "check same thread" and may be controlled
219
+ using the ``sqlite3`` parameter ``check_same_thread``, which will disable or
220
+ enable this check. SQLAlchemy's default behavior here is to set
221
+ ``check_same_thread`` to ``False`` automatically whenever a file-based database
222
+ is in use, to establish compatibility with the default pool class
223
+ :class:`.QueuePool`.
224
+
225
+ The SQLAlchemy ``pysqlite`` DBAPI establishes the connection pool differently
226
+ based on the kind of SQLite database that's requested:
227
+
228
+ * When a ``:memory:`` SQLite database is specified, the dialect by default
229
+ will use :class:`.SingletonThreadPool`. This pool maintains a single
230
+ connection per thread, so that all access to the engine within the current
231
+ thread use the same ``:memory:`` database - other threads would access a
232
+ different ``:memory:`` database. The ``check_same_thread`` parameter
233
+ defaults to ``True``.
234
+ * When a file-based database is specified, the dialect will use
235
+ :class:`.QueuePool` as the source of connections. at the same time,
236
+ the ``check_same_thread`` flag is set to False by default unless overridden.
237
+
238
+ .. versionchanged:: 2.0
239
+
240
+ SQLite file database engines now use :class:`.QueuePool` by default.
241
+ Previously, :class:`.NullPool` were used. The :class:`.NullPool` class
242
+ may be used by specifying it via the
243
+ :paramref:`_sa.create_engine.poolclass` parameter.
244
+
245
+ Disabling Connection Pooling for File Databases
246
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
247
+
248
+ Pooling may be disabled for a file based database by specifying the
249
+ :class:`.NullPool` implementation for the :func:`_sa.create_engine.poolclass`
250
+ parameter::
251
+
252
+ from sqlalchemy import NullPool
253
+
254
+ engine = create_engine("sqlite:///myfile.db", poolclass=NullPool)
255
+
256
+ It's been observed that the :class:`.NullPool` implementation incurs an
257
+ extremely small performance overhead for repeated checkouts due to the lack of
258
+ connection reuse implemented by :class:`.QueuePool`. However, it still
259
+ may be beneficial to use this class if the application is experiencing
260
+ issues with files being locked.
261
+
262
+ Using a Memory Database in Multiple Threads
263
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
264
+
265
+ To use a ``:memory:`` database in a multithreaded scenario, the same
266
+ connection object must be shared among threads, since the database exists
267
+ only within the scope of that connection. The
268
+ :class:`.StaticPool` implementation will maintain a single connection
269
+ globally, and the ``check_same_thread`` flag can be passed to Pysqlite
270
+ as ``False``::
271
+
272
+ from sqlalchemy.pool import StaticPool
273
+
274
+ engine = create_engine(
275
+ "sqlite://",
276
+ connect_args={"check_same_thread": False},
277
+ poolclass=StaticPool,
278
+ )
279
+
280
+ Note that using a ``:memory:`` database in multiple threads requires a recent
281
+ version of SQLite.
282
+
283
+ Using Temporary Tables with SQLite
284
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
285
+
286
+ Due to the way SQLite deals with temporary tables, if you wish to use a
287
+ temporary table in a file-based SQLite database across multiple checkouts
288
+ from the connection pool, such as when using an ORM :class:`.Session` where
289
+ the temporary table should continue to remain after :meth:`.Session.commit` or
290
+ :meth:`.Session.rollback` is called, a pool which maintains a single
291
+ connection must be used. Use :class:`.SingletonThreadPool` if the scope is
292
+ only needed within the current thread, or :class:`.StaticPool` is scope is
293
+ needed within multiple threads for this case::
294
+
295
+ # maintain the same connection per thread
296
+ from sqlalchemy.pool import SingletonThreadPool
297
+
298
+ engine = create_engine("sqlite:///mydb.db", poolclass=SingletonThreadPool)
299
+
300
+
301
+ # maintain the same connection across all threads
302
+ from sqlalchemy.pool import StaticPool
303
+
304
+ engine = create_engine("sqlite:///mydb.db", poolclass=StaticPool)
305
+
306
+ Note that :class:`.SingletonThreadPool` should be configured for the number
307
+ of threads that are to be used; beyond that number, connections will be
308
+ closed out in a non deterministic way.
309
+
310
+
311
+ Dealing with Mixed String / Binary Columns
312
+ ------------------------------------------------------
313
+
314
+ The SQLite database is weakly typed, and as such it is possible when using
315
+ binary values, which in Python are represented as ``b'some string'``, that a
316
+ particular SQLite database can have data values within different rows where
317
+ some of them will be returned as a ``b''`` value by the Pysqlite driver, and
318
+ others will be returned as Python strings, e.g. ``''`` values. This situation
319
+ is not known to occur if the SQLAlchemy :class:`.LargeBinary` datatype is used
320
+ consistently, however if a particular SQLite database has data that was
321
+ inserted using the Pysqlite driver directly, or when using the SQLAlchemy
322
+ :class:`.String` type which was later changed to :class:`.LargeBinary`, the
323
+ table will not be consistently readable because SQLAlchemy's
324
+ :class:`.LargeBinary` datatype does not handle strings so it has no way of
325
+ "encoding" a value that is in string format.
326
+
327
+ To deal with a SQLite table that has mixed string / binary data in the
328
+ same column, use a custom type that will check each row individually::
329
+
330
+ from sqlalchemy import String
331
+ from sqlalchemy import TypeDecorator
332
+
333
+
334
+ class MixedBinary(TypeDecorator):
335
+ impl = String
336
+ cache_ok = True
337
+
338
+ def process_result_value(self, value, dialect):
339
+ if isinstance(value, str):
340
+ value = bytes(value, "utf-8")
341
+ elif value is not None:
342
+ value = bytes(value)
343
+
344
+ return value
345
+
346
+ Then use the above ``MixedBinary`` datatype in the place where
347
+ :class:`.LargeBinary` would normally be used.
348
+
349
+ .. _pysqlite_serializable:
350
+
351
+ Serializable isolation / Savepoints / Transactional DDL
352
+ -------------------------------------------------------
353
+
354
+ A newly revised version of this important section is now available
355
+ at the top level of the SQLAlchemy SQLite documentation, in the section
356
+ :ref:`sqlite_transactions`.
357
+
358
+
359
+ .. _pysqlite_udfs:
360
+
361
+ User-Defined Functions
362
+ ----------------------
363
+
364
+ pysqlite supports a `create_function() <https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function>`_
365
+ method that allows us to create our own user-defined functions (UDFs) in Python and use them directly in SQLite queries.
366
+ These functions are registered with a specific DBAPI Connection.
367
+
368
+ SQLAlchemy uses connection pooling with file-based SQLite databases, so we need to ensure that the UDF is attached to the
369
+ connection when it is created. That is accomplished with an event listener::
370
+
371
+ from sqlalchemy import create_engine
372
+ from sqlalchemy import event
373
+ from sqlalchemy import text
374
+
375
+
376
+ def udf():
377
+ return "udf-ok"
378
+
379
+
380
+ engine = create_engine("sqlite:///./db_file")
381
+
382
+
383
+ @event.listens_for(engine, "connect")
384
+ def connect(conn, rec):
385
+ conn.create_function("udf", 0, udf)
386
+
387
+
388
+ for i in range(5):
389
+ with engine.connect() as conn:
390
+ print(conn.scalar(text("SELECT UDF()")))
391
+
392
+ """ # noqa
393
+ from __future__ import annotations
394
+
395
+ import math
396
+ import os
397
+ import re
398
+ from typing import Any
399
+ from typing import Callable
400
+ from typing import cast
401
+ from typing import Optional
402
+ from typing import Pattern
403
+ from typing import TYPE_CHECKING
404
+ from typing import TypeVar
405
+ from typing import Union
406
+
407
+ from .base import DATE
408
+ from .base import DATETIME
409
+ from .base import SQLiteDialect
410
+ from ... import exc
411
+ from ... import pool
412
+ from ... import types as sqltypes
413
+ from ... import util
414
+ from ...util.typing import Self
415
+
416
+ if TYPE_CHECKING:
417
+ from ...engine.interfaces import ConnectArgsType
418
+ from ...engine.interfaces import DBAPIConnection
419
+ from ...engine.interfaces import DBAPICursor
420
+ from ...engine.interfaces import DBAPIModule
421
+ from ...engine.interfaces import IsolationLevel
422
+ from ...engine.interfaces import VersionInfoType
423
+ from ...engine.url import URL
424
+ from ...pool.base import PoolProxiedConnection
425
+ from ...sql.type_api import _BindProcessorType
426
+ from ...sql.type_api import _ResultProcessorType
427
+
428
+
429
+ class _SQLite_pysqliteTimeStamp(DATETIME):
430
+ def bind_processor( # type: ignore[override]
431
+ self, dialect: SQLiteDialect
432
+ ) -> Optional[_BindProcessorType[Any]]:
433
+ if dialect.native_datetime:
434
+ return None
435
+ else:
436
+ return DATETIME.bind_processor(self, dialect)
437
+
438
+ def result_processor( # type: ignore[override]
439
+ self, dialect: SQLiteDialect, coltype: object
440
+ ) -> Optional[_ResultProcessorType[Any]]:
441
+ if dialect.native_datetime:
442
+ return None
443
+ else:
444
+ return DATETIME.result_processor(self, dialect, coltype)
445
+
446
+
447
+ class _SQLite_pysqliteDate(DATE):
448
+ def bind_processor( # type: ignore[override]
449
+ self, dialect: SQLiteDialect
450
+ ) -> Optional[_BindProcessorType[Any]]:
451
+ if dialect.native_datetime:
452
+ return None
453
+ else:
454
+ return DATE.bind_processor(self, dialect)
455
+
456
+ def result_processor( # type: ignore[override]
457
+ self, dialect: SQLiteDialect, coltype: object
458
+ ) -> Optional[_ResultProcessorType[Any]]:
459
+ if dialect.native_datetime:
460
+ return None
461
+ else:
462
+ return DATE.result_processor(self, dialect, coltype)
463
+
464
+
465
+ class SQLiteDialect_pysqlite(SQLiteDialect):
466
+ default_paramstyle = "qmark"
467
+ supports_statement_cache = True
468
+ returns_native_bytes = True
469
+
470
+ colspecs = util.update_copy(
471
+ SQLiteDialect.colspecs,
472
+ {
473
+ sqltypes.Date: _SQLite_pysqliteDate,
474
+ sqltypes.TIMESTAMP: _SQLite_pysqliteTimeStamp,
475
+ },
476
+ )
477
+
478
+ description_encoding = None
479
+
480
+ driver = "pysqlite"
481
+
482
+ @classmethod
483
+ def import_dbapi(cls) -> DBAPIModule:
484
+ from sqlite3 import dbapi2 as sqlite
485
+
486
+ return cast("DBAPIModule", sqlite)
487
+
488
+ @classmethod
489
+ def _is_url_file_db(cls, url: URL) -> bool:
490
+ if (url.database and url.database != ":memory:") and (
491
+ url.query.get("mode", None) != "memory"
492
+ ):
493
+ return True
494
+ else:
495
+ return False
496
+
497
+ @classmethod
498
+ def get_pool_class(cls, url: URL) -> type[pool.Pool]:
499
+ if cls._is_url_file_db(url):
500
+ return pool.QueuePool
501
+ else:
502
+ return pool.SingletonThreadPool
503
+
504
+ def _get_server_version_info(self, connection: Any) -> VersionInfoType:
505
+ return self.dbapi.sqlite_version_info # type: ignore
506
+
507
+ _isolation_lookup = SQLiteDialect._isolation_lookup.union(
508
+ {
509
+ "AUTOCOMMIT": None,
510
+ }
511
+ )
512
+
513
+ def set_isolation_level(
514
+ self, dbapi_connection: DBAPIConnection, level: IsolationLevel
515
+ ) -> None:
516
+ if level == "AUTOCOMMIT":
517
+ dbapi_connection.isolation_level = None
518
+ else:
519
+ dbapi_connection.isolation_level = ""
520
+ return super().set_isolation_level(dbapi_connection, level)
521
+
522
+ def detect_autocommit_setting(self, dbapi_conn: DBAPIConnection) -> bool:
523
+ return dbapi_conn.isolation_level is None
524
+
525
+ def on_connect(self) -> Callable[[DBAPIConnection], None]:
526
+ def regexp(a: str, b: Optional[str]) -> Optional[bool]:
527
+ if b is None:
528
+ return None
529
+ return re.search(a, b) is not None
530
+
531
+ if self._get_server_version_info(None) >= (3, 9):
532
+ # sqlite must be greater than 3.8.3 for deterministic=True
533
+ # https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function
534
+ # the check is more conservative since there were still issues
535
+ # with following 3.8 sqlite versions
536
+ create_func_kw = {"deterministic": True}
537
+ else:
538
+ create_func_kw = {}
539
+
540
+ def set_regexp(dbapi_connection: DBAPIConnection) -> None:
541
+ dbapi_connection.create_function(
542
+ "regexp", 2, regexp, **create_func_kw
543
+ )
544
+
545
+ def floor_func(dbapi_connection: DBAPIConnection) -> None:
546
+ # NOTE: floor is optionally present in sqlite 3.35+ , however
547
+ # as it is normally non-present we deliver floor() unconditionally
548
+ # for now.
549
+ # https://www.sqlite.org/lang_mathfunc.html
550
+ dbapi_connection.create_function(
551
+ "floor", 1, math.floor, **create_func_kw
552
+ )
553
+
554
+ fns = [set_regexp, floor_func]
555
+
556
+ def connect(conn: DBAPIConnection) -> None:
557
+ for fn in fns:
558
+ fn(conn)
559
+
560
+ return connect
561
+
562
+ def create_connect_args(self, url: URL) -> ConnectArgsType:
563
+ if url.username or url.password or url.host or url.port:
564
+ raise exc.ArgumentError(
565
+ "Invalid SQLite URL: %s\n"
566
+ "Valid SQLite URL forms are:\n"
567
+ " sqlite:///:memory: (or, sqlite://)\n"
568
+ " sqlite:///relative/path/to/file.db\n"
569
+ " sqlite:////absolute/path/to/file.db" % (url,)
570
+ )
571
+
572
+ # theoretically, this list can be augmented, at least as far as
573
+ # parameter names accepted by sqlite3/pysqlite, using
574
+ # inspect.getfullargspec(). for the moment this seems like overkill
575
+ # as these parameters don't change very often, and as always,
576
+ # parameters passed to connect_args will always go to the
577
+ # sqlite3/pysqlite driver.
578
+ pysqlite_args = [
579
+ ("uri", bool),
580
+ ("timeout", float),
581
+ ("isolation_level", str),
582
+ ("detect_types", int),
583
+ ("check_same_thread", bool),
584
+ ("cached_statements", int),
585
+ ]
586
+ opts = url.query
587
+ pysqlite_opts: dict[str, Any] = {}
588
+ for key, type_ in pysqlite_args:
589
+ util.coerce_kw_type(opts, key, type_, dest=pysqlite_opts)
590
+
591
+ if pysqlite_opts.get("uri", False):
592
+ uri_opts = dict(opts)
593
+ # here, we are actually separating the parameters that go to
594
+ # sqlite3/pysqlite vs. those that go the SQLite URI. What if
595
+ # two names conflict? again, this seems to be not the case right
596
+ # now, and in the case that new names are added to
597
+ # either side which overlap, again the sqlite3/pysqlite parameters
598
+ # can be passed through connect_args instead of in the URL.
599
+ # If SQLite native URIs add a parameter like "timeout" that
600
+ # we already have listed here for the python driver, then we need
601
+ # to adjust for that here.
602
+ for key, type_ in pysqlite_args:
603
+ uri_opts.pop(key, None)
604
+ filename: str = url.database # type: ignore[assignment]
605
+ if uri_opts:
606
+ # sorting of keys is for unit test support
607
+ filename += "?" + (
608
+ "&".join(
609
+ "%s=%s" % (key, uri_opts[key])
610
+ for key in sorted(uri_opts)
611
+ )
612
+ )
613
+ else:
614
+ filename = url.database or ":memory:"
615
+ if filename != ":memory:":
616
+ filename = os.path.abspath(filename)
617
+
618
+ pysqlite_opts.setdefault(
619
+ "check_same_thread", not self._is_url_file_db(url)
620
+ )
621
+
622
+ return ([filename], pysqlite_opts)
623
+
624
+ def is_disconnect(
625
+ self,
626
+ e: DBAPIModule.Error,
627
+ connection: Optional[Union[PoolProxiedConnection, DBAPIConnection]],
628
+ cursor: Optional[DBAPICursor],
629
+ ) -> bool:
630
+ self.dbapi = cast("DBAPIModule", self.dbapi)
631
+ return isinstance(
632
+ e, self.dbapi.ProgrammingError
633
+ ) and "Cannot operate on a closed database." in str(e)
634
+
635
+
636
+ dialect = SQLiteDialect_pysqlite
637
+
638
+
639
+ class _SQLiteDialect_pysqlite_numeric(SQLiteDialect_pysqlite):
640
+ """numeric dialect for testing only
641
+
642
+ internal use only. This dialect is **NOT** supported by SQLAlchemy
643
+ and may change at any time.
644
+
645
+ """
646
+
647
+ supports_statement_cache = True
648
+ default_paramstyle = "numeric"
649
+ driver = "pysqlite_numeric"
650
+
651
+ _first_bind = ":1"
652
+ _not_in_statement_regexp: Optional[Pattern[str]] = None
653
+
654
+ def __init__(self, *arg: Any, **kw: Any) -> None:
655
+ kw.setdefault("paramstyle", "numeric")
656
+ super().__init__(*arg, **kw)
657
+
658
+ def create_connect_args(self, url: URL) -> ConnectArgsType:
659
+ arg, opts = super().create_connect_args(url)
660
+ opts["factory"] = self._fix_sqlite_issue_99953()
661
+ return arg, opts
662
+
663
+ def _fix_sqlite_issue_99953(self) -> Any:
664
+ import sqlite3
665
+
666
+ first_bind = self._first_bind
667
+ if self._not_in_statement_regexp:
668
+ nis = self._not_in_statement_regexp
669
+
670
+ def _test_sql(sql: str) -> None:
671
+ m = nis.search(sql)
672
+ assert not m, f"Found {nis.pattern!r} in {sql!r}"
673
+
674
+ else:
675
+
676
+ def _test_sql(sql: str) -> None:
677
+ pass
678
+
679
+ def _numeric_param_as_dict(
680
+ parameters: Any,
681
+ ) -> Union[dict[str, Any], tuple[Any, ...]]:
682
+ if parameters:
683
+ assert isinstance(parameters, tuple)
684
+ return {
685
+ str(idx): value for idx, value in enumerate(parameters, 1)
686
+ }
687
+ else:
688
+ return ()
689
+
690
+ class SQLiteFix99953Cursor(sqlite3.Cursor):
691
+ def execute(self, sql: str, parameters: Any = ()) -> Self:
692
+ _test_sql(sql)
693
+ if first_bind in sql:
694
+ parameters = _numeric_param_as_dict(parameters)
695
+ return super().execute(sql, parameters)
696
+
697
+ def executemany(self, sql: str, parameters: Any) -> Self:
698
+ _test_sql(sql)
699
+ if first_bind in sql:
700
+ parameters = [
701
+ _numeric_param_as_dict(p) for p in parameters
702
+ ]
703
+ return super().executemany(sql, parameters)
704
+
705
+ class SQLiteFix99953Connection(sqlite3.Connection):
706
+ _CursorT = TypeVar("_CursorT", bound=sqlite3.Cursor)
707
+
708
+ def cursor(
709
+ self,
710
+ factory: Optional[
711
+ Callable[[sqlite3.Connection], _CursorT]
712
+ ] = None,
713
+ ) -> _CursorT:
714
+ if factory is None:
715
+ factory = SQLiteFix99953Cursor # type: ignore[assignment]
716
+ return super().cursor(factory=factory) # type: ignore[return-value] # noqa[E501]
717
+
718
+ def execute(
719
+ self, sql: str, parameters: Any = ()
720
+ ) -> sqlite3.Cursor:
721
+ _test_sql(sql)
722
+ if first_bind in sql:
723
+ parameters = _numeric_param_as_dict(parameters)
724
+ return super().execute(sql, parameters)
725
+
726
+ def executemany(self, sql: str, parameters: Any) -> sqlite3.Cursor:
727
+ _test_sql(sql)
728
+ if first_bind in sql:
729
+ parameters = [
730
+ _numeric_param_as_dict(p) for p in parameters
731
+ ]
732
+ return super().executemany(sql, parameters)
733
+
734
+ return SQLiteFix99953Connection
735
+
736
+
737
+ class _SQLiteDialect_pysqlite_dollar(_SQLiteDialect_pysqlite_numeric):
738
+ """numeric dialect that uses $ for testing only
739
+
740
+ internal use only. This dialect is **NOT** supported by SQLAlchemy
741
+ and may change at any time.
742
+
743
+ """
744
+
745
+ supports_statement_cache = True
746
+ default_paramstyle = "numeric_dollar"
747
+ driver = "pysqlite_dollar"
748
+
749
+ _first_bind = "$1"
750
+ _not_in_statement_regexp = re.compile(r"[^\d]:\d+")
751
+
752
+ def __init__(self, *arg: Any, **kw: Any) -> None:
753
+ kw.setdefault("paramstyle", "numeric_dollar")
754
+ super().__init__(*arg, **kw)