00001 #include <qstring.h>
00002 #include <qdir.h>
00003
00004 #include <iostream>
00005 using namespace std;
00006
00007 #include "dbcheck.h"
00008 #include "mythtv/mythcontext.h"
00009 #include "mythtv/mythdbcon.h"
00010
00011 const QString currentDatabaseVersion = "1001";
00012
00013 static bool UpdateDBVersionNumber(const QString &newnumber)
00014 {
00015
00016 if (!gContext->SaveSettingOnHost("FlixDBSchemaVer",newnumber,NULL))
00017 {
00018 VERBOSE(VB_IMPORTANT, QString("DB Error (Setting new DB version number): %1\n")
00019 .arg(newnumber));
00020
00021 return false;
00022 }
00023
00024 return true;
00025 }
00026
00027 static bool performActualUpdate(const QString updates[], QString version,
00028 QString &dbver)
00029 {
00030 MSqlQuery query(MSqlQuery::InitCon());
00031
00032 VERBOSE(VB_IMPORTANT, QString("Upgrading to MythFlix schema version ") +
00033 version);
00034
00035 int counter = 0;
00036 QString thequery = updates[counter];
00037
00038 while (thequery != "")
00039 {
00040 query.prepare(thequery);
00041 query.exec();
00042
00043 if (query.lastError().type() != QSqlError::None)
00044 {
00045 QString msg =
00046 QString("DB Error (Performing database upgrade): \n"
00047 "Query was: %1 \nError was: %2 \nnew version: %3")
00048 .arg(thequery)
00049 .arg(MythContext::DBErrorMessage(query.lastError()))
00050 .arg(version);
00051 VERBOSE(VB_IMPORTANT, msg);
00052 return false;
00053 }
00054
00055 counter++;
00056 thequery = updates[counter];
00057 }
00058
00059 if (!UpdateDBVersionNumber(version))
00060 return false;
00061
00062 dbver = version;
00063 return true;
00064 }
00065
00066 bool UpgradeFlixDatabaseSchema(void)
00067 {
00068 QString dbver = gContext->GetSetting("FlixDBSchemaVer");
00069
00070 if (dbver == currentDatabaseVersion)
00071 return true;
00072
00073 if (dbver == "")
00074 {
00075 VERBOSE(VB_IMPORTANT, "Inserting MythFlix initial database information.");
00076
00077 const QString updates[] = {
00078 "CREATE TABLE IF NOT EXISTS netflix ("
00079 " name VARCHAR(100) NOT NULL PRIMARY KEY,"
00080 " category VARCHAR(255) NOT NULL,"
00081 " url VARCHAR(255) NOT NULL,"
00082 " ico VARCHAR(255),"
00083 " updated INT UNSIGNED,"
00084 " is_queue INT UNSIGNED"
00085 ");",
00086 ""
00087 };
00088 if (!performActualUpdate(updates, "1000", dbver))
00089 return false;
00090 }
00091
00092 if (dbver == "1000")
00093 {
00094 const QString updates[] = {
00095 "ALTER TABLE netflix ADD queue VARCHAR(32) NOT NULL DEFAULT '';",
00096 "ALTER TABLE netflix DROP PRIMARY KEY, ADD PRIMARY KEY (name, queue);",
00097 ""
00098 };
00099 if (!performActualUpdate(updates, "1001", dbver))
00100 return false;
00101 }
00102
00103 return true;
00104 }