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