@@ -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
@@ -274,7 +272,7 @@ async def _get_statement(self, query, timeout, *, named: bool=False):
274
272
# Only use the cache when:
275
273
# * `statement_cache_size` is greater than 0;
276
274
# * query size is less than `max_cacheable_statement_size`.
277
- use_cache = self ._stmt_cache .get_max_size () > 0
275
+ use_cache = cache_enabled = self ._stmt_cache .get_max_size () > 0
278
276
if (use_cache and
279
277
self ._config .max_cacheable_statement_size and
280
278
len (query ) > self ._config .max_cacheable_statement_size ):
@@ -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 = await self .__execute (self ._intro_query , (list (ready ),),
290
+ 0 , timeout )
296
291
self ._protocol .get_settings ().register_data_types (types )
292
+ if not cache_enabled :
293
+ # The execution of the introspection query with statement
294
+ # cache turned off has blown away the anonymous statement
295
+ # we've prepared 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
@@ -1215,6 +1204,13 @@ async def _execute(self, query, args, limit, timeout, return_status=False):
1215
1204
with self ._stmt_exclusive_section :
1216
1205
return await self ._do_execute (query , executor , timeout )
1217
1206
1207
+ async def __execute (self , query , args , limit , timeout ,
1208
+ return_status = False ):
1209
+ executor = lambda stmt , timeout : self ._protocol .bind_execute (
1210
+ stmt , args , '' , limit , return_status , timeout )
1211
+ timeout = self ._protocol ._get_timeout (timeout )
1212
+ return await self ._do_execute (query , executor , timeout )
1213
+
1218
1214
async def _executemany (self , query , args , timeout ):
1219
1215
executor = lambda stmt , timeout : self ._protocol .bind_execute_many (
1220
1216
stmt , args , '' , timeout )
0 commit comments