Clean up.
[emufront] / src / db / dbcreator.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 <QObject>
21 #include <QSqlDatabase>
22 #include <QSqlQuery>
23 #include <QSqlError>
24 #include <QDebug>
25 #include <exception>
26 #include "dbcreator.h"
27
28 using namespace std;
29
30 const int DbCreator::TABLES_COUNT = 3;
31 const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer", "mediaimage", "mediaimagecontainer_mediaimage"};
32
33 DbCreator::DbCreator(QObject *parent) : QObject(parent)
34 {
35 }
36
37
38 bool DbCreator::createDB()
39 {
40     bool ret = false;
41     QSqlQuery query;
42
43     try
44     {
45         query.exec("DROP TABLE IF EXISTS mediaimagecontainer_file");
46         query.exec("DROP TABLE IF EXISTS mediaimagecontainer");
47         query.exec("DROP TABLE IF EXISTS filepath");
48         query.exec("DROP TABLE IF EXISTS setup");
49         query.exec("DROP TABLE IF EXISTS mediatype");
50         query.exec("DROP TABLE IF EXISTS platform");
51         query.exec("DROP TABLE IF EXISTS file");
52         query.exec("DROP TABLE IF EXISTS executable");
53
54         qDebug() << "Creating TABLE file";
55
56         ret = query.exec("CREATE TABLE IF NOT EXISTS file"
57                         "(id INTEGER PRIMARY KEY, "
58                         "name TEXT, "
59                         "type INTEGER, "
60                         "checksum TEXT, "
61                         "size INTEGER, "
62                         "updatetime NUMERIC)");
63
64         if (!ret) throw QString("tbl file");
65
66         qDebug() << "Creating TABLE platform";
67
68         ret = query.exec("CREATE TABLE IF NOT EXISTS platform "
69                          "(id INTEGER PRIMARY KEY, "
70                          "name TEXT, "
71                          "fileid INTEGER REFERENCES file(id))");
72
73         if (!ret) throw QString("tbl platform");
74
75         qDebug() << "Creating TABLE mediatype ";
76
77         ret = query.exec("CREATE TABLE IF NOT EXISTS mediatype "
78                          "(id INTEGER PRIMARY KEY, "
79                          "name TEXT, "
80                          "fileid INTEGER REFERENCES file(id))");
81
82         if (!ret) throw QString("tbl mediatype");
83
84         qDebug() << "Creating TABLE setup";
85
86         ret = query.exec("CREATE TABLE IF NOT EXISTS setup "
87                         "(id INTEGER PRIMARY KEY, "
88                         "platformid INTEGER REFERENCES platform(id) ON DELETE CASCADE, "
89                         "mediatypeid INTEGER REFERENCES mediatype(id) ON DELETE CASCADE, "
90                         "filetypeextensions TEXT)");
91
92         if (!ret) throw QString("tbl setup");
93
94         qDebug() << "Creating table executable";
95
96         ret = query.exec("CREATE TABLE IF NOT EXISTS executable "
97                         "(id INTEGER PRIMARY KEY, "
98                         "name TEXT, "
99                         "executable TEXT, "
100                         "options TEXT, "
101                         "type INTEGER, "
102                         "setupid INTEGER REFERENCES setup(id))");
103
104         if (!ret) throw QString("tbl executable");
105
106         qDebug() << "Creating TABLE filepath";
107
108         ret = query.exec("CREATE TABLE IF NOT EXISTS filepath "
109                          "(id INTEGER PRIMARY KEY, "
110                          "name TEXT, "
111                          "filetypeid INTEGER, "
112                          "setupid INTEGER, "
113                          "lastscanned NUMERIC, "
114                          "FOREIGN KEY (setupid) REFERENCES setup(id))");
115
116         if (!ret) throw QString("tbl filepath");
117
118         qDebug() << "Creating TABLE mediaimagecontainer";
119
120         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer "
121                         "(fileid INTEGER REFERENCES file(id), "
122                         "filepathid INTEGER REFERENCES filepath(id), "
123                         "updatetime NUMERIC)");
124
125         if (!ret) throw QString("tbl mediaimagecontainer");
126
127
128         qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
129
130         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
131                         "(mediaimagecontainerid INTEGER REFERENCES file(id), "
132                         "mediaimageid INTEGER REFERENCES file(id))");
133
134         if (!ret) throw QString("tbl mediaimagecontainer_mediaimage");
135
136         query.exec(
137             "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
138             "AFTER DELETE ON platform "
139             "BEGIN "
140             "   DELETE FROM setup WHERE setup.platformid = old.id;"
141             "END;"
142             );
143
144         if (!ret) throw QString("trg_onplatformdelete");
145
146         query.exec(
147             "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
148             "AFTER DELETE ON mediatype "
149             "BEGIN "
150             "   DELETE FROM setup WHERE setup.mediatypeid = old.id;"
151             "END;"
152             );
153
154         if (!ret) throw QString("trg_onmediatypedelete");
155
156         query.exec(
157             "CREATE TRIGGER IF NOT EXISTS trg_onsetupdelete "
158             "AFTER DELETE ON setup "
159             "BEGIN "
160             "   DELETE FROM filepath WHERE filepath.setupid = old.id; "
161             "   DELETE FROM executable WHERE executable.setupid = old.id; "
162             "END;"
163             );
164
165         if (!ret) throw QString("trg_onsetupdelete");
166
167         query.exec(
168             "CREATE TRIGGER IF NOT EXISTS trg_onfilepathdelete "
169             "AFTER DELETE ON filepath "
170             "BEGIN "
171             "   DELETE FROM mediaimagecontainer WHERE mediaimagecontainer.filepathid=old.id; "
172             "END;"
173         );
174
175         if (!ret) throw QString("trg_onfilepathdelete");
176
177         query.exec(
178             "CREATE TRIGGER IF NOT EXISTS trg_onmediaimagecontainerdelete "
179             "AFTER DELETE ON mediaimagecontainer "
180             "BEGIN "
181             "   DELETE FROM mediaimagecontainer_mediaimage WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid=old.fileid;"
182             "END;"
183         );
184
185         if (!ret) throw QString("trg_onmediaimagecontainerdelete");
186
187         query.exec(
188             "CREATE TRIGGER IF NOT EXISTS trg_onmediaimagecontainer_mediaimagedelete "
189             "AFTER DELETE ON mediaimagecontainer_mediaimage "
190             "BEGIN "
191             "    DELETE FROM file WHERE file.id=old.mediaimageid; "
192             "    DELETE FROM file WHERE file.id=old.mediaimagecontainerid; "
193             "END;"
194         );
195         if (!ret) throw QString("trg_onmediaimagecontainer_mediaimagedelete");
196
197     }
198     catch (QString tbl)
199     {
200         QString err = query.lastError().text();
201         throw QString("Couldn't CREATE '%1'!").arg(tbl).append(err);
202     }
203     return ret;
204 }
205
206 /**
207  * Check if database already exists.
208  * Returns false if doesn't or we don't have a connection.
209 */
210 bool DbCreator::dbExists()
211 {
212     for (int i = 0; i < TABLES_COUNT; ++i)
213     {
214         if (!tableExists(TABLES[i]))
215         {
216             qDebug() << "Table " << TABLES[i] << " missing.";
217             return false;
218         }
219        qDebug() << "Table " << TABLES[i] << " exists.";
220     }
221     return true;
222 }
223
224 bool DbCreator::tableExists(QString TABLE)
225 {
226     QSqlQuery query;
227     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
228     return query.next();
229 }
230
231 bool DbCreator::deleteDB()
232 {
233     // return QFile::remove(getDbPath());
234     return false;
235 }