EmuFront ... NOT Foobar :D
[emufront] / src / db / dbcreator.cpp
index 37ba731..d15e13d 100644 (file)
@@ -9,17 +9,18 @@
 // the Free Software Foundation, either version 3 of the License, or
 // (at your option) any later version.
 //
-// Foobar is distributed in the hope that it will be useful,
+// EmuFront is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
 //
 // You should have received a copy of the GNU General Public License
-// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <QObject>
 #include <QSqlDatabase>
 #include <QSqlQuery>
+#include <QSqlError>
 #include <QDebug>
 #include <exception>
 #include "dbcreator.h"
@@ -27,7 +28,7 @@
 using namespace std;
 
 const int DbCreator::TABLES_COUNT = 3;
-const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath"};
+const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath", "mediaimagecontainer", "mediaimage", "mediaimagecontainer_mediaimage"};
 
 DbCreator::DbCreator(QObject *parent) : QObject(parent)
 {
@@ -41,59 +42,130 @@ bool DbCreator::createDB()
 
     try
     {
-        /*if (!tableExists("platform"))
-        {*/
-            qDebug() << "Creating table platform";
-            query.exec("drop table if exists platform");
-            ret = query.exec("create table if not exists platform "
-                             "(id integer primary key, "
-                             "name varchar(30), "
-                             "filename varchar(125))");
-            if (!ret) throw QString("platform.");
-        /*}
-        if (!tableExists("mediatype"))
-        {*/
-            qDebug() << "Creating table mediatype ";
-            query.exec("drop table if exists mediatype");
-            ret = query.exec("create table if not exists mediatype "
-                             "(id integer primary key, "
-                             "name varchar(30), "
-                             "filename varchar(125))");
-            if (!ret) throw QString("mediatype.");
-        /*}
-        if (!tableExists("filetype"))
-        {*/
-            /*qDebug() << "Creating table filetype";
-            ret = query.exec("create table filetype if not exists"
-                             "(id integer primary key, "
-                             "name varchar(30))");
+        query.exec("DROP TABLE IF EXISTS mediaimagecontainer_file");
+        query.exec("DROP TABLE IF EXISTS mediaimagecontainer");
+        query.exec("DROP TABLE IF EXISTS filepath");
+        query.exec("DROP TABLE IF EXISTS setup");
+        query.exec("DROP TABLE IF EXISTS mediatype");
+        query.exec("DROP TABLE IF EXISTS platform");
+        query.exec("DROP TABLE IF EXISTS file");
+
+        qDebug() << "Creating TABLE file";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS file"
+                        "(id INTEGER PRIMARY KEY, "
+                        "name TEXT, "
+                        "type INTEGER, "
+                        "checksum TEXT, "
+                        "size INTEGER, "
+                        "updatetime NUMERIC)");
+
+        qDebug() << "Creating TABLE platform";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS platform "
+                         "(id INTEGER PRIMARY KEY, "
+                         "name TEXT, "
+                         "fileid INTEGER REFERENCES file(id))");
+
+        if (!ret) throw QString("platform.");
+
+        qDebug() << "Creating TABLE mediatype ";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS mediatype "
+                         "(id INTEGER PRIMARY KEY, "
+                         "name TEXT, "
+                         "fileid INTEGER REFERENCES file(id))");
+
+        if (!ret) throw QString("mediatype.");
+
+        qDebug() << "Creating TABLE setup";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS setup "
+                        "(id INTEGER PRIMARY KEY, "
+                        "platformid INTEGER REFERENCES platform(id) ON DELETE CASCADE, "
+                        "mediatypeid INTEGER REFERENCES mediatype(id) ON DELETE CASCADE, "
+                        "filetypeextensions TEXT)");
+
+        /*qDebug() << "Creating TABLE filetype";
+            ret = query.exec("CREATE TABLE filetype IF NOT EXISTS"
+                             "(id INTEGER PRIMARY KEY, "
+                             "name TEXT)");
             if (!ret) throw QString("filetype.");
             query.exec("insert into filetype (id, name) values (1, 'media image container')");
             query.exec("insert into filetype (id, name) values (2, 'screenshot')");
             query.exec("insert into filetype (id, name) values (3, 'platform icon')");
             query.exec("insert into filetype (id, name) values (4, 'media type icon')");*/
-        /*}
-        if (!tableExists("filepath"))
-        {*/
-            qDebug() << "Creating table filepath";
-            query.exec("drop table if exists filepath");
-            ret = query.exec("create table if not exists filepath "
-                       "(id integer primary key, "
-                       "name text, "
-                       "filetypeid integer, "
-                       "platformid integer, "
-                       "mediatypeid integer, "
-                       "lastscanned numeric, "
-                       "foreign key (platformid) references platform(id), "
-                       "foreign key (mediatypeid) references mediatype(id))");
-            if (ret) qDebug() << "Table filepath created succesfully!";
-
-            if (!ret) throw QString("filepath");
-        //}
+
+        qDebug() << "Creating TABLE filepath";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS filepath "
+                         "(id INTEGER PRIMARY KEY, "
+                         "name TEXT, "
+                         "filetypeid INTEGER, "
+                         "setupid INTEGER, "
+                         "lastscanned NUMERIC, "
+                         "FOREIGN KEY (setupid) REFERENCES setup(id))");
+
+        if (!ret) throw QString("filepath");
+
+        qDebug() << "Creating TABLE mediaimagecontainer";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer "
+                        "(id INTEGER PRIMARY KEY, "
+                        "fileid INTEGER REFERENCES file(id), "
+                        "filepathid INTEGER REFERENCES filepath(id), "
+                        "updatetime NUMERIC)");
+
+        if (!ret) throw QString("mediaimagecontainer");
+
+
+        qDebug() << "Creating TABLE mediaimagecontainer_mediaimage";
+
+        ret = query.exec("CREATE TABLE IF NOT EXISTS mediaimagecontainer_mediaimage "
+                        "(mediaimagecontainerid INTEGER, "
+                        "fileid INTEGER, "
+                        "FOREIGN KEY (mediaimagecontainerid) REFERENCES mediaimagecontainer(id), "
+                        "FOREIGN KEY (fileid) REFERENCES file(id))");
+
+        if (!ret) throw QString("mediaimagecontainer_mediaimage");
+
+        query.exec(
+            "CREATE TRIGGER IF NOT EXISTS trg_onplatformdelete "
+            "AFTER DELETE ON platform "
+            "BEGIN "
+            "   DELETE FROM setup WHERE setup.platformid = old.id;"
+            "END;"
+            );
+
+        query.exec(
+            "CREATE TRIGGER IF NOT EXISTS trg_onmediatypedelete "
+            "AFTER DELETE ON mediatype "
+            "BEGIN "
+            "   DELETE FROM setup WHERE setup.mediatypeid = old.id;"
+            "END;"
+            );
+
+        query.exec(
+            "CREATE TRIGGER IF NOT EXISTS trg_onsetupdelete "
+            "AFTER DELETE ON setup "
+            "BEGIN "
+            "   DELETE FROM filepath WHERE filepath.setupid = old.id;"
+            "END;"
+            );
+        query.exec(
+            "CREATE TRIGGER IF NOT EXISTS trg_onfiledelete "
+            "AFTER DELETE ON file "
+            "BEGIN "
+            "   UPDATE platform SET platform.inconfileid=NULL WHERE platform.iconfileid = old.id;"
+            "   UPDATE mediatype SET mediatype.iconfileid=NULL WHERE mediatype.iconfileid = old.id;"
+            "   DELETE FROM mediaimagecontainer_mediaimage WHERE mediaimagecontainer_mediaimage.fileid = old.id;"
+            "END;"
+        );
     }
     catch (QString tbl)
     {
-        throw QString("Couldn't create database '%1'!").arg(tbl);
+        QString err = query.lastError().text();
+        throw QString("Couldn't CREATE table '%1'!").arg(tbl).append(err);
     }
     return ret;
 }
@@ -116,10 +188,10 @@ bool DbCreator::dbExists()
     return true;
 }
 
-bool DbCreator::tableExists(QString table)
+bool DbCreator::tableExists(QString TABLE)
 {
     QSqlQuery query;
-    query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(table));
+    query.exec(QString("SELECT name FROM sqlite_master WHERE name='%1'").arg(TABLE));
     return query.next();
 }