Collect affected items

This commit is contained in:
Christian Hohnstaedt 2017-11-26 13:33:16 +01:00
parent 0f582044e2
commit d079dbde09
11 changed files with 51 additions and 7 deletions

View File

@ -518,6 +518,7 @@ void db_base::updateItem(pki_base *pki, QString name, QString comment)
q.bindValue(2, pki->getSqlItemId());
q.exec();
e = q.lastError();
AffectedItems(pki->getSqlItemId());
mainwin->dbSqlError(e);
if (e.isValid())
return;

View File

@ -71,7 +71,7 @@ class db_base: public QAbstractItemModel
.arg(i).arg(typeid(p).name())
.arg(p?p->getIntName() : "<NULL item>")
.arg(typeid(T*).name());
qCritical(CCHAR(f));
qCritical("%s", CCHAR(f));
}
return pki;
}

View File

@ -292,6 +292,7 @@ void db_crl::newItem(pki_x509 *cert)
q.bindValue(0, (uint)cert->getCrlNumber().getLong());
q.bindValue(1, widget->nextUpdate->getDate().toPlain());
q.bindValue(2, cert->getSqlItemId());
AffectedItems(cert->getSqlItemId());
q.exec();
QSqlError err = q.lastError();
if (err.isValid())

View File

@ -104,6 +104,7 @@ void db_key::inToCont(pki_base *pki)
/* Found item matching this key */
x509s->setRefKey(key);
q.bindValue(1, x509s->getSqlItemId());
AffectedItems(x509s->getSqlItemId());
q.exec();
mainwin->dbSqlError(q.lastError());
}

View File

@ -117,6 +117,7 @@ void db_x509::remFromCont(QModelIndex &idx)
foreach(pki_x509 *child, childs) {
q.bindValue(0, child->getSigner()->getSqlItemId());
q.bindValue(1, child->getSqlItemId());
AffectedItems(child->getSqlItemId());
q.exec();
}
mainwin->crls->removeSigner(pki);
@ -242,6 +243,7 @@ void db_x509::inToCont(pki_base *pki)
insertChild(cert, child);
}
q.bindValue(1, child->getSqlItemId());
AffectedItems(child->getSqlItemId());
q.exec();
mainwin->dbSqlError(q.lastError());
if (child->isRevoked())
@ -262,6 +264,7 @@ void db_x509::inToCont(pki_base *pki)
continue;
q.bindValue(0, cert->getSqlItemId());
q.bindValue(1, crl->getSqlItemId());
AffectedItems(crl->getSqlItemId());
q.exec();
mainwin->dbSqlError(q.lastError());
}
@ -396,6 +399,7 @@ void db_x509::markRequestSigned(pki_x509req *req, pki_x509 *cert)
SQL_PREPARE(q, "UPDATE requests SET signed=? WHERE item=?");
q.bindValue(0, req->getDone());
q.bindValue(1, req->getSqlItemId());
AffectedItems(req->getSqlItemId());
q.exec();
a1time a;
@ -1142,6 +1146,7 @@ void db_x509::caProperties(QModelIndex idx)
q.bindValue(1, policy);
q.bindValue(2, tmplId);
q.bindValue(3, cert->getSqlItemId());
AffectedItems(cert->getSqlItemId());
q.exec();
TransDone(q.lastError());
mainwin->dbSqlError(q.lastError());

View File

@ -75,6 +75,7 @@ bool pki_evp::sqlUpdatePrivateKey()
q.bindValue(0, encKey_b64());
q.bindValue(1, ownPass);
q.bindValue(2, sqlItemId);
AffectedItems(sqlItemId);
q.exec();
encKey.fill(0);

View File

@ -480,6 +480,7 @@ QSqlError pki_key::insertSqlData()
foreach(pki_x509super* x, list) {
q.bindValue(1, x->getSqlItemId());
q.exec();
AffectedItems(x->getSqlItemId());
if (q.lastError().isValid())
return q.lastError();
}
@ -518,6 +519,7 @@ QSqlError pki_key::deleteSqlData()
return e;
SQL_PREPARE(q, "UPDATE x509super SET pkey=NULL WHERE pkey=?");
q.bindValue(0, sqlItemId);
AffectedItems(sqlItemId);
q.exec();
return q.lastError();
}

