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,860 @@
1
+ # dialects/postgresql/psycopg2.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"""
10
+ .. dialect:: postgresql+psycopg2
11
+ :name: psycopg2
12
+ :dbapi: psycopg2
13
+ :connectstring: postgresql+psycopg2://user:password@host:port/dbname[?key=value&key=value...]
14
+ :url: https://pypi.org/project/psycopg2/
15
+
16
+ .. _psycopg2_toplevel:
17
+
18
+ psycopg2 Connect Arguments
19
+ --------------------------
20
+
21
+ Keyword arguments that are specific to the SQLAlchemy psycopg2 dialect
22
+ may be passed to :func:`_sa.create_engine()`, and include the following:
23
+
24
+
25
+ * ``isolation_level``: This option, available for all PostgreSQL dialects,
26
+ includes the ``AUTOCOMMIT`` isolation level when using the psycopg2
27
+ dialect. This option sets the **default** isolation level for the
28
+ connection that is set immediately upon connection to the database before
29
+ the connection is pooled. This option is generally superseded by the more
30
+ modern :paramref:`_engine.Connection.execution_options.isolation_level`
31
+ execution option, detailed at :ref:`dbapi_autocommit`.
32
+
33
+ .. seealso::
34
+
35
+ :ref:`psycopg2_isolation_level`
36
+
37
+ :ref:`dbapi_autocommit`
38
+
39
+
40
+ * ``client_encoding``: sets the client encoding in a libpq-agnostic way,
41
+ using psycopg2's ``set_client_encoding()`` method.
42
+
43
+ .. seealso::
44
+
45
+ :ref:`psycopg2_unicode`
46
+
47
+
48
+ * ``executemany_mode``, ``executemany_batch_page_size``,
49
+ ``executemany_values_page_size``: Allows use of psycopg2
50
+ extensions for optimizing "executemany"-style queries. See the referenced
51
+ section below for details.
52
+
53
+ .. seealso::
54
+
55
+ :ref:`psycopg2_executemany_mode`
56
+
57
+ .. tip::
58
+
59
+ The above keyword arguments are **dialect** keyword arguments, meaning
60
+ that they are passed as explicit keyword arguments to :func:`_sa.create_engine()`::
61
+
62
+ engine = create_engine(
63
+ "postgresql+psycopg2://scott:tiger@localhost/test",
64
+ isolation_level="SERIALIZABLE",
65
+ )
66
+
67
+ These should not be confused with **DBAPI** connect arguments, which
68
+ are passed as part of the :paramref:`_sa.create_engine.connect_args`
69
+ dictionary and/or are passed in the URL query string, as detailed in
70
+ the section :ref:`custom_dbapi_args`.
71
+
72
+ .. _psycopg2_ssl:
73
+
74
+ SSL Connections
75
+ ---------------
76
+
77
+ The psycopg2 module has a connection argument named ``sslmode`` for
78
+ controlling its behavior regarding secure (SSL) connections. The default is
79
+ ``sslmode=prefer``; it will attempt an SSL connection and if that fails it
80
+ will fall back to an unencrypted connection. ``sslmode=require`` may be used
81
+ to ensure that only secure connections are established. Consult the
82
+ psycopg2 / libpq documentation for further options that are available.
83
+
84
+ Note that ``sslmode`` is specific to psycopg2 so it is included in the
85
+ connection URI::
86
+
87
+ engine = sa.create_engine(
88
+ "postgresql+psycopg2://scott:tiger@192.168.0.199:5432/test?sslmode=require"
89
+ )
90
+
91
+ Unix Domain Connections
92
+ ------------------------
93
+
94
+ psycopg2 supports connecting via Unix domain connections. When the ``host``
95
+ portion of the URL is omitted, SQLAlchemy passes ``None`` to psycopg2,
96
+ which specifies Unix-domain communication rather than TCP/IP communication::
97
+
98
+ create_engine("postgresql+psycopg2://user:password@/dbname")
99
+
100
+ By default, the socket file used is to connect to a Unix-domain socket
101
+ in ``/tmp``, or whatever socket directory was specified when PostgreSQL
102
+ was built. This value can be overridden by passing a pathname to psycopg2,
103
+ using ``host`` as an additional keyword argument::
104
+
105
+ create_engine(
106
+ "postgresql+psycopg2://user:password@/dbname?host=/var/lib/postgresql"
107
+ )
108
+
109
+ .. warning:: The format accepted here allows for a hostname in the main URL
110
+ in addition to the "host" query string argument. **When using this URL
111
+ format, the initial host is silently ignored**. That is, this URL::
112
+
113
+ engine = create_engine(
114
+ "postgresql+psycopg2://user:password@myhost1/dbname?host=myhost2"
115
+ )
116
+
117
+ Above, the hostname ``myhost1`` is **silently ignored and discarded.** The
118
+ host which is connected is the ``myhost2`` host.
119
+
120
+ This is to maintain some degree of compatibility with PostgreSQL's own URL
121
+ format which has been tested to behave the same way and for which tools like
122
+ PifPaf hardcode two hostnames.
123
+
124
+ .. seealso::
125
+
126
+ `PQconnectdbParams \
127
+ <https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS>`_
128
+
129
+ .. _psycopg2_multi_host:
130
+
131
+ Specifying multiple fallback hosts
132
+ -----------------------------------
133
+
134
+ psycopg2 supports multiple connection points in the connection string.
135
+ When the ``host`` parameter is used multiple times in the query section of
136
+ the URL, SQLAlchemy will create a single string of the host and port
137
+ information provided to make the connections. Tokens may consist of
138
+ ``host::port`` or just ``host``; in the latter case, the default port
139
+ is selected by libpq. In the example below, three host connections
140
+ are specified, for ``HostA::PortA``, ``HostB`` connecting to the default port,
141
+ and ``HostC::PortC``::
142
+
143
+ create_engine(
144
+ "postgresql+psycopg2://user:password@/dbname?host=HostA:PortA&host=HostB&host=HostC:PortC"
145
+ )
146
+
147
+ As an alternative, libpq query string format also may be used; this specifies
148
+ ``host`` and ``port`` as single query string arguments with comma-separated
149
+ lists - the default port can be chosen by indicating an empty value
150
+ in the comma separated list::
151
+
152
+ create_engine(
153
+ "postgresql+psycopg2://user:password@/dbname?host=HostA,HostB,HostC&port=PortA,,PortC"
154
+ )
155
+
156
+ With either URL style, connections to each host is attempted based on a
157
+ configurable strategy, which may be configured using the libpq
158
+ ``target_session_attrs`` parameter. Per libpq this defaults to ``any``
159
+ which indicates a connection to each host is then attempted until a connection is successful.
160
+ Other strategies include ``primary``, ``prefer-standby``, etc. The complete
161
+ list is documented by PostgreSQL at
162
+ `libpq connection strings <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING>`_.
163
+
164
+ For example, to indicate two hosts using the ``primary`` strategy::
165
+
166
+ create_engine(
167
+ "postgresql+psycopg2://user:password@/dbname?host=HostA:PortA&host=HostB&host=HostC:PortC&target_session_attrs=primary"
168
+ )
169
+
170
+ .. versionchanged:: 1.4.40 Port specification in psycopg2 multiple host format
171
+ is repaired, previously ports were not correctly interpreted in this context.
172
+ libpq comma-separated format is also now supported.
173
+
174
+ .. seealso::
175
+
176
+ `libpq connection strings <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING>`_ - please refer
177
+ to this section in the libpq documentation for complete background on multiple host support.
178
+
179
+
180
+ Empty DSN Connections / Environment Variable Connections
181
+ ---------------------------------------------------------
182
+
183
+ The psycopg2 DBAPI can connect to PostgreSQL by passing an empty DSN to the
184
+ libpq client library, which by default indicates to connect to a localhost
185
+ PostgreSQL database that is open for "trust" connections. This behavior can be
186
+ further tailored using a particular set of environment variables which are
187
+ prefixed with ``PG_...``, which are consumed by ``libpq`` to take the place of
188
+ any or all elements of the connection string.
189
+
190
+ For this form, the URL can be passed without any elements other than the
191
+ initial scheme::
192
+
193
+ engine = create_engine("postgresql+psycopg2://")
194
+
195
+ In the above form, a blank "dsn" string is passed to the ``psycopg2.connect()``
196
+ function which in turn represents an empty DSN passed to libpq.
197
+
198
+ .. seealso::
199
+
200
+ `Environment Variables\
201
+ <https://www.postgresql.org/docs/current/libpq-envars.html>`_ -
202
+ PostgreSQL documentation on how to use ``PG_...``
203
+ environment variables for connections.
204
+
205
+ .. _psycopg2_execution_options:
206
+
207
+ Per-Statement/Connection Execution Options
208
+ -------------------------------------------
209
+
210
+ The following DBAPI-specific options are respected when used with
211
+ :meth:`_engine.Connection.execution_options`,
212
+ :meth:`.Executable.execution_options`,
213
+ :meth:`_query.Query.execution_options`,
214
+ in addition to those not specific to DBAPIs:
215
+
216
+ * ``isolation_level`` - Set the transaction isolation level for the lifespan
217
+ of a :class:`_engine.Connection` (can only be set on a connection,
218
+ not a statement
219
+ or query). See :ref:`psycopg2_isolation_level`.
220
+
221
+ * ``stream_results`` - Enable or disable usage of psycopg2 server side
222
+ cursors - this feature makes use of "named" cursors in combination with
223
+ special result handling methods so that result rows are not fully buffered.
224
+ Defaults to False, meaning cursors are buffered by default.
225
+
226
+ * ``max_row_buffer`` - when using ``stream_results``, an integer value that
227
+ specifies the maximum number of rows to buffer at a time. This is
228
+ interpreted by the :class:`.BufferedRowCursorResult`, and if omitted the
229
+ buffer will grow to ultimately store 1000 rows at a time.
230
+
231
+ .. versionchanged:: 1.4 The ``max_row_buffer`` size can now be greater than
232
+ 1000, and the buffer will grow to that size.
233
+
234
+ .. _psycopg2_batch_mode:
235
+
236
+ .. _psycopg2_executemany_mode:
237
+
238
+ Psycopg2 Fast Execution Helpers
239
+ -------------------------------
240
+
241
+ Modern versions of psycopg2 include a feature known as
242
+ `Fast Execution Helpers \
243
+ <https://www.psycopg.org/docs/extras.html#fast-execution-helpers>`_, which
244
+ have been shown in benchmarking to improve psycopg2's executemany()
245
+ performance, primarily with INSERT statements, by at least
246
+ an order of magnitude.
247
+
248
+ SQLAlchemy implements a native form of the "insert many values"
249
+ handler that will rewrite a single-row INSERT statement to accommodate for
250
+ many values at once within an extended VALUES clause; this handler is
251
+ equivalent to psycopg2's ``execute_values()`` handler; an overview of this
252
+ feature and its configuration are at :ref:`engine_insertmanyvalues`.
253
+
254
+ .. versionadded:: 2.0 Replaced psycopg2's ``execute_values()`` fast execution
255
+ helper with a native SQLAlchemy mechanism known as
256
+ :ref:`insertmanyvalues <engine_insertmanyvalues>`.
257
+
258
+ The psycopg2 dialect retains the ability to use the psycopg2-specific
259
+ ``execute_batch()`` feature, although it is not expected that this is a widely
260
+ used feature. The use of this extension may be enabled using the
261
+ ``executemany_mode`` flag which may be passed to :func:`_sa.create_engine`::
262
+
263
+ engine = create_engine(
264
+ "postgresql+psycopg2://scott:tiger@host/dbname",
265
+ executemany_mode="values_plus_batch",
266
+ )
267
+
268
+ Possible options for ``executemany_mode`` include:
269
+
270
+ * ``values_only`` - this is the default value. SQLAlchemy's native
271
+ :ref:`insertmanyvalues <engine_insertmanyvalues>` handler is used for qualifying
272
+ INSERT statements, assuming
273
+ :paramref:`_sa.create_engine.use_insertmanyvalues` is left at
274
+ its default value of ``True``. This handler rewrites simple
275
+ INSERT statements to include multiple VALUES clauses so that many
276
+ parameter sets can be inserted with one statement.
277
+
278
+ * ``'values_plus_batch'``- SQLAlchemy's native
279
+ :ref:`insertmanyvalues <engine_insertmanyvalues>` handler is used for qualifying
280
+ INSERT statements, assuming
281
+ :paramref:`_sa.create_engine.use_insertmanyvalues` is left at its default
282
+ value of ``True``. Then, psycopg2's ``execute_batch()`` handler is used for
283
+ qualifying UPDATE and DELETE statements when executed with multiple parameter
284
+ sets. When using this mode, the :attr:`_engine.CursorResult.rowcount`
285
+ attribute will not contain a value for executemany-style executions against
286
+ UPDATE and DELETE statements.
287
+
288
+ .. versionchanged:: 2.0 Removed the ``'batch'`` and ``'None'`` options
289
+ from psycopg2 ``executemany_mode``. Control over batching for INSERT
290
+ statements is now configured via the
291
+ :paramref:`_sa.create_engine.use_insertmanyvalues` engine-level parameter.
292
+
293
+ The term "qualifying statements" refers to the statement being executed
294
+ being a Core :func:`_expression.insert`, :func:`_expression.update`
295
+ or :func:`_expression.delete` construct, and **not** a plain textual SQL
296
+ string or one constructed using :func:`_expression.text`. It also may **not** be
297
+ a special "extension" statement such as an "ON CONFLICT" "upsert" statement.
298
+ When using the ORM, all insert/update/delete statements used by the ORM flush process
299
+ are qualifying.
300
+
301
+ The "page size" for the psycopg2 "batch" strategy can be affected
302
+ by using the ``executemany_batch_page_size`` parameter, which defaults to
303
+ 100.
304
+
305
+ For the "insertmanyvalues" feature, the page size can be controlled using the
306
+ :paramref:`_sa.create_engine.insertmanyvalues_page_size` parameter,
307
+ which defaults to 1000. An example of modifying both parameters
308
+ is below::
309
+
310
+ engine = create_engine(
311
+ "postgresql+psycopg2://scott:tiger@host/dbname",
312
+ executemany_mode="values_plus_batch",
313
+ insertmanyvalues_page_size=5000,
314
+ executemany_batch_page_size=500,
315
+ )
316
+
317
+ .. seealso::
318
+
319
+ :ref:`engine_insertmanyvalues` - background on "insertmanyvalues"
320
+
321
+ :ref:`tutorial_multiple_parameters` - General information on using the
322
+ :class:`_engine.Connection`
323
+ object to execute statements in such a way as to make
324
+ use of the DBAPI ``.executemany()`` method.
325
+
326
+
327
+ .. _psycopg2_unicode:
328
+
329
+ Unicode with Psycopg2
330
+ ----------------------
331
+
332
+ The psycopg2 DBAPI driver supports Unicode data transparently.
333
+
334
+ The client character encoding can be controlled for the psycopg2 dialect
335
+ in the following ways:
336
+
337
+ * For PostgreSQL 9.1 and above, the ``client_encoding`` parameter may be
338
+ passed in the database URL; this parameter is consumed by the underlying
339
+ ``libpq`` PostgreSQL client library::
340
+
341
+ engine = create_engine(
342
+ "postgresql+psycopg2://user:pass@host/dbname?client_encoding=utf8"
343
+ )
344
+
345
+ Alternatively, the above ``client_encoding`` value may be passed using
346
+ :paramref:`_sa.create_engine.connect_args` for programmatic establishment with
347
+ ``libpq``::
348
+
349
+ engine = create_engine(
350
+ "postgresql+psycopg2://user:pass@host/dbname",
351
+ connect_args={"client_encoding": "utf8"},
352
+ )
353
+
354
+ * For all PostgreSQL versions, psycopg2 supports a client-side encoding
355
+ value that will be passed to database connections when they are first
356
+ established. The SQLAlchemy psycopg2 dialect supports this using the
357
+ ``client_encoding`` parameter passed to :func:`_sa.create_engine`::
358
+
359
+ engine = create_engine(
360
+ "postgresql+psycopg2://user:pass@host/dbname", client_encoding="utf8"
361
+ )
362
+
363
+ .. tip:: The above ``client_encoding`` parameter admittedly is very similar
364
+ in appearance to usage of the parameter within the
365
+ :paramref:`_sa.create_engine.connect_args` dictionary; the difference
366
+ above is that the parameter is consumed by psycopg2 and is
367
+ passed to the database connection using ``SET client_encoding TO
368
+ 'utf8'``; in the previously mentioned style, the parameter is instead
369
+ passed through psycopg2 and consumed by the ``libpq`` library.
370
+
371
+ * A common way to set up client encoding with PostgreSQL databases is to
372
+ ensure it is configured within the server-side postgresql.conf file;
373
+ this is the recommended way to set encoding for a server that is
374
+ consistently of one encoding in all databases::
375
+
376
+ # postgresql.conf file
377
+
378
+ # client_encoding = sql_ascii # actually, defaults to database
379
+ # encoding
380
+ client_encoding = utf8
381
+
382
+ Transactions
383
+ ------------
384
+
385
+ The psycopg2 dialect fully supports SAVEPOINT and two-phase commit operations.
386
+
387
+ .. _psycopg2_isolation_level:
388
+
389
+ Psycopg2 Transaction Isolation Level
390
+ -------------------------------------
391
+
392
+ As discussed in :ref:`postgresql_isolation_level`,
393
+ all PostgreSQL dialects support setting of transaction isolation level
394
+ both via the ``isolation_level`` parameter passed to :func:`_sa.create_engine`
395
+ ,
396
+ as well as the ``isolation_level`` argument used by
397
+ :meth:`_engine.Connection.execution_options`. When using the psycopg2 dialect
398
+ , these
399
+ options make use of psycopg2's ``set_isolation_level()`` connection method,
400
+ rather than emitting a PostgreSQL directive; this is because psycopg2's
401
+ API-level setting is always emitted at the start of each transaction in any
402
+ case.
403
+
404
+ The psycopg2 dialect supports these constants for isolation level:
405
+
406
+ * ``READ COMMITTED``
407
+ * ``READ UNCOMMITTED``
408
+ * ``REPEATABLE READ``
409
+ * ``SERIALIZABLE``
410
+ * ``AUTOCOMMIT``
411
+
412
+ .. seealso::
413
+
414
+ :ref:`postgresql_isolation_level`
415
+
416
+ :ref:`pg8000_isolation_level`
417
+
418
+
419
+ NOTICE logging
420
+ ---------------
421
+
422
+ The psycopg2 dialect will log PostgreSQL NOTICE messages
423
+ via the ``sqlalchemy.dialects.postgresql`` logger. When this logger
424
+ is set to the ``logging.INFO`` level, notice messages will be logged::
425
+
426
+ import logging
427
+
428
+ logging.getLogger("sqlalchemy.dialects.postgresql").setLevel(logging.INFO)
429
+
430
+ Above, it is assumed that logging is configured externally. If this is not
431
+ the case, configuration such as ``logging.basicConfig()`` must be utilized::
432
+
433
+ import logging
434
+
435
+ logging.basicConfig() # log messages to stdout
436
+ logging.getLogger("sqlalchemy.dialects.postgresql").setLevel(logging.INFO)
437
+
438
+ .. seealso::
439
+
440
+ `Logging HOWTO <https://docs.python.org/3/howto/logging.html>`_ - on the python.org website
441
+
442
+ .. _psycopg2_hstore:
443
+
444
+ HSTORE type
445
+ ------------
446
+
447
+ The ``psycopg2`` DBAPI includes an extension to natively handle marshalling of
448
+ the HSTORE type. The SQLAlchemy psycopg2 dialect will enable this extension
449
+ by default when psycopg2 version 2.4 or greater is used, and
450
+ it is detected that the target database has the HSTORE type set up for use.
451
+ In other words, when the dialect makes the first
452
+ connection, a sequence like the following is performed:
453
+
454
+ 1. Request the available HSTORE oids using
455
+ ``psycopg2.extras.HstoreAdapter.get_oids()``.
456
+ If this function returns a list of HSTORE identifiers, we then determine
457
+ that the ``HSTORE`` extension is present.
458
+ This function is **skipped** if the version of psycopg2 installed is
459
+ less than version 2.4.
460
+
461
+ 2. If the ``use_native_hstore`` flag is at its default of ``True``, and
462
+ we've detected that ``HSTORE`` oids are available, the
463
+ ``psycopg2.extensions.register_hstore()`` extension is invoked for all
464
+ connections.
465
+
466
+ The ``register_hstore()`` extension has the effect of **all Python
467
+ dictionaries being accepted as parameters regardless of the type of target
468
+ column in SQL**. The dictionaries are converted by this extension into a
469
+ textual HSTORE expression. If this behavior is not desired, disable the
470
+ use of the hstore extension by setting ``use_native_hstore`` to ``False`` as
471
+ follows::
472
+
473
+ engine = create_engine(
474
+ "postgresql+psycopg2://scott:tiger@localhost/test",
475
+ use_native_hstore=False,
476
+ )
477
+
478
+ The ``HSTORE`` type is **still supported** when the
479
+ ``psycopg2.extensions.register_hstore()`` extension is not used. It merely
480
+ means that the coercion between Python dictionaries and the HSTORE
481
+ string format, on both the parameter side and the result side, will take
482
+ place within SQLAlchemy's own marshalling logic, and not that of ``psycopg2``
483
+ which may be more performant.
484
+
485
+ """ # noqa
486
+ from __future__ import annotations
487
+
488
+ import collections.abc as collections_abc
489
+ import logging
490
+ import re
491
+ from typing import cast
492
+
493
+ from . import ranges
494
+ from ._psycopg_common import _PGDialect_common_psycopg
495
+ from ._psycopg_common import _PGExecutionContext_common_psycopg
496
+ from .base import PGIdentifierPreparer
497
+ from .json import JSON
498
+ from .json import JSONB
499
+ from ... import types as sqltypes
500
+ from ... import util
501
+ from ...util import FastIntFlag
502
+ from ...util import parse_user_argument_for_enum
503
+
504
+ logger = logging.getLogger("sqlalchemy.dialects.postgresql")
505
+
506
+
507
+ class _PGJSON(JSON):
508
+ def result_processor(self, dialect, coltype):
509
+ return None
510
+
511
+
512
+ class _PGJSONB(JSONB):
513
+ def result_processor(self, dialect, coltype):
514
+ return None
515
+
516
+
517
+ class _Psycopg2Range(ranges.AbstractSingleRangeImpl):
518
+ _psycopg2_range_cls = "none"
519
+
520
+ def bind_processor(self, dialect):
521
+ psycopg2_Range = getattr(
522
+ cast(PGDialect_psycopg2, dialect)._psycopg2_extras,
523
+ self._psycopg2_range_cls,
524
+ )
525
+
526
+ def to_range(value):
527
+ if isinstance(value, ranges.Range):
528
+ value = psycopg2_Range(
529
+ value.lower, value.upper, value.bounds, value.empty
530
+ )
531
+ return value
532
+
533
+ return to_range
534
+
535
+ def result_processor(self, dialect, coltype):
536
+ def to_range(value):
537
+ if value is not None:
538
+ value = ranges.Range(
539
+ value._lower,
540
+ value._upper,
541
+ bounds=value._bounds if value._bounds else "[)",
542
+ empty=not value._bounds,
543
+ )
544
+ return value
545
+
546
+ return to_range
547
+
548
+
549
+ class _Psycopg2NumericRange(_Psycopg2Range):
550
+ _psycopg2_range_cls = "NumericRange"
551
+
552
+
553
+ class _Psycopg2DateRange(_Psycopg2Range):
554
+ _psycopg2_range_cls = "DateRange"
555
+
556
+
557
+ class _Psycopg2DateTimeRange(_Psycopg2Range):
558
+ _psycopg2_range_cls = "DateTimeRange"
559
+
560
+
561
+ class _Psycopg2DateTimeTZRange(_Psycopg2Range):
562
+ _psycopg2_range_cls = "DateTimeTZRange"
563
+
564
+
565
+ class PGExecutionContext_psycopg2(_PGExecutionContext_common_psycopg):
566
+ _psycopg2_fetched_rows = None
567
+
568
+ def post_exec(self):
569
+ self._log_notices(self.cursor)
570
+
571
+ def _log_notices(self, cursor):
572
+ # check also that notices is an iterable, after it's already
573
+ # established that we will be iterating through it. This is to get
574
+ # around test suites such as SQLAlchemy's using a Mock object for
575
+ # cursor
576
+ if not cursor.connection.notices or not isinstance(
577
+ cursor.connection.notices, collections_abc.Iterable
578
+ ):
579
+ return
580
+
581
+ for notice in cursor.connection.notices:
582
+ # NOTICE messages have a
583
+ # newline character at the end
584
+ logger.info(notice.rstrip())
585
+
586
+ cursor.connection.notices[:] = []
587
+
588
+
589
+ class PGIdentifierPreparer_psycopg2(PGIdentifierPreparer):
590
+ pass
591
+
592
+
593
+ class ExecutemanyMode(FastIntFlag):
594
+ EXECUTEMANY_VALUES = 0
595
+ EXECUTEMANY_VALUES_PLUS_BATCH = 1
596
+
597
+
598
+ (
599
+ EXECUTEMANY_VALUES,
600
+ EXECUTEMANY_VALUES_PLUS_BATCH,
601
+ ) = ExecutemanyMode.__members__.values()
602
+
603
+
604
+ class PGDialect_psycopg2(_PGDialect_common_psycopg):
605
+ driver = "psycopg2"
606
+
607
+ supports_statement_cache = True
608
+ supports_server_side_cursors = True
609
+
610
+ default_paramstyle = "pyformat"
611
+ # set to true based on psycopg2 version
612
+ supports_sane_multi_rowcount = False
613
+ execution_ctx_cls = PGExecutionContext_psycopg2
614
+ preparer = PGIdentifierPreparer_psycopg2
615
+ psycopg2_version = (0, 0)
616
+ use_insertmanyvalues_wo_returning = True
617
+
618
+ returns_native_bytes = False
619
+
620
+ _has_native_hstore = True
621
+
622
+ colspecs = util.update_copy(
623
+ _PGDialect_common_psycopg.colspecs,
624
+ {
625
+ JSON: _PGJSON,
626
+ sqltypes.JSON: _PGJSON,
627
+ JSONB: _PGJSONB,
628
+ ranges.INT4RANGE: _Psycopg2NumericRange,
629
+ ranges.INT8RANGE: _Psycopg2NumericRange,
630
+ ranges.NUMRANGE: _Psycopg2NumericRange,
631
+ ranges.DATERANGE: _Psycopg2DateRange,
632
+ ranges.TSRANGE: _Psycopg2DateTimeRange,
633
+ ranges.TSTZRANGE: _Psycopg2DateTimeTZRange,
634
+ },
635
+ )
636
+
637
+ def __init__(
638
+ self,
639
+ executemany_mode="values_only",
640
+ executemany_batch_page_size=100,
641
+ **kwargs,
642
+ ):
643
+ _PGDialect_common_psycopg.__init__(self, **kwargs)
644
+
645
+ if self._native_inet_types:
646
+ raise NotImplementedError(
647
+ "The psycopg2 dialect does not implement "
648
+ "ipaddress type handling; native_inet_types cannot be set "
649
+ "to ``True`` when using this dialect."
650
+ )
651
+
652
+ # Parse executemany_mode argument, allowing it to be only one of the
653
+ # symbol names
654
+ self.executemany_mode = parse_user_argument_for_enum(
655
+ executemany_mode,
656
+ {
657
+ EXECUTEMANY_VALUES: ["values_only"],
658
+ EXECUTEMANY_VALUES_PLUS_BATCH: ["values_plus_batch"],
659
+ },
660
+ "executemany_mode",
661
+ )
662
+
663
+ self.executemany_batch_page_size = executemany_batch_page_size
664
+
665
+ if self.dbapi and hasattr(self.dbapi, "__version__"):
666
+ m = re.match(r"(\d+)\.(\d+)(?:\.(\d+))?", self.dbapi.__version__)
667
+ if m:
668
+ self.psycopg2_version = tuple(
669
+ int(x) for x in m.group(1, 2, 3) if x is not None
670
+ )
671
+
672
+ if self.psycopg2_version < (2, 7):
673
+ raise ImportError(
674
+ "psycopg2 version 2.7 or higher is required."
675
+ )
676
+
677
+ def initialize(self, connection):
678
+ super().initialize(connection)
679
+ self._has_native_hstore = (
680
+ self.use_native_hstore
681
+ and self._hstore_oids(connection.connection.dbapi_connection)
682
+ is not None
683
+ )
684
+
685
+ self.supports_sane_multi_rowcount = (
686
+ self.executemany_mode is not EXECUTEMANY_VALUES_PLUS_BATCH
687
+ )
688
+
689
+ @classmethod
690
+ def import_dbapi(cls):
691
+ import psycopg2
692
+
693
+ return psycopg2
694
+
695
+ @util.memoized_property
696
+ def _psycopg2_extensions(cls):
697
+ from psycopg2 import extensions
698
+
699
+ return extensions
700
+
701
+ @util.memoized_property
702
+ def _psycopg2_extras(cls):
703
+ from psycopg2 import extras
704
+
705
+ return extras
706
+
707
+ @util.memoized_property
708
+ def _isolation_lookup(self):
709
+ extensions = self._psycopg2_extensions
710
+ return {
711
+ "AUTOCOMMIT": extensions.ISOLATION_LEVEL_AUTOCOMMIT,
712
+ "READ COMMITTED": extensions.ISOLATION_LEVEL_READ_COMMITTED,
713
+ "READ UNCOMMITTED": extensions.ISOLATION_LEVEL_READ_UNCOMMITTED,
714
+ "REPEATABLE READ": extensions.ISOLATION_LEVEL_REPEATABLE_READ,
715
+ "SERIALIZABLE": extensions.ISOLATION_LEVEL_SERIALIZABLE,
716
+ }
717
+
718
+ def set_isolation_level(self, dbapi_connection, level):
719
+ dbapi_connection.set_isolation_level(self._isolation_lookup[level])
720
+
721
+ def set_readonly(self, connection, value):
722
+ connection.readonly = value
723
+
724
+ def get_readonly(self, connection):
725
+ return connection.readonly
726
+
727
+ def set_deferrable(self, connection, value):
728
+ connection.deferrable = value
729
+
730
+ def get_deferrable(self, connection):
731
+ return connection.deferrable
732
+
733
+ def on_connect(self):
734
+ extras = self._psycopg2_extras
735
+
736
+ fns = []
737
+ if self.client_encoding is not None:
738
+
739
+ def on_connect(dbapi_conn):
740
+ dbapi_conn.set_client_encoding(self.client_encoding)
741
+
742
+ fns.append(on_connect)
743
+
744
+ if self.dbapi:
745
+
746
+ def on_connect(dbapi_conn):
747
+ extras.register_uuid(None, dbapi_conn)
748
+
749
+ fns.append(on_connect)
750
+
751
+ if self.dbapi and self.use_native_hstore:
752
+
753
+ def on_connect(dbapi_conn):
754
+ hstore_oids = self._hstore_oids(dbapi_conn)
755
+ if hstore_oids is not None:
756
+ oid, array_oid = hstore_oids
757
+ kw = {"oid": oid}
758
+ kw["array_oid"] = array_oid
759
+ extras.register_hstore(dbapi_conn, **kw)
760
+
761
+ fns.append(on_connect)
762
+
763
+ if self.dbapi and self._json_deserializer:
764
+
765
+ def on_connect(dbapi_conn):
766
+ extras.register_default_json(
767
+ dbapi_conn, loads=self._json_deserializer
768
+ )
769
+ extras.register_default_jsonb(
770
+ dbapi_conn, loads=self._json_deserializer
771
+ )
772
+
773
+ fns.append(on_connect)
774
+
775
+ if fns:
776
+
777
+ def on_connect(dbapi_conn):
778
+ for fn in fns:
779
+ fn(dbapi_conn)
780
+
781
+ return on_connect
782
+ else:
783
+ return None
784
+
785
+ def do_executemany(self, cursor, statement, parameters, context=None):
786
+ if self.executemany_mode is EXECUTEMANY_VALUES_PLUS_BATCH:
787
+ if self.executemany_batch_page_size:
788
+ kwargs = {"page_size": self.executemany_batch_page_size}
789
+ else:
790
+ kwargs = {}
791
+ self._psycopg2_extras.execute_batch(
792
+ cursor, statement, parameters, **kwargs
793
+ )
794
+ else:
795
+ cursor.executemany(statement, parameters)
796
+
797
+ def _twophase_idle_check(self, dbapi_conn):
798
+ return dbapi_conn.status == self._psycopg2_extensions.STATUS_READY
799
+
800
+ @util.memoized_instancemethod
801
+ def _hstore_oids(self, dbapi_connection):
802
+ extras = self._psycopg2_extras
803
+ oids = extras.HstoreAdapter.get_oids(dbapi_connection)
804
+ if oids is not None and oids[0]:
805
+ return oids[0:2]
806
+ else:
807
+ return None
808
+
809
+ def is_disconnect(self, e, connection, cursor):
810
+ if isinstance(e, self.dbapi.Error):
811
+ # check the "closed" flag. this might not be
812
+ # present on old psycopg2 versions. Also,
813
+ # this flag doesn't actually help in a lot of disconnect
814
+ # situations, so don't rely on it.
815
+ if getattr(connection, "closed", False):
816
+ return True
817
+
818
+ # checks based on strings. in the case that .closed
819
+ # didn't cut it, fall back onto these.
820
+ str_e = str(e).partition("\n")[0]
821
+ for msg in self._is_disconnect_messages:
822
+ idx = str_e.find(msg)
823
+ if idx >= 0 and '"' not in str_e[:idx]:
824
+ return True
825
+ return False
826
+
827
+ @util.memoized_property
828
+ def _is_disconnect_messages(self):
829
+ return (
830
+ # these error messages from libpq: interfaces/libpq/fe-misc.c
831
+ # and interfaces/libpq/fe-secure.c.
832
+ "terminating connection",
833
+ "closed the connection",
834
+ "connection not open",
835
+ "could not receive data from server",
836
+ "could not send data to server",
837
+ # psycopg2 client errors, psycopg2/connection.h,
838
+ # psycopg2/cursor.h
839
+ "connection already closed",
840
+ "cursor already closed",
841
+ # not sure where this path is originally from, it may
842
+ # be obsolete. It really says "losed", not "closed".
843
+ "losed the connection unexpectedly",
844
+ # these can occur in newer SSL
845
+ "connection has been closed unexpectedly",
846
+ "SSL error: decryption failed or bad record mac",
847
+ "SSL SYSCALL error: Bad file descriptor",
848
+ "SSL SYSCALL error: EOF detected",
849
+ "SSL SYSCALL error: Operation timed out",
850
+ "SSL SYSCALL error: Bad address",
851
+ # This can occur in OpenSSL 1 when an unexpected EOF occurs.
852
+ # https://www.openssl.org/docs/man1.1.1/man3/SSL_get_error.html#BUGS
853
+ # It may also occur in newer OpenSSL for a non-recoverable I/O
854
+ # error as a result of a system call that does not set 'errno'
855
+ # in libc.
856
+ "SSL SYSCALL error: Success",
857
+ )
858
+
859
+
860
+ dialect = PGDialect_psycopg2