Reorganizing database creation to a new class: DbCreator
[emufront] / src / db / dbcreator.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QObject>
21 #include <QSqlDatabase>
22 #include <QSqlQuery>
23 #include "dbcreator.h"
24
25 DbCreator::DbCreator(QObject *parent) : QObject(parent)
26 {
27 }
28
29
30 bool DbCreator::createDB()
31 {
32     bool ret = false;
33     QSqlQuery query;
34
35     if (!tableExists("platform"))
36     {
37         query.exec("create table platform "
38                          "(id integer primary key, "
39                          "name varchar(30), "
40                          "filename varchar(125))");
41     }
42     if (!tableExists("mediatype"))
43     {
44         query.exec("create table mediatype "
45                          "(id integer primary key, "
46                          "name varchar(30), "
47                          "filename varchar(125))");
48     }
49     if (!tableExists("filetype"))
50     {
51         query.exec("create table filetype "
52                             "(id integer primary key, "
53                             "name varchar(30))");
54         query.exec("insert into filetype (id, name) values (1, 'media image container')");
55         query.exec("insert into filetype (id, name) values (2, 'screenshot')");
56         query.exec("insert into filetype (id, name) values (3, 'platform icon')");
57         query.exec("insert into filetype (id, name) values (4, 'media type icon')");
58     }
59     /*if (!tableExists("filepath"))
60     {
61         query.exec("create table filepath "
62                     "(id integer primary key, "
63                     "name varchar(255))");
64
65     }*/
66     return ret;
67 }
68
69 /**
70  * Check if database already exists.
71  * Returns false if doesn't or we don't have a connection.
72 */
73 bool DbCreator::dbExists()
74 {
75     return tableExists("platform");
76 }
77
78 bool DbCreator::tableExists(QString table)
79 {
80     QSqlQuery query;
81     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
82     return query.next();
83 }
84
85 bool DbCreator::deleteDB()
86 {
87     // return QFile::remove(getDbPath());
88     return false;
89 }