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>
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 "(id INTEGER PRIMARY KEY, "
115 "fileid INTEGER REFERENCES file(id), "
116 "filepathid INTEGER REFERENCES filepath(id), "
117 "updatetime NUMERIC)");
119 if (!ret) throw QString("mediaimagecontainer");
122 qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
124 ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
125 "(mediaimagecontainerid INTEGER, "
127 "FOREIGN KEY (mediaimagecontainerid) REFERENCES mediaimagecontainer(id), "
128 "FOREIGN KEY (fileid) REFERENCES file(id))");
130 if (!ret) throw QString("mediaimagecontainer_mediaimage");
133 "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
134 "AFTER DELETE ON platform "
136 " DELETE FROM setup WHERE setup.platformid = old.id;"
141 "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
142 "AFTER DELETE ON mediatype "
144 " DELETE FROM setup WHERE setup.mediatypeid = old.id;"
149 "CREATE TRIGGER IF NOT EXISTS trg_onsetupdelete "
150 "AFTER DELETE ON setup "
152 " DELETE FROM filepath WHERE filepath.setupid = old.id;"
156 "CREATE TRIGGER IF NOT EXISTS trg_onfiledelete "
157 "AFTER DELETE ON file "
159 " UPDATE platform SET platform.inconfileid=NULL WHERE platform.iconfileid = old.id;"
160 " UPDATE mediatype SET mediatype.iconfileid=NULL WHERE mediatype.iconfileid = old.id;"
161 " DELETE FROM mediaimagecontainer_mediaimage WHERE mediaimagecontainer_mediaimage.fileid = old.id;"
167 QString err = query.lastError().text();
168 throw QString("Couldn't CREATE table '%1'!").arg(tbl).append(err);
174 * Check if database already exists.
175 * Returns false if doesn't or we don't have a connection.
177 bool DbCreator::dbExists()
179 for (int i = 0; i < TABLES_COUNT; ++i)
181 if (!tableExists(TABLES[i]))
183 qDebug() << "Table " << TABLES[i] << " missing.";
186 qDebug() << "Table " << TABLES[i] << " exists.";
191 bool DbCreator::tableExists(QString TABLE)
194 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
198 bool DbCreator::deleteDB()
200 // return QFile::remove(getDbPath());