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 mediatype");
49 query.exec("drop table if exists platform");
51 qDebug() << "Creating table platform";
53 ret = query.exec("create table if not exists platform "
54 "(id integer primary key, "
56 "filename varchar(125))");
58 if (!ret) throw QString("platform.");
60 qDebug() << "Creating table mediatype ";
62 ret = query.exec("create table if not exists mediatype "
63 "(id integer primary key, "
65 "filename varchar(125))");
67 if (!ret) throw QString("mediatype.");
69 /*qDebug() << "Creating table filetype";
70 ret = query.exec("create table filetype if not exists"
71 "(id integer primary key, "
73 if (!ret) throw QString("filetype.");
74 query.exec("insert into filetype (id, name) values (1, 'media image container')");
75 query.exec("insert into filetype (id, name) values (2, 'screenshot')");
76 query.exec("insert into filetype (id, name) values (3, 'platform icon')");
77 query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
79 qDebug() << "Creating table filepath";
81 ret = query.exec("create table if not exists filepath "
82 "(id integer primary key, "
84 "filetypeid integer, "
85 "platformid integer, "
86 "mediatypeid integer, "
87 "lastscanned numeric, "
88 "foreign key (platformid) references platform(id), "
89 "foreign key (mediatypeid) references mediatype(id))");
91 if (!ret) throw QString("filepath");
93 qDebug() << "Creating table mediaimagecontainer";
95 ret = query.exec("create table if not exists mediaimagecontainer "
96 "(id integer primary key, "
101 "filepathid integer, "
102 "platformid integer, "
103 "mediatypeid integer, "
104 "updatetime numeric, "
105 "foreign key (filepathid) references filepath(id), "
106 "foreign key (platformid) references platform(id), "
107 "foreign key (mediatypeid) references mediatype(id))");
109 if (!ret) throw QString("mediaimagecontainer");
111 qDebug() << "Creating table mediaimage";
113 ret = query.exec("create table if not exists mediaimage "
114 "(id integer primary key, "
119 "updatetime numeric)");
121 qDebug() << "Creating table mediaimagecontainer_mediaimage";
123 ret = query.exec("create table if not exists mediaimagecontainer_mediaimage "
124 "(mediaimagecontainerid integer, "
125 "mediaimageid integer, "
126 "foreign key (mediaimagecontainerid) references mediaimagecontainer(id), "
127 "foreign key (mediaimageid) references mediaimage(id))");
129 if (!ret) throw QString("mediaimagecontainer_mediaimage");
133 throw QString("Couldn't create database '%1'!").arg(tbl);
139 * Check if database already exists.
140 * Returns false if doesn't or we don't have a connection.
142 bool DbCreator::dbExists()
144 for (int i = 0; i < TABLES_COUNT; ++i)
146 if (!tableExists(TABLES[i]))
148 qDebug() << "Table " << TABLES[i] << " missing.";
151 qDebug() << "Table " << TABLES[i] << " exists.";
156 bool DbCreator::tableExists(QString table)
159 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
163 bool DbCreator::deleteDB()
165 // return QFile::remove(getDbPath());