2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlDatabase>
25 #include "dbcreator.h"
29 const int DbCreator::TABLES_COUNT = 3;
30 const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer", "mediaimage", "mediaimagecontainer_mediaimage"};
32 DbCreator::DbCreator(QObject *parent) : QObject(parent)
37 bool DbCreator::createDB()
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");
52 qDebug() << "Creating TABLE platform";
54 ret = query.exec("CREATE TABLE IF NOT EXISTS platform "
55 "(id INTEGER PRIMARY KEY, "
59 if (!ret) throw QString("platform.");
61 qDebug() << "Creating TABLE mediatype ";
63 ret = query.exec("CREATE TABLE IF NOT EXISTS mediatype "
64 "(id INTEGER PRIMARY KEY, "
68 if (!ret) throw QString("mediatype.");
70 qDebug() << "Creating TABLE setup";
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)");
78 /*qDebug() << "Creating TABLE filetype";
79 ret = query.exec("CREATE TABLE filetype IF NOT EXISTS"
80 "(id INTEGER PRIMARY KEY, "
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')");*/
88 qDebug() << "Creating TABLE filepath";
90 ret = query.exec("CREATE TABLE IF NOT EXISTS filepath "
91 "(id INTEGER PRIMARY KEY, "
93 "filetypeid INTEGER, "
95 "lastscanned NUMERIC, "
96 "FOREIGN KEY (setupid) REFERENCES setup(id))");
98 if (!ret) throw QString("filepath");
100 qDebug() << "Creating TABLE mediaimagecontainer";
102 ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer "
103 "(id INTEGER PRIMARY KEY, "
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))");
116 if (!ret) throw QString("mediaimagecontainer");
118 qDebug() << "Creating TABLE mediaimage";
120 ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimage "
121 "(id INTEGER PRIMARY KEY, "
126 "updatetime NUMERIC)");
128 qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
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))");
136 if (!ret) throw QString("mediaimagecontainer_mediaimage");
139 "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
140 "AFTER DELETE ON platform "
142 " DELETE FROM setup WHERE setup.platformid = old.id;"
147 "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
148 "AFTER DELETE ON mediatype"
150 " DELETE FROM setup WHERE setup.mediatypeid = old.id;"
157 throw QString("Couldn't CREATE database '%1'!").arg(tbl);
163 * Check if database already exists.
164 * Returns false if doesn't or we don't have a connection.
166 bool DbCreator::dbExists()
168 for (int i = 0; i < TABLES_COUNT; ++i)
170 if (!tableExists(TABLES[i]))
172 qDebug() << "Table " << TABLES[i] << " missing.";
175 qDebug() << "Table " << TABLES[i] << " exists.";
180 bool DbCreator::tableExists(QString TABLE)
183 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
187 bool DbCreator::deleteDB()
189 // return QFile::remove(getDbPath());