EmuFront ... NOT Foobar :D
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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
53         qDebug() << "Creating TABLE file";
54
55         ret = query.exec("CREATE TABLE IF NOT EXISTS file"
56                         "(id INTEGER PRIMARY KEY, "
57                         "name TEXT, "
58                         "type INTEGER, "
59                         "checksum TEXT, "
60                         "size INTEGER, "
61                         "updatetime NUMERIC)");
62
63         qDebug() << "Creating TABLE platform";
64
65         ret = query.exec("CREATE TABLE IF NOT EXISTS platform "
66                          "(id INTEGER PRIMARY KEY, "
67                          "name TEXT, "
68                          "fileid INTEGER REFERENCES file(id))");
69
70         if (!ret) throw QString("platform.");
71
72         qDebug() << "Creating TABLE mediatype ";
73
74         ret = query.exec("CREATE TABLE IF NOT EXISTS mediatype "
75                          "(id INTEGER PRIMARY KEY, "
76                          "name TEXT, "
77                          "fileid INTEGER REFERENCES file(id))");
78
79         if (!ret) throw QString("mediatype.");
80
81         qDebug() << "Creating TABLE setup";
82
83         ret = query.exec("CREATE TABLE IF NOT EXISTS setup "
84                         "(id INTEGER PRIMARY KEY, "
85                         "platformid INTEGER REFERENCES platform(id) ON DELETE CASCADE, "
86                         "mediatypeid INTEGER REFERENCES mediatype(id) ON DELETE CASCADE, "
87                         "filetypeextensions TEXT)");
88
89         /*qDebug() << "Creating TABLE filetype";
90             ret = query.exec("CREATE TABLE filetype IF NOT EXISTS"
91                              "(id INTEGER PRIMARY KEY, "
92                              "name TEXT)");
93             if (!ret) throw QString("filetype.");
94             query.exec("insert into filetype (id, name) values (1, 'media image container')");
95             query.exec("insert into filetype (id, name) values (2, 'screenshot')");
96             query.exec("insert into filetype (id, name) values (3, 'platform icon')");
97             query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
98
99         qDebug() << "Creating TABLE filepath";
100
101         ret = query.exec("CREATE TABLE IF NOT EXISTS filepath "
102                          "(id INTEGER PRIMARY KEY, "
103                          "name TEXT, "
104                          "filetypeid INTEGER, "
105                          "setupid INTEGER, "
106                          "lastscanned NUMERIC, "
107                          "FOREIGN KEY (setupid) REFERENCES setup(id))");
108
109         if (!ret) throw QString("filepath");
110
111         qDebug() << "Creating TABLE mediaimagecontainer";
112
113         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer "
114                         "(id INTEGER PRIMARY KEY, "
115                         "fileid INTEGER REFERENCES file(id), "
116                         "filepathid INTEGER REFERENCES filepath(id), "
117                         "updatetime NUMERIC)");
118
119         if (!ret) throw QString("mediaimagecontainer");
120
121
122         qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
123
124         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
125                         "(mediaimagecontainerid INTEGER, "
126                         "fileid INTEGER, "
127                         "FOREIGN KEY (mediaimagecontainerid) REFERENCES mediaimagecontainer(id), "
128                         "FOREIGN KEY (fileid) REFERENCES file(id))");
129
130         if (!ret) throw QString("mediaimagecontainer_mediaimage");
131
132         query.exec(
133             "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
134             "AFTER DELETE ON platform "
135             "BEGIN "
136             "   DELETE FROM setup WHERE setup.platformid = old.id;"
137             "END;"
138             );
139
140         query.exec(
141             "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
142             "AFTER DELETE ON mediatype "
143             "BEGIN "
144             "   DELETE FROM setup WHERE setup.mediatypeid = old.id;"
145             "END;"
146             );
147
148         query.exec(
149             "CREATE TRIGGER IF NOT EXISTS trg_onsetupdelete "
150             "AFTER DELETE ON setup "
151             "BEGIN "
152             "   DELETE FROM filepath WHERE filepath.setupid = old.id;"
153             "END;"
154             );
155         query.exec(
156             "CREATE TRIGGER IF NOT EXISTS trg_onfiledelete "
157             "AFTER DELETE ON file "
158             "BEGIN "
159             "   UPDATE platform SET platform.inconfileid=NULL WHERE platform.iconfileid = old.id;"
160             "   UPDATE mediatype SET mediatype.iconfileid=NULL WHERE mediatype.iconfileid = old.id;"
161             "   DELETE FROM mediaimagecontainer_mediaimage WHERE mediaimagecontainer_mediaimage.fileid = old.id;"
162             "END;"
163         );
164     }
165     catch (QString tbl)
166     {
167         QString err = query.lastError().text();
168         throw QString("Couldn't CREATE table '%1'!").arg(tbl).append(err);
169     }
170     return ret;
171 }
172
173 /**
174  * Check if database already exists.
175  * Returns false if doesn't or we don't have a connection.
176 */
177 bool DbCreator::dbExists()
178 {
179     for (int i = 0; i < TABLES_COUNT; ++i)
180     {
181         if (!tableExists(TABLES[i]))
182         {
183             qDebug() << "Table " << TABLES[i] << " missing.";
184             return false;
185         }
186        qDebug() << "Table " << TABLES[i] << " exists.";
187     }
188     return true;
189 }
190
191 bool DbCreator::tableExists(QString TABLE)
192 {
193     QSqlQuery query;
194     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
195     return query.next();
196 }
197
198 bool DbCreator::deleteDB()
199 {
200     // return QFile::remove(getDbPath());
201     return false;
202 }