00001 #include <qstring.h>
00002 #include <qdir.h>
00003 #include <qstringlist.h>
00004
00005 #include <mythtv/mythcontext.h>
00006 #include <mythtv/mythdbcon.h>
00007
00008 #include "dbcheck.h"
00009 #include "defs.h"
00010
00011 const QString currentDatabaseVersion = "1000";
00012
00013 static void UpdateDBVersionNumber(const QString &newnumber)
00014 {
00015 MSqlQuery query(MSqlQuery::InitCon());
00016
00017 query.exec("DELETE FROM settings WHERE value='WeatherDBSchemaVer';");
00018 query.exec(QString("INSERT INTO settings (value, data, hostname) "
00019 "VALUES ('WeatherDBSchemaVer', %1, NULL);")
00020 .arg(newnumber));
00021 }
00022
00023 static void performActualUpdate(const QStringList updates, QString version,
00024 QString &dbver)
00025 {
00026 VERBOSE(VB_IMPORTANT, QString("Upgrading to MythWeather schema version ") +
00027 version);
00028
00029 MSqlQuery query(MSqlQuery::InitCon());
00030
00031 for (size_t i = 0; i < updates.size(); ++i)
00032 {
00033 if (!query.exec(updates[i]))
00034 VERBOSE(VB_IMPORTANT,
00035 QObject::tr("ERROR Executing query %1").arg(updates[i]));
00036 }
00037
00038 UpdateDBVersionNumber(version);
00039 dbver = version;
00040 }
00041
00042
00043
00044
00045
00046
00047
00048 void InitializeDatabase()
00049 {
00050 QString dbver = gContext->GetSetting("WeatherDBSchemaVer");
00051
00052 if (dbver == currentDatabaseVersion)
00053 return;
00054
00055 if (dbver == "")
00056 {
00057 VERBOSE(VB_IMPORTANT,
00058 "Inserting MythWeather initial database information.");
00059 QStringList updates;
00060 updates << "CREATE TABLE IF NOT EXISTS weathersourcesettings ("
00061 "sourceid INT UNSIGNED NOT NULL AUTO_INCREMENT,"
00062 "source_name VARCHAR(64) NOT NULL,"
00063 "update_timeout INT UNSIGNED NOT NULL DEFAULT '600',"
00064 "retrieve_timeout INT UNSIGNED NOT NULL DEFAULT '60',"
00065 "hostname VARCHAR(255) NULL,"
00066 "path VARCHAR(255) NULL,"
00067 "author VARCHAR(128) NULL,"
00068 "version VARCHAR(32) NULL,"
00069 "email VARCHAR(255) NULL,"
00070 "types MEDIUMTEXT NULL,"
00071 "PRIMARY KEY(sourceid)) TYPE=InnoDB;"
00072 << "CREATE TABLE IF NOT EXISTS weatherscreens ("
00073 "screen_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
00074 "draworder INT UNSIGNED NOT NULL,"
00075 "container VARCHAR(64) NOT NULL,"
00076 "hostname VARCHAR(255) NULL,"
00077 "units TINYINT UNSIGNED NOT NULL,"
00078 "PRIMARY KEY(screen_id)) TYPE=InnoDB;"
00079 << "CREATE TABLE IF NOT EXISTS weatherdatalayout ("
00080 "location VARCHAR(64) NOT NULL,"
00081 "dataitem VARCHAR(64) NOT NULL,"
00082 "weatherscreens_screen_id INT UNSIGNED NOT NULL,"
00083 "weathersourcesettings_sourceid INT UNSIGNED NOT NULL,"
00084 "PRIMARY KEY(location, dataitem, weatherscreens_screen_id,"
00085 "weathersourcesettings_sourceid),"
00086 "INDEX weatherdatalayout_FKIndex1(weatherscreens_screen_id),"
00087 "INDEX weatherdatalayout_FKIndex2(weathersourcesettings_sourceid),"
00088 "FOREIGN KEY(weatherscreens_screen_id) "
00089 "REFERENCES weatherscreens(screen_id) "
00090 "ON DELETE CASCADE "
00091 "ON UPDATE CASCADE,"
00092 "FOREIGN KEY(weathersourcesettings_sourceid) "
00093 "REFERENCES weathersourcesettings(sourceid) "
00094 "ON DELETE RESTRICT "
00095 "ON UPDATE CASCADE) TYPE=InnoDB;";
00096
00097
00098
00099
00100
00101 performActualUpdate(updates, "1000", dbver);
00102 }
00103 }