Added foreign keys to filepath table. Database reset command line
authorMikko Keinänen <mikko.keinanen@gmail.com>
Wed, 26 May 2010 21:46:14 +0000 (00:46 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Wed, 26 May 2010 21:46:14 +0000 (00:46 +0300)
argument.

src/db/dbcreator.cpp
src/main.cpp

index f3ec981..37ba731 100644 (file)
@@ -27,7 +27,7 @@
 using namespace std;
 
 const int DbCreator::TABLES_COUNT = 3;
-const QString DbCreator::TABLES[] = {"platform", "mediatype", "filetype"};
+const QString DbCreator::TABLES[] = {"platform", "mediatype", "filepath"};
 
 DbCreator::DbCreator(QObject *parent) : QObject(parent)
 {
@@ -44,6 +44,7 @@ bool DbCreator::createDB()
         /*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), "
@@ -53,6 +54,7 @@ bool DbCreator::createDB()
         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), "
@@ -74,13 +76,17 @@ bool DbCreator::createDB()
         if (!tableExists("filepath"))
         {*/
             qDebug() << "Creating table filepath";
-            query.exec("create 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)");
+                       "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");
         //}
index cbba0ae..648b2fe 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <QApplication>
 #include <QTextStream>
+#include <QDebug>
 #include <iostream>
 #include "mainwindow.h"
 #include "db/databasemanager.h"
@@ -28,16 +29,17 @@ int main(int argc, char *argv[])
 {
        QApplication app(argc, argv);
        QTextStream cout(stdout, QIODevice::WriteOnly);
+    QStringList arglst = app.arguments();
+
+    bool reset = arglst.contains("reset", Qt::CaseInsensitive);
+
+    if (reset) qDebug() << "Database Reset requested";
 
     if (DatabaseManager::openDB())
         cout << " Database opened succesfully!" << endl;
        else cout << " Database connection failed!" << endl;
 
-    if (DbCreator::dbExists())
-    {
-               cout << " Database exists!" << endl;
-    }
-       else 
+    if (reset || !DbCreator::dbExists())
        {
         try
         {
@@ -51,7 +53,9 @@ int main(int argc, char *argv[])
                        exit(1);
                }
     }
-       MainWindow *mw = new MainWindow;
+    else cout << " Database exists!" << endl;
+
+    MainWindow *mw = new MainWindow;
        mw->show();
        return app.exec();
 }