Update of /cvsroot/pywin32/pywin32/win32/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28795
Modified Files:
Tag: py3k
dbi.cpp dbi.def dbi.h odbc.cpp
Log Message:
Return sql dates as datetime objects
Remove dbiDate and dbiRowId (with an eye toward removing dbi
module altogether)
Index: dbi.h
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/dbi.h,v
retrieving revision 1.4
retrieving revision 1.4.4.1
diff -C2 -d -r1.4 -r1.4.4.1
*** dbi.h 13 Jul 2004 05:15:00 -0000 1.4
--- dbi.h 16 Sep 2008 01:01:17 -0000 1.4.4.1
***************
*** 22,34 ****
#endif
- DBI_FUNC(int) dbiIsDate(const PyObject *o);
DBI_FUNC(int) dbiIsRaw(const PyObject *o);
- DBI_FUNC(int) dbiIsRowId(const PyObject *o);
/* These do not INCREF */
DBI_FUNC(PyObject) *dbiValue(PyObject *o);
- DBI_FUNC(PyObject) *dbiMakeDate(PyObject *contents);
DBI_FUNC(PyObject) *dbiMakeRaw(PyObject *contents);
- DBI_FUNC(PyObject) *dbiMakeRowId(PyObject *contents);
DBI_FUNC(PyObject)*DbiString;
--- 22,30 ----
Index: odbc.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/odbc.cpp,v
retrieving revision 1.20.2.4
retrieving revision 1.20.2.5
diff -C2 -d -r1.20.2.4 -r1.20.2.5
*** odbc.cpp 15 Sep 2008 07:46:37 -0000 1.20.2.4
--- odbc.cpp 16 Sep 2008 01:01:17 -0000 1.20.2.5
***************
*** 16,19 ****
--- 16,20 ----
#include "PyWinTypes.h"
+ #include "PyWinObjects.h"
#include "structmember.h"
***************
*** 37,40 ****
--- 38,42 ----
#include "dbi.h" /*$ This is a hack */
static PyObject *odbcError;
+ static PyObject *datetime_module, *datetime_class;
#define MAX_STR 45
***************
*** 611,623 ****
{
const TIMESTAMP_STRUCT *dt = (const TIMESTAMP_STRUCT *) v;
! struct tm gt;
! gt.tm_isdst = -1; /* figure out DST */
! gt.tm_year = dt->year-1900;
! gt.tm_mon = dt->month-1;
! gt.tm_mday = dt->day;
! gt.tm_hour = dt->hour;
! gt.tm_min = dt->minute;
! gt.tm_sec = dt->second;
! return dbiMakeDate(PyLong_FromLongLong(mktime(>)));
}
--- 613,620 ----
{
const TIMESTAMP_STRUCT *dt = (const TIMESTAMP_STRUCT *) v;
! return PyObject_CallFunction(datetime_class, "hhhhhhl",
! dt->year, dt->month, dt->day,
! dt->hour, dt->minute, dt->second, dt->fraction);
! // ??? Not sure what units fraction uses ???
}
***************
*** 819,839 ****
static int ibindDate(cursorObject*cur, int column, PyObject *item)
{
- long val = PyInt_AsLong(item);
int len = sizeof(TIMESTAMP_STRUCT);
-
InputBinding *ib = initInputBinding(cur, len);
if (!ib)
return 0;
-
TIMESTAMP_STRUCT *dt = (TIMESTAMP_STRUCT*) ib->bind_area ;
! struct tm *gt = localtime((const time_t *)&val);
!
! dt->year = 1900 + gt->tm_year;
! dt->month = gt->tm_mon + 1;
! dt->day = gt->tm_mday;
! dt->hour = gt->tm_hour;
! dt->minute = gt->tm_min;
! dt->second = gt->tm_sec;
! dt->fraction = 0;
if (unsuccessful(SQLBindParameter(
--- 816,862 ----
static int ibindDate(cursorObject*cur, int column, PyObject *item)
{
int len = sizeof(TIMESTAMP_STRUCT);
InputBinding *ib = initInputBinding(cur, len);
if (!ib)
return 0;
TIMESTAMP_STRUCT *dt = (TIMESTAMP_STRUCT*) ib->bind_area ;
! ZeroMemory(dt, sizeof(*dt));
! // Accept either a PyTime or datetime object
! if (PyTime_Check(item)){
! SYSTEMTIME st;
! if (!((PyTime *)item)->GetTime(&st))
! return 0;
! dt->year = st.wYear;
! dt->month = st.wMonth;
! dt->day = st.wDay;
! dt->hour = st.wHour;
! dt->minute = st.wMinute;
! dt->second = st.wSecond;
! dt->fraction = st.wMilliseconds;
! // ??? Not sure what units fraction is in ???
! }
! else{
! // Python 2.3 doesn't have C Api for datetime
! PyObject *timeseq = PyObject_CallMethod(item, "timetuple", NULL);
! if (timeseq==NULL)
! return 0;
! PyObject *timetuple=PySequence_Tuple(timeseq);
! if (timetuple==NULL){
! Py_DECREF(timeseq);
! return 0;
! }
! // Last 3 items are ignored. Need to figure out how to get fractional seconds also.
! PyObject *obwday, *obyday, *obdst;
! if (!PyArg_ParseTuple(timetuple, "hhh|hhhOOO:TIMESTAMP_STRUCT",
! &dt->year, &dt->month, &dt->day,
! &dt->hour, &dt->minute, &dt->second,
! &obwday, &obyday, &obdst)){
! Py_DECREF(timeseq);
! Py_DECREF(timetuple);
! return 0;
! }
! Py_DECREF(timeseq);
! Py_DECREF(timetuple);
! }
if (unsuccessful(SQLBindParameter(
***************
*** 965,969 ****
{
const WCHAR *wval = (WCHAR *)PyUnicode_AsUnicode(item);
! Py_ssize_t nchars = PyUnicode_GetSize(item);
Py_ssize_t nbytes = nchars * sizeof(WCHAR);
--- 988,992 ----
{
const WCHAR *wval = (WCHAR *)PyUnicode_AsUnicode(item);
! Py_ssize_t nchars = PyUnicode_GetSize(item) + 1;
Py_ssize_t nbytes = nchars * sizeof(WCHAR);
***************
*** 972,976 ****
return 0;
! wcscpy((WCHAR *)ib->bind_area, wval);
/* See above re SQL_VARCHAR */
int sqlType = SQL_WVARCHAR;
--- 995,999 ----
return 0;
! wcsncpy((WCHAR *)ib->bind_area, wval, nchars);
/* See above re SQL_VARCHAR */
int sqlType = SQL_WVARCHAR;
***************
*** 1045,1052 ****
{
rv = ibindRaw(cur, iCol, dbiValue(item));
- }
- else if (dbiIsDate(item))
- {
- rv = ibindDate(cur, iCol, dbiValue(item));
}
else if (PyLong_Check(item))
--- 1068,1071 ----
***************
*** 1074,1077 ****
--- 1093,1100 ----
rv = ibindFloat(cur, iCol, item);
}
+ else if (PyTime_Check(item) || PyObject_HasAttrString(item, "timetuple"))
+ {
+ rv = ibindDate(cur, iCol, item);
+ }
else
{
***************
*** 1241,1246 ****
}
new_tuple = Py_BuildValue(
! "(sOiiiii)",
! name, typeOf, dsize,
(int)vsize, prec, scale, nullok);
--- 1264,1269 ----
}
new_tuple = Py_BuildValue(
! "(NOiiiii)",
! PyWinObject_FromTCHAR(name), typeOf, dsize,
(int)vsize, prec, scale, nullok);
***************
*** 1936,1939 ****
--- 1959,1971 ----
RETURN_ERROR;
+ // Sql dates are now returned as python's datetime object.
+ // C Api for datetime didn't exist in 2.3, stick to dynamic semantics for now.
+ datetime_module=PyImport_ImportModule("datetime");
+ if (datetime_module == NULL)
+ RETURN_ERROR;
+ datetime_class = PyObject_GetAttrString(datetime_module, "datetime");
+ if (datetime_class == NULL)
+ RETURN_ERROR;
+
if (unsuccessful(SQLAllocEnv(&Env)))
{
Index: dbi.def
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/dbi.def,v
retrieving revision 1.1.4.1
retrieving revision 1.1.4.2
diff -C2 -d -r1.1.4.1 -r1.1.4.2
*** dbi.def 29 Aug 2008 04:59:26 -0000 1.1.4.1
--- dbi.def 16 Sep 2008 01:01:17 -0000 1.1.4.2
***************
*** 1,12 ****
EXPORTS
dbiIsRaw
- dbiIsDate
dbiValue
- dbiMakeDate
dbiMakeRaw
- dbiMakeRowId
DbiString
DbiRaw
- DbiRowId
DbiNumber
DbiDate
--- 1,8 ----
Index: dbi.cpp
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/src/dbi.cpp,v
retrieving revision 1.10.2.2
retrieving revision 1.10.2.3
diff -C2 -d -r1.10.2.2 -r1.10.2.3
*** dbi.cpp 6 Sep 2008 22:33:20 -0000 1.10.2.2
--- dbi.cpp 16 Sep 2008 01:01:16 -0000 1.10.2.3
***************
*** 15,25 ****
#include "structmember.h"
- /* Python 1.5.2 doesn't have PyObject_New
- PyObject_NEW is not *quite* as safe, but seem to work fine
- (as all win32all for 1.5.2 used it! */
- #ifndef PyObject_New
- #define PyObject_New PyObject_NEW
- #endif
-
#define DBI_EXPORT
--- 15,18 ----
***************
*** 63,71 ****
{
Py_DECREF(((DbiContainer *) self)->objectOf);
- #ifdef PyObject_Del /* see top of file for 1.5.2 comments */
PyObject_Del(self);
- #else
- PyMem_Free(self);
- #endif
}
--- 56,60 ----
***************
*** 82,93 ****
}
- static PyObject *dateStr(PyObject *o)
- {
- time_t t;
- if (!PyWinObject_Astime_t(dbiValue(o), &t))
- return NULL;
- return PyString_FromStringAndSize(ctime(&t), 24); /* less \n */
- }
-
#define delg(a) dbiValue(a)->ob_type->tp_as_number
--- 71,74 ----
***************
*** 96,220 ****
return delg(a)->nb_add(dbiValue(a),b);
}
- static PyObject* dt_nb_subtract(PyObject* a, PyObject* b)
- {
- return delg(a)->nb_subtract(dbiValue(a),b);
- }
- static PyObject* dt_nb_int(PyObject* a)
- {
- return delg(a)->nb_int(dbiValue(a));
- }
- static PyObject* dt_nb_long(PyObject* a)
- {
- return delg(a)->nb_long(dbiValue(a));
- }
- static PyObject* dt_nb_float(PyObject* a)
- {
- return delg(a)->nb_float(dbiValue(a));
- }
-
- #if (PY_VERSION_HEX < 0x03000000)
- static PyObject* dt_nb_oct(PyObject* a)
- {
- return delg(a)->nb_oct(dbiValue(a));
- }
- static PyObject* dt_nb_hex(PyObject* a)
- {
- return delg(a)->nb_hex(dbiValue(a));
- }
- #endif
- static int dt_cmp(PyObject *a, PyObject *b)
- {
- return dbiValue(a)->ob_type->tp_compare(a,b);
- }
- static int dt_nb_coerce(PyObject **, PyObject **);
-
- PyNumberMethods dt_as_number =
- {
- dt_nb_add , /* nb_add */
- dt_nb_subtract , /* nb_subtract */
- 0, /* nb_multiply */
- #if (PY_VERSION_HEX < 0x03000000)
- 0, /* nb_divide (removed in Python 3)*/
- #endif
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- 0, /* nb_nonzero */
- 0, /* nb_invert */
- 0, /* nb_lshift */
- 0, /* nb_rshift */
- 0, /* nb_and */
- 0, /* nb_xor */
- 0, /* nb_or */
- #if (PY_VERSION_HEX < 0x03000000)
- dt_nb_coerce, /* nb_coerce (removed in Python 3) */
- #endif
- dt_nb_int , /* nb_int */
- dt_nb_long , /* nb_long */
- dt_nb_float , /* nb_float */
- #if (PY_VERSION_HEX < 0x03000000)
- dt_nb_oct , /* nb_oct */
- dt_nb_hex /* nb_hex */
- #endif
- };
-
- static PyTypeObject DbiDate_Type =
- {
- PYWIN_OBJECT_HEAD
- "DbiDate", /*tp_name */
- sizeof(DbiContainer), /*tp_basicsize */
- 0, /*tp_itemsize */
- dbiDealloc, /*tp_dealloc */
- 0, /*tp_print */
- 0, /*tp_getattr */
- 0, /*tp_setattr */
- dt_cmp, /*tp_compare */
- 0, /*tp_repr */
- &dt_as_number, /**tp_as_number */
- 0, /**tp_as_sequence */
- 0, /**tp_as_mapping */
- 0, /*tp_hash */
- 0, /*tp_call */
- dateStr, /*tp_str */
- PyObject_GenericGetAttr, /* tp_getattro dbiGetAttr */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- noMethods, /* tp_methods */
- DbiContainer_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- };
-
- static int dt_nb_coerce(PyObject **pv, PyObject **pw)
- {
- if (PyNumber_Check(*pw))
- {
- DbiContainer *dbt = PyObject_New(DbiContainer, &DbiDate_Type);
- dbt->objectOf = PyNumber_Int(*pw);
- Py_INCREF(*pv);
- *pw = (PyObject *)dbt;
- return 0;
- }
- return 1; /* Can't do it */
- }
static PyTypeObject DbiRaw_Type =
--- 77,81 ----
***************
*** 260,310 ****
};
- static PyTypeObject DbiRowId_Type =
- {
- PYWIN_OBJECT_HEAD
- "DbiRowId", /*tp_name */
- sizeof(DbiContainer), /*tp_basicsize */
- 0, /*tp_itemsize */
- dbiDealloc, /*tp_dealloc */
- 0, /*tp_print */
- 0, /*tp_getattr */
- 0, /*tp_setattr */
- 0, /*tp_compare */
- 0, /*tp_repr */
- 0, /**tp_as_number */
- 0, /**tp_as_sequence */
- 0, /**tp_as_mapping */
- 0, /*tp_hash */
- 0, /*tp_call */
- dbiRawStr, /*tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- noMethods, /* tp_methods */
- DbiContainer_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- };
-
- static PyObject *makeDate(PyObject *self, PyObject *args)
- {
- return makeDbiTypeTP(args, &DbiDate_Type);
- }
-
static PyObject *makeRaw(PyObject *self, PyObject *args)
{
--- 121,124 ----
***************
*** 312,320 ****
}
- static PyObject *makeRowId(PyObject *self, PyObject *args)
- {
- return makeDbiTypeTP(args, &DbiRowId_Type);
- }
-
static PyObject *makeDbiType(PyObject *o, PyTypeObject *ty)
{
--- 126,129 ----
***************
*** 324,332 ****
}
- PyObject *dbiMakeDate(PyObject *contents)
- {
- return makeDbiType(contents, &DbiDate_Type);
- }
-
PyObject *dbiMakeRaw(PyObject *contents)
{
--- 133,136 ----
***************
*** 334,348 ****
}
- PyObject *dbiMakeRowId(PyObject *contents)
- {
- return makeDbiType(contents, &DbiRowId_Type);
- }
-
-
-
static PyMethodDef globalMethods[] =
{
! { "dbDate", makeDate, 1} ,
! { "dbiDate", makeDate, 1} ,
{ "dbRaw", makeRaw, 1} ,
{ "dbiRaw", makeRaw, 1} ,
--- 138,145 ----
}
static PyMethodDef globalMethods[] =
{
! // { "dbDate", makeDate, 1} ,
! // { "dbiDate", makeDate, 1} ,
{ "dbRaw", makeRaw, 1} ,
{ "dbiRaw", makeRaw, 1} ,
***************
*** 382,388 ****
RETURN_ERROR;
! if (PyType_Ready(&DbiDate_Type) == -1
! ||PyType_Ready(&DbiRaw_Type) == -1
! ||PyType_Ready(&DbiRowId_Type) == -1)
RETURN_ERROR;
--- 179,183 ----
RETURN_ERROR;
! if (PyType_Ready(&DbiRaw_Type) == -1)
RETURN_ERROR;
***************
*** 454,469 ****
}
- int dbiIsDate(const PyObject *o)
- {
- return o->ob_type == &DbiDate_Type;
- }
-
int dbiIsRaw(const PyObject *o)
{
return o->ob_type == &DbiRaw_Type;
}
-
- int dbiIsRowId(const PyObject *o)
- {
- return o->ob_type == &DbiRowId_Type;
- }
--- 249,254 ----
|