PyGreSQL 6.1.0__cp313-cp313-win_amd64.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.
pgdb/connection.py ADDED
@@ -0,0 +1,156 @@
1
+ """The DB API 2 Connection objects."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from contextlib import suppress
6
+ from typing import Any, Sequence
7
+
8
+ from pg.core import Connection as Cnx
9
+ from pg.core import (
10
+ DatabaseError,
11
+ DataError,
12
+ Error,
13
+ IntegrityError,
14
+ InterfaceError,
15
+ InternalError,
16
+ NotSupportedError,
17
+ OperationalError,
18
+ ProgrammingError,
19
+ Warning,
20
+ )
21
+ from pg.error import op_error
22
+
23
+ from .cast import TypeCache
24
+ from .constants import shortcutmethods
25
+ from .cursor import Cursor
26
+
27
+ __all__ = ['Connection']
28
+
29
+ class Connection:
30
+ """Connection object."""
31
+
32
+ # expose the exceptions as attributes on the connection object
33
+ Error = Error
34
+ Warning = Warning
35
+ InterfaceError = InterfaceError
36
+ DatabaseError = DatabaseError
37
+ InternalError = InternalError
38
+ OperationalError = OperationalError
39
+ ProgrammingError = ProgrammingError
40
+ IntegrityError = IntegrityError
41
+ DataError = DataError
42
+ NotSupportedError = NotSupportedError
43
+
44
+ def __init__(self, cnx: Cnx) -> None:
45
+ """Create a database connection object."""
46
+ self._cnx: Cnx | None = cnx # connection
47
+ self._tnx = False # transaction state
48
+ self.type_cache = TypeCache(cnx)
49
+ self.cursor_type = Cursor
50
+ self.autocommit = False
51
+ try:
52
+ self._cnx.source()
53
+ except Exception as e:
54
+ raise op_error("Invalid connection") from e
55
+
56
+ def __enter__(self) -> Connection:
57
+ """Enter the runtime context for the connection object.
58
+
59
+ The runtime context can be used for running transactions.
60
+
61
+ This also starts a transaction in autocommit mode.
62
+ """
63
+ if self.autocommit:
64
+ cnx = self._cnx
65
+ if not cnx:
66
+ raise op_error("Connection has been closed")
67
+ try:
68
+ cnx.source().execute("BEGIN")
69
+ except DatabaseError:
70
+ raise # database provides error message
71
+ except Exception as e:
72
+ raise op_error("Can't start transaction") from e
73
+ else:
74
+ self._tnx = True
75
+ return self
76
+
77
+ def __exit__(self, et: type[BaseException] | None,
78
+ ev: BaseException | None, tb: Any) -> None:
79
+ """Exit the runtime context for the connection object.
80
+
81
+ This does not close the connection, but it ends a transaction.
82
+ """
83
+ if et is None and ev is None and tb is None:
84
+ self.commit()
85
+ else:
86
+ self.rollback()
87
+
88
+ def close(self) -> None:
89
+ """Close the connection object."""
90
+ if not self._cnx:
91
+ raise op_error("Connection has been closed")
92
+ if self._tnx:
93
+ with suppress(DatabaseError):
94
+ self.rollback()
95
+ self._cnx.close()
96
+ self._cnx = None
97
+
98
+ @property
99
+ def closed(self) -> bool:
100
+ """Check whether the connection has been closed or is broken."""
101
+ try:
102
+ return not self._cnx or self._cnx.status != 1
103
+ except TypeError:
104
+ return True
105
+
106
+ def commit(self) -> None:
107
+ """Commit any pending transaction to the database."""
108
+ if not self._cnx:
109
+ raise op_error("Connection has been closed")
110
+ if self._tnx:
111
+ self._tnx = False
112
+ try:
113
+ self._cnx.source().execute("COMMIT")
114
+ except DatabaseError:
115
+ raise # database provides error message
116
+ except Exception as e:
117
+ raise op_error("Can't commit transaction") from e
118
+
119
+ def rollback(self) -> None:
120
+ """Roll back to the start of any pending transaction."""
121
+ if not self._cnx:
122
+ raise op_error("Connection has been closed")
123
+ if self._tnx:
124
+ self._tnx = False
125
+ try:
126
+ self._cnx.source().execute("ROLLBACK")
127
+ except DatabaseError:
128
+ raise # database provides error message
129
+ except Exception as e:
130
+ raise op_error("Can't rollback transaction") from e
131
+
132
+ def cursor(self) -> Cursor:
133
+ """Return a new cursor object using the connection."""
134
+ if not self._cnx:
135
+ raise op_error("Connection has been closed")
136
+ try:
137
+ return self.cursor_type(self)
138
+ except Exception as e:
139
+ raise op_error("Invalid connection") from e
140
+
141
+ if shortcutmethods: # otherwise do not implement and document this
142
+
143
+ def execute(self, operation: str,
144
+ parameters: Sequence | None = None) -> Cursor:
145
+ """Shortcut method to run an operation on an implicit cursor."""
146
+ cursor = self.cursor()
147
+ cursor.execute(operation, parameters)
148
+ return cursor
149
+
150
+ def executemany(self, operation: str,
151
+ seq_of_parameters: Sequence[Sequence | None]
152
+ ) -> Cursor:
153
+ """Shortcut method to run an operation against a sequence."""
154
+ cursor = self.cursor()
155
+ cursor.executemany(operation, seq_of_parameters)
156
+ return cursor
pgdb/constants.py ADDED
@@ -0,0 +1,14 @@
1
+ """The DB API 2 module constants."""
2
+
3
+ # compliant with DB API 2.0
4
+ apilevel = '2.0'
5
+
6
+ # module may be shared, but not connections
7
+ threadsafety = 1
8
+
9
+ # this module use extended python format codes
10
+ paramstyle = 'pyformat'
11
+
12
+ # shortcut methods have been excluded from DB API 2 and
13
+ # are not recommended by the DB SIG, but they can be handy
14
+ shortcutmethods = 1