Changeset View
Changeset View
Standalone View
Standalone View
src/common/database/Database.cpp
Show All 29 Lines | |||||
30 | #include <QSqlDriver> | 30 | #include <QSqlDriver> | ||
31 | #include <QThread> | 31 | #include <QThread> | ||
32 | #include <QDebug> | 32 | #include <QDebug> | ||
33 | 33 | | |||
34 | #include <memory> | 34 | #include <memory> | ||
35 | #include <mutex> | 35 | #include <mutex> | ||
36 | #include <map> | 36 | #include <map> | ||
37 | 37 | | |||
38 | #include "Debug.h" | ||||
39 | | ||||
38 | namespace Common { | 40 | namespace Common { | ||
39 | 41 | | |||
40 | namespace { | 42 | namespace { | ||
41 | #ifdef QT_DEBUG | 43 | #ifdef QT_DEBUG | ||
42 | QString lastExecutedQuery; | 44 | QString lastExecutedQuery; | ||
43 | #endif | 45 | #endif | ||
44 | 46 | | |||
45 | std::mutex databases_mutex; | 47 | std::mutex databases_mutex; | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | 74 | { | |||
87 | } | 89 | } | ||
88 | 90 | | |||
89 | // We are allowing the database file to be overridden mostly for testing purposes | 91 | // We are allowing the database file to be overridden mostly for testing purposes | ||
90 | m_database.setDatabaseName(ResourcesDatabaseSchema::path()); | 92 | m_database.setDatabaseName(ResourcesDatabaseSchema::path()); | ||
91 | 93 | | |||
92 | m_open = m_database.open(); | 94 | m_open = m_database.open(); | ||
93 | 95 | | |||
94 | if (!m_open) { | 96 | if (!m_open) { | ||
95 | qWarning() << "KActivities: Database is not open: " | 97 | qCWarning(KAMD_LOG_RESOURCES) << "KActivities: Database is not open: " | ||
96 | << m_database.connectionName() | 98 | << m_database.connectionName() | ||
97 | << m_database.databaseName() | 99 | << m_database.databaseName() | ||
98 | << m_database.lastError(); | 100 | << m_database.lastError(); | ||
99 | 101 | | |||
100 | if (info.openMode == Database::ReadWrite) { | 102 | if (info.openMode == Database::ReadWrite) { | ||
101 | qFatal("KActivities: Opening the database in RW mode should always succeed"); | 103 | qFatal("KActivities: Opening the database in RW mode should always succeed"); | ||
102 | } | 104 | } | ||
103 | } | 105 | } | ||
104 | } | 106 | } | ||
105 | 107 | | |||
106 | ~QSqlDatabaseWrapper() | 108 | ~QSqlDatabaseWrapper() | ||
107 | { | 109 | { | ||
108 | qDebug() << "Closing SQL connection: " << m_connectionName; | 110 | qCDebug(KAMD_LOG_RESOURCES) << "Closing SQL connection: " << m_connectionName; | ||
109 | } | 111 | } | ||
110 | 112 | | |||
111 | QSqlDatabase &get() | 113 | QSqlDatabase &get() | ||
112 | { | 114 | { | ||
113 | return m_database; | 115 | return m_database; | ||
114 | } | 116 | } | ||
115 | 117 | | |||
116 | bool isOpen() const | 118 | bool isOpen() const | ||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Line(s) | 198 | } else { | |||
197 | // Using the write-ahead log and sync = NORMAL for faster writes | 199 | // Using the write-ahead log and sync = NORMAL for faster writes | ||
198 | ptr->setPragma(QStringLiteral("synchronous = 1")); | 200 | ptr->setPragma(QStringLiteral("synchronous = 1")); | ||
199 | } | 201 | } | ||
200 | 202 | | |||
201 | // Maybe we should use the write-ahead log | 203 | // Maybe we should use the write-ahead log | ||
202 | auto walResult = ptr->pragma(QStringLiteral("journal_mode = WAL")); | 204 | auto walResult = ptr->pragma(QStringLiteral("journal_mode = WAL")); | ||
203 | 205 | | |||
204 | if (walResult != "wal") { | 206 | if (walResult != "wal") { | ||
205 | qWarning() << "KActivities: Database can not be opened in WAL mode. Check the " | 207 | qCWarning(KAMD_LOG_RESOURCES) << "KActivities: Database can not be opened in WAL mode. Check the " | ||
206 | "SQLite version (required >3.7.0). And whether your filesystem " | 208 | "SQLite version (required >3.7.0). And whether your filesystem " | ||
207 | "supports shared memory"; | 209 | "supports shared memory"; | ||
208 | return nullptr; | 210 | return nullptr; | ||
209 | } | 211 | } | ||
210 | 212 | | |||
211 | // We don't have a big database, lets flush the WAL when | 213 | // We don't have a big database, lets flush the WAL when | ||
212 | // it reaches 400k, not 4M as is default | 214 | // it reaches 400k, not 4M as is default | ||
213 | ptr->setPragma(QStringLiteral("wal_autocheckpoint = 100")); | 215 | ptr->setPragma(QStringLiteral("wal_autocheckpoint = 100")); | ||
214 | 216 | | |||
215 | qDebug() << "KActivities: Database connection: " << ptr->d->database->connectionName() | 217 | qCDebug(KAMD_LOG_RESOURCES) << "KActivities: Database connection: " << ptr->d->database->connectionName() | ||
216 | << "\n query_only: " << ptr->pragma(QStringLiteral("query_only")) | 218 | << "\n query_only: " << ptr->pragma(QStringLiteral("query_only")) | ||
217 | << "\n journal_mode: " << ptr->pragma(QStringLiteral("journal_mode")) | 219 | << "\n journal_mode: " << ptr->pragma(QStringLiteral("journal_mode")) | ||
218 | << "\n wal_autocheckpoint: " << ptr->pragma(QStringLiteral("wal_autocheckpoint")) | 220 | << "\n wal_autocheckpoint: " << ptr->pragma(QStringLiteral("wal_autocheckpoint")) | ||
219 | << "\n synchronous: " << ptr->pragma(QStringLiteral("synchronous")) | 221 | << "\n synchronous: " << ptr->pragma(QStringLiteral("synchronous")) | ||
220 | ; | 222 | ; | ||
221 | 223 | | |||
222 | return ptr; | 224 | return ptr; | ||
223 | } | 225 | } | ||
Show All 24 Lines | |||||
248 | #ifdef QT_NO_DEBUG | 250 | #ifdef QT_NO_DEBUG | ||
249 | return d->query(query); | 251 | return d->query(query); | ||
250 | #else | 252 | #else | ||
251 | auto result = d->query(query); | 253 | auto result = d->query(query); | ||
252 | 254 | | |||
253 | lastExecutedQuery = query; | 255 | lastExecutedQuery = query; | ||
254 | 256 | | |||
255 | if (!ignoreErrors && result.lastError().isValid()) { | 257 | if (!ignoreErrors && result.lastError().isValid()) { | ||
256 | qWarning() << "SQL: " | 258 | qCWarning(KAMD_LOG_RESOURCES) << "SQL: " | ||
257 | << "\n error: " << result.lastError() | 259 | << "\n error: " << result.lastError() | ||
258 | << "\n query: " << query; | 260 | << "\n query: " << query; | ||
259 | } | 261 | } | ||
260 | 262 | | |||
261 | return result; | 263 | return result; | ||
262 | #endif | 264 | #endif | ||
263 | } | 265 | } | ||
264 | 266 | | |||
Show All 28 Lines |