View File

@ -166,6 +166,16 @@ QSqlError pki_x509::deleteSqlData()
SQL_PREPARE(q, "DELETE FROM revocations WHERE caId=?");
q.bindValue(0, sqlItemId);
q.exec();
// Select affected items
QList<pki_base*> list = db_base::sqlSELECTpki<pki_base>(
"SELECT DISTINCT items.id FROM items, certs, crls "
"WHERE (items.id = certs.item OR items.id = crls.item) "
"AND crls.issuer = ? AND certs.issuer = ?",
QList<QVariant>() << QVariant(sqlItemId));
foreach(pki_base *pki, list)
AffectedItems(pki->getSqlItemId());
return q.lastError();
}

View File

@ -12,10 +12,12 @@
int DbTransaction::mutex;
int DbTransaction::error;
QList<quint64> DbTransaction::items;
quint64 DatabaseStamp;
void DbTransaction::debug(const char *func, const char *file, int line)
{
QString f = file;
qDebug() << QString("%1(%2) Transaction: %3 Level %4, E:%5 ")
.arg(file + QString(file).lastIndexOf("/") +1)
.arg(line).arg(func).arg(mutex).arg(error);
@ -56,14 +58,26 @@ bool DbTransaction::finish(const char *oper, const char *file, int line)
QSqlDatabase db = QSqlDatabase::database();
if (error) {
error = 0;
items.clear();
return db.rollback();
}
mutex++;
XSqlQuery q;
if (db.driverName() == "QPSQL7")
SQL_PREPARE(q, "UPDATE settings SET value = CAST (value AS INTEGER) +1 WHERE key_ = 'counter'");
else
SQL_PREPARE(q, "UPDATE settings SET value = value +1 WHERE key_ = 'counter'");
SQL_PREPARE(q, "SELECT MAX(stamp) +1 from items");
q.exec();
if (q.first())
DatabaseStamp = q.value(0).toULongLong();
SQL_PREPARE(q, "UPDATE items SET stamp=? WHERE stamp=0");
q.bindValue(0, DatabaseStamp);
q.exec();
SQL_PREPARE(q, "UPDATE items SET stamp=? WHERE item=?");
q.bindValue(0, DatabaseStamp);
foreach(quint64 id, DbTransaction::items) {
q.bindValue(1, id);
q.exec();
}
mutex--;
return db.commit();
}

View File

@ -9,6 +9,7 @@
#define __SQL_H
#include <QtSql>
#include <QList>
#define SQL_PREPARE(q,cmd) do { \
(q).prepare(cmd); \
@ -20,6 +21,7 @@ class DbTransaction
private:
static int mutex;
static int error;
static QList<quint64> items;
bool has_begun;
void debug(const char *func, const char *file, int line);
bool finish(const char *oper, const char *file, int line);
@ -35,6 +37,10 @@ class DbTransaction
{
return mutex > 0;
}
static void addItems(QVariant v)
{
items << v.toULongLong();
}
};
#define Transaction DbTransaction __trans
@ -44,6 +50,8 @@ class DbTransaction
#define TransCommit() __trans.commit(__FILE__, __LINE__)
#define TransRollback() __trans.rollback(__FILE__, __LINE__)
#define TransDone(e) __trans.done(e, __FILE__, __LINE__);
#define AffectedItems(v) (DbTransaction::addItems(v))
class XSqlQuery: public QSqlQuery
{

View File

@ -317,7 +317,8 @@ QSqlError MainWindow::initSqlDB()
;
schemas[4]
<< "INSERT INTO settings (key_, value) VALUES ('counter', '1')"
<< "ALTER TABLE items ADD stamp INTEGER NOT NULL DEFAULT (0)"
<< "CREATE INDEX i_items_stamp ON items (stamp)"
<< "UPDATE settings SET value='5' WHERE key_='schema'"
;