7
7
8
8
import json
9
9
10
+ from . import connresource
10
11
from . import cursor
11
12
from . import exceptions
12
13
13
14
14
- class PreparedStatement :
15
+ class PreparedStatement ( connresource . ConnectionResource ) :
15
16
"""A representation of a prepared statement."""
16
17
17
- __slots__ = ('_connection' , ' _state' , '_query' , '_last_status' )
18
+ __slots__ = ('_state' , '_query' , '_last_status' )
18
19
19
20
def __init__ (self , connection , query , state ):
20
- self . _connection = connection
21
+ super (). __init__ ( connection )
21
22
self ._state = state
22
23
self ._query = query
23
24
state .attach ()
24
25
self ._last_status = None
25
26
27
+ @connresource .guarded
26
28
def get_query (self ) -> str :
27
29
"""Return the text of the query for this prepared statement.
28
30
@@ -33,6 +35,7 @@ def get_query(self) -> str:
33
35
"""
34
36
return self ._query
35
37
38
+ @connresource .guarded
36
39
def get_statusmsg (self ) -> str :
37
40
"""Return the status of the executed command.
38
41
@@ -46,6 +49,7 @@ def get_statusmsg(self) -> str:
46
49
return self ._last_status
47
50
return self ._last_status .decode ()
48
51
52
+ @connresource .guarded
49
53
def get_parameters (self ):
50
54
"""Return a description of statement parameters types.
51
55
@@ -60,9 +64,9 @@ def get_parameters(self):
60
64
# (Type(oid=23, name='int4', kind='scalar', schema='pg_catalog'),
61
65
# Type(oid=25, name='text', kind='scalar', schema='pg_catalog'))
62
66
"""
63
- self ._check_open ()
64
67
return self ._state ._get_parameters ()
65
68
69
+ @connresource .guarded
66
70
def get_attributes (self ):
67
71
"""Return a description of relation attributes (columns).
68
72
@@ -85,9 +89,9 @@ def get_attributes(self):
85
89
# type=Type(oid=26, name='oid', kind='scalar',
86
90
# schema='pg_catalog')))
87
91
"""
88
- self ._check_open ()
89
92
return self ._state ._get_attributes ()
90
93
94
+ @connresource .guarded
91
95
def cursor (self , * args , prefetch = None ,
92
96
timeout = None ) -> cursor .CursorFactory :
93
97
"""Return a *cursor factory* for the prepared statement.
@@ -99,11 +103,11 @@ def cursor(self, *args, prefetch=None,
99
103
100
104
:return: A :class:`~cursor.CursorFactory` object.
101
105
"""
102
- self ._check_open ()
103
106
return cursor .CursorFactory (self ._connection , self ._query ,
104
107
self ._state , args , prefetch ,
105
108
timeout )
106
109
110
+ @connresource .guarded
107
111
async def explain (self , * args , analyze = False ):
108
112
"""Return the execution plan of the statement.
109
113
@@ -145,6 +149,7 @@ async def explain(self, *args, analyze=False):
145
149
146
150
return json .loads (data )
147
151
152
+ @connresource .guarded
148
153
async def fetch (self , * args , timeout = None ):
149
154
r"""Execute the statement and return a list of :class:`Record` objects.
150
155
@@ -157,6 +162,7 @@ async def fetch(self, *args, timeout=None):
157
162
data = await self .__bind_execute (args , 0 , timeout )
158
163
return data
159
164
165
+ @connresource .guarded
160
166
async def fetchval (self , * args , column = 0 , timeout = None ):
161
167
"""Execute the statement and return a value in the first row.
162
168
@@ -175,6 +181,7 @@ async def fetchval(self, *args, column=0, timeout=None):
175
181
return None
176
182
return data [0 ][column ]
177
183
184
+ @connresource .guarded
178
185
async def fetchrow (self , * args , timeout = None ):
179
186
"""Execute the statement and return the first row.
180
187
@@ -190,16 +197,21 @@ async def fetchrow(self, *args, timeout=None):
190
197
return data [0 ]
191
198
192
199
async def __bind_execute (self , args , limit , timeout ):
193
- self ._check_open ()
194
200
protocol = self ._connection ._protocol
195
201
data , status , _ = await protocol .bind_execute (
196
202
self ._state , args , '' , limit , True , timeout )
197
203
self ._last_status = status
198
204
return data
199
205
200
- def _check_open (self ):
206
+ def _check_open (self , meth_name ):
201
207
if self ._state .closed :
202
- raise exceptions .InterfaceError ('prepared statement is closed' )
208
+ raise exceptions .InterfaceError (
209
+ 'cannot call PreparedStmt.{}(): '
210
+ 'the prepared statement is closed' .format (meth_name ))
211
+
212
+ def _check_conn_validity (self , meth_name ):
213
+ self ._check_open (meth_name )
214
+ super ()._check_conn_validity (meth_name )
203
215
204
216
def __del__ (self ):
205
217
self ._state .detach ()
0 commit comments