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