Skip to content

Commit

Permalink
Added readonly keyword to connect.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkleehammer committed Feb 14, 2012
1 parent dc35242 commit 7555f7d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/connection.cpp
Expand Up @@ -140,7 +140,7 @@ static bool Connect(PyObject* pConnectString, HDBC hdbc, bool fAnsi, long timeou
}


PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, bool fAnsi, bool fUnicodeResults, long timeout)
PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, bool fAnsi, bool fUnicodeResults, long timeout, bool fReadOnly)
{
// pConnectString
// A string or unicode object. (This must be checked by the caller.)
Expand Down Expand Up @@ -226,6 +226,21 @@ PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, bool fAnsi,
}
}

if (fReadOnly)
{
SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
ret = SQLSetConnectAttr(cnxn->hdbc, SQL_ATTR_ACCESS_MODE, (SQLPOINTER)SQL_MODE_READ_ONLY, 0);
Py_END_ALLOW_THREADS

if (!SQL_SUCCEEDED(ret))
{
RaiseErrorFromHandle("SQLSetConnnectAttr(SQL_ATTR_ACCESS_MODE)", cnxn->hdbc, SQL_NULL_HANDLE);
Py_DECREF(cnxn);
return 0;
}
}

TRACE("cnxn.new cnxn=%p hdbc=%d\n", cnxn, cnxn->hdbc);

//
Expand Down
2 changes: 1 addition & 1 deletion src/connection.h
Expand Up @@ -71,6 +71,6 @@ struct Connection
* Used by the module's connect function to create new connection objects. If unable to connect to the database, an
* exception is set and zero is returned.
*/
PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, bool fAnsi, bool fUnicodeResults, long timeout);
PyObject* Connection_New(PyObject* pConnectString, bool fAutoCommit, bool fAnsi, bool fUnicodeResults, long timeout, bool fReadOnly);

#endif
10 changes: 8 additions & 2 deletions src/pyodbcmodule.cpp
Expand Up @@ -252,6 +252,7 @@ static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs)
int fAutoCommit = 0;
int fAnsi = 0; // force ansi
int fUnicodeResults = 0;
int fReadOnly = 0;
long timeout = 0;

Py_ssize_t size = args ? PyTuple_Size(args) : 0;
Expand Down Expand Up @@ -317,7 +318,12 @@ static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs)
return 0;
continue;
}

if (Text_EqualsI(key, "readonly"))
{
fReadOnly = PyObject_IsTrue(value);
continue;
}

// Map DB API recommended names to ODBC names (e.g. user --> uid).

for (size_t i = 0; i < _countof(keywordmaps); i++)
Expand Down Expand Up @@ -362,7 +368,7 @@ static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs)
return 0;
}

return (PyObject*)Connection_New(pConnectString.Get(), fAutoCommit != 0, fAnsi != 0, fUnicodeResults != 0, timeout);
return (PyObject*)Connection_New(pConnectString.Get(), fAutoCommit != 0, fAnsi != 0, fUnicodeResults != 0, timeout, fReadOnly != 0);
}


Expand Down

0 comments on commit 7555f7d

Please sign in to comment.