Added directory path selection. TODO: a "last scanned" time stamp should
[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             // TODO: add last scanned column
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             if (ret) qDebug() << "Table filepath created succesfully!";
91
92             if (!ret) throw QString("filepath");
93         //}
94     }
95     catch (QString tbl)
96     {
97         throw QString("Couldn't create database '%1'!").arg(tbl);
98     }
99     return ret;
100 }
101
102 /**
103  * Check if database already exists.
104  * Returns false if doesn't or we don't have a connection.
105 */
106 bool DbCreator::dbExists()
107 {
108     for (int i = 0; i < TABLES_COUNT; ++i)
109     {
110         if (!tableExists(TABLES[i]))
111         {
112             qDebug() << "Table " << TABLES[i] << " missing.";
113             return false;
114         }
115        qDebug() << "Table " << TABLES[i] << " exists.";
116     }
117     return true;
118 }
119
120 bool DbCreator::tableExists(QString table)
121 {
122     QSqlQuery query;
123     query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
124     return query.next();
125 }
126
127 bool DbCreator::deleteDB()
128 {
129     // return QFile::remove(getDbPath());
130     return false;
131 }