Added some database creation logic.
[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 <QDebug>
24 #include <exception>
25 #include "dbcreator.h"
26
27 using namespace std;
28
29 const int DbCreator::TABLES_COUNT = 3;
30 const QString DbCreator::TABLES[] = {"platform", "mediatype", "filetype"};
31
32 DbCreator::DbCreator(QObject *parent) : QObject(parent)
33 {
34 }
35
36
37 bool DbCreator::createDB()
38 {
39     bool ret = false;
40     QSqlQuery query;
41
42     try
43     {
44         if (!tableExists("platform"))
45         {
46             qDebug() << "Creating table platform";
47             ret = query.exec("create table platform "
48                              "(id integer primary key, "
49                              "name varchar(30), "
50                              "filename varchar(125))");
51             if (!ret) throw QString("platform.");
52         }
53         if (!tableExists("mediatype"))
54         {
55             qDebug() << "Creating table mediatype";
56             ret = query.exec("create table mediatype "
57                              "(id integer primary key, "
58                              "name varchar(30), "
59                              "filename varchar(125))");
60             if (!ret) throw QString("mediatype.");
61         }
62         if (!tableExists("filetype"))
63         {
64             qDebug() << "Creating table filetype";
65             ret = query.exec("create table filetype "
66                              "(id integer primary key, "
67                              "name varchar(30))");
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')");
73         }
74         /*if (!tableExists("filepath"))
75     {
76         qDebug() << "Creating table filepath";
77         query.exec("create table filepath "
78                     "(id integer primary key, "
79                     "name varchar(255))");
80
81         if (!ret) throw QString("filepath");
82     }*/
83     }
84     catch (QString tbl)
85     {
86         throw QString("Couldn't create database '%1'!").arg(tbl);
87     }
88     return ret;
89 }
90
91 /**
92  * Check if database already exists.
93  * Returns false if doesn't or we don't have a connection.
94 */
95 bool DbCreator::dbExists()
96 {
97     for (int i = 0; i < TABLES_COUNT; ++i)
98     {
99         if (!tableExists(TABLES[i]))
100         {
101             qDebug() << "Table " << TABLES[i] << " missing.";
102             return false;
103         }
104        qDebug() << "Table " << TABLES[i] << " exists.";
105     }
106     return true;
107 }
108
109 bool DbCreator::tableExists(QString table)
110 {
111     QSqlQuery query;
112     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
113     return query.next();
114 }
115
116 bool DbCreator::deleteDB()
117 {
118     // return QFile::remove(getDbPath());
119     return false;
120 }