Added new data class Setup to contain the setup information (platform,
[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", "filepath", "mediaimagecontainer", "mediaimage", "mediaimagecontainer_mediaimage"};
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         query.exec("drop table if exists mediaimagecontainer_mediaimage");
45         query.exec("drop table if exists mediaimage");
46         query.exec("drop table if exists mediaimagecontainer");
47         query.exec("drop table if exists filepath");
48         query.exec("drop table if exists setup");
49         query.exec("drop table if exists mediatype");
50         query.exec("drop table if exists platform");
51
52         qDebug() << "Creating table platform";
53
54         ret = query.exec("create table if not exists platform "
55                          "(id integer primary key, "
56                          "name varchar(30), "
57                          "filename varchar(125))");
58
59         if (!ret) throw QString("platform.");
60
61         qDebug() << "Creating table mediatype ";
62
63         ret = query.exec("create table if not exists mediatype "
64                          "(id integer primary key, "
65                          "name varchar(30), "
66                          "filename varchar(125))");
67
68         if (!ret) throw QString("mediatype.");
69
70         qDebug() << "Creating table setup";
71
72         ret = query.exec("create table if not exists setup "
73                         "(id integer primary key, "
74                         "platformid integer, "
75                         "mediatypeid integer, "
76                         "filetypeextensions text, "
77                         "foreign key (platformid) references platform(id), "
78                         "foreign key (mediatypeid) references mediatype(id))");
79
80         /*qDebug() << "Creating table filetype";
81             ret = query.exec("create table filetype if not exists"
82                              "(id integer primary key, "
83                              "name varchar(30))");
84             if (!ret) throw QString("filetype.");
85             query.exec("insert into filetype (id, name) values (1, 'media image container')");
86             query.exec("insert into filetype (id, name) values (2, 'screenshot')");
87             query.exec("insert into filetype (id, name) values (3, 'platform icon')");
88             query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
89
90         qDebug() << "Creating table filepath";
91
92         ret = query.exec("create table if not exists filepath "
93                          "(id integer primary key, "
94                          "name text, "
95                          "filetypeid integer, "
96                          "setupid integer, "
97                          "lastscanned numeric, "
98                          "foreign key (setupid) references setup(id))");
99
100         if (!ret) throw QString("filepath");
101
102         qDebug() << "Creating table mediaimagecontainer";
103
104         ret = query.exec("create table if not exists mediaimagecontainer "
105                         "(id integer primary key, "
106                         "name text, "
107                         "filename text, "
108                         "sha1 text, "
109                         "md5 text, "
110                         "filepathid integer, "
111                         "platformid integer, "
112                         "mediatypeid integer, "
113                         "updatetime numeric, "
114                         "foreign key (filepathid) references filepath(id), "
115                         "foreign key (platformid) references platform(id), "
116                         "foreign key (mediatypeid) references mediatype(id))");
117
118         if (!ret) throw QString("mediaimagecontainer");
119
120         qDebug() << "Creating table mediaimage";
121
122         ret = query.exec("create table if not exists mediaimage "
123                         "(id integer primary key, "
124                         "filename text, "
125                         "sha1 text, "
126                         "md5 text, "
127                         "filesize integer, "
128                         "updatetime numeric)");
129
130         qDebug() << "Creating table mediaimagecontainer_mediaimage";
131
132         ret = query.exec("create table if not exists mediaimagecontainer_mediaimage "
133                         "(mediaimagecontainerid integer, "
134                         "mediaimageid integer, "
135                         "foreign key (mediaimagecontainerid) references mediaimagecontainer(id), "
136                         "foreign key (mediaimageid) references mediaimage(id))");
137
138         if (!ret) throw QString("mediaimagecontainer_mediaimage");
139     }
140     catch (QString tbl)
141     {
142         throw QString("Couldn't create database '%1'!").arg(tbl);
143     }
144     return ret;
145 }
146
147 /**
148  * Check if database already exists.
149  * Returns false if doesn't or we don't have a connection.
150 */
151 bool DbCreator::dbExists()
152 {
153     for (int i = 0; i < TABLES_COUNT; ++i)
154     {
155         if (!tableExists(TABLES[i]))
156         {
157             qDebug() << "Table " << TABLES[i] << " missing.";
158             return false;
159         }
160        qDebug() << "Table " << TABLES[i] << " exists.";
161     }
162     return true;
163 }
164
165 bool DbCreator::tableExists(QString table)
166 {
167     QSqlQuery query;
168     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
169     return query.next();
170 }
171
172 bool DbCreator::deleteDB()
173 {
174     // return QFile::remove(getDbPath());
175     return false;
176 }