Fixed a syntax error in SQL.
[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                         "(fileid INTEGER REFERENCES file(id), "
115                         "filepathid INTEGER REFERENCES filepath(id), "
116                         "updatetime NUMERIC)");
117
118         if (!ret) throw QString("mediaimagecontainer");
119
120
121         qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
122
123         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
124                         "(mediaimagecontainerid INTEGER REFERENCES file(id), "
125                         "mediaimageid INTEGER REFERENCES file(id))");
126
127         if (!ret) throw QString("mediaimagecontainer_mediaimage");
128
129         query.exec(
130             "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
131             "AFTER DELETE ON platform "
132             "BEGIN "
133             "   DELETE FROM setup WHERE setup.platformid = old.id;"
134             "END;"
135             );
136
137         query.exec(
138             "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
139             "AFTER DELETE ON mediatype "
140             "BEGIN "
141             "   DELETE FROM setup WHERE setup.mediatypeid = old.id;"
142             "END;"
143             );
144
145         query.exec(
146             "CREATE TRIGGER IF NOT EXISTS trg_onsetupdelete "
147             "AFTER DELETE ON setup "
148             "BEGIN "
149             "   DELETE FROM filepath WHERE filepath.setupid = old.id;"
150             "END;"
151             );
152         query.exec(
153             "CREATE TRIGGER IF NOT EXISTS trg_onfiledelete "
154             "AFTER DELETE ON file "
155             "BEGIN "
156             "   UPDATE platform SET platform.inconfileid=NULL WHERE platform.iconfileid = old.id;"
157             "   UPDATE mediatype SET mediatype.iconfileid=NULL WHERE mediatype.iconfileid = old.id;"
158             "   DELETE FROM mediaimagecontainer_mediaimage WHERE mediaimagecontainer_mediaimage.fileid = old.id;"
159             "END;"
160         );
161     }
162     catch (QString tbl)
163     {
164         QString err = query.lastError().text();
165         throw QString("Couldn't CREATE table '%1'!").arg(tbl).append(err);
166     }
167     return ret;
168 }
169
170 /**
171  * Check if database already exists.
172  * Returns false if doesn't or we don't have a connection.
173 */
174 bool DbCreator::dbExists()
175 {
176     for (int i = 0; i < TABLES_COUNT; ++i)
177     {
178         if (!tableExists(TABLES[i]))
179         {
180             qDebug() << "Table " << TABLES[i] << " missing.";
181             return false;
182         }
183        qDebug() << "Table " << TABLES[i] << " exists.";
184     }
185     return true;
186 }
187
188 bool DbCreator::tableExists(QString TABLE)
189 {
190     QSqlQuery query;
191     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
192     return query.next();
193 }
194
195 bool DbCreator::deleteDB()
196 {
197     // return QFile::remove(getDbPath());
198     return false;
199 }