From: Denis Laxalde Date: Mon, 18 Oct 2021 09:42:11 +0000 (+0200) Subject: Add handle() and validate() methods to PrepareManager X-Git-Tag: pool-3.1~98^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13af2e165beb9cdf6ed2a57dbd3bdced2ddc7a45;p=thirdparty%2Fpsycopg.git Add handle() and validate() methods to PrepareManager These essentially perform the same steps as maintain() but with only query information first, and then once results become available. --- diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 3a96ce386..50bee31e5 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -142,6 +142,42 @@ class PrepareManager: else: return None + def handle( + self, query: PostgresQuery, prep: Prepare, name: bytes + ) -> Optional[Key]: + """Handle 'query' for possible addition to the cache. + + If a new entry has been added, return its key. Return None otherwise + (meaning the query is already in cache or cache is not enabled). + """ + if self.prepare_threshold is None: + return None + cached = self.setdefault(query, prep, name) + if cached is None: + return None + key, value = cached + self._prepared[key] = value + return key + + def validate( + self, + key: Key, + prep: Prepare, + name: bytes, + results: Sequence["PGresult"], + ) -> Optional[bytes]: + """Validate cached entry with 'key' by checking query 'results'. + + Possibly return a command to perform maintainance on database side. + """ + cmd = self.should_discard(prep, results) + if cmd: + return cmd + if not self.check_results(results): + del self._prepared[key] + return None + return self.rotate() + def maintain( self, query: PostgresQuery,