Skip to content

Commit

Permalink
Make prepared statement uid generator global
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Oct 23, 2017
1 parent f4e17dd commit 3e43fcf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 7 additions & 4 deletions asyncpg/connection.py
Expand Up @@ -39,7 +39,7 @@ class Connection(metaclass=ConnectionMeta):
"""

__slots__ = ('_protocol', '_transport', '_loop',
'_top_xact', '_uid', '_aborted',
'_top_xact', '_aborted',
'_pool_release_ctr', '_stmt_cache', '_stmts_to_close',
'_listeners', '_server_version', '_server_caps',
'_intro_query', '_reset_query', '_proxy',
Expand All @@ -54,7 +54,6 @@ def __init__(self, protocol, transport, loop,
self._transport = transport
self._loop = loop
self._top_xact = None
self._uid = 0
self._aborted = False
# Incremented very time the connection is released back to a pool.
# Used to catch invalid references to connection-related resources
Expand Down Expand Up @@ -990,8 +989,9 @@ def _check_open(self):
raise exceptions.InterfaceError('connection is closed')

def _get_unique_id(self, prefix):
self._uid += 1
return '__asyncpg_{}_{}__'.format(prefix, self._uid)
global _uid
_uid += 1
return '__asyncpg_{}_{:x}__'.format(prefix, _uid)

def _mark_stmts_as_closed(self):
for stmt in self._stmt_cache.iter_statements():
Expand Down Expand Up @@ -1598,3 +1598,6 @@ def _detect_server_capabilities(server_version, connection_settings):
sql_reset=sql_reset,
sql_close_all=sql_close_all
)


_uid = 0
13 changes: 10 additions & 3 deletions tests/test_introspection.py
Expand Up @@ -6,6 +6,7 @@


from asyncpg import _testbase as tb
from asyncpg import connection as apg_con


MAX_RUNTIME = 0.1
Expand Down Expand Up @@ -47,6 +48,8 @@ async def test_introspection_on_large_db(self):

@tb.with_connection_options(statement_cache_size=0)
async def test_introspection_no_stmt_cache_01(self):
old_uid = apg_con._uid

self.assertEqual(self.con._stmt_cache.get_max_size(), 0)
await self.con.fetchval('SELECT $1::int[]', [1, 2])

Expand All @@ -62,12 +65,14 @@ async def test_introspection_no_stmt_cache_01(self):
DROP EXTENSION hstore
''')

self.assertEqual(self.con._uid, 0)
self.assertEqual(apg_con._uid, old_uid)

@tb.with_connection_options(max_cacheable_statement_size=1)
async def test_introspection_no_stmt_cache_02(self):
# max_cacheable_statement_size will disable caching both for
# the user query and for the introspection query.
old_uid = apg_con._uid

await self.con.fetchval('SELECT $1::int[]', [1, 2])

await self.con.execute('''
Expand All @@ -82,13 +87,15 @@ async def test_introspection_no_stmt_cache_02(self):
DROP EXTENSION hstore
''')

self.assertEqual(self.con._uid, 0)
self.assertEqual(apg_con._uid, old_uid)

@tb.with_connection_options(max_cacheable_statement_size=10000)
async def test_introspection_no_stmt_cache_03(self):
# max_cacheable_statement_size will disable caching for
# the user query but not for the introspection query.
old_uid = apg_con._uid

await self.con.fetchval(
"SELECT $1::int[], '{foo}'".format(foo='a' * 10000), [1, 2])

self.assertEqual(self.con._uid, 1)
self.assertEqual(apg_con._uid, old_uid + 1)

0 comments on commit 3e43fcf

Please sign in to comment.