From: <sk...@po...> - 2008-04-14 17:42:08
|
In the python-sybase documentation it says 0.39 implements Carsten Haese's type mapping extension to the DB-API. That specification is a bit ambiguous when it comes to naming the keys in the input and output maps. It says: When a value is fetched from the database, if the value is not None, its column type (as it would be indicated in cursor.description) is looked up in outputmap, and the resulting callable object is called upon to convert the fetched value... The DB-API uses integer type codes in the description tuple, not strings. PEP 249 says: The type_code can be interpreted by comparing it to the Type Objects specified in the section below. Later on it says: The type_code must compare equal to one of Type Objects defined below. What am I supposed to use as keys in my input and output maps? Python strings (as Carsten's document suggests)? Ints (implied by how type_codes are represeted in .description)? Type Objects? Is there a list somewhere of valid key values? Any example usage? I'm specifically interested in transparently representing dates and times using objects from Python's datetime module. Thanks, -- Skip Montanaro - sk...@po... - http://www.webfast.com/~skip/ |
From: Sébastien S. <sa...@us...> - 2008-04-15 16:41:04
|
Hi Skip, I should definitely write some documentation about this. In the mean time, here is an example that I use myself to shift the origin of Sybase dates and datetimes so that it corresponds to the ones used by Oracle: import Sybase from datetime import date, datetime MY_DATE_ORIGIN = date(1752, 9, 14) MY_DATETIME_ORIGIN = datetime(1752, 9, 14, 0, 0) SYBASE_DATE_ORIGIN = date(1753, 1, 1) SYBASE_DATETIME_ORIGIN = datetime(1753, 1, 1, 0, 0) outputmap = dict(Sybase.DateTimeAsPython) def change_outdatetime_origin(val): d = datetime(val.year, val.month + 1, val.day, val.hour, val.minute, val.second, val.msecond * 1000) if d.date() == SYBASE_DATE_ORIGIN: d = MY_DATETIME_ORIGIN return d def change_outdate_origin(val): d = date(val.year, val.month + 1, val.day) if d == SYBASE_DATE_ORIGIN: d = MY_DATE_ORIGIN return d outputmap.update({ Sybase.CS_DATETIME_TYPE: change_outdatetime_origin, Sybase.CS_DATETIME4_TYPE: change_outdatetime_origin, Sybase.CS_DATE_TYPE: change_outdate_origin }) def change_indate_origin(val): if val < SYBASE_DATE_ORIGIN: val = SYBASE_DATE_ORIGIN return val def change_indatetime_origin(val): if val < SYBASE_DATETIME_ORIGIN: val = SYBASE_DATETIME_ORIGIN return val inputmap = { datetime: change_indatetime_origin, date: change_indate_origin } conn = Sybase.connect(server, user, password, database, inputmap=inputmap, outputmap=outputmap) regards -- Sébastien Sablé sk...@po... a écrit : > In the python-sybase documentation it says 0.39 implements Carsten Haese's > type mapping extension to the DB-API. That specification is a bit ambiguous > when it comes to naming the keys in the input and output maps. It says: > > When a value is fetched from the database, if the value is not None, its > column type (as it would be indicated in cursor.description) is looked > up in outputmap, and the resulting callable object is called upon to > convert the fetched value... > > The DB-API uses integer type codes in the description tuple, not strings. > PEP 249 says: > > The type_code can be interpreted by comparing it to the Type Objects > specified in the section below. > > Later on it says: > > The type_code must compare equal to one of Type Objects defined below. > > What am I supposed to use as keys in my input and output maps? Python > strings (as Carsten's document suggests)? Ints (implied by how type_codes > are represeted in .description)? Type Objects? Is there a list somewhere > of valid key values? Any example usage? I'm specifically interested in > transparently representing dates and times using objects from Python's > datetime module. > > Thanks, > |
From: <sk...@po...> - 2008-06-17 15:48:27
|
(Sorry for the late followup. Just getting back to this.) Sébastien> In the mean time, here is an example that I use myself to Sébastien> shift the origin of Sybase dates and datetimes so that it Sébastien> corresponds to the ones used by Oracle: ... Sébastien> outputmap = dict(Sybase.DateTimeAsPython) ... Sébastien, Thanks for the example. I see that the Sybase module has a DateTimeAsPython dict but not a PythonAsDateTime dict for use as an inputmap. Is that not needed? Even if Sybase.PythonAsDateTime would be empty it would be useful for symmetry to have it, with perhaps a comment why. Thanks, -- Skip Montanaro - sk...@po... - http://www.webfast.com/~skip/ "Be different, express yourself like everyone else." |