Exception handling.
[emufront] / src / db / databasemanager.cpp
index 235d6fd..faca76e 100644 (file)
@@ -5,17 +5,17 @@
 //
 //
 // EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
 //
-// Foobar is distributed in the hope that it will be useful,
+// EmuFront 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 General Public License
-// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "databasemanager.h"
 #include <QObject>
 #include <QDir>
 #include <QVariant>
 #include <QDebug>
+#include <QDateTime>
 
-const QString DatabaseManager::DB_FILENAME = QString("my.db.sqlite");
+const QString DatabaseManager::DB_FILENAME = QString("emufront.db.sqlite");
 const QString DatabaseManager::DATABASE = QString("QSQLITE");
 const QString DatabaseManager::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
 const QString DatabaseManager::DB_TABLE_NAME_PLATFORM = QString("platform");
+const QString DatabaseManager::DB_TABLE_NAME_FILE= QString("file");
 const QString DatabaseManager::DB_TABLE_NAME_FILEPATH = QString("filepath");
 const QString DatabaseManager::DB_TABLE_NAME_SETUP = QString("setup");
+const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER = QString("mediaimagecontainer");
+const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER_MEDIAIMAGE = QString("mediaimagecontainer_mediaimage");
+const QString DatabaseManager::DB_TABLE_EXECUTABLE = QString("executable");
 
 DatabaseManager::DatabaseManager(QObject *parent)
        : QObject(parent)
-{}
+{
+    sqlTableModel = 0;
+}
 
 DatabaseManager::~DatabaseManager()
 {
@@ -47,9 +54,17 @@ DatabaseManager::~DatabaseManager()
     // be destroyed when parent is destroyed
 }
 
-QSqlQueryModel* DatabaseManager::getDataModel()
+/*
+ You may wanna set the possible filters (filterDataObjects) before calling getDataModel.
+ After filtering do not set update to true. Data model is already updated.
+*/
+QSqlQueryModel* DatabaseManager::getDataModel(bool update)
 {
-    if (!sqlTableModel) sqlTableModel = getData();
+    if (!sqlTableModel) {
+        sqlTableModel = getData();
+    }
+    else if (update)
+        clearFilters();
     return sqlTableModel;
 }
 
@@ -90,24 +105,62 @@ int DatabaseManager::countRows(QString tableName, QString columnName, int id) co
     return numEntries;
 }
 
+
+/* Throws EmuFrontException if filtered data was not found. */
 EmuFrontObject* DatabaseManager::getDataObject(int id)
 {
     filterById(id);
+    return getFilteredDataObject();
+}
+
+/* Throws EmuFrontException if filtered data was not found. */
+EmuFrontObject* DatabaseManager::getDataObject(QString filter)
+{
+    QList<QString> filters;
+    filters.append(filter);
+    filterDataObjects(filters);
+    return getFilteredDataObject();
+}
+
+/* Throws EmuFrontException if filtered data was not found. */
+EmuFrontObject* DatabaseManager::getFilteredDataObject()
+{
     EmuFrontObject *plf = 0;
-    if (sqlTableModel->rowCount() == 1)
+    // TODO: if record has more than one the first instance is returned
+    // ... check if this is ok in all cases!
+    if (sqlTableModel->rowCount() >= 1)
     {
         QSqlRecord record = sqlTableModel->record(0);
-        if (record.isEmpty())
-        {
-            throw new EmuFrontException(tr("No data available for id %1").arg(id));
+        if (record.isEmpty()) {
+            throw EmuFrontException(tr("No filtered data available"));
         }
         else plf = recordToDataObject(&record);
     }
-    return plf;
+     return plf;
 }
-
+/* Throws EmuFrontException */
 EmuFrontObject* DatabaseManager::getDataObjectFromModel(QModelIndex *index)
 {
+    if (!sqlTableModel) sqlTableModel = getDataModel();
     QSqlRecord record = sqlTableModel->record(index->row());
     return recordToDataObject(&record);
 }
+
+int DatabaseManager::getCurrentTimeStamp() {
+    return QDateTime::currentDateTime().toTime_t();
+}
+
+int DatabaseManager::countDataObjectRefs(int id) const
+{
+    int ret = 0;
+    QSqlQuery q;
+    q.prepare(getCountRefsSelect(id));
+    q.exec();
+    QSqlRecord rec;
+    if (q.next()) {
+        rec = q.record();
+        ret = rec.value(0).toInt();
+    }
+    qDebug() << "Found " << ret << " references.";
+    return ret;
+}