1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
from __future__ import annotations
import logging
import os
from collections.abc import Callable, Sequence
from typing import Any, TypeVar, overload
Row = TypeVar("Row")
try:
if "_PGACTIVITY_USE_PSYCOPG2" in os.environ:
raise ImportError("psycopg2 requested from environment")
import psycopg
from psycopg import sql as sql
from psycopg._encodings import pg2pyenc
from psycopg.adapt import Buffer, Loader
from psycopg.conninfo import conninfo_to_dict, make_conninfo
from psycopg.errors import FeatureNotSupported as FeatureNotSupported
from psycopg.errors import InsufficientPrivilege as InsufficientPrivilege
from psycopg.errors import InterfaceError as InterfaceError
from psycopg.errors import InvalidPassword as InvalidPassword
from psycopg.errors import NotSupportedError
from psycopg.errors import OperationalError as OperationalError
from psycopg.errors import ProgrammingError as ProgrammingError
from psycopg.errors import QueryCanceled as QueryCanceled
from psycopg.rows import dict_row
__version__ = psycopg.__version__
Connection = psycopg.Connection[dict[str, Any]]
class BytesLoader(Loader):
def load(self, data: Buffer) -> bytes | bytearray:
if isinstance(data, memoryview):
return bytes(data)
return data
class AutoTextLoader(Loader):
def load(self, data: Buffer) -> str:
if not isinstance(data, bytes):
data = bytes(data)
return data.decode(errors="replace")
def connect(dsn: str = "", **kwargs: Any) -> Connection:
if "PGCLIENTENCODING" not in os.environ:
# Set client_encoding to 'auto', if not set by the user.
# This is (more or less) what's done by psql.
conninfo = conninfo_to_dict(dsn)
conninfo.setdefault("client_encoding", "auto")
dsn = make_conninfo("", **conninfo)
conn = psycopg.connect(dsn, autocommit=True, row_factory=dict_row, **kwargs)
if conn.info.encoding == "ascii":
# If client encoding is still 'ascii', fall back to a loader with a replace
# policy.
logging.getLogger("pgactivity").warning(
"client encoding is 'ascii', using a fallback loader for character types"
)
conn.adapters.register_loader("text", AutoTextLoader)
conn.adapters.register_loader("varchar", AutoTextLoader)
conn.adapters.register_loader('"char"', AutoTextLoader)
return conn
def server_version(conn: Connection) -> int:
return conn.info.server_version
def connection_parameters(conn: Connection) -> dict[str, Any]:
return conn.info.get_parameters()
def execute(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
) -> None:
conn.execute(query, args, prepare=True)
@overload
def cursor(
conn: Connection, mkrow: Callable[..., Row], text_as_bytes: bool
) -> psycopg.Cursor[Row]: ...
@overload
def cursor(
conn: Connection, mkrow: None, text_as_bytes: bool
) -> psycopg.Cursor[psycopg.rows.DictRow]: ...
def cursor(
conn: Connection, mkrow: Callable[..., Row] | None, text_as_bytes: bool
) -> psycopg.Cursor[psycopg.rows.DictRow] | psycopg.Cursor[Row]:
if mkrow is not None:
cur = conn.cursor(row_factory=psycopg.rows.kwargs_row(mkrow))
else:
cur = conn.cursor() # type: ignore[assignment]
if text_as_bytes:
cur.adapters.register_loader("text", BytesLoader)
return cur
@overload
def fetchone(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
mkrow: Callable[..., Row],
text_as_bytes: bool = False,
) -> Row: ...
@overload
def fetchone(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
text_as_bytes: bool = False,
) -> dict[str, Any]: ...
def fetchone(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
mkrow: Callable[..., Row] | None = None,
text_as_bytes: bool = False,
) -> dict[str, Any] | Row:
with cursor(conn, mkrow, text_as_bytes) as cur:
row = cur.execute(query, args, prepare=True).fetchone()
assert row is not None
return row
@overload
def fetchall(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
mkrow: Callable[..., Row],
text_as_bytes: bool = False,
) -> list[Row]: ...
@overload
def fetchall(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
text_as_bytes: bool = False,
) -> list[dict[str, Any]]: ...
def fetchall(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
text_as_bytes: bool = False,
mkrow: Callable[..., Row] | None = None,
) -> list[dict[str, Any]] | list[Row]:
with cursor(conn, mkrow, text_as_bytes) as cur:
return cur.execute(query, args, prepare=True).fetchall()
def decode(value: bytes, pgenc: bytes, *, errors: str) -> str:
"""Decode 'value' with PostgreSQL encoding 'pgenc' converted to Python encoding
name if available.
"""
try:
pyenc = pg2pyenc(pgenc)
except NotSupportedError:
pyenc = "utf-8"
return value.decode(pyenc, errors=errors)
def needs_password(exc: OperationalError) -> bool:
assert exc.pgconn is not None
return exc.pgconn.needs_password
except ImportError:
import codecs
import psycopg2
import psycopg2.extensions
from psycopg2 import sql as sql # type: ignore[no-redef]
# isort: off
from psycopg2.errors import FeatureNotSupported as FeatureNotSupported # type: ignore[assignment]
from psycopg2.errors import InsufficientPrivilege as InsufficientPrivilege # type: ignore[assignment]
from psycopg2.errors import InterfaceError as InterfaceError # type: ignore[assignment]
from psycopg2.errors import InvalidPassword as InvalidPassword # type: ignore[assignment]
from psycopg2.errors import OperationalError as OperationalError # type: ignore[assignment]
from psycopg2.errors import ProgrammingError as ProgrammingError # type: ignore[assignment]
from psycopg2.errors import QueryCanceled as QueryCanceled # type: ignore[assignment]
# isort: on
from psycopg2.extensions import connection as Connection # type: ignore[assignment]
from psycopg2.extras import DictCursor
__version__ = psycopg2.__version__ # type: ignore[attr-defined]
def connect(dsn: str = "", **kwargs: Any) -> Connection:
try:
kwargs.setdefault("database", kwargs.pop("dbname"))
except KeyError:
pass
conn = psycopg2.connect(dsn, cursor_factory=DictCursor, **kwargs)
conn.autocommit = True
return conn # type: ignore[no-any-return]
def server_version(conn: Connection) -> int:
return conn.server_version # type: ignore[attr-defined, no-any-return]
def connection_parameters(conn: Connection) -> dict[str, Any]:
return conn.info.dsn_parameters # type: ignore[attr-defined, no-any-return]
def execute(
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
) -> None:
with conn.cursor() as cur:
cur.execute(query, args)
def fetchone( # type: ignore[no-redef]
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
mkrow: Callable[..., Row] | None = None,
text_as_bytes: bool = False,
) -> dict[str, Any] | Row:
with conn.cursor() as cur:
if text_as_bytes:
psycopg2.extensions.register_type(psycopg2.extensions.BYTES, cur) # type: ignore[arg-type]
cur.execute(query, args)
row = cur.fetchone()
assert row is not None
if mkrow is not None:
return mkrow(**row)
return row
def fetchall( # type: ignore[no-redef]
conn: Connection,
query: str | sql.Composed,
args: None | Sequence[Any] | dict[str, Any] = None,
*,
mkrow: Callable[..., Row] | None = None,
text_as_bytes: bool = False,
) -> list[dict[str, Any]] | list[Row]:
with conn.cursor() as cur:
if text_as_bytes:
psycopg2.extensions.register_type(psycopg2.extensions.BYTES, cur) # type: ignore[arg-type]
cur.execute(query, args)
rows = cur.fetchall()
if mkrow is not None:
return [mkrow(**row) for row in rows]
return rows
def decode(value: bytes, pgenc: bytes, *, errors: str) -> str:
"""Decode 'value' with PostgreSQL encoding 'pgenc' converted to Python encoding
name if available.
"""
try:
pyenc = codecs.lookup(pgenc.decode()).name
except LookupError:
pyenc = "utf-8"
return value.decode(pyenc, errors=errors)
def needs_password(exc: OperationalError) -> bool:
if isinstance(exc, InvalidPassword):
return True
msg = str(exc)
return (
msg.startswith("FATAL: password authentication failed for user")
or "fe_sendauth: no password supplied" in msg
)
__all__ = [
"__version__",
"Connection",
"FeatureNotSupported",
"InterfaceError",
"InvalidPassword",
"InsufficientPrivilege",
"OperationalError",
"ProgrammingError",
"QueryCanceled",
"connect",
"connection_parameters",
"decode",
"execute",
"fetchall",
"fetchone",
"server_version",
"sql",
]
|