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