MCPcopy
hub / github.com/psycopg/psycopg / maybe_add_to_cache

Method maybe_add_to_cache

psycopg/psycopg/_preparing.py:121–151  ·  view source on GitHub ↗

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).

(
        self, query: PostgresQuery, prep: Prepare, name: bytes
    )

Source from the content-addressed store, hash-verified

119 self._to_flush.append(name)
120
121 def maybe_add_to_cache(
122 self, query: PostgresQuery, prep: Prepare, name: bytes
123 ) -> Key | None:
124 """Handle 'query' for possible addition to the cache.
125
126 If a new entry has been added, return its key. Return None otherwise
127 (meaning the query is already in cache or cache is not enabled).
128 """
129 # don't do anything if prepared statements are disabled
130 if self.prepare_threshold is None:
131 return None
132
133 if (key := self.key(query)) in self._counts:
134 if prep is Prepare.SHOULD:
135 del self._counts[key]
136 self._names[key] = name
137 else:
138 self._counts[key] += 1
139 self._counts.move_to_end(key)
140 return None
141
142 elif key in self._names:
143 self._names.move_to_end(key)
144 return None
145
146 else:
147 if prep is Prepare.SHOULD:
148 self._names[key] = name
149 else:
150 self._counts[key] = 1
151 return key
152
153 def validate(
154 self, key: Key, prep: Prepare, name: bytes, results: Sequence[PGresult]

Callers 1

_maybe_prepare_genMethod · 0.80

Calls 1

keyMethod · 0.95

Tested by

no test coverage detected