From 0f422eb76d93e5fdd2364926a909fcb9154e48a5 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Tue, 10 Mar 2015 16:11:26 -0400 Subject: [PATCH] Factor out a method to fetch type information --- postgres/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/postgres/__init__.py b/postgres/__init__.py index 5372841..45b0fc8 100644 --- a/postgres/__init__.py +++ b/postgres/__init__.py @@ -835,7 +835,7 @@ def parse(self, s, curs, retry=False): tokens = self.tokenize(s) if len(tokens) != len(self.atttypes): # The number of columns has changed, re-fetch the type info - self.__dict__.update(self._from_db(self.name, curs).__dict__) + self._refetch_type_info(curs) try: values = [ curs.cast(oid, token) @@ -844,7 +844,7 @@ def parse(self, s, curs, retry=False): # The type of a column has changed, re-fetch it and retry once if retry: raise - self.__dict__.update(self._from_db(self.name, curs).__dict__) + self._refetch_type_info(curs) return self.parse(s, curs, True) return self.make(values) @@ -864,6 +864,12 @@ def make(self, values): instance = ModelSubclass(record) return instance + def _refetch_type_info(self, curs): + """Given a cursor, update the current object with a fresh type definition. + """ + new_self = self._from_db(self.name, curs) + self.__dict__.update(new_self.__dict__) + return DelegatingCaster