Exceptions are instantiated in stack instead of heap.
[emufront] / src / db / databasemanager.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include "databasemanager.h"
21 #include <QObject>
22 #include <QSqlDatabase>
23 #include <QSqlTableModel>
24 #include <QSqlError>
25 #include <QSqlQuery>
26 #include <QSqlRecord>
27 #include <QFile>
28 #include <QDir>
29 #include <QVariant>
30 #include <QDebug>
31 #include <QDateTime>
32
33 const QString DatabaseManager::DB_FILENAME = QString("emufront.db.sqlite");
34 const QString DatabaseManager::DATABASE = QString("QSQLITE");
35 const QString DatabaseManager::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
36 const QString DatabaseManager::DB_TABLE_NAME_PLATFORM = QString("platform");
37 const QString DatabaseManager::DB_TABLE_NAME_FILE= QString("file");
38 const QString DatabaseManager::DB_TABLE_NAME_FILEPATH = QString("filepath");
39 const QString DatabaseManager::DB_TABLE_NAME_SETUP = QString("setup");
40 const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER = QString("mediaimagecontainer");
41 const QString DatabaseManager::DB_TABLE_MEDIAIMAGECONTAINER_MEDIAIMAGE = QString("mediaimagecontainer_mediaimage");
42 const QString DatabaseManager::DB_TABLE_EXECUTABLE = QString("executable");
43
44 DatabaseManager::DatabaseManager(QObject *parent)
45         : QObject(parent)
46 {
47     sqlTableModel = 0;
48 }
49
50 DatabaseManager::~DatabaseManager()
51 {
52     // no need to explicitily destroy sqlTableModel
53     // because it is parented QObject and will
54     // be destroyed when parent is destroyed
55 }
56
57 /*
58  You may wanna set the possible filters (filterDataObjects) before calling getDataModel.
59  After filtering do not set update to true. Data model is already updated.
60 */
61 QSqlQueryModel* DatabaseManager::getDataModel(bool update)
62 {
63     if (!sqlTableModel) {
64         sqlTableModel = getData();
65     }
66     else if (update)
67         clearFilters();
68     return sqlTableModel;
69 }
70
71 bool DatabaseManager::openDB()
72 {
73     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
74     db.setDatabaseName(DatabaseManager::getDbPath());
75     return db.open();
76 }
77
78 QString DatabaseManager::getDbPath()
79 {
80         QString path;
81 #ifdef Q_OS_LINUX
82         path.append(QDir::home().path());
83         path.append(QDir::separator()).append(DB_FILENAME);
84 #else
85         path.append(DB_FILENAME);       
86 #endif
87         return path;
88 }
89
90 void DatabaseManager::resetModel()
91 {
92     if (!sqlTableModel) return;
93     clearFilters();
94 }
95
96 // sql must return a count(*) value
97 int DatabaseManager::countRows(QString tableName, QString columnName, int id) const
98 {
99     QString sql = QString("SELECT COUNT(*) FROM %1 WHERE %2 = %3")
100         .arg(tableName).arg(columnName).arg(id);
101     int numEntries = 0;
102     QSqlQuery query(sql);
103     if (query.next())
104         numEntries = query.value(0).toInt();
105     return numEntries;
106 }
107
108 EmuFrontObject* DatabaseManager::getDataObject(int id)
109 {
110     filterById(id);
111     return getFilteredDataObject();
112 }
113
114 EmuFrontObject* DatabaseManager::getDataObject(QString filter)
115 {
116     qDebug() << "Filtering data object" << filter;
117     QList<QString> filters;
118     filters.append(filter);
119     filterDataObjects(filters);
120     qDebug() << "...done filtering.";
121     return getFilteredDataObject();
122 }
123
124 EmuFrontObject* DatabaseManager::getFilteredDataObject()
125 {
126     EmuFrontObject *plf = 0;
127     // TODO: if record has more than one the first instance is returned
128     // ... check if this is ok in all cases!
129     if (sqlTableModel->rowCount() >= 1)
130     {
131         QSqlRecord record = sqlTableModel->record(0);
132         if (record.isEmpty()) {
133             throw EmuFrontException(tr("No filtered data available"));
134         }
135         else plf = recordToDataObject(&record);
136     }
137      return plf;
138 }
139
140 EmuFrontObject* DatabaseManager::getDataObjectFromModel(QModelIndex *index)
141 {
142     if (!sqlTableModel) sqlTableModel = getDataModel();
143     QSqlRecord record = sqlTableModel->record(index->row());
144     return recordToDataObject(&record);
145 }
146
147 int DatabaseManager::getCurrentTimeStamp() {
148     return QDateTime::currentDateTime().toTime_t();
149 }
150
151 int DatabaseManager::countDataObjectRefs(int id) const
152 {
153     int ret = 0;
154     QSqlQuery q;
155     q.prepare(getCountRefsSelect(id));
156     q.exec();
157     QSqlRecord rec;
158     if (q.next()) {
159         rec = q.record();
160         ret = rec.value(0).toInt();
161     }
162     qDebug() << "Found " << ret << " references.";
163     return ret;
164 }