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