From: Billy G. A. <bal...@us...> - 2002-11-01 04:34:20
|
Update of /cvsroot/pypgsql/pypgsql/pyPgSQL In directory usw-pr-cvs1:/tmp/cvs-serv15058/pyPgSQL Modified Files: PgSQL.py Log Message: 26OCT2002 bga Column access by name (attribute and dictionary) now supports mixed-case column name. Be aware that if you define mixed-case column names in the database, you have to use the mixed-case name to access the column or it will not be found. For column names that were not defined with mixed-case in the database, the column access by name is case insensitive. Index: PgSQL.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pyPgSQL/PgSQL.py,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** PgSQL.py 27 Oct 2002 04:14:52 -0000 1.19 --- PgSQL.py 1 Nov 2002 04:34:16 -0000 1.20 *************** *** 38,41 **** --- 38,48 ---- # useless nowadays that we've mostly left line printers | # beyond us. | + # 26OCT2002 bga - Column access by name (attribute and dictionary) now | + # supports mixed-case column name. Be aware that if | + # you define mixed-case column names in the database, | + # you have to use the mixed-case name to access the co- | + # lumn or it will not be found. For column names that | + # were not defined with mixed-case in the database, the | + # column access by name is case insensitive. | # 02OCT2002 gh - Only support mxDateTime 2.x and give useful error | # message if import fails. | *************** *** 1663,1669 **** def __getattr__(self, key): ! key = key.upper() if self._xlatkey.has_key(key): return self.baseObj[self._xlatkey[key]] raise AttributeError, key --- 1670,1685 ---- def __getattr__(self, key): ! # When retrieving column data by name as an attribute, we must be ! # aware that a column name can be defiend with mixed-case within the ! # database. Because of this we must first check for an exact match ! # with the given key. If that fails, then we match with the key that ! # has been changed to lower case. Note: we are relying on the fact ! # that PostgreSQL sends column names that are not defined with mixed- ! # case to the client as lower-case names. ! keyl = key.lower() if self._xlatkey.has_key(key): return self.baseObj[self._xlatkey[key]] + if self._xlatkey.has_key(keyl): + return self.baseObj[self._xlatkey[keyl]] raise AttributeError, key *************** *** 1674,1680 **** raise AttributeError, "%s is read-only." % key ! key = key.upper() if self._xlatkey.has_key(key): self.__dict__['baseObj'][self._xlatkey[key]] = value else: raise AttributeError, key --- 1690,1700 ---- raise AttributeError, "%s is read-only." % key ! # Try an exact match first, then the case-insensitive match. ! # See comment in __getattr__ for details. ! keyl = key.lower() if self._xlatkey.has_key(key): self.__dict__['baseObj'][self._xlatkey[key]] = value + elif self._xlatkey.has_key(keyl): + self.__dict__['baseObj'][self._xlatkey[keyl]] = value else: raise AttributeError, key *************** *** 1685,1694 **** def __getitem__(self, key): if isinstance(key, StringType): ! key = self._xlatkey[key.upper()] return self.baseObj[key] def __setitem__(self, key, value): if isinstance(key, StringType): ! key = self._xlatkey[key.upper()] self.baseObj[key] = value --- 1705,1724 ---- def __getitem__(self, key): if isinstance(key, StringType): ! # Try an exact match first, then the case-insensitive match. ! # See comment in __getattr__ for details. ! try: ! key = self._xlatkey[key] ! except: ! key = self._xlatkey[key.lower()] return self.baseObj[key] def __setitem__(self, key, value): if isinstance(key, StringType): ! # Try an exact match first, then the case-insensitive match. ! # See comment in __getattr__ for details. ! try: ! key = self._xlatkey[key] ! except: ! key = self._xlatkey[key.lower()] self.baseObj[key] = value *************** *** 1729,1738 **** def has_key(self, key): ! return self._xlatkey.has_key(key.upper()) def get(self, key, defaultval=None): ! if self.has_key(key): ! return self[key] ! else: return defaultval --- 1759,1779 ---- def has_key(self, key): ! # Try an exact match first, then the case-insensitive match. ! # See comment in __getattr__ for details. ! if not self._xlatkey.has_key(key): ! key = key.lower() ! return self._xlatkey.has_key(key) def get(self, key, defaultval=None): ! try: ! if isinstance(key, StringType): ! # Try an exact match first, then the case-insensitive match. ! # See comment in __getattr__ for details. ! try: ! key = self._xlatkey[key] ! except: ! key = self._xlatkey[key.lower()] ! return self[key] ! except: return defaultval *************** *** 1745,1752 **** if mapname is None: for _i in range(len(description)): ! klass.__dict__['_xlatkey'][description[_i][0].upper()] = _i else: for k, v in mapname.items(): ! klass.__dict__['_xlatkey'][k.upper()] = v return klass --- 1786,1793 ---- if mapname is None: for _i in range(len(description)): ! klass.__dict__['_xlatkey'][description[_i][0]] = _i else: for k, v in mapname.items(): ! klass.__dict__['_xlatkey'][k] = v return klass |