Exception handling
[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 DbCreator::DbCreator(QObject *parent) : QObject(parent)
30 {
31 }
32
33
34 bool DbCreator::createDB()
35 {
36     bool ret = false;
37     QSqlQuery query;
38
39     try
40     {
41         if (!tableExists("platform"))
42         {
43             qDebug() << "Creating table platform";
44             ret = query.exec("create table platform "
45                              "(id integer primary key, "
46                              "name varchar(30), "
47                              "filename varchar(125))");
48             if (!ret) throw QString("platform.");
49         }
50         if (!tableExists("mediatype"))
51         {
52             qDebug() << "Creating table mediatype";
53             ret = query.exec("create table mediatype "
54                              "(id integer primary key, "
55                              "name varchar(30), "
56                              "filename varchar(125))");
57             if (!ret) throw QString("mediatype.");
58         }
59         if (!tableExists("filetype"))
60         {
61             qDebug() << "Creating table filetype";
62             ret = query.exec("create table filetype "
63                              "(id integer primary key, "
64                              "name varchar(30))");
65             if (!ret) throw QString("filetype.");
66             query.exec("insert into filetype (id, name) values (1, 'media image container')");
67             query.exec("insert into filetype (id, name) values (2, 'screenshot')");
68             query.exec("insert into filetype (id, name) values (3, 'platform icon')");
69             query.exec("insert into filetype (id, name) values (4, 'media type icon')");
70         }
71         /*if (!tableExists("filepath"))
72     {
73         qDebug() << "Creating table filepath";
74         query.exec("create table filepath "
75                     "(id integer primary key, "
76                     "name varchar(255))");
77
78         if (!ret) throw QString("filepath");
79     }*/
80     }
81     catch (QString tbl)
82     {
83         throw QString("Couldn't create database '%1'!").arg(tbl);
84     }
85     return ret;
86 }
87
88 /**
89  * Check if database already exists.
90  * Returns false if doesn't or we don't have a connection.
91 */
92 bool DbCreator::dbExists()
93 {
94     return tableExists("platform");
95 }
96
97 bool DbCreator::tableExists(QString table)
98 {
99     QSqlQuery query;
100     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
101     return query.next();
102 }
103
104 bool DbCreator::deleteDB()
105 {
106     // return QFile::remove(getDbPath());
107     return false;
108 }