pywin32 icon indicating copy to clipboard operation
pywin32 copied to clipboard

adodbapi lower/mixed case column names

Open ghost opened this issue 11 years ago • 3 comments

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

Reported by: peterschwalm

Original Ticket: pywin32/feature-requests/106

ghost avatar Jun 24 '13 15:06 ghost

As required by PEP-249, the information you request is stored as part of the cursor's .description attribute:

Cursor Objects should respond to the following methods and attributes. Cursor attributes

.description

This read-only attribute is a sequence of 7-item sequences.

Each of these sequences contains information describing one result column:

   name
   type_code
   display_size
   internal_size
   precision
   scale
   null_ok

The first two items (name and type_code) are mandatory, the other five are optional and are set to None if no meaningful values can be provided.

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.

Original comment by: kf7xm

ghost avatar Apr 22 '14 09:04 ghost

@xoviat @mhammond -- Moving my final reply into an example is a great idea. Where should the example be published?

vernondcole avatar Feb 06 '18 20:02 vernondcole

I guess in the adodbapi/examples dir would be a reasonable start?

mhammond avatar Feb 06 '18 23:02 mhammond