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"};
32 DbCreator::DbCreator(QObject *parent) : QObject(parent)
37 bool DbCreator::createDB()
44 /*if (!tableExists("platform"))
46 qDebug() << "Creating table platform";
47 query.exec("drop table if exists platform");
48 ret = query.exec("create table if not exists platform "
49 "(id integer primary key, "
51 "filename varchar(125))");
52 if (!ret) throw QString("platform.");
54 if (!tableExists("mediatype"))
56 qDebug() << "Creating table mediatype ";
57 query.exec("drop table if exists mediatype");
58 ret = query.exec("create table if not exists mediatype "
59 "(id integer primary key, "
61 "filename varchar(125))");
62 if (!ret) throw QString("mediatype.");
64 if (!tableExists("filetype"))
66 /*qDebug() << "Creating table filetype";
67 ret = query.exec("create table filetype if not exists"
68 "(id integer primary key, "
70 if (!ret) throw QString("filetype.");
71 query.exec("insert into filetype (id, name) values (1, 'media image container')");
72 query.exec("insert into filetype (id, name) values (2, 'screenshot')");
73 query.exec("insert into filetype (id, name) values (3, 'platform icon')");
74 query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
76 if (!tableExists("filepath"))
78 qDebug() << "Creating table filepath";
79 query.exec("drop table if exists filepath");
80 ret = query.exec("create table if not exists filepath "
81 "(id integer primary key, "
83 "filetypeid integer, "
84 "platformid integer, "
85 "mediatypeid integer, "
86 "lastscanned numeric, "
87 "foreign key (platformid) references platform(id), "
88 "foreign key (mediatypeid) references mediatype(id))");
89 if (ret) qDebug() << "Table filepath created succesfully!";
91 if (!ret) throw QString("filepath");
96 throw QString("Couldn't create database '%1'!").arg(tbl);
102 * Check if database already exists.
103 * Returns false if doesn't or we don't have a connection.
105 bool DbCreator::dbExists()
107 for (int i = 0; i < TABLES_COUNT; ++i)
109 if (!tableExists(TABLES[i]))
111 qDebug() << "Table " << TABLES[i] << " missing.";
114 qDebug() << "Table " << TABLES[i] << " exists.";
119 bool DbCreator::tableExists(QString table)
122 query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
126 bool DbCreator::deleteDB()
128 // return QFile::remove(getDbPath());