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

set statement attributes #1315

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,9 @@ static int Connection_settimeout(PyObject* self, PyObject* value, void* closure)
return -1;
}

// this same value is used for the cursor timeout
cnxn->timeout = timeout;

SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
ret = SQLSetConnectAttr(cnxn->hdbc, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER)(uintptr_t)timeout, SQL_IS_UINTEGER);
Expand All @@ -986,8 +989,6 @@ static int Connection_settimeout(PyObject* self, PyObject* value, void* closure)
return -1;
}

cnxn->timeout = timeout;

return 0;
}

Expand Down
41 changes: 38 additions & 3 deletions src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,14 +1129,48 @@ static PyObject* Cursor_executemany(PyObject* self, PyObject* args)
Py_RETURN_NONE;
}

static char set_attr_doc[] =
"set_attr(attr_id, value) -> None\n\n"
"Calls SQLSetStmtAttr with the given values.\n\n"
"attr_id\n"
" The attribute id (integer) to set. These are ODBC or driver constants.\n\n"
"value\n"
" An integer value.\n\n"
"At this time, only integer values are supported and are always passed as SQLUINTEGER.";

static PyObject* Cursor_set_attr(PyObject* self, PyObject* args)
{
if (!Cursor_Check(self))
{
PyErr_SetString(ProgrammingError, "Invalid cursor object.");
return 0;
}

int id;
int value;
if (!PyArg_ParseTuple(args, "ii", &id, &value))
return 0;

Cursor* cursor = (Cursor *)self;

SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
ret = SQLSetStmtAttr(cursor->hstmt, id, (SQLPOINTER)(intptr_t)value, SQL_IS_INTEGER);
Py_END_ALLOW_THREADS

if (!SQL_SUCCEEDED(ret))
return RaiseErrorFromHandle(cursor->cnxn, "SQLSetStmtAttr", cursor->cnxn->hdbc, cursor->hstmt);
Py_RETURN_NONE;
}

static PyObject* Cursor_setinputsizes(PyObject* self, PyObject* sizes)
{
if (!Cursor_Check(self))
{
PyErr_SetString(ProgrammingError, "Invalid cursor object.");
return 0;
}

Cursor *cur = (Cursor*)self;
if (Py_None == sizes)
{
Expand Down Expand Up @@ -2410,7 +2444,8 @@ static PyMethodDef Cursor_methods[] =
{ "skip", (PyCFunction)Cursor_skip, METH_VARARGS, skip_doc },
{ "commit", (PyCFunction)Cursor_commit, METH_NOARGS, commit_doc },
{ "rollback", (PyCFunction)Cursor_rollback, METH_NOARGS, rollback_doc },
{"cancel", (PyCFunction)Cursor_cancel, METH_NOARGS, cancel_doc},
{ "cancel", (PyCFunction)Cursor_cancel, METH_NOARGS, cancel_doc },
{ "set_attr", Cursor_set_attr, METH_VARARGS, set_attr_doc },
{"__enter__", Cursor_enter, METH_NOARGS, enter_doc },
{"__exit__", Cursor_exit, METH_VARARGS, exit_doc },
{0, 0, 0, 0}
Expand Down Expand Up @@ -2527,7 +2562,7 @@ Cursor_New(Connection* cnxn)
if (cnxn->timeout)
{
Py_BEGIN_ALLOW_THREADS
ret = SQLSetStmtAttr(cur->hstmt, SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER)(uintptr_t)cnxn->timeout, 0);
ret = SQLSetStmtAttr(cur->hstmt, SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER)(uintptr_t)cnxn->timeout, SQL_IS_UINTEGER);
Py_END_ALLOW_THREADS

if (!SQL_SUCCEEDED(ret))
Expand Down
9 changes: 8 additions & 1 deletion src/pyodbcmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ static bool AllocateEnv()
}
}
Py_DECREF(odbcversion);

if (!SQL_SUCCEEDED(SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, defaultVersion, sizeof(int))))
{
PyErr_SetString(PyExc_RuntimeError, "Unable to set SQL_ATTR_ODBC_VERSION attribute.");
Expand Down Expand Up @@ -1064,6 +1064,9 @@ static const ConstantDef aConstants[] = {
MAKECONST(SQL_PACKET_SIZE),
MAKECONST(SQL_ATTR_ANSI_APP),

// Cursor Attributes
MAKECONST(SQL_ATTR_QUERY_TIMEOUT),

// SQL_CONVERT_X
MAKECONST(SQL_CONVERT_FUNCTIONS),
MAKECONST(SQL_CONVERT_BIGINT),
Expand Down Expand Up @@ -1107,6 +1110,10 @@ static const ConstantDef aConstants[] = {
MAKECONST(SQL_OJ_NOT_ORDERED),
MAKECONST(SQL_OJ_INNER),
MAKECONST(SQL_OJ_ALL_COMPARISON_OPS),

// Misc
MAKECONST(SQL_IS_UINTEGER),
MAKECONST(SQL_IS_UINTEGER),
Comment on lines +1115 to +1116
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate lines. Should one of these be SQL_IS_INTEGER perhaps?

};


Expand Down