diff options
Diffstat (limited to 'Modules/_sqlite/connection.c')
-rw-r--r-- | Modules/_sqlite/connection.c | 95 |
1 files changed, 35 insertions, 60 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 70d0995f83..1c6aa54c22 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -165,7 +165,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject self->statement_cache->decref_factory = 0; Py_DECREF(self); - self->inTransaction = 0; self->detect_types = detect_types; self->timeout = timeout; (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000)); @@ -202,26 +201,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject return 0; } -/* Empty the entire statement cache of this connection */ -void pysqlite_flush_statement_cache(pysqlite_Connection* self) -{ - pysqlite_Node* node; - pysqlite_Statement* statement; - - node = self->statement_cache->first; - - while (node) { - statement = (pysqlite_Statement*)(node->data); - (void)pysqlite_statement_finalize(statement); - node = node->next; - } - - Py_SETREF(self->statement_cache, - (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self)); - Py_DECREF(self); - self->statement_cache->decref_factory = 0; -} - /* action in (ACTION_RESET, ACTION_FINALIZE) */ void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors) { @@ -366,8 +345,7 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args) } } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } /* @@ -406,9 +384,7 @@ PyObject* _pysqlite_connection_begin(pysqlite_Connection* self) } rc = pysqlite_step(statement, self); - if (rc == SQLITE_DONE) { - self->inTransaction = 1; - } else { + if (rc != SQLITE_DONE) { _pysqlite_seterror(self->db, statement); } @@ -439,7 +415,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) return NULL; } - if (self->inTransaction) { + if (!sqlite3_get_autocommit(self->db)) { Py_BEGIN_ALLOW_THREADS rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail); @@ -450,9 +426,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args) } rc = pysqlite_step(statement, self); - if (rc == SQLITE_DONE) { - self->inTransaction = 0; - } else { + if (rc != SQLITE_DONE) { _pysqlite_seterror(self->db, statement); } @@ -484,7 +458,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args return NULL; } - if (self->inTransaction) { + if (!sqlite3_get_autocommit(self->db)) { pysqlite_do_all_statements(self, ACTION_RESET, 1); Py_BEGIN_ALLOW_THREADS @@ -496,9 +470,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args } rc = pysqlite_step(statement, self); - if (rc == SQLITE_DONE) { - self->inTransaction = 0; - } else { + if (rc != SQLITE_DONE) { _pysqlite_seterror(self->db, statement); } @@ -533,7 +505,7 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) } else if (PyFloat_Check(py_val)) { sqlite3_result_double(context, PyFloat_AsDouble(py_val)); } else if (PyUnicode_Check(py_val)) { - const char *str = _PyUnicode_AsString(py_val); + const char *str = PyUnicode_AsUTF8(py_val); if (str == NULL) return -1; sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); @@ -673,7 +645,7 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_ aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); if (*aggregate_instance == 0) { - *aggregate_instance = PyObject_CallFunction(aggregate_class, ""); + *aggregate_instance = PyObject_CallFunction(aggregate_class, NULL); if (PyErr_Occurred()) { *aggregate_instance = 0; @@ -745,7 +717,7 @@ void _pysqlite_final_callback(sqlite3_context* context) PyErr_Fetch(&exception, &value, &tb); restore = 1; - function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, ""); + function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL); Py_DECREF(*aggregate_instance); @@ -874,8 +846,7 @@ PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObjec if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1) return NULL; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } } @@ -906,8 +877,7 @@ PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObje if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1) return NULL; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } } @@ -963,7 +933,7 @@ static int _progress_handler(void* user_arg) gilstate = PyGILState_Ensure(); #endif - ret = PyObject_CallFunction((PyObject*)user_arg, ""); + ret = PyObject_CallFunction((PyObject*)user_arg, NULL); if (!ret) { if (_enable_callback_tracebacks) { @@ -1042,8 +1012,7 @@ static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, P if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1) return NULL; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } } @@ -1072,8 +1041,7 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) @@ -1100,8 +1068,7 @@ static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* sel sqlite3_trace(self->db, _trace_callback, trace_callback); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } #ifdef HAVE_LOAD_EXTENSION @@ -1124,8 +1091,7 @@ static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObj PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension"); return NULL; } else { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } } @@ -1148,8 +1114,7 @@ static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* ar PyErr_SetString(pysqlite_OperationalError, errmsg); return NULL; } else { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } } #endif @@ -1186,6 +1151,17 @@ static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self } } +static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused) +{ + if (!pysqlite_check_connection(self)) { + return NULL; + } + if (!sqlite3_get_autocommit(self->db)) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} + static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level) { if (isolation_level == Py_None) { @@ -1196,7 +1172,6 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py Py_DECREF(res); self->begin_statement = NULL; - self->inTransaction = 0; } else { const char * const *candidate; PyObject *uppercase_level; @@ -1299,7 +1274,7 @@ PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args) PyObject* result = 0; PyObject* method = 0; - cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, ""); + cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); if (!cursor) { goto error; } @@ -1328,7 +1303,7 @@ PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* a PyObject* result = 0; PyObject* method = 0; - cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, ""); + cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); if (!cursor) { goto error; } @@ -1357,7 +1332,7 @@ PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* PyObject* result = 0; PyObject* method = 0; - cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, ""); + cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL); if (!cursor) { goto error; } @@ -1552,7 +1527,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) } } - uppercase_name_str = _PyUnicode_AsString(uppercase_name); + uppercase_name_str = PyUnicode_AsUTF8(uppercase_name); if (!uppercase_name_str) goto finally; @@ -1621,7 +1596,7 @@ pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) method_name = "rollback"; } - result = PyObject_CallMethod((PyObject*)self, method_name, ""); + result = PyObject_CallMethod((PyObject*)self, method_name, NULL); if (!result) { return NULL; } @@ -1630,12 +1605,13 @@ pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) Py_RETURN_FALSE; } -static char connection_doc[] = +static const char connection_doc[] = PyDoc_STR("SQLite database connection object."); static PyGetSetDef connection_getset[] = { {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, + {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0}, {NULL} }; @@ -1697,7 +1673,6 @@ static struct PyMemberDef connection_members[] = {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY}, {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, - {"in_transaction", T_BOOL, offsetof(pysqlite_Connection, inTransaction), READONLY}, {NULL} }; |