MythTV  0.26-pre
dbcheck.cpp
Go to the documentation of this file.
00001 // C++ headers
00002 #include <iostream>
00003 
00004 // QT headers
00005 #include <QString>
00006 #include <QSqlError>
00007 
00008 // Myth headers
00009 #include <mythcontext.h>
00010 #include <mythdb.h>
00011 
00012 // MythNews headers
00013 #include "dbcheck.h"
00014 
00015 const QString currentDatabaseVersion = "1001";
00016 
00017 static bool UpdateDBVersionNumber(const QString &newnumber)
00018 {
00019 
00020     if (!gCoreContext->SaveSettingOnHost("NewsDBSchemaVer", newnumber, NULL))
00021     {
00022         LOG(VB_GENERAL, LOG_ERR,
00023             QString("DB Error (Setting new DB version number): %1\n")
00024                 .arg(newnumber));
00025 
00026         return false;
00027     }
00028 
00029     return true;
00030 }
00031 
00032 static bool performActualUpdate(const QString updates[], QString version,
00033                                 QString &dbver)
00034 {
00035     MSqlQuery query(MSqlQuery::InitCon());
00036 
00037     LOG(VB_GENERAL, LOG_NOTICE,
00038         "Upgrading to MythNews schema version " + version);
00039 
00040     int counter = 0;
00041     QString thequery = updates[counter];
00042 
00043     while (thequery != "")
00044     {
00045         if (!query.exec(thequery))
00046         {
00047             QString msg =
00048                 QString("DB Error (Performing database upgrade): \n"
00049                         "Query was: %1 \nError was: %2 \nnew version: %3")
00050                 .arg(thequery)
00051                 .arg(MythDB::DBErrorMessage(query.lastError()))
00052                 .arg(version);
00053             LOG(VB_GENERAL, LOG_ERR, msg);
00054             return false;
00055         }
00056 
00057         counter++;
00058         thequery = updates[counter];
00059     }
00060 
00061     if (!UpdateDBVersionNumber(version))
00062         return false;
00063 
00064     dbver = version;
00065     return true;
00066 }
00067 
00068 bool UpgradeNewsDatabaseSchema(void)
00069 {
00070     QString dbver = gCoreContext->GetSetting("NewsDBSchemaVer");
00071 
00072     if (dbver == currentDatabaseVersion)
00073         return true;
00074 
00075     if (dbver.isEmpty())
00076     {
00077         LOG(VB_GENERAL, LOG_NOTICE,
00078             "Inserting MythNews initial database information.");
00079 
00080         const QString updates[] =
00081         {
00082             "CREATE TABLE IF NOT EXISTS newssites "
00083             "( name VARCHAR(100) NOT NULL PRIMARY KEY,"
00084             "  category  VARCHAR(255) NOT NULL,"
00085             "  url  VARCHAR(255) NOT NULL,"
00086             "  ico  VARCHAR(255),"
00087             "  updated INT UNSIGNED);",
00088             ""
00089         };
00090         if (!performActualUpdate(updates, "1000", dbver))
00091             return false;
00092     }
00093 
00094     if (dbver == "1000")
00095     {
00096         const QString updates[] =
00097         {
00098             "ALTER TABLE `newssites` ADD `podcast` BOOL NOT NULL DEFAULT '0';",
00099             ""
00100         };
00101 
00102         if (!performActualUpdate(updates, "1001", dbver))
00103             return false;
00104     }
00105 
00106     return true;
00107 }
00108 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends