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", "filetype"};
32 DbCreator::DbCreator(QObject *parent) : QObject(parent)
37 bool DbCreator::createDB()
44 if (!tableExists("platform"))
46 qDebug() << "Creating table platform";
47 ret = query.exec("create table platform "
48 "(id integer primary key, "
50 "filename varchar(125))");
51 if (!ret) throw QString("platform.");
53 if (!tableExists("mediatype"))
55 qDebug() << "Creating table mediatype";
56 ret = query.exec("create table mediatype "
57 "(id integer primary key, "
59 "filename varchar(125))");
60 if (!ret) throw QString("mediatype.");
62 if (!tableExists("filetype"))
64 qDebug() << "Creating table filetype";
65 ret = query.exec("create table filetype "
66 "(id integer primary key, "
68 if (!ret) throw QString("filetype.");
69 query.exec("insert into filetype (id, name) values (1, 'media image container')");
70 query.exec("insert into filetype (id, name) values (2, 'screenshot')");
71 query.exec("insert into filetype (id, name) values (3, 'platform icon')");
72 query.exec("insert into filetype (id, name) values (4, 'media type icon')");
74 /*if (!tableExists("filepath"))
76 qDebug() << "Creating table filepath";
77 query.exec("create table filepath "
78 "(id integer primary key, "
79 "name varchar(255))");
81 if (!ret) throw QString("filepath");
86 throw QString("Couldn't create database '%1'!").arg(tbl);
92 * Check if database already exists.
93 * Returns false if doesn't or we don't have a connection.
95 bool DbCreator::dbExists()
97 for (int i = 0; i < TABLES_COUNT; ++i)
99 if (!tableExists(TABLES[i]))
101 qDebug() << "Table " << TABLES[i] << " missing.";
104 qDebug() << "Table " << TABLES[i] << " exists.";
109 bool DbCreator::tableExists(QString table)
112 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
116 bool DbCreator::deleteDB()
118 // return QFile::remove(getDbPath());