@@ -118,6 +118,7 @@ async def add_listener(self, channel, callback):
118
118
**channel**: name of the channel the notification was sent to;
119
119
**payload**: the payload.
120
120
"""
121
+ self .__check_open ()
121
122
if channel not in self ._listeners :
122
123
await self .fetch ('LISTEN {}' .format (channel ))
123
124
self ._listeners [channel ] = set ()
@@ -181,6 +182,7 @@ def transaction(self, *, isolation='read_committed', readonly=False,
181
182
.. _`PostgreSQL documentation`: https://www.postgresql.org/docs/\
182
183
current/static/sql-set-transaction.html
183
184
"""
185
+ self .__check_open ()
184
186
return transaction .Transaction (self , isolation , readonly , deferrable )
185
187
186
188
async def execute (self , query : str , * args , timeout : float = None ) -> str :
@@ -211,6 +213,8 @@ async def execute(self, query: str, *args, timeout: float=None) -> str:
211
213
.. versionchanged:: 0.5.4
212
214
Made it possible to pass query arguments.
213
215
"""
216
+ self .__check_open ()
217
+
214
218
if not args :
215
219
return await self ._protocol .query (query , timeout )
216
220
@@ -240,6 +244,8 @@ async def executemany(self, command: str, args,
240
244
241
245
.. versionadded:: 0.7.0
242
246
"""
247
+ self .__check_open ()
248
+
243
249
if 'timeout' in kw :
244
250
timeout = kw .pop ('timeout' )
245
251
else :
@@ -311,6 +317,7 @@ def cursor(self, query, *args, prefetch=None, timeout=None):
311
317
312
318
:return: A :class:`~cursor.CursorFactory` object.
313
319
"""
320
+ self .__check_open ()
314
321
return cursor .CursorFactory (self , query , None , args ,
315
322
prefetch , timeout )
316
323
@@ -322,6 +329,7 @@ async def prepare(self, query, *, timeout=None):
322
329
323
330
:return: A :class:`~prepared_stmt.PreparedStatement` instance.
324
331
"""
332
+ self .__check_open ()
325
333
stmt = await self ._get_statement (query , timeout , named = True )
326
334
return prepared_stmt .PreparedStatement (self , query , stmt )
327
335
@@ -334,6 +342,7 @@ async def fetch(self, query, *args, timeout=None) -> list:
334
342
335
343
:return list: A list of :class:`Record` instances.
336
344
"""
345
+ self .__check_open ()
337
346
return await self ._execute (query , args , 0 , timeout )
338
347
339
348
async def fetchval (self , query , * args , column = 0 , timeout = None ):
@@ -350,6 +359,7 @@ async def fetchval(self, query, *args, column=0, timeout=None):
350
359
351
360
:return: The value of the specified column of the first record.
352
361
"""
362
+ self .__check_open ()
353
363
data = await self ._execute (query , args , 1 , timeout )
354
364
if not data :
355
365
return None
@@ -364,6 +374,7 @@ async def fetchrow(self, query, *args, timeout=None):
364
374
365
375
:return: The first row as a :class:`Record` instance.
366
376
"""
377
+ self .__check_open ()
367
378
data = await self ._execute (query , args , 1 , timeout )
368
379
if not data :
369
380
return None
@@ -384,6 +395,8 @@ async def set_type_codec(self, typename, *,
384
395
data. If ``False`` (the default), the data is
385
396
expected to be encoded/decoded in text.
386
397
"""
398
+ self .__check_open ()
399
+
387
400
if self ._type_by_name_stmt is None :
388
401
self ._type_by_name_stmt = await self .prepare (
389
402
introspection .TYPE_BY_NAME )
@@ -412,6 +425,8 @@ async def set_builtin_type_codec(self, typename, *,
412
425
(defaults to 'public')
413
426
:param codec_name: The name of the builtin codec.
414
427
"""
428
+ self .__check_open ()
429
+
415
430
if self ._type_by_name_stmt is None :
416
431
self ._type_by_name_stmt = await self .prepare (
417
432
introspection .TYPE_BY_NAME )
@@ -455,11 +470,16 @@ def terminate(self):
455
470
self ._protocol .abort ()
456
471
457
472
async def reset (self ):
473
+ self .__check_open ()
458
474
self ._listeners .clear ()
459
475
reset_query = self ._get_reset_query ()
460
476
if reset_query :
461
477
await self .execute (reset_query )
462
478
479
+ def __check_open (self ):
480
+ if self .is_closed ():
481
+ raise exceptions .InterfaceError ('connection is closed' )
482
+
463
483
def _get_unique_id (self , prefix ):
464
484
self ._uid += 1
465
485
return '__asyncpg_{}_{}__' .format (prefix , self ._uid )
0 commit comments