From: Daniele Varrazzo Date: Wed, 17 Nov 2021 13:27:35 +0000 (+0100) Subject: Refactor away internal prepared statements manager function X-Git-Tag: pool-3.1~98^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=995809b5a95ae20428fa9f55288b18fc3795c3c7;p=thirdparty%2Fpsycopg.git Refactor away internal prepared statements manager function --- diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 34ad77ea2..5ae79ae0c 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -91,27 +91,6 @@ class PrepareManager: return self.clear() return None - def _check_in_cache_or_increment( - self, query: PostgresQuery, prep: Prepare, name: bytes - ) -> Optional[Tuple[Key, Value]]: - """Check if the query is already in cache. - - If not, return a new 'key' matching given query. Otherwise, just - update the count for that query and record as last used. - """ - key = self.key(query) - if key in self._prepared: - if isinstance(self._prepared[key], int): - if prep is Prepare.SHOULD: - self._prepared[key] = name - else: - self._prepared[key] += 1 # type: ignore[operator] - self._prepared.move_to_end(key) - return None - - value: Value = name if prep is Prepare.SHOULD else 1 - return key, value - @staticmethod def _check_results(results: Sequence["PGresult"]) -> bool: """Return False if 'results' are invalid for prepared statement cache.""" @@ -154,12 +133,20 @@ class PrepareManager: # don't do anything if prepared statements are disabled if self.prepare_threshold is None: return None - cached = self._check_in_cache_or_increment(query, prep, name) - if cached is None: + + key = self.key(query) + if key in self._prepared: + if isinstance(self._prepared[key], int): + if prep is Prepare.SHOULD: + self._prepared[key] = name + else: + self._prepared[key] += 1 # type: ignore[operator] + self._prepared.move_to_end(key) return None - key, value = cached - self._prepared[key] = value - return key + else: + value: Value = name if prep is Prepare.SHOULD else 1 + self._prepared[key] = value + return key def validate( self,