/** Convert a Python datetime.time to MySQL TIME. Convert the PyObject obj, which must be a datetime.time, to MySQL TIME value. Raises TypeError when obj is not a PyTime_Type. @param obj the PyObject to be converted @return Converted time object. @retval PyBytes Python v3 @retval PyString Python v2 @retval NULL Exception */ PyObject* pytomy_time(PyObject *obj) { char result[17]= ""; PyDateTime_IMPORT; if (!obj || !PyTime_Check(obj)) { PyErr_SetString(PyExc_ValueError, "Object must be a datetime.time"); return NULL; } if (PyDateTime_TIME_GET_MICROSECOND(obj)) { PyOS_snprintf(result, 17, "%02d:%02d:%02d.%06d", PyDateTime_TIME_GET_HOUR(obj), PyDateTime_TIME_GET_MINUTE(obj), PyDateTime_TIME_GET_SECOND(obj), PyDateTime_TIME_GET_MICROSECOND(obj)); } else { PyOS_snprintf(result, 17, "%02d:%02d:%02d", PyDateTime_TIME_GET_HOUR(obj), PyDateTime_TIME_GET_MINUTE(obj), PyDateTime_TIME_GET_SECOND(obj)); } return PyBytesFromString(result); }
//----------------------------------------------------------------------------- // Variable_TypeByValue() // Return a variable type given a Python object or NULL if the Python // object does not have a corresponding variable type. //----------------------------------------------------------------------------- static udt_VariableType *Variable_TypeByValue( PyObject* value, // Python type SQLUINTEGER* size) // size to use (OUT) { if (value == Py_None) { *size = 1; return &ceString_VariableType; } if (ceString_Check(value)) { *size = ceString_GetSize(value); return &ceString_VariableType; } #if PY_MAJOR_VERSION < 3 if (PyUnicode_Check(value)) { *size = PyUnicode_GET_SIZE(value); return &vt_Unicode; } #endif if (ceBinary_Check(value)) { udt_StringBuffer temp; if (StringBuffer_FromBinary(&temp, value) < 0) return NULL; *size = temp.size; StringBuffer_Clear(&temp); return &vt_Binary; } if (PyBool_Check(value)) return &vt_Bit; if (PyInt_Check(value)) return &vt_Integer; if (PyLong_Check(value)) return &vt_BigInteger; if (PyFloat_Check(value)) return &vt_Double; if (Py_TYPE(value) == (PyTypeObject*) g_DecimalType) return &vt_Decimal; if (PyTime_Check(value)) return &vt_Time; if (PyDateTime_Check(value)) return &vt_Timestamp; if (PyDate_Check(value)) return &vt_Timestamp; PyErr_Format(g_NotSupportedErrorException, "Variable_TypeByValue(): unhandled data type %s", Py_TYPE(value)->tp_name); return NULL; }
int fudgepyc_convertPythonToTime ( FudgeTime * target, PyObject * source ) { FudgeStatus status; int hour, minute, second, microsecond, offset = 0, result, hastz; PyObject * utcoffset; if ( ! ( PyTime_Check ( source ) || PyDateTime_Check ( source ) ) ) { exception_raise_any ( PyExc_TypeError, "Only datetime.time and datetime.datetime " "types can be converted in to FudgeTime" ); return -1; } if ( ! ( utcoffset = PyObject_CallMethod ( source, "utcoffset", "" ) ) ) return -1; if ( ( hastz = ( utcoffset != Py_None ) ) ) result = fudgepyc_convertUtcOffset ( &offset, utcoffset ); else result = offset = 0; Py_DECREF( utcoffset ); if ( result ) return -1; if ( fudgepyc_convertAttrToInt ( &hour, source, "hour" ) || fudgepyc_convertAttrToInt ( &minute, source, "minute" ) || fudgepyc_convertAttrToInt ( &second, source, "second" ) || fudgepyc_convertAttrToInt ( µsecond, source, "microsecond" ) ) return -1; if ( hastz ) status = FudgeTime_initialiseWithTimezone ( target, second + minute * 60 + hour * 3600, microsecond * 1000, FUDGE_DATETIME_PRECISION_MICROSECOND, offset ); else status = FudgeTime_initialise ( target, second + minute * 60 + hour * 3600, microsecond * 1000, FUDGE_DATETIME_PRECISION_MICROSECOND ); return exception_raiseOnError ( status ); }
//----------------------------------------------------------------------------- // TimeVar_SetValue() // Set the value of the variable. //----------------------------------------------------------------------------- static int TimeVar_SetValue( udt_TimeVar *var, // variable to set value for unsigned pos, // array position to set PyObject *value) // value to set { TIME_STRUCT *sqlValue; sqlValue = &var->data[pos]; if (PyDateTime_Check(value)) { sqlValue->hour = PyDateTime_DATE_GET_HOUR(value); sqlValue->minute = PyDateTime_DATE_GET_MINUTE(value); sqlValue->second = PyDateTime_DATE_GET_SECOND(value); } else if (PyTime_Check(value)) { sqlValue->hour = PyDateTime_TIME_GET_HOUR(value); sqlValue->minute = PyDateTime_TIME_GET_MINUTE(value); sqlValue->second = PyDateTime_TIME_GET_SECOND(value); } else { PyErr_SetString(PyExc_TypeError, "expecting datetime or time data"); return -1; } return 0; }
static bool GetParameterInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInfo& info) { // Determines the type of SQL parameter that will be used for this parameter based on the Python data type. // // Populates `info`. // TODO: if the param is a SQLParameter object, then bind to the wrapped value and use the input/output type from that // Hold a reference to param until info is freed, because info will often be holding data borrowed from param. if (PyObject_TypeCheck(param, (PyTypeObject*)SQLParameter_type)) { info.pParam = ((SQLParameter*)param)->value; info.InputOutputType = ((SQLParameter*)param)->type; } else { info.pParam = param; info.InputOutputType = SQL_PARAM_INPUT; } if (info.pParam == Py_None) return GetNullInfo(cur, index, info); if (info.pParam == null_binary) return GetNullBinaryInfo(cur, index, info); if (PyBytes_Check(info.pParam)) return GetBytesInfo(cur, index, info.pParam, info); if (PyUnicode_Check(info.pParam)) return GetUnicodeInfo(cur, index, info.pParam, info); if (PyBool_Check(info.pParam)) return GetBooleanInfo(cur, index, info.pParam, info); if (PyDateTime_Check(info.pParam)) return GetDateTimeInfo(cur, index, info.pParam, info); if (PyDate_Check(info.pParam)) return GetDateInfo(cur, index, info.pParam, info); if (PyTime_Check(info.pParam)) return GetTimeInfo(cur, index, info.pParam, info); if (PyLong_Check(info.pParam)) return GetLongInfo(cur, index, info.pParam, info); if (PyFloat_Check(info.pParam)) return GetFloatInfo(cur, index, info.pParam, info); if (PyDecimal_Check(info.pParam)) return GetDecimalInfo(cur, index, info.pParam, info); #if PY_VERSION_HEX >= 0x02060000 if (PyByteArray_Check(info.pParam)) return GetByteArrayInfo(cur, index, info.pParam, info); #endif #if PY_MAJOR_VERSION < 3 if (PyInt_Check(info.pParam)) return GetIntInfo(cur, index, info.pParam, info); if (PyBuffer_Check(info.pParam)) return GetBufferInfo(cur, index, info.pParam, info); #endif RaiseErrorV("HY105", ProgrammingError, "Invalid parameter type. param-index=%zd param-type=%s", index, Py_TYPE(info.pParam)->tp_name); return false; }