2011-04-28 17:51:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Marco Martin <mart@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "storagethread_p.h"
|
|
|
|
|
2011-04-29 01:03:17 +02:00
|
|
|
#include <QCoreApplication>
|
2011-04-28 21:31:11 +02:00
|
|
|
#include <QSqlError>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlField>
|
|
|
|
#include <QSqlDriver>
|
|
|
|
#include <QSqlRecord>
|
|
|
|
|
2011-04-28 21:43:36 +02:00
|
|
|
#include <kdebug.h>
|
2012-06-12 15:01:57 +02:00
|
|
|
#include <qstandardpaths.h>
|
|
|
|
|
2011-04-28 18:40:02 +02:00
|
|
|
|
2011-04-28 17:51:51 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class StorageThreadSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageThreadSingleton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageThread self;
|
|
|
|
};
|
|
|
|
|
2012-07-19 21:16:59 +02:00
|
|
|
Q_GLOBAL_STATIC(StorageThreadSingleton, privateStorageThreadSelf)
|
2011-04-28 17:51:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
StorageThread::StorageThread(QObject *parent)
|
|
|
|
: QThread(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageThread::~StorageThread()
|
|
|
|
{
|
2011-04-29 01:16:14 +02:00
|
|
|
if (!QCoreApplication::closingDown()) {
|
2011-04-29 01:03:17 +02:00
|
|
|
QString name = m_db.connectionName();
|
|
|
|
QSqlDatabase::removeDatabase(name);
|
|
|
|
}
|
2011-04-28 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 18:40:02 +02:00
|
|
|
Plasma::StorageThread *StorageThread::self()
|
2011-04-28 17:51:51 +02:00
|
|
|
{
|
2012-07-19 21:16:59 +02:00
|
|
|
return &privateStorageThreadSelf()->self;
|
2011-04-28 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 23:10:33 +02:00
|
|
|
void StorageThread::initializeDb(StorageJob *caller)
|
2011-04-28 21:31:11 +02:00
|
|
|
{
|
2011-04-28 21:43:36 +02:00
|
|
|
if (!m_db.open()) {
|
|
|
|
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
|
2012-05-30 20:25:25 +02:00
|
|
|
m_db.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + "plasma-storage2.db");
|
2011-04-28 21:43:36 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
if (!m_db.open()) {
|
|
|
|
kWarning() << "Unable to open the plasma storage cache database: " << m_db.lastError();
|
|
|
|
} else if (!m_db.tables().contains(caller->clientName())) {
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
query.prepare(QString("create table ") + caller->clientName() + " (valueGroup varchar(256), id varchar(256), txt TEXT, int INTEGER, float REAL, binary BLOB, creationTime datetime, accessTime datetime, primary key (valueGroup, id))");
|
|
|
|
if (!query.exec()) {
|
|
|
|
kWarning() << "Unable to create table for" << caller->clientName();
|
|
|
|
m_db.close();
|
|
|
|
}
|
|
|
|
}
|
2011-04-28 21:43:36 +02:00
|
|
|
m_db.transaction();
|
2011-04-28 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 08:01:40 +02:00
|
|
|
void StorageThread::save(QWeakPointer<StorageJob> wcaller, const QVariantHash ¶ms)
|
2011-04-28 17:51:51 +02:00
|
|
|
{
|
2011-04-28 23:10:33 +02:00
|
|
|
StorageJob *caller = wcaller.data();
|
|
|
|
if (!caller) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
initializeDb(caller);
|
|
|
|
QString valueGroup = params["group"].toString();
|
|
|
|
if (valueGroup.isEmpty()) {
|
|
|
|
valueGroup = "default";
|
|
|
|
}
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
if (params.value("key").toString().isNull()) {
|
|
|
|
caller->data().insert(params.value("key").toString(), params.value("data"));
|
|
|
|
}
|
2011-04-28 17:51:51 +02:00
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
QHashIterator<QString, QVariant> it(caller->data());
|
|
|
|
|
|
|
|
QString ids;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
QSqlField field(":id", QVariant::String);
|
|
|
|
field.setValue(it.key());
|
|
|
|
if (!ids.isEmpty()) {
|
|
|
|
ids.append(", ");
|
|
|
|
}
|
|
|
|
ids.append(m_db.driver()->formatValue(field));
|
|
|
|
}
|
|
|
|
|
|
|
|
query.prepare("delete from " + caller->clientName() + " where valueGroup = :valueGroup and id in (" + ids + ");");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
m_db.commit();
|
|
|
|
emit newResult(caller, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.prepare("insert into " + caller->clientName() + " values(:valueGroup, :id, :txt, :int, :float, :binary, date('now'), date('now'))");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":txt", QVariant());
|
|
|
|
query.bindValue(":int", QVariant());
|
|
|
|
query.bindValue(":float", QVariant());
|
|
|
|
query.bindValue(":binary", QVariant());
|
|
|
|
|
|
|
|
const QString key = params.value("key").toString();
|
|
|
|
if (!key.isEmpty()) {
|
|
|
|
caller->data().insert(key, params["data"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
it.toFront();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
//kDebug() << "going to insert" << valueGroup << it.key();
|
|
|
|
query.bindValue(":id", it.key());
|
|
|
|
|
|
|
|
QString field;
|
2011-04-29 01:03:17 +02:00
|
|
|
bool binary = false;
|
2011-04-28 21:31:11 +02:00
|
|
|
switch (QMetaType::Type(it.value().type())) {
|
|
|
|
case QVariant::String:
|
|
|
|
field = ":txt";
|
|
|
|
break;
|
|
|
|
case QVariant::Int:
|
|
|
|
field = ":int";
|
|
|
|
break;
|
|
|
|
case QVariant::Double:
|
|
|
|
case QMetaType::Float:
|
|
|
|
field = ":float";
|
|
|
|
break;
|
|
|
|
case QVariant::ByteArray:
|
2011-04-29 01:03:17 +02:00
|
|
|
binary = true;
|
2011-04-28 21:31:11 +02:00
|
|
|
field = ":binary";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-29 01:03:17 +02:00
|
|
|
if (binary) {
|
|
|
|
QByteArray b;
|
|
|
|
QDataStream ds(&b, QIODevice::WriteOnly);
|
|
|
|
ds << it.value();
|
2011-05-21 14:38:08 +02:00
|
|
|
query.bindValue(field, b);
|
2011-04-29 01:03:17 +02:00
|
|
|
} else {
|
|
|
|
query.bindValue(field, it.value());
|
|
|
|
}
|
2011-04-28 21:31:11 +02:00
|
|
|
|
|
|
|
if (!query.exec()) {
|
|
|
|
//kDebug() << "query failed:" << query.lastQuery() << query.lastError().text();
|
|
|
|
m_db.commit();
|
|
|
|
emit newResult(caller, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.bindValue(field, QVariant());
|
|
|
|
}
|
2011-04-28 21:43:36 +02:00
|
|
|
m_db.commit();
|
2011-04-28 21:31:11 +02:00
|
|
|
|
|
|
|
emit newResult(caller, true);
|
2011-04-28 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 08:01:40 +02:00
|
|
|
void StorageThread::retrieve(QWeakPointer<StorageJob> wcaller, const QVariantHash ¶ms)
|
2011-04-28 17:51:51 +02:00
|
|
|
{
|
2011-04-28 23:10:33 +02:00
|
|
|
StorageJob *caller = wcaller.data();
|
|
|
|
if (!caller) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:34:02 +02:00
|
|
|
const QString clientName = caller->clientName();
|
2011-04-28 21:31:11 +02:00
|
|
|
initializeDb(caller);
|
|
|
|
QString valueGroup = params["group"].toString();
|
|
|
|
if (valueGroup.isEmpty()) {
|
|
|
|
valueGroup = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
2011-04-28 23:34:02 +02:00
|
|
|
//a bit redundant but should be the faster way with less string concatenation as possible
|
|
|
|
if (params["key"].toString().isEmpty()) {
|
|
|
|
//update modification time
|
|
|
|
query.prepare("update " + clientName + " set accessTime=date('now') where valueGroup=:valueGroup");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.exec();
|
2011-04-28 21:31:11 +02:00
|
|
|
|
2011-04-28 23:34:02 +02:00
|
|
|
query.prepare("select * from " + clientName + " where valueGroup=:valueGroup");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
} else {
|
|
|
|
//update modification time
|
|
|
|
query.prepare("update " + clientName + " set accessTime=date('now') where valueGroup=:valueGroup and id=:key");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
query.prepare("select * from " + clientName + " where valueGroup=:valueGroup and id=:key");
|
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool success = query.exec();
|
2011-04-28 21:31:11 +02:00
|
|
|
|
2011-04-28 23:34:02 +02:00
|
|
|
QVariant result;
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
QSqlRecord rec = query.record();
|
|
|
|
const int keyColumn = rec.indexOf("id");
|
|
|
|
const int textColumn = rec.indexOf("txt");
|
|
|
|
const int intColumn = rec.indexOf("int");
|
|
|
|
const int floatColumn = rec.indexOf("float");
|
|
|
|
const int binaryColumn = rec.indexOf("binary");
|
|
|
|
|
|
|
|
QVariantHash data;
|
|
|
|
while (query.next()) {
|
|
|
|
const QString key = query.value(keyColumn).toString();
|
|
|
|
if (!query.value(textColumn).isNull()) {
|
|
|
|
data.insert(key, query.value(textColumn));
|
|
|
|
} else if (!query.value(intColumn).isNull()) {
|
|
|
|
data.insert(key, query.value(intColumn));
|
|
|
|
} else if (!query.value(floatColumn).isNull()) {
|
|
|
|
data.insert(key, query.value(floatColumn));
|
|
|
|
} else if (!query.value(binaryColumn).isNull()) {
|
2011-04-29 01:03:17 +02:00
|
|
|
QByteArray bytes = query.value(binaryColumn).toByteArray();
|
|
|
|
QDataStream in(bytes);
|
|
|
|
QVariant v(in);
|
|
|
|
data.insert(key, v);
|
2011-04-28 23:34:02 +02:00
|
|
|
}
|
2011-04-28 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 23:37:25 +02:00
|
|
|
result = data;
|
2011-04-28 23:34:02 +02:00
|
|
|
} else {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
emit newResult(caller, result);
|
2011-04-28 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 08:01:40 +02:00
|
|
|
void StorageThread::deleteEntry(QWeakPointer<StorageJob> wcaller, const QVariantHash ¶ms)
|
2011-04-28 17:51:51 +02:00
|
|
|
{
|
2011-04-28 23:10:33 +02:00
|
|
|
StorageJob *caller = wcaller.data();
|
|
|
|
if (!caller) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
initializeDb(caller);
|
|
|
|
QString valueGroup = params["group"].toString();
|
|
|
|
if (valueGroup.isEmpty()) {
|
|
|
|
valueGroup = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
|
|
|
|
if (params["key"].toString().isEmpty()) {
|
2011-04-28 23:38:50 +02:00
|
|
|
query.prepare("delete from " + caller->clientName() + " where valueGroup=:valueGroup");
|
2011-04-28 21:31:11 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
} else {
|
2011-04-28 23:38:50 +02:00
|
|
|
query.prepare("delete from " + caller->clientName() + " where valueGroup=:valueGroup and id=:key");
|
2011-04-28 21:31:11 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
query.bindValue(":key", params["key"].toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool success = query.exec();
|
2011-04-28 23:58:40 +02:00
|
|
|
m_db.commit();
|
2011-04-28 17:51:51 +02:00
|
|
|
|
|
|
|
emit newResult(caller, success);
|
|
|
|
}
|
|
|
|
|
2011-09-19 08:01:40 +02:00
|
|
|
void StorageThread::expire(QWeakPointer<StorageJob> wcaller, const QVariantHash ¶ms)
|
2011-04-28 17:51:51 +02:00
|
|
|
{
|
2011-04-28 23:10:33 +02:00
|
|
|
StorageJob *caller = wcaller.data();
|
|
|
|
if (!caller) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:31:11 +02:00
|
|
|
initializeDb(caller);
|
|
|
|
QString valueGroup = params["group"].toString();
|
|
|
|
if (valueGroup.isEmpty()) {
|
|
|
|
valueGroup = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query(m_db);
|
|
|
|
if (valueGroup.isEmpty()) {
|
2011-04-28 23:38:50 +02:00
|
|
|
query.prepare("delete from " + caller->clientName() + " where accessTime < :date");
|
2011-04-28 21:31:11 +02:00
|
|
|
QDateTime time(QDateTime::currentDateTime());
|
|
|
|
time.addSecs(-params["age"].toUInt());
|
|
|
|
query.bindValue(":date", time.toTime_t());
|
|
|
|
} else {
|
2011-04-28 23:38:50 +02:00
|
|
|
query.prepare("delete from " + caller->clientName() + " where valueGroup=:valueGroup and accessTime < :date");
|
2011-04-28 21:31:11 +02:00
|
|
|
query.bindValue(":valueGroup", valueGroup);
|
|
|
|
QDateTime time(QDateTime::currentDateTime());
|
|
|
|
time.addSecs(-params["age"].toUInt());
|
|
|
|
query.bindValue(":date", time.toTime_t());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool success = query.exec();
|
|
|
|
|
2011-04-28 17:51:51 +02:00
|
|
|
emit newResult(caller, success);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageThread::run()
|
|
|
|
{
|
2011-04-28 18:40:02 +02:00
|
|
|
exec();
|
2011-04-28 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-22 15:37:37 +01:00
|
|
|
#include "moc_storagethread_p.cpp"
|