Added triggers (on delete platform / mediatype)
[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 // Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QObject>
21 #include <QSqlDatabase>
22 #include <QSqlQuery>
23 #include <QDebug>
24 #include <exception>
25 #include "dbcreator.h"
26
27 using namespace std;
28
29 const int DbCreator::TABLES_COUNT = 3;
30 const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer", "mediaimage", "mediaimagecontainer_mediaimage"};
31
32 DbCreator::DbCreator(QObject *parent) : QObject(parent)
33 {
34 }
35
36
37 bool DbCreator::createDB()
38 {
39     bool ret = false;
40     QSqlQuery query;
41
42     try
43     {
44         query.exec("DROP TABLE IF EXISTS mediaimagecontainer_mediaimage");
45         query.exec("DROP TABLE IF EXISTS mediaimage");
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
52         qDebug() << "Creating TABLE platform";
53
54         ret = query.exec("CREATE TABLE IF NOT EXISTS platform "
55                          "(id INTEGER PRIMARY KEY, "
56                          "name TEXT, "
57                          "filename TEXT)");
58
59         if (!ret) throw QString("platform.");
60
61         qDebug() << "Creating TABLE mediatype ";
62
63         ret = query.exec("CREATE TABLE IF NOT EXISTS mediatype "
64                          "(id INTEGER PRIMARY KEY, "
65                          "name TEXT, "
66                          "filename TEXT)");
67
68         if (!ret) throw QString("mediatype.");
69
70         qDebug() << "Creating TABLE setup";
71
72         ret = query.exec("CREATE TABLE IF NOT EXISTS setup "
73                         "(id INTEGER PRIMARY KEY, "
74                         "platformid INTEGER REFERENCES platform(id) ON DELETE CASCADE, "
75                         "mediatypeid INTEGER REFERENCES mediatype(id) ON DELETE CASCADE, "
76                         "filetypeextensions TEXT)");
77
78         /*qDebug() << "Creating TABLE filetype";
79             ret = query.exec("CREATE TABLE filetype IF NOT EXISTS"
80                              "(id INTEGER PRIMARY KEY, "
81                              "name TEXT)");
82             if (!ret) throw QString("filetype.");
83             query.exec("insert into filetype (id, name) values (1, 'media image container')");
84             query.exec("insert into filetype (id, name) values (2, 'screenshot')");
85             query.exec("insert into filetype (id, name) values (3, 'platform icon')");
86             query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
87
88         qDebug() << "Creating TABLE filepath";
89
90         ret = query.exec("CREATE TABLE IF NOT EXISTS filepath "
91                          "(id INTEGER PRIMARY KEY, "
92                          "name TEXT, "
93                          "filetypeid INTEGER, "
94                          "setupid INTEGER, "
95                          "lastscanned NUMERIC, "
96                          "FOREIGN KEY (setupid) REFERENCES setup(id))");
97
98         if (!ret) throw QString("filepath");
99
100         qDebug() << "Creating TABLE mediaimagecontainer";
101
102         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer "
103                         "(id INTEGER PRIMARY KEY, "
104                         "name TEXT, "
105                         "filename TEXT, "
106                         "sha1 TEXT, "
107                         "md5 TEXT, "
108                         "filepathid INTEGER, "
109                         "platformid INTEGER, "
110                         "mediatypeid INTEGER, "
111                         "updatetime NUMERIC, "
112                         "FOREIGN KEY (filepathid) REFERENCES filepath(id), "
113                         "FOREIGN KEY (platformid) REFERENCES platform(id), "
114                         "FOREIGN KEY (mediatypeid) REFERENCES mediatype(id))");
115
116         if (!ret) throw QString("mediaimagecontainer");
117
118         qDebug() << "Creating TABLE mediaimage";
119
120         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimage "
121                         "(id INTEGER PRIMARY KEY, "
122                         "filename TEXT, "
123                         "sha1 TEXT, "
124                         "md5 TEXT, "
125                         "filesize INTEGER, "
126                         "updatetime NUMERIC)");
127
128         qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
129
130         ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
131                         "(mediaimagecontainerid INTEGER, "
132                         "mediaimageid INTEGER, "
133                         "FOREIGN KEY (mediaimagecontainerid) REFERENCES mediaimagecontainer(id), "
134                         "FOREIGN KEY (mediaimageid) REFERENCES mediaimage(id))");
135
136         if (!ret) throw QString("mediaimagecontainer_mediaimage");
137
138         ret = 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         if (!ret) throw QString("trg_onplatformdelete");
147
148         ret = query.exec(
149             "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
150             "AFTER DELETE ON mediatype"
151             "BEGIN "
152             "   DELETE FROM setup WHERE setup.mediatypeid = old.id;"
153             "END;"
154             );
155
156         if (!ret) throw QString("trg_onmediatypedelete");
157
158     }
159     catch (QString tbl)
160     {
161         throw QString("Couldn't CREATE database '%1'!").arg(tbl);
162     }
163     return ret;
164 }
165
166 /**
167  * Check if database already exists.
168  * Returns false if doesn't or we don't have a connection.
169 */
170 bool DbCreator::dbExists()
171 {
172     for (int i = 0; i < TABLES_COUNT; ++i)
173     {
174         if (!tableExists(TABLES[i]))
175         {
176             qDebug() << "Table " << TABLES[i] << " missing.";
177             return false;
178         }
179        qDebug() << "Table " << TABLES[i] << " exists.";
180     }
181     return true;
182 }
183
184 bool DbCreator::tableExists(QString TABLE)
185 {
186     QSqlQuery query;
187     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
188     return query.next();
189 }
190
191 bool DbCreator::deleteDB()
192 {
193     // return QFile::remove(getDbPath());
194     return false;
195 }