7#include "storagethread_p.h"
9#include <QCoreApplication>
20#include <QStandardPaths>
24class StorageThreadSingleton
27 StorageThreadSingleton()
34Q_GLOBAL_STATIC(StorageThreadSingleton, privateStorageThreadSelf)
36static void closeConnection()
38 StorageThread::self()->closeDb();
39 StorageThread::self()->quit();
42StorageThread::StorageThread(
QObject *parent)
45 qAddPostRoutine(closeConnection);
48StorageThread::~StorageThread()
52Plasma5Support::StorageThread *StorageThread::self()
54 return &privateStorageThreadSelf()->self;
57void StorageThread::closeDb()
64void StorageThread::initializeDb(StorageJob *caller)
70 m_db.setDatabaseName(storageDir +
QLatin1Char(
'/') + QStringLiteral(
"plasma-storage2.db"));
74 qCWarning(LOG_PLASMA5SUPPORT) <<
"Unable to open the plasma storage cache database: " << m_db.lastError();
75 }
else if (!m_db.tables().contains(caller->clientName())) {
77 query.prepare(QStringLiteral(
"create table ") + caller->clientName()
78 + QStringLiteral(
" (valueGroup varchar(256), id varchar(256), txt TEXT, int INTEGER, float REAL, binary BLOB, creationTime datetime, "
79 "accessTime datetime, primary key (valueGroup, id))"));
81 qCWarning(LOG_PLASMA5SUPPORT) <<
"Unable to create table for" << caller->clientName();
90 StorageJob *caller = wcaller.
data();
96 QString valueGroup = params[QStringLiteral(
"group")].toString();
98 valueGroup = QStringLiteral(
"default");
102 QVariantMap data = caller->data();
103 if (params.value(QStringLiteral(
"key")).
toString().isNull()) {
104 data.insert(params.value(QStringLiteral(
"key")).
toString(), params.value(QStringLiteral(
"data")));
106 caller->setData(data);
111 while (it.hasNext()) {
114 field.setValue(it.key());
116 ids.
append(QStringLiteral(
", "));
118 ids.
append(m_db.driver()->formatValue(field));
121 query.prepare(QStringLiteral(
"delete from ") + caller->clientName() + QStringLiteral(
" where valueGroup = :valueGroup and id in (") + ids
122 + QStringLiteral(
");"));
123 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
127 Q_EMIT newResult(caller,
false);
131 query.prepare(QStringLiteral(
"insert into ") + caller->clientName()
132 + QStringLiteral(
" values(:valueGroup, :id, :txt, :int, :float, :binary, date('now'), date('now'))"));
133 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
139 const QString key = params.value(QStringLiteral(
"key")).toString();
141 QVariantMap data = caller->data();
142 data.insert(key, params[QStringLiteral(
"data")]);
143 caller->setData(data);
147 while (it.hasNext()) {
150 query.bindValue(QStringLiteral(
":id"), it.key());
154 switch (it.value().userType()) {
156 field = QStringLiteral(
":txt");
159 field = QStringLiteral(
":int");
162 field = QStringLiteral(
":float");
166 field = QStringLiteral(
":binary");
176 query.bindValue(field, b);
178 query.bindValue(field, it.value());
184 Q_EMIT newResult(caller,
false);
192 Q_EMIT newResult(caller,
true);
197 StorageJob *caller = wcaller.
data();
202 const QString clientName = caller->clientName();
203 initializeDb(caller);
204 QString valueGroup = params[QStringLiteral(
"group")].toString();
206 valueGroup = QStringLiteral(
"default");
212 if (params[QStringLiteral(
"key")].
toString().isEmpty()) {
214 query.prepare(QStringLiteral(
"update ") + clientName + QStringLiteral(
" set accessTime=date('now') where valueGroup=:valueGroup"));
215 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
218 query.prepare(QStringLiteral(
"select * from ") + clientName + QStringLiteral(
" where valueGroup=:valueGroup"));
219 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
222 query.prepare(QStringLiteral(
"update ") + clientName + QStringLiteral(
" set accessTime=date('now') where valueGroup=:valueGroup and id=:key"));
223 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
224 query.bindValue(QStringLiteral(
":key"), params[QStringLiteral(
"key")].
toString());
227 query.prepare(QStringLiteral(
"select * from ") + clientName + QStringLiteral(
" where valueGroup=:valueGroup and id=:key"));
228 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
229 query.bindValue(QStringLiteral(
":key"), params[QStringLiteral(
"key")].
toString());
232 const bool success =
query.exec();
245 while (
query.next()) {
247 if (!
query.value(textColumn).isNull()) {
248 data.insert(key,
query.value(textColumn));
249 }
else if (!
query.value(intColumn).isNull()) {
250 data.insert(key,
query.value(intColumn));
251 }
else if (!
query.value(floatColumn).isNull()) {
252 data.insert(key,
query.value(floatColumn));
253 }
else if (!
query.value(binaryColumn).isNull()) {
266 Q_EMIT newResult(caller, result);
271 StorageJob *caller = wcaller.
data();
276 initializeDb(caller);
277 QString valueGroup = params[QStringLiteral(
"group")].toString();
279 valueGroup = QStringLiteral(
"default");
284 if (params[QStringLiteral(
"key")].
toString().isEmpty()) {
285 query.prepare(QStringLiteral(
"delete from ") + caller->clientName() + QStringLiteral(
" where valueGroup=:valueGroup"));
286 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
288 query.prepare(QStringLiteral(
"delete from ") + caller->clientName() + QStringLiteral(
" where valueGroup=:valueGroup and id=:key"));
289 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
290 query.bindValue(QStringLiteral(
":key"), params[QStringLiteral(
"key")].
toString());
293 const bool success =
query.exec();
296 Q_EMIT newResult(caller, success);
301 StorageJob *caller = wcaller.
data();
306 initializeDb(caller);
307 QString valueGroup = params[QStringLiteral(
"group")].toString();
309 valueGroup = QStringLiteral(
"default");
314 query.prepare(QStringLiteral(
"delete from ") + caller->clientName() + QStringLiteral(
" where accessTime < :date"));
316 query.bindValue(QStringLiteral(
":date"), time.toSecsSinceEpoch());
318 query.prepare(QStringLiteral(
"delete from ") + caller->clientName() + QStringLiteral(
" where valueGroup=:valueGroup and accessTime < :date"));
319 query.bindValue(QStringLiteral(
":valueGroup"), valueGroup);
321 query.bindValue(QStringLiteral(
":date"), time.toSecsSinceEpoch());
324 const bool success =
query.exec();
326 Q_EMIT newResult(caller, success);
329void StorageThread::run()
336#include "moc_storagethread_p.cpp"
std::optional< QSqlQuery > query(const QString &queryStatement)
char * toString(const EngineQuery &query)
QString name(StandardAction id)
Namespace for everything in libplasma.
QDateTime currentDateTime()
bool mkpath(const QString &dirPath) const const
QSqlDatabase addDatabase(QSqlDriver *driver, const QString &connectionName)
void removeDatabase(const QString &connectionName)
int indexOf(const QString &name) const const
QString writableLocation(StandardLocation type)
QString & append(QChar ch)
bool isEmpty() const const