/[xmltv_parser]/trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs
ViewVC logotype

Diff of /trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/17 01:01:10	213
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/17 04:13:59	229
@@ -11,6 +11,10 @@
 using libxmltv.Core;
 namespace GBPVRProgramDatabaseFixer
 {
+    public interface ICloneable<T>
+    {
+        T Clone();
+    }
     public interface IOldNewProgram
     {
         SQLLITE.IPROGRAMME OldProgram { get; }
@@ -22,17 +26,18 @@
         List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
         List<SQLLITE.ICHANNEL> Channels { get; }
         IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
-        List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
+        List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
 
         void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs);
-        void UpdateGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs);
+        void UpdateGBPVRPrograms(List<IOldNewProgram> programs);
 
     }
     public class SQLLITE : ISQLLITE
     {
         private class OldNewProgram : IOldNewProgram
         {
-            public OldNewProgram() : this(new PROGRAMME(), new PROGRAMME()) { }
+            public OldNewProgram() : this(new PROGRAMME()) { }
+            public OldNewProgram(IPROGRAMME _old) : this(_old,_old) {  }
             public OldNewProgram(IPROGRAMME _old, IPROGRAMME _new) { OldProgram = _old; NewProgram = _new; }
 
             public IPROGRAMME OldProgram { get; private set; }
@@ -44,56 +49,7 @@
         }
 
         #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<CHANNEL>.CreateDefault(this);
-            }
-            //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
-
-            public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index)
-            {
-                BaseDatabaseDefinition<CHANNEL>.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; }
-        }
-
+        #region BaseDatabaseDefinition
         private static class BaseDatabaseDefinition<T>
         {
             public static void CreateDefault(T instance)
@@ -129,6 +85,23 @@
                     throw ex;
                 }
             }
+            public static void Create(T source, ref T destination)
+            {
+                Type t = source.GetType();
+                var props = t.GetProperties();
+                foreach (var prop in props)
+                {
+                    try
+                    {
+                        object value = prop.GetValue(source, null);
+                        prop.SetValue(destination, value, null);
+                    }
+                    catch (Exception ex)
+                    {
+                        throw ex;
+                    }
+                }
+            }
             public static void Create(ref T instance, SQLiteDataReader r, int index)
             {
                 string field_name = r.GetName(index);
@@ -172,8 +145,67 @@
 
             }
         }
+        #endregion
+        public interface ICHANNEL : ICloneable<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, ICloneable<CHANNEL>
+        {
+            public CHANNEL()
+            {
+                BaseDatabaseDefinition<CHANNEL>.CreateDefault(this);
+            }
+            //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
 
-        private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE
+            public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index)
+            {
+                BaseDatabaseDefinition<CHANNEL>.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
+
+            ICHANNEL ICloneable<ICHANNEL>.Clone()
+            {
+                return Clone();
+            }
+            public CHANNEL Clone()
+            {
+                CHANNEL p = new CHANNEL();
+                BaseDatabaseDefinition<CHANNEL>.Create(this, ref p);
+                return p;
+            }
+        }
+        public interface IRECORDING_SCHEDULE : ICloneable<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 class RECORDING_SCHEDULE : IRECORDING_SCHEDULE, ICloneable<RECORDING_SCHEDULE>
         {
             public RECORDING_SCHEDULE()
             {
@@ -185,7 +217,16 @@
             {
                 BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(ref instance, r, index);
             }
-
+            IRECORDING_SCHEDULE ICloneable<IRECORDING_SCHEDULE>.Clone()
+            {
+                return Clone();
+            }
+            public RECORDING_SCHEDULE Clone()
+            {
+                RECORDING_SCHEDULE p = new RECORDING_SCHEDULE();
+                BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, ref p);
+                return p;
+            }
             #region IRECORDING_SCHEDULE members
             public Int64 oid { get; set; }
             public Int64 programme_oid { get; set; }
@@ -203,9 +244,10 @@
             public Int32 priority { get; set; }
             public String conversion_profile { get; set; }
             #endregion
-        }
 
-        public interface IPROGRAMME : IEquatable<IPROGRAMME>
+            
+        }
+        public interface IPROGRAMME : IEquatable<IPROGRAMME>, ICloneable<IPROGRAMME>
         {
             Int64 oid { get; set; }
             String name { get; set; }
@@ -220,8 +262,19 @@
             string ToString();
             IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite);
         }
