Skip to content

Commit da81adc

Browse files
author
James William Pye
committed
Migrate some common cursor metadata into a Result class.
Chunks and Cursor will share these interfaces.
1 parent 59e77a7 commit da81adc

1 file changed

Lines changed: 59 additions & 36 deletions

File tree

postgresql/api.py

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
]
4444

4545
class Message(Element):
46-
"A message emitted by PostgreSQL"
47-
_e_label = property(lambda x: getattr(x, 'details', None).get('severity', 'MESSAGE'))
46+
"""
47+
A message emitted by PostgreSQL. This element is universal, so
48+
`postgresql.api.Message` is a complete implementation for representing a
49+
message. Any interface should produce these objects.
50+
"""
51+
_e_label = property(lambda x: getattr(x, 'details').get('severity', 'MESSAGE'))
4852
_e_factors = ('creator',)
4953

5054
severities = (
@@ -154,44 +158,17 @@ def raise_message(self, starting_point = None):
154158
# send the message to postgresql.sys.msghook
155159
pg_sys.msghook(self)
156160

157-
class Chunks(
158-
collections.Iterator,
159-
collections.Iterable,
160-
):
161-
"""
162-
A `Chunks` object is an interface to an iterator of row-sets produced
163-
by a cursor.
161+
class Result(Element):
164162
"""
163+
A result is an object managing the results of a prepared statement.
165164
166-
def __iter__(self):
167-
return self
168-
169-
class Cursor(
170-
Element,
171-
collections.Iterator,
172-
collections.Iterable,
173-
):
174-
"""
175-
A `Cursor` object is an interface to a sequence of tuples(rows). A result
176-
set. Cursors publish a file-like interface for reading tuples from a cursor
177-
declared on the database.
165+
These objects represent a binding of parameters to a given statement object.
178166
179-
`Cursor` objects are created by invoking the `PreparedStatement.declare`
180-
method or by opening a cursor using an identifier via the
181-
`Database.cursor_from_id` method.
167+
For results that were constructed on the server and a reference passed back
168+
to the client, parameters may be None.
182169
"""
183-
_e_label = 'CURSOR'
184-
_e_factors = ('statement', 'parameters')
185-
186-
_seek_whence_map = {
187-
0 : 'ABSOLUTE',
188-
1 : 'RELATIVE',
189-
2 : 'FROM_END',
190-
}
191-
_direction_map = {
192-
True : 'FORWARD',
193-
False : 'BACKWARD',
194-
}
170+
_e_label = 'RESULT'
171+
_e_factors = ('statement', 'parameters', 'cursor_id')
195172

196173
@propertydoc
197174
@abstractproperty
@@ -252,15 +229,61 @@ def parameters(self) -> (tuple, None):
252229
`()`, if no parameters were given.
253230
254231
These should be the *original* parameters given to the invoked statement.
232+
233+
This should only be `None` when the cursor is created from an identifier,
234+
`postgresql.api.Database.cursor_from_id`.
255235
"""
256236

257237
@propertydoc
258238
@abstractproperty
259239
def statement(self) -> ("PreparedStatement", None):
260240
"""
261241
The query object used to create the cursor. `None`, if unknown.
242+
243+
This should only be `None` when the cursor is created from an identifier,
244+
`postgresql.api.Database.cursor_from_id`.
262245
"""
263246

247+
class Chunks(
248+
collections.Iterator,
249+
collections.Iterable,
250+
):
251+
"""
252+
A `Chunks` object is an interface to an iterator of row-sets produced
253+
by a cursor.
254+
"""
255+
def __iter__(self):
256+
return self
257+
258+
class Cursor(
259+
Result,
260+
collections.Iterator,
261+
collections.Iterable,
262+
):
263+
"""
264+
A `Cursor` object is an interface to a sequence of tuples(rows). A result
265+
set. Cursors publish a file-like interface for reading tuples from a cursor
266+
declared on the database.
267+
268+
`Cursor` objects are created by invoking the `PreparedStatement.declare`
269+
method or by opening a cursor using an identifier via the
270+
`Database.cursor_from_id` method.
271+
"""
272+
_e_label = 'CURSOR'
273+
274+
_seek_whence_map = {
275+
0 : 'ABSOLUTE',
276+
1 : 'RELATIVE',
277+
2 : 'FROM_END',
278+
}
279+
_direction_map = {
280+
True : 'FORWARD',
281+
False : 'BACKWARD',
282+
}
283+
284+
def __iter__(self):
285+
return self
286+
264287
@propertydoc
265288
@abstractproperty
266289
def direction(self) -> bool:

0 commit comments

Comments
 (0)