It would be nice if I could access the original mixed case column names of a query result (not only the lower cased). Reason: I would like the names as headers for a .csv, and the original mixed case versions are more readable. (I don't like underline in names and use camel notation instead.)
For myself I have written an extension in a class inheriting from Cursor - which is not nice, because it is dependant of internal implementation of Cursor.
The call of getMixedCaseColumnNames() for the cursor instance makes these names available.
Of course this is only a workaround and an implementation in adodbapi would be much better.
Here it is:
class XCursor0(adodbapi.Cursor):
"wie adodbapi.Cursor aber mit Erweiterung getMixedCaseColumnNames()" def __init__(self, baseCursor): self.__dict__["_cursor"] = baseCursor def __getattr__(self, attr): #print "getAttribute", attr return getattr(self.__dict__["_cursor"], attr) def __setattr__(self, attr, value): #print "setAttr", attr, value return setattr(self.__dict__["_cursor"], attr, value) def getMixedCaseColumnNames(self): "wie columnNames, aber mixed case" colNames = structuredConfig.CommonOptions() for i in range(self.numberOfColumns): fld = self.rs.Fields(i) colNames[fld.Name] = i # columnNames lookup return colNames
(Of course there is also a similar construction for Connection, by which this class is instantiated.)
As required by PEP-249, the information you request is stored as part of the cursor's .description attribute:
http://legacy.python.org/dev/peps/pep-0249/#connection-objects
In adodbapi, the value of
cursor.description[i][0]
is obtained from the recordset as your patch suggests. However, some database engines (such as PostgreSQL) return their column header values as lower cased strings. There is no help for that. You should be able to get your list of the column names as provided by the engine using:colNames = [d[0] for d in cursor.description]
Perhaps confusion is caused by adodbapi's cursor.columnNames attribute which is not part of the PEP. It is a dictionary of lower cased column names used to provide a namedtuple sort of index into the rowset. I should have named it cursor._columnNames to emphasize that it is an internal implementation detail, but I failed to do that.