-        private class PROGRAMME : IPROGRAMME
+        private class PROGRAMME : IPROGRAMME, ICloneable<PROGRAMME>
         {
+           
+            IPROGRAMME ICloneable<IPROGRAMME>.Clone()
+            {
+                return Clone();
+            }
+            public PROGRAMME Clone()
+            {
+                PROGRAMME p = new PROGRAMME();
+                BaseDatabaseDefinition<PROGRAMME>.Create(this, ref p);
+                return p; 
+            }
             public PROGRAMME()
             {
                 BaseDatabaseDefinition<PROGRAMME>.CreateDefault(this);
@@ -255,6 +308,7 @@
                 definition.Stop = this.end_time.ToDateTimeString();
                 definition.SubTitle = this.sub_title;
                 definition.Title = this.name;
+                definition.Rating = this.rating;
                 return definition;
             }
             #endregion
@@ -292,6 +346,14 @@
             {
                 return this.ToString().GetHashCode();
             }
+
+
+
+
+
+
+
+          
         }
         #endregion
         private static class TABLES
@@ -347,15 +409,16 @@
             return range;
         }
 
-        public List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
+        public List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
         {
             source_invalid = new List<IPROGRAMME>();
-            List<IPROGRAMME> source_valid = new List<IPROGRAMME>();
+            List<IOldNewProgram> source_valid = new List<IOldNewProgram>();
             gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();
             xmltv_programs = xmltv_programs.OrderBy(s => DateTime.Parse(s.Start)).ToList();
             gbpvr_programs.TrimExcess();
             xmltv_programs.TrimExcess();
 
+            int updated_program_count = 0;
             double total = gbpvr_programs.Count;
             double index = 0;
             double progress = 0;
@@ -377,7 +440,7 @@
                     if (!xmltv_entry.Equals(program_xmltv_entry))
                     {
                         // data is different
-                        var updated_program = program;
+                        var updated_program = program.Clone();
 
                         if (program_xmltv_entry.Title != xmltv_entry.Title)
                         {
@@ -405,19 +468,26 @@
                             updated_program.description = xmltv_entry.Description;
                         }
 
+                        if(program_xmltv_entry.Rating != xmltv_entry.Rating)
+                        {
+                            gLog.Verbose.Warn.WriteLine("    Program oid: '{0}' - replacing rating", updated_program.oid);
+                            updated_program.rating = xmltv_entry.Rating;
+                        }
+
                         var updated_xmltv_entry = updated_program.AsXMLTVProgramDefinition(this);
                         if (!xmltv_entry.Equals(updated_xmltv_entry))
                         {
                             throw new Exception(string.Format("Program oid '{0}' was not properly updated.", updated_program.oid));
                         }
                         //source_valid.Add(new OldNewProgram(program, updated_program));
-                        source_valid.Add(updated_program);
+                        source_valid.Add(new OldNewProgram(program, updated_program));
+                        updated_program_count++;
                     }
                     else
                     {
                         // data is the same
                         //source_valid.Add(new OldNewProgram(program, program));
-                        source_valid.Add(program);
+                        source_valid.Add(new OldNewProgram(program));
                     }
                 }
                 index++;
@@ -426,6 +496,7 @@
             gLog.Info.WriteLine("Total Valid GB-PVR Programs: 0x{0:x8}", source_valid.Count);
             gLog.Info.WriteLine("Total Invalid GB-PVR Programs: 0x{0:x8}", source_invalid.Count);
             gLog.Info.WriteLine("Total GB-PVR Programs (Valid+Invalid): 0x{0:x8} == 0x{1:x8}", source_valid.Count + source_invalid.Count, gbpvr_programs.Count);
+            gLog.Info.WriteLine("GB-PVR Programs needing an update: 0x{0:x8}", updated_program_count);
             #region old-code
             //List<OldNewProgram> source_update = new List<OldNewProgram>();
             ////if (range == null)
