File indexing completed on 2024-04-28 04:50:50
0001 /* 0002 * sqlhelper.cpp 0003 * 0004 * Copyright (C) 2009-2011 Christoph Pfister <christophpfister@gmail.com> 0005 * 0006 * This program is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU General Public License as published by 0008 * the Free Software Foundation; either version 2 of the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License along 0017 * with this program; if not, write to the Free Software Foundation, Inc., 0018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0019 */ 0020 0021 #include "log.h" 0022 0023 #include <KMessageBox> 0024 #include <QSqlError> 0025 #include <QStandardPaths> 0026 0027 #include "sqlhelper.h" 0028 #include "sqlinterface.h" 0029 0030 SqlHelper::SqlHelper() 0031 { 0032 database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), QLatin1String("kaffeine")); 0033 database.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1String("/sqlite.db")); 0034 0035 timer.setInterval(5000); 0036 connect(&timer, SIGNAL(timeout()), this, SLOT(collectSubmissions())); 0037 } 0038 0039 SqlHelper::~SqlHelper() 0040 { 0041 } 0042 0043 bool SqlHelper::createInstance() 0044 { 0045 Q_ASSERT(instance == NULL); 0046 0047 if (!QSqlDatabase::isDriverAvailable(QLatin1String("QSQLITE"))) { 0048 KMessageBox::error(NULL, i18nc("message box", "Please install the Qt SQLite plugin.")); 0049 return false; 0050 } 0051 0052 instance = new SqlHelper(); 0053 0054 if (!instance->database.open()) { 0055 QString details = instance->database.lastError().databaseText(); 0056 0057 if (!details.isEmpty() && !details.endsWith(QLatin1Char('\n'))) { 0058 details.append(QLatin1Char('\n')); 0059 } 0060 0061 details.append(instance->database.lastError().driverText()); 0062 KMessageBox::detailedError(NULL, 0063 i18nc("message box", "Cannot open the SQLite database."), details); 0064 delete instance; 0065 instance = NULL; 0066 return false; 0067 } 0068 0069 return true; 0070 } 0071 0072 SqlHelper *SqlHelper::getInstance() 0073 { 0074 return instance; 0075 } 0076 0077 QSqlQuery SqlHelper::prepare(const QString &statement) 0078 { 0079 QSqlQuery query(database); 0080 query.setForwardOnly(true); 0081 0082 if (!query.prepare(statement)) { 0083 qCWarning(logSql, "Error while preparing statement '%s'", qPrintable(query.lastError().text())); 0084 } 0085 0086 return query; 0087 } 0088 0089 QSqlQuery SqlHelper::exec(const QString &statement) 0090 { 0091 QSqlQuery query(database); 0092 query.setForwardOnly(true); 0093 0094 if (!query.exec(statement)) { 0095 qCWarning(logSql, "Error while executing statement '%s'", qPrintable(query.lastError().text())); 0096 } 0097 0098 return query; 0099 } 0100 0101 void SqlHelper::exec(QSqlQuery &query) 0102 { 0103 if (!query.exec()) { 0104 qCWarning(logSql, "Error while executing statement '%s'", qPrintable(query.lastError().text())); 0105 } 0106 } 0107 0108 void SqlHelper::requestSubmission(SqlInterface *object) 0109 { 0110 if (!timer.isActive()) { 0111 timer.start(); 0112 } 0113 0114 objects.append(object); 0115 } 0116 0117 void SqlHelper::collectSubmissions() 0118 { 0119 exec(QLatin1String("BEGIN")); 0120 0121 for (int i = 0; i < objects.size(); ++i) { 0122 objects.at(i)->sqlSubmit(); 0123 } 0124 0125 exec(QLatin1String("COMMIT")); 0126 timer.stop(); 0127 objects.clear(); 0128 } 0129 0130 SqlHelper *SqlHelper::instance = NULL; 0131 0132 #include "moc_sqlhelper.cpp"