Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend setinputsizes() to allow specifying type+length+precision/scale #280

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,24 @@ static bool GetParameterInfo(Cursor* cur, Py_ssize_t index, PyObject* param, Par
return false;
}

static bool GetIntVal(PyObject *obj, SQLULEN *pOut)
{
bool ret = false;
#if PY_MAJOR_VERSION < 3
if (ret = PyInt_Check(obj))
{
*pOut = (SQLULEN)PyInt_AS_LONG(obj);
}
else
#endif
if (ret = PyLong_Check(obj))
{
*pOut = (SQLULEN)PyLong_AsLong(obj);
}
Py_XDECREF(obj);
return ret;
}

bool BindParameter(Cursor* cur, Py_ssize_t index, ParamInfo& info)
{
SQLSMALLINT sqltype = info.ParameterType;
Expand All @@ -1229,7 +1247,7 @@ bool BindParameter(Cursor* cur, Py_ssize_t index, ParamInfo& info)
// integer - sets colsize
// type object - sets sqltype (not implemented yet; mapping between Python
// and SQL types is not 1:1 so doesn't seem to offer much)
// Consider: sequence of (colsize, sqltype, scale) ?
// sequence (sqltype, colsize, decimaldigits)
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(desc))
{
Expand All @@ -1241,6 +1259,26 @@ bool BindParameter(Cursor* cur, Py_ssize_t index, ParamInfo& info)
{
colsize = (SQLULEN)PyLong_AsLong(desc);
}
else if (PySequence_Check(desc))
{
SQLULEN tmp;
Py_ssize_t len = PySequence_Size(desc);

if (len > 0 && GetIntVal(PySequence_ITEM(desc, 0), &tmp))
{
sqltype = (SQLSMALLINT)tmp;
}

if (len > 1 && GetIntVal(PySequence_ITEM(desc, 1), &tmp))
{
colsize = tmp;
}

if (len > 2 && GetIntVal(PySequence_ITEM(desc, 2), &tmp))
{
scale = (SQLSMALLINT)tmp;
}
}
}
Py_XDECREF(desc);
}
Expand Down