@@ -823,34 +894,9 @@
 
         public void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs)
         {            
-            //gLog.Info.WriteLine("Removing {0} gbpvr programs.", programs.Count);
-            //double total = programs.Count;
-            //double index = 0;
-            //double progress = 0;
-            //Stopwatch st = new Stopwatch();
-            //st.Start();
-            //foreach (var program in programs)
-            //{
-            //    progress = 100.0 * (index / total);
-            //    gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing GBPVR Program ({0:00}%) oid='{1}'", (int)progress, program.oid)));
-            //    if (!RemoveProgramDatabaseEntry(program))
-            //    {
-            //        gLog.Error.WriteLine("Failed to remove program with oid: {0}", program.oid);
-            //    }
-            //    index++;
-            //}
-            ////Stopwatch st = new Stopwatch();
-            ////st.Start();
-            ////if (!RemoveAllProgramDatabaseEntries())
-            ////{
-            ////    gLog.Error.WriteLine("Failed to remove one or more program(s)");
-            ////}
-            //st.Stop();
-            //gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds", st.Elapsed.TotalSeconds);
-
+            gLog.Info.WriteLine("Removing {0} gbpvr programs.", programs.Count);
             int ChunkSize = 1024;
             var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
-
             double total = programs.Count;
             double index = 0;
             double progress = 0;
@@ -863,16 +909,18 @@
                 {
                     gLog.Error.WriteLine("Failed to remove one or more program(s)");
                 }
-                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing {0} gbpvr programs ({1} of {2}) {3:00}%", p.Count(), index, total, (int)progress)));
+                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing old gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
                 index += (double)p.Count(); 
             }
+            gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing old gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
             st1.Stop();
             gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
         }
-        public void UpdateGBPVRPrograms(List<IPROGRAMME> programs)
+        public void UpdateGBPVRPrograms(List<IOldNewProgram> programs)
         {
-            int ChunkSize = 1024;
-            var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
+            gLog.Info.WriteLine("Updating {0} gbpvr programs.", programs.Count);
+            int ChunkSize = 4096;
+            var ChunkList = programs.Chunk<IOldNewProgram>(ChunkSize);
 
             double total = programs.Count;
             double index = 0;
@@ -881,20 +929,15 @@
             st1.Start();
             foreach (var p in ChunkList)
             {
-                //Stopwatch st2 = new Stopwatch();
-                //st2.Start();
                 progress = 100.0 * (index / total);
-                //gLog.Warn.WriteLine("Inserting {0} gbpvr programs ({1} of {2})", p.Count(), index, total);   
                 if (!UpdateProgramEntryDatabase(p))
                 {
                     gLog.Error.WriteLine("Failed to update one or more program(s)");
                 }
-                //st2.Stop();
-                //gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds", st2.Elapsed.TotalSeconds);
-                //gLog.Warn.WriteLine(System.Environment.NewLine);
-                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating {0} gbpvr programs ({1} of {2}) {3:00}%", p.Count(), index, total, (int)progress)));
+                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
                 index += (double)p.Count();                
             }
+            gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
             st1.Stop();
             gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
         }
@@ -979,7 +1022,7 @@
             }
             return result;
         }
-        private bool UpdateProgramEntryDatabase(IEnumerable<IPROGRAMME> list)
+        private bool UpdateProgramEntryDatabase(IEnumerable<IOldNewProgram> list)
         {
             bool result = false;
             try
@@ -1088,7 +1131,7 @@
         }
         #endregion
         #region Multi-Update Command Support
-        private string BuildGBPVRMultiUpdateCommand(IEnumerable<IPROGRAMME> list)
+        private string BuildGBPVRMultiUpdateCommand(IEnumerable<IOldNewProgram> list)
         {
             StringBuilder builder = new StringBuilder();
             /*
@@ -1100,7 +1143,11 @@
             builder.AppendLine("begin transaction;");
             foreach (var t in list)
             {
-                builder.AppendLine(BuildGBPVRSingleInsertCommand(t));
+                if (!t.OldProgram.Equals(t.NewProgram))
+                {
+                    // only update the entry if it is different
+                    builder.AppendLine(BuildGBPVRSingleUpdateCommand(t.NewProgram));
+                }                
             }
             builder.AppendLine("end transaction;");
             return builder.ToString();
@@ -1112,8 +1159,8 @@
             builder.AppendFormat("name=\"{0}\", ", program.name);
             builder.AppendFormat("sub_title=\"{0}\", ", program.sub_title);
             builder.AppendFormat("description=\"{0}\", ", program.description);
-            builder.AppendFormat("start_time='{0}', ", program.start_time.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
-            builder.AppendFormat("end_time='{0}', ", program.end_time.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
+            builder.AppendFormat("start_time='{0}', ", program.start_time.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
+            builder.AppendFormat("end_time='{0}', ", program.end_time.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
             builder.AppendFormat("channel_oid={0}, ", program.channel_oid);
             builder.AppendFormat("unique_identifier=\"{0}\", ", program.unique_identifier);
             builder.AppendFormat("rating=\"{0}\" ", program.rating);

 

  ViewVC Help
Powered by ViewVC 1.1.22