using System;
using System.Data;
using System.Windows.Forms;
namespace RSSFeeder.Helpers{
/// <summary>
/// Summary description for UpgradeHelper.
/// </summary>
public class UpgradeHelper
{
public static void UpgradeOldVersions()
{
try
{
DoDatabaseEnhancements();
}
catch( Exception x )
{
MessageBox.Show("There was a problem upgrading your database." + Environment.NewLine +
x.Message + Environment.NewLine +
"RSS Feeder may not work properly without successful update", "Upgrade Problem",
MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
private static void DoDatabaseEnhancements()
{
UpgradeChannelTable();
RecoverLostFeeds();
UpdateRssTable();
}
private static void RecoverLostFeeds()
{
string sql = "UPDATE RssFeeds SET pubDate = #{0}# where pubdate = #12:00:00 AM#";
sql = string.Format(sql, DateTime.Today.ToShortDateString());
DatabaseHelper.ExecuteNonQuery(sql);
}
private static void UpdateRssTable()
{
bool hasOutlookEntryIDColumn = false;
using( IDataReader reader = DatabaseHelper.ExecuteReader("SELECT * FROM RssFeeds WHERE 1=2") )
{
DataTable schema = reader.GetSchemaTable();
foreach( DataRow row in schema.Rows )
{
if( (string)row["ColumnName"] == "OutlookEntryID" ) hasOutlookEntryIDColumn = true;
}
reader.Close();
}
if( !hasOutlookEntryIDColumn )
{
DatabaseHelper.ExecuteNonQuery("ALTER TABLE RssFeeds ADD OutlookEntryID varchar(255);");
DatabaseHelper.ExecuteNonQuery("UPDATE RssFeeds SET OutlookEntryID = '';");
}
}
private static void UpgradeChannelTable()
{
bool hasUserNameColumn = false;
bool hasPasswordColumn = false;
using( IDataReader reader = DatabaseHelper.ExecuteReader("SELECT * FROM Channels WHERE 1=2") )
{
DataTable schema = reader.GetSchemaTable();
foreach( DataRow row in schema.Rows )
{
if( (string)row["ColumnName"] == "UserName" ) hasUserNameColumn = true;
if( (string)row["ColumnName"] == "UserPassword" ) hasPasswordColumn = true;
}
reader.Close();
}
if( !hasUserNameColumn )
{
DatabaseHelper.ExecuteNonQuery("ALTER TABLE Channels ADD UserName varchar(255);");
DatabaseHelper.ExecuteNonQuery("UPDATE Channels SET UserName = '';");
}
if( !hasPasswordColumn )
{
DatabaseHelper.ExecuteNonQuery("ALTER TABLE Channels ADD UserPassword text;");
DatabaseHelper.ExecuteNonQuery("UPDATE Channels SET UserPassword = '';");
}
}
}
}
|