pysqlite.py 25 KB

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