@@ -38,8 +38,8 @@ class Connection(metaclass=ConnectionMeta):
38
38
Connections are created by calling :func:`~asyncpg.connection.connect`.
39
39
"""
40
40
41
- __slots__ = ('_protocol' , '_transport' , '_loop' , '_types_stmt' ,
42
- '_type_by_name_stmt' , ' _top_xact' , '_uid' , '_aborted' ,
41
+ __slots__ = ('_protocol' , '_transport' , '_loop' ,
42
+ '_top_xact' , '_uid' , '_aborted' ,
43
43
'_pool_release_ctr' , '_stmt_cache' , '_stmts_to_close' ,
44
44
'_listeners' , '_server_version' , '_server_caps' ,
45
45
'_intro_query' , '_reset_query' , '_proxy' ,
@@ -53,8 +53,6 @@ def __init__(self, protocol, transport, loop,
53
53
self ._protocol = protocol
54
54
self ._transport = transport
55
55
self ._loop = loop
56
- self ._types_stmt = None
57
- self ._type_by_name_stmt = None
58
56
self ._top_xact = None
59
57
self ._uid = 0
60
58
self ._aborted = False
@@ -286,14 +284,17 @@ async def _get_statement(self, query, timeout, *, named: bool=False):
286
284
stmt_name = ''
287
285
288
286
statement = await self ._protocol .prepare (stmt_name , query , timeout )
289
-
290
287
ready = statement ._init_types ()
291
288
if ready is not True :
292
- if self ._types_stmt is None :
293
- self ._types_stmt = await self .prepare (self ._intro_query )
294
-
295
- types = await self ._types_stmt .fetch (list (ready ))
289
+ types , intro_stmt = await self .__execute (
290
+ self ._intro_query , (list (ready ),), 0 , timeout )
296
291
self ._protocol .get_settings ().register_data_types (types )
292
+ if not intro_stmt .name and not statement .name :
293
+ # The introspection query has used an anonymous statement,
294
+ # which has blown away the anonymous statement we've prepared
295
+ # for the query, so we need to re-prepare it.
296
+ statement = await self ._protocol .prepare (
297
+ stmt_name , query , timeout )
297
298
298
299
if use_cache :
299
300
self ._stmt_cache .put (query , statement )
@@ -886,12 +887,8 @@ async def set_type_codec(self, typename, *,
886
887
"asyncpg 0.13.0. Use the `format` keyword argument instead." ,
887
888
DeprecationWarning , stacklevel = 2 )
888
889
889
- if self ._type_by_name_stmt is None :
890
- self ._type_by_name_stmt = await self .prepare (
891
- introspection .TYPE_BY_NAME )
892
-
893
- typeinfo = await self ._type_by_name_stmt .fetchrow (
894
- typename , schema )
890
+ typeinfo = await self .fetchrow (
891
+ introspection .TYPE_BY_NAME , typename , schema )
895
892
if not typeinfo :
896
893
raise ValueError ('unknown type: {}.{}' .format (schema , typename ))
897
894
@@ -921,12 +918,8 @@ async def reset_type_codec(self, typename, *, schema='public'):
921
918
.. versionadded:: 0.12.0
922
919
"""
923
920
924
- if self ._type_by_name_stmt is None :
925
- self ._type_by_name_stmt = await self .prepare (
926
- introspection .TYPE_BY_NAME )
927
-
928
- typeinfo = await self ._type_by_name_stmt .fetchrow (
929
- typename , schema )
921
+ typeinfo = await self .fetchrow (
922
+ introspection .TYPE_BY_NAME , typename , schema )
930
923
if not typeinfo :
931
924
raise ValueError ('unknown type: {}.{}' .format (schema , typename ))
932
925
@@ -949,12 +942,8 @@ async def set_builtin_type_codec(self, typename, *,
949
942
"""
950
943
self ._check_open ()
951
944
952
- if self ._type_by_name_stmt is None :
953
- self ._type_by_name_stmt = await self .prepare (
954
- introspection .TYPE_BY_NAME )
955
-
956
- typeinfo = await self ._type_by_name_stmt .fetchrow (
957
- typename , schema )
945
+ typeinfo = await self .fetchrow (
946
+ introspection .TYPE_BY_NAME , typename , schema )
958
947
if not typeinfo :
959
948
raise ValueError ('unknown type: {}.{}' .format (schema , typename ))
960
949
@@ -1209,18 +1198,25 @@ def _drop_global_statement_cache(self):
1209
1198
self ._drop_local_statement_cache ()
1210
1199
1211
1200
async def _execute (self , query , args , limit , timeout , return_status = False ):
1201
+ with self ._stmt_exclusive_section :
1202
+ result , _ = await self .__execute (
1203
+ query , args , limit , timeout , return_status = return_status )
1204
+ return result
1205
+
1206
+ async def __execute (self , query , args , limit , timeout ,
1207
+ return_status = False ):
1212
1208
executor = lambda stmt , timeout : self ._protocol .bind_execute (
1213
1209
stmt , args , '' , limit , return_status , timeout )
1214
1210
timeout = self ._protocol ._get_timeout (timeout )
1215
- with self ._stmt_exclusive_section :
1216
- return await self ._do_execute (query , executor , timeout )
1211
+ return await self ._do_execute (query , executor , timeout )
1217
1212
1218
1213
async def _executemany (self , query , args , timeout ):
1219
1214
executor = lambda stmt , timeout : self ._protocol .bind_execute_many (
1220
1215
stmt , args , '' , timeout )
1221
1216
timeout = self ._protocol ._get_timeout (timeout )
1222
1217
with self ._stmt_exclusive_section :
1223
- return await self ._do_execute (query , executor , timeout )
1218
+ result , _ = await self ._do_execute (query , executor , timeout )
1219
+ return result
1224
1220
1225
1221
async def _do_execute (self , query , executor , timeout , retry = True ):
1226
1222
if timeout is None :
@@ -1269,10 +1265,10 @@ async def _do_execute(self, query, executor, timeout, retry=True):
1269
1265
if self ._protocol .is_in_transaction () or not retry :
1270
1266
raise
1271
1267
else :
1272
- result = await self ._do_execute (
1268
+ return await self ._do_execute (
1273
1269
query , executor , timeout , retry = False )
1274
1270
1275
- return result
1271
+ return result , stmt
1276
1272
1277
1273
1278
1274
async def connect (dsn = None , * ,
0 commit comments