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