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 // 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.
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/>.
21 #include <QSqlDatabase>
26 #include "dbcreator.h"
30 const int DbCreator::TABLES_COUNT = 3;
31 const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer", "mediaimage", "mediaimagecontainer_mediaimage"};
33 DbCreator::DbCreator(QObject *parent) : QObject(parent)
38 bool DbCreator::createDB()
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");
53 qDebug() << "Creating TABLE file";
55 ret = query.exec("CREATE TABLE IF NOT EXISTS file"
56 "(id INTEGER PRIMARY KEY, "
61 "updatetime NUMERIC)");
63 qDebug() << "Creating TABLE platform";
65 ret = query.exec("CREATE TABLE IF NOT EXISTS platform "
66 "(id INTEGER PRIMARY KEY, "
68 "fileid INTEGER REFERENCES file(id))");
70 if (!ret) throw QString("platform.");
72 qDebug() << "Creating TABLE mediatype ";
74 ret = query.exec("CREATE TABLE IF NOT EXISTS mediatype "
75 "(id INTEGER PRIMARY KEY, "
77 "fileid INTEGER REFERENCES file(id))");
79 if (!ret) throw QString("mediatype.");
81 qDebug() << "Creating TABLE setup";
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)");
89 /*qDebug() << "Creating TABLE filetype";
90 ret = query.exec("CREATE TABLE filetype IF NOT EXISTS"
91 "(id INTEGER PRIMARY KEY, "
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')");*/
99 qDebug() << "Creating TABLE filepath";
101 ret = query.exec("CREATE TABLE IF NOT EXISTS filepath "
102 "(id INTEGER PRIMARY KEY, "
104 "filetypeid INTEGER, "
106 "lastscanned NUMERIC, "
107 "FOREIGN KEY (setupid) REFERENCES setup(id))");
109 if (!ret) throw QString("filepath");
111 qDebug() << "Creating TABLE mediaimagecontainer";
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)");
118 if (!ret) throw QString("mediaimagecontainer");
121 qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
123 ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
124 "(mediaimagecontainerid INTEGER REFERENCES file(id), "
125 "mediaimageid INTEGER REFERENCES file(id))");
127 if (!ret) throw QString("mediaimagecontainer_mediaimage");
130 "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
131 "AFTER DELETE ON platform "
133 " DELETE FROM setup WHERE setup.platformid = old.id;"
138 "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
139 "AFTER DELETE ON mediatype "
141 " DELETE FROM setup WHERE setup.mediatypeid = old.id;"
146 "CREATE TRIGGER IF NOT EXISTS trg_onsetupdelete "
147 "AFTER DELETE ON setup "
149 " DELETE FROM filepath WHERE filepath.setupid = old.id;"
153 "CREATE TRIGGER IF NOT EXISTS trg_onfiledelete "
154 "AFTER DELETE ON file "
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;"
164 QString err = query.lastError().text();
165 throw QString("Couldn't CREATE table '%1'!").arg(tbl).append(err);
171 * Check if database already exists.
172 * Returns false if doesn't or we don't have a connection.
174 bool DbCreator::dbExists()
176 for (int i = 0; i < TABLES_COUNT; ++i)
178 if (!tableExists(TABLES[i]))
180 qDebug() << "Table " << TABLES[i] << " missing.";
183 qDebug() << "Table " << TABLES[i] << " exists.";
188 bool DbCreator::tableExists(QString TABLE)
191 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
195 bool DbCreator::deleteDB()
197 // return QFile::remove(getDbPath());