00001 #include <qstring.h>
00002
00003 #include <iostream>
00004 using namespace std;
00005
00006 #include "dbcheck.h"
00007
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 if (!gContext->SaveSettingOnHost("PhoneDBSchemaVer",newnumber,NULL))
00016 {
00017 VERBOSE(VB_IMPORTANT, QString("DB Error (Setting new DB version number): %1\n")
00018 .arg(newnumber));
00019
00020 return false;
00021 }
00022
00023 return true;
00024 }
00025
00026 static bool performActualUpdate(const QString updates[], QString version,
00027 QString &dbver)
00028 {
00029 MSqlQuery query(MSqlQuery::InitCon());
00030
00031 VERBOSE(VB_IMPORTANT, QString("Upgrading to MythPhone schema version ") +
00032 version);
00033
00034 int counter = 0;
00035 QString thequery = updates[counter];
00036
00037 while (thequery != "")
00038 {
00039 query.prepare(thequery);
00040 query.exec();
00041
00042 if (query.lastError().type() != QSqlError::None)
00043 {
00044 QString msg =
00045 QString("DB Error (Performing database upgrade): \n"
00046 "Query was: %1 \nError was: %2 \nnew version: %3")
00047 .arg(thequery)
00048 .arg(MythContext::DBErrorMessage(query.lastError()))
00049 .arg(version);
00050 VERBOSE(VB_IMPORTANT, msg);
00051 return false;
00052 }
00053
00054 counter++;
00055 thequery = updates[counter];
00056 }
00057
00058 if (!UpdateDBVersionNumber(version))
00059 return false;
00060
00061 dbver = version;
00062 return true;
00063 }
00064
00065 bool UpgradePhoneDatabaseSchema(void)
00066 {
00067 QString dbver = gContext->GetSetting("PhoneDBSchemaVer");
00068
00069 if (dbver == currentDatabaseVersion)
00070 return true;
00071
00072 if (dbver == "")
00073 {
00074 VERBOSE(VB_IMPORTANT, "Inserting MythPhone initial database information.");
00075
00076 const QString updates[] = {
00077 "CREATE TABLE IF NOT EXISTS phonedirectory ("
00078 " intid INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,"
00079 " nickname TEXT NOT NULL,"
00080 " firstname TEXT,"
00081 " surname TEXT,"
00082 " url TEXT NOT NULL,"
00083 " directory TEXT NOT NULL,"
00084 " photofile TEXT,"
00085 " speeddial INT UNSIGNED NOT NULL"
00086 ");",
00087 "CREATE TABLE IF NOT EXISTS phonecallhistory ("
00088 " recid INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,"
00089 " displayname TEXT NOT NULL,"
00090 " url TEXT NOT NULL,"
00091 " timestamp TEXT NOT NULL,"
00092 " duration INT UNSIGNED NOT NULL,"
00093 " directionin INT UNSIGNED NOT NULL,"
00094 " directoryref INT UNSIGNED"
00095 ");",
00096 ""
00097 };
00098 if (!performActualUpdate(updates, "1000", dbver))
00099 return false;
00100 }
00101
00102 if (dbver == "1000")
00103 {
00104 const QString updates[] = {
00105 "ALTER TABLE phonedirectory ADD onhomelan INT UNSIGNED DEFAULT 0;"
00106 ,
00107 ""
00108 };
00109 if (!performActualUpdate(updates, "1001", dbver))
00110 return false;
00111 }
00112
00113 return true;
00114 }
00115