//#define SQLLITE_CONNECTION_TEST using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; using System.Diagnostics; using Enterprise.Logging; namespace GBPVRProgramDatabaseFixer { internal class SQLLITE { #region DATABASE DEFINITIONS public interface ICHANNEL { Int64 oid { get; } String name { get; } String channelID { get; } Int64 channel_number { get; } String favourite_channel { get; } String display_name { get; } } private class CHANNEL : ICHANNEL { public CHANNEL() { BaseDatabaseDefinition.CreateDefault(this); } //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition.Create(this, r, index); } public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index) { BaseDatabaseDefinition.Create(ref instance, r, index); } #region ICHANNEL members public Int64 oid { get; set; } public String name { get; set; } public String channelID { get; set; } public Int64 channel_number { get; set; } public String favourite_channel { get; set; } public String display_name { get; set; } #endregion } public interface IRECORDING_SCHEDULE { Int64 oid { get; } Int64 programme_oid { get; } Int64 capture_source_oid { get; } Int16 status { get; } String filename { get; } Int64 recording_type { get; } Int64 recording_group { get; } DateTime manual_start_time { get; } DateTime manual_end_time { get; } Int64 manual_channel_oid { get; } Int64 quality_level { get; } Int64 pre_pad_minutes { get; } Int64 post_pad_minutes { get;} Int32 priority { get; } String conversion_profile { get; } } private static class BaseDatabaseDefinition { public static void CreateDefault(T instance) { try { Type t = typeof(T); var props = t.GetProperties(); foreach (var prop in props) { Type prop_type = prop.PropertyType; object field_value = null; try { if (prop_type == typeof(string)) { field_value = string.Empty; } else { field_value = Activator.CreateInstance(prop_type); } } catch (Exception ex) { throw ex; } prop.SetValue(instance, field_value, null); } } catch (Exception ex) { throw ex; } } public static void Create(ref T instance, SQLiteDataReader r, int index) { string field_name = r.GetName(index); Type field_type = r.GetFieldType(index); object field_value = r.GetValue(index); //gLog.Verbose.Debug.WriteLine("Name: '{0}' Type: '{1}' Value: '{2}'", field_name, field_type.Name, field_value == null ? "null" : field_value.ToString()); Type t = typeof(T); var props = t.GetProperties(); foreach (var prop in props) { if (prop.Name.ToLower() == field_name.ToLower()) { if (prop.PropertyType == field_type) { Type db_type = field_value.GetType(); try { if (db_type == typeof(System.DBNull)) { prop.SetValue(instance, null, null); } else { prop.SetValue(instance, field_value, null); } } catch (Exception ex) { throw ex; } } else { gLog.Verbose.Debug.WriteLine("Found Property: {0} but there was a type mismatch. Property Type: '{1}' Expected: '{2}'", prop.Name, prop.PropertyType.Name, field_type.Name); throw new InvalidOperationException(string.Format("Found Property: {0} but there was a type mismatch. Property Type: '{1}' Expected: '{2}'", prop.Name, prop.PropertyType.Name, field_type.Name)); } } } } } private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE { public RECORDING_SCHEDULE() { BaseDatabaseDefinition.CreateDefault(this); } //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition.Create(this, r, index); } public static void Create(ref RECORDING_SCHEDULE instance, SQLiteDataReader r, int index) { BaseDatabaseDefinition.Create(ref instance, r, index); } #region IRECORDING_SCHEDULE members public Int64 oid { get; set;} public Int64 programme_oid { get; set;} public Int64 capture_source_oid { get; set;} public Int16 status { get; set; } public String filename { get; set; } public Int64 recording_type { get;set; } public Int64 recording_group { get; set;} public DateTime manual_start_time { get; set;} public DateTime manual_end_time { get; set; } public Int64 manual_channel_oid { get; set; } public Int64 quality_level { get; set; } public Int64 pre_pad_minutes { get; set; } public Int64 post_pad_minutes { get; set; } public Int32 priority { get; set; } public String conversion_profile { get; set; } #endregion } public interface IPROGRAMME { Int64 oid { get; } String name { get; } String sub_title { get; } String description { get; } DateTime start_time { get; } DateTime end_time { get; } Int64 channel_oid { get; } String unique_identifier { get; } String rating { get; } } private class PROGRAMME : IPROGRAMME { public PROGRAMME() { BaseDatabaseDefinition.CreateDefault(this); } //public PROGRAMME(SQLiteDataReader r, int index) : base(r, index) { } public static void Create(ref PROGRAMME instance, SQLiteDataReader r, int index) { BaseDatabaseDefinition.Create(ref instance, r, index); } #region IPROGRAMME members public Int64 oid { get; set; } public String name { get; set; } public String sub_title { get; set; } public String description { get; set; } public DateTime start_time { get; set; } public DateTime end_time { get; set; } public Int64 channel_oid { get; set; } public String unique_identifier { get; set; } public String rating { get; set; } #endregion } #endregion private static class TABLES { public const string RECORDING_SCHEDULE = "RECORDING_SCHEDULE"; public const string PROGRAMME = "PROGRAMME"; public const string CHANNEL = "CHANNEL"; } //public SQLLite() { } public SQLLITE(string database, EventHandler OnInstanceCreated) { this.OnInstanceCreated = OnInstanceCreated; //CreateConnection(database); this.Database = database; if (!CreateDatabaseBackup()) { gLog.Error.WriteLine("Failed to backup database."); return; } ConnectionTest(); ReadChannelData(); ReadRecodringScheduleData(); ReadProgrammeData(); OnCreatedInstance(this, new EventArgs()); } [NonSerialized] private EventHandler _OnInstanceCreated; private EventHandler OnInstanceCreated { get { return _OnInstanceCreated; } set { _OnInstanceCreated = value; } } private void OnCreatedInstance(object sender, EventArgs e) { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); } } #region SQLLite private string Database; public List Programs { get; private set; } public List Recordings { get; private set; } public List Channels { get; private set; } #endregion private string CreateConnectionString() { string connection_string = string.Format("Data Source={0}", this.Database); return connection_string; } private SQLiteConnection CreateConnection() { SQLiteConnection connection = new SQLiteConnection(CreateConnectionString()); return connection; } [Conditional("SQLLITE_CONNECTION_TEST")] private void ConnectionTest() { try { using (SQLiteConnection con = CreateConnection()) { con.Open(); string command_text = string.Format("select * from {0};", TABLES.CHANNEL); gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text); using (SQLiteCommand cmd = new SQLiteCommand(command_text, con)) { using (SQLiteDataReader r = cmd.ExecuteReader()) { if (!r.HasRows) { gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText); } else { while (r.Read()) { for (int i = 0; i < r.FieldCount; i++) { string field_name = r.GetName(i); Type field_type = r.GetFieldType(i); object field_value = r.GetValue(i); gLog.Verbose.Debug.WriteLine("Name: '{0}' Type: '{1}' Value: '{2}'", field_name, field_type.Name, field_value == null ? "null" : field_value.ToString()); } break; } } } } con.Clone(); } OnCreatedInstance(this, new EventArgs()); } catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); } } private bool CreateDatabaseBackup() { try { string backup_file = string.Format("{0}.{1}", this.Database, DateTime.Now.ToString("yyyyMMddHHmmss")); gLog.Info.WriteLine("Creating Database backup..."); gLog.Info.WriteLine("\tSource: {0}", this.Database); gLog.Info.WriteLine("\tDestination: {0}", backup_file); System.IO.File.Copy(this.Database, backup_file); return true; } catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); return false; } } private void ReadChannelData() { try { List channels = new List(); using (SQLiteConnection con = CreateConnection()) { con.Open(); string command_text = string.Format("select * from {0};", TABLES.CHANNEL); gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text); using (SQLiteCommand cmd = new SQLiteCommand(command_text, con)) { using (SQLiteDataReader r = cmd.ExecuteReader()) { if (!r.HasRows) { gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText); } else { while (r.Read()) { CHANNEL channel = new CHANNEL(); for (int i = 0; i < r.FieldCount; i++) { CHANNEL.Create(ref channel, r, i); } channels.Add(channel); } } } } con.Clone(); } this.Channels = channels; } catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); } } private void ReadProgrammeData() { try { List programs = new List(); using (SQLiteConnection con = CreateConnection()) { con.Open(); string command_text = string.Format("select * from {0};", TABLES.PROGRAMME); gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text); using (SQLiteCommand cmd = new SQLiteCommand(command_text, con)) { using (SQLiteDataReader r = cmd.ExecuteReader()) { if (!r.HasRows) { gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText); } else { while (r.Read()) { PROGRAMME program = new PROGRAMME(); for (int i = 0; i < r.FieldCount; i++) { PROGRAMME.Create(ref program, r, i); } programs.Add(program); } } } } con.Clone(); } this.Programs = programs; } catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); } } private void ReadRecodringScheduleData() { try { List recordings = new List(); using (SQLiteConnection con = CreateConnection()) { con.Open(); string command_text = string.Format("select * from {0};", TABLES.RECORDING_SCHEDULE); gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text); using (SQLiteCommand cmd = new SQLiteCommand(command_text, con)) { using (SQLiteDataReader r = cmd.ExecuteReader()) { if (!r.HasRows) { gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText); } else { while (r.Read()) { RECORDING_SCHEDULE recording = new RECORDING_SCHEDULE(); for (int i = 0; i < r.FieldCount; i++) { RECORDING_SCHEDULE.Create(ref recording, r, i); } recordings.Add(recording); } } } } con.Clone(); } this.Recordings = recordings; } catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); } } } }