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, "
57 "filename varchar(125))");
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, "
66 "filename varchar(125))");
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, "
75 "mediatypeid integer, "
76 "filetypeextensions text, "
77 "foreign key (platformid) references platform(id), "
78 "foreign key (mediatypeid) references mediatype(id))");
80 /*qDebug() << "Creating table filetype";
81 ret = query.exec("create table filetype if not exists"
82 "(id integer primary key, "
84 if (!ret) throw QString("filetype.");
85 query.exec("insert into filetype (id, name) values (1, 'media image container')");
86 query.exec("insert into filetype (id, name) values (2, 'screenshot')");
87 query.exec("insert into filetype (id, name) values (3, 'platform icon')");
88 query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
90 qDebug() << "Creating table filepath";
92 ret = query.exec("create table if not exists filepath "
93 "(id integer primary key, "
95 "filetypeid integer, "
97 "lastscanned numeric, "
98 "foreign key (setupid) references setup(id))");
100 if (!ret) throw QString("filepath");
102 qDebug() << "Creating table mediaimagecontainer";
104 ret = query.exec("create table if not exists mediaimagecontainer "
105 "(id integer primary key, "
110 "filepathid integer, "
111 "platformid integer, "
112 "mediatypeid integer, "
113 "updatetime numeric, "
114 "foreign key (filepathid) references filepath(id), "
115 "foreign key (platformid) references platform(id), "
116 "foreign key (mediatypeid) references mediatype(id))");
118 if (!ret) throw QString("mediaimagecontainer");
120 qDebug() << "Creating table mediaimage";
122 ret = query.exec("create table if not exists mediaimage "
123 "(id integer primary key, "
128 "updatetime numeric)");
130 qDebug() << "Creating table mediaimagecontainer_mediaimage";
132 ret = query.exec("create table if not exists mediaimagecontainer_mediaimage "
133 "(mediaimagecontainerid integer, "
134 "mediaimageid integer, "
135 "foreign key (mediaimagecontainerid) references mediaimagecontainer(id), "
136 "foreign key (mediaimageid) references mediaimage(id))");
138 if (!ret) throw QString("mediaimagecontainer_mediaimage");
142 throw QString("Couldn't create database '%1'!").arg(tbl);
148 * Check if database already exists.
149 * Returns false if doesn't or we don't have a connection.
151 bool DbCreator::dbExists()
153 for (int i = 0; i < TABLES_COUNT; ++i)
155 if (!tableExists(TABLES[i]))
157 qDebug() << "Table " << TABLES[i] << " missing.";
160 qDebug() << "Table " << TABLES[i] << " exists.";
165 bool DbCreator::tableExists(QString table)
168 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
172 bool DbCreator::deleteDB()
174 // return QFile::remove(getDbPath());