use a pointer since to remove a connection is necesary that the qsqldatabase is deleted, ouch

svn path=/trunk/KDE/kdelibs/; revision=1212424
This commit is contained in:
Marco Martin 2011-01-06 23:26:14 +00:00
parent 6d042ac25a
commit 0c09d4b058
2 changed files with 24 additions and 14 deletions

View File

@ -45,22 +45,27 @@ StorageJob::StorageJob(const QString& destination,
: ServiceJob(destination, operation, parameters, parent), : ServiceJob(destination, operation, parameters, parent),
m_clientName(destination) m_clientName(destination)
{ {
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg(++connectionId)); m_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg(++connectionId)));
m_db.setDatabaseName(KStandardDirs::locateLocal("appdata", "plasma-storage.db")); m_db->setDatabaseName(KStandardDirs::locateLocal("appdata", "plasma-storage.db"));
if (!m_db.open()) { if (!m_db->open()) {
kWarning() << "Unable to open the plasma storage cache database: " << m_db.lastError(); kWarning() << "Unable to open the plasma storage cache database: " << m_db->lastError();
} else if (!m_db.tables().contains(m_clientName)) { } else if (!m_db->tables().contains(m_clientName)) {
QSqlQuery query(m_db); QSqlQuery query(*m_db);
//bindValue doesn't seem to be able to replace stuff in create table //bindValue doesn't seem to be able to replace stuff in create table
query.prepare(QString("create table ")+m_clientName+" (valueGroup varchar(256), id varchar(256), data clob, creationTime datetime, accessTime datetime, primary key (valueGroup, id))"); query.prepare(QString("create table ")+m_clientName+" (valueGroup varchar(256), id varchar(256), data clob, creationTime datetime, accessTime datetime, primary key (valueGroup, id))");
query.exec(); query.exec();
} }
} }
StorageJob::~StorageJob()
{
delete m_db;
}
void StorageJob::start() void StorageJob::start()
{ {
if (!m_db.isOpen()) { if (!m_db->isOpen()) {
return; return;
} }
@ -73,7 +78,7 @@ void StorageJob::start()
if (operationName() == "save") { if (operationName() == "save") {
QSqlQuery query(m_db); QSqlQuery query(*m_db);
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and id = :id"); query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and id = :id");
query.bindValue(":valueGroup", valueGroup); query.bindValue(":valueGroup", valueGroup);
query.bindValue(":id", params["key"].toString()); query.bindValue(":id", params["key"].toString());
@ -87,7 +92,7 @@ void StorageJob::start()
setResult(success); setResult(success);
} else if (operationName() == "retrieve") { } else if (operationName() == "retrieve") {
QSqlQuery query(m_db); QSqlQuery query(*m_db);
//a bit redundant but should be the faster way with less string concatenation as possible //a bit redundant but should be the faster way with less string concatenation as possible
if (params["key"].toString().isEmpty()) { if (params["key"].toString().isEmpty()) {
@ -126,7 +131,7 @@ void StorageJob::start()
} }
} else if (operationName() == "delete") { } else if (operationName() == "delete") {
QSqlQuery query(m_db); QSqlQuery query(*m_db);
if (params["key"].toString().isEmpty()) { if (params["key"].toString().isEmpty()) {
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup"); query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup");
@ -141,7 +146,7 @@ void StorageJob::start()
setResult(success); setResult(success);
} else if (operationName() == "expire") { } else if (operationName() == "expire") {
QSqlQuery query(m_db); QSqlQuery query(*m_db);
if (valueGroup.isEmpty()) { if (valueGroup.isEmpty()) {
query.prepare("delete from "+m_clientName+" where accessTime < :date"); query.prepare("delete from "+m_clientName+" where accessTime < :date");
QDateTime time(QDateTime::currentDateTime()); QDateTime time(QDateTime::currentDateTime());
@ -162,8 +167,12 @@ void StorageJob::start()
setError(true); setError(true);
} }
//TODO: use a single shared db per thread //TODO: use a single shared db per thread
m_db.close(); m_db->commit();
QSqlDatabase::removeDatabase(m_db.connectionName()); m_db->close();
QString name = m_db->connectionName();
delete m_db;
m_db = 0;
QSqlDatabase::removeDatabase(name);
} }
Plasma::ServiceJob* Storage::createJob(const QString &operation, QMap<QString, QVariant> &parameters) Plasma::ServiceJob* Storage::createJob(const QString &operation, QMap<QString, QVariant> &parameters)

View File

@ -37,9 +37,10 @@ public:
const QString& operation, const QString& operation,
const QMap<QString, QVariant>& parameters, const QMap<QString, QVariant>& parameters,
QObject *parent = 0); QObject *parent = 0);
~StorageJob();
void start(); void start();
private: private:
QSqlDatabase m_db; QSqlDatabase *m_db;
QString m_clientName; QString m_clientName;
}; };
//End StorageJob //End StorageJob