[go: nahoru, domu]

Skip to content

Commit

Permalink
Use sqlite3_get_autocommit() to determine if we are within a
Browse files Browse the repository at this point in the history
transaction instead of trying to be smart.
  • Loading branch information
ghaering committed Aug 18, 2015
1 parent 40b349c commit 9b79188
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
19 changes: 5 additions & 14 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,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));
Expand Down Expand Up @@ -407,9 +406,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);
}

Expand Down Expand Up @@ -440,7 +437,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_v2(self->db, "COMMIT", -1, &statement, &tail);
Py_END_ALLOW_THREADS
Expand All @@ -450,9 +447,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);
}

Expand Down Expand Up @@ -484,7 +479,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
Expand All @@ -496,9 +491,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);
}

Expand Down Expand Up @@ -1203,8 +1196,6 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
return -1;
}
Py_DECREF(res);

self->inTransaction = 0;
} else {
Py_INCREF(isolation_level);
self->isolation_level = isolation_level;
Expand Down
4 changes: 0 additions & 4 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ typedef struct
PyObject_HEAD
sqlite3* db;

/* 1 if we are currently within a transaction, i. e. if a BEGIN has been
* issued */
int inTransaction;

/* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
* bitwise combination thereof makes sense */
int detect_types;
Expand Down
9 changes: 2 additions & 7 deletions src/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
case STATEMENT_DELETE:
case STATEMENT_INSERT:
case STATEMENT_REPLACE:
if (!self->connection->inTransaction) {
if (sqlite3_get_autocommit(self->connection->db)) {
result = _pysqlite_connection_begin(self->connection);
if (!result) {
goto error;
Expand All @@ -607,7 +607,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
case STATEMENT_OTHER:
/* it's a DDL statement or something similar
- we better COMMIT first so it works for all cases */
if (self->connection->inTransaction) {
if (!sqlite3_get_autocommit(self->connection->db)) {
result = pysqlite_connection_commit(self->connection, NULL);
if (!result) {
goto error;
Expand Down Expand Up @@ -728,11 +728,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
}

error:
/* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
* ROLLBACK could have happened */
if (self->connection && self->connection->db)
self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);

Py_XDECREF(operation_bytestr);
Py_XDECREF(parameters);
Py_XDECREF(parameters_iter);
Expand Down

0 comments on commit 9b79188

Please sign in to comment.