]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add handle() and validate() methods to PrepareManager
authorDenis Laxalde <denis.laxalde@dalibo.com>
Mon, 18 Oct 2021 09:42:11 +0000 (11:42 +0200)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Mon, 29 Nov 2021 08:50:50 +0000 (09:50 +0100)
These essentially perform the same steps as maintain() but with only
query information first, and then once results become available.

psycopg/psycopg/_preparing.py

index 3a96ce38690bce241e4e46f668b2f61e4668e5d4..50bee31e52ed409f43ce96e46fa5e46e9461c78a 100644 (file)
@@ -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,