int type; // cr_DatabaseType value or -1
void *statements;
sqlite3 *db;
- int closed; // Is db closed?
} _SqliteObject;
// Forward declaration
assert(self != NULL);
assert(SqliteObject_Check(self));
if (self->db == NULL) {
- PyErr_SetString(CrErr_Exception, "Improper createrepo_c Sqlite object.");
+ PyErr_SetString(CrErr_Exception,
+ "Improper createrepo_c Sqlite object (Already closed db?)");
return -1;
}
return 0;
self->type = -1;
self->statements = NULL;
self->db = NULL;
- self->closed = 0;
}
return (PyObject *)self;
}
char *path;
int db_type;
GError *err = NULL;
+ PyObject *ret;
CR_UNUSED(kwds);
return -1;
/* Check arguments */
- if (db_type < CR_DB_PRIMARY || db_type > CR_DB_OTHER) {
+ if (db_type < CR_DB_PRIMARY || db_type >= CR_DB_SENTINEL) {
PyErr_SetString(PyExc_ValueError, "Unknown type value");
return -1;
}
/* Free all previous resources when reinitialization */
- Py_XDECREF(close_db(self, NULL));
+ ret = close_db(self, NULL);
+ Py_XDECREF(ret);
+ if (ret == NULL) {
+ // Error encountered!
+ return -1;
+ }
+
if (self->statements) {
if (self->type == CR_DB_PRIMARY)
cr_db_destroy_primary_statements(self->statements);
self->statements = NULL;
}
- if (err) {
- PyErr_Format(CrErr_Exception, "Error while freeing previous content: %s", err->message);
- g_clear_error(&err);
- return -1;
- }
-
/* Init */
self->type = db_type;
- self->closed = 0;
if (self->type == CR_DB_PRIMARY)
self->db = cr_db_open_primary(path, &err);
else if (self->type == CR_DB_FILELISTS)
else if (self->type == CR_DB_OTHER)
self->db = cr_db_open_other(path, &err);
- if (self->db == NULL) {
- PyErr_SetString(CrErr_Exception, "Sqlite initialization failed");
- return -1;
- }
-
if (err) {
PyErr_Format(CrErr_Exception, "Sqlite initialization failed: %s", err->message);
g_clear_error(&err);
- Py_XDECREF(close_db(self, NULL));
+ return -1;
+ }
+
+ if (self->db == NULL) {
+ PyErr_SetString(CrErr_Exception, "Sqlite initialization failed");
return -1;
}
cr_db_destroy_other_statements(self->statements);
self->statements = NULL;
}
- Py_XDECREF(close_db(self, NULL));
+
+ if (self->db)
+ cr_db_close(self->db, self->type, NULL);
+
Py_TYPE(self)->tp_free(self);
}
static PyObject *
close_db(_SqliteObject *self, void *nothing)
{
+ GError *err = NULL;
+
CR_UNUSED(nothing);
- if (self->db && !self->closed) {
- self->closed = 1;
- cr_db_close(self->db, self->type, NULL);
+
+ if (self->db) {
+ cr_db_close(self->db, self->type, &err);
+ self->db = NULL;
+ if (err) {
+ PyErr_Format(CrErr_Exception, "Sqlite close error: %s", err->message);
+ g_clear_error(&err);
+ return NULL;
+ }
}
+
Py_RETURN_NONE;
}