Added foreign keys to filepath table. Database reset command line
[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"};
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             query.exec("drop table if exists platform");
48             ret = query.exec("create table if not exists platform "
49                              "(id integer primary key, "
50                              "name varchar(30), "
51                              "filename varchar(125))");
52             if (!ret) throw QString("platform.");
53         /*}
54         if (!tableExists("mediatype"))
55         {*/
56             qDebug() << "Creating table mediatype ";
57             query.exec("drop table if exists mediatype");
58             ret = query.exec("create table if not exists mediatype "
59                              "(id integer primary key, "
60                              "name varchar(30), "
61                              "filename varchar(125))");
62             if (!ret) throw QString("mediatype.");
63         /*}
64         if (!tableExists("filetype"))
65         {*/
66             /*qDebug() << "Creating table filetype";
67             ret = query.exec("create table filetype if not exists"
68                              "(id integer primary key, "
69                              "name varchar(30))");
70             if (!ret) throw QString("filetype.");
71             query.exec("insert into filetype (id, name) values (1, 'media image container')");
72             query.exec("insert into filetype (id, name) values (2, 'screenshot')");
73             query.exec("insert into filetype (id, name) values (3, 'platform icon')");
74             query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
75         /*}
76         if (!tableExists("filepath"))
77         {*/
78             qDebug() << "Creating table filepath";
79             query.exec("drop table if exists filepath");
80             ret = query.exec("create table if not exists filepath "
81                        "(id integer primary key, "
82                        "name text, "
83                        "filetypeid integer, "
84                        "platformid integer, "
85                        "mediatypeid integer, "
86                        "lastscanned numeric, "
87                        "foreign key (platformid) references platform(id), "
88                        "foreign key (mediatypeid) references mediatype(id))");
89             if (ret) qDebug() << "Table filepath created succesfully!";
90
91             if (!ret) throw QString("filepath");
92         //}
93     }
94     catch (QString tbl)
95     {
96         throw QString("Couldn't create database '%1'!").arg(tbl);
97     }
98     return ret;
99 }
100
101 /**
102  * Check if database already exists.
103  * Returns false if doesn't or we don't have a connection.
104 */
105 bool DbCreator::dbExists()
106 {
107     for (int i = 0; i < TABLES_COUNT; ++i)
108     {
109         if (!tableExists(TABLES[i]))
110         {
111             qDebug() << "Table " << TABLES[i] << " missing.";
112             return false;
113         }
114        qDebug() << "Table " << TABLES[i] << " exists.";
115     }
116     return true;
117 }
118
119 bool DbCreator::tableExists(QString table)
120 {
121     QSqlQuery query;
122     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
123     return query.next();
124 }
125
126 bool DbCreator::deleteDB()
127 {
128     // return QFile::remove(getDbPath());
129     return false;
130 }