/[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 02:40:26	222
@@ -22,17 +22,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; }
@@ -45,7 +46,7 @@
 
         #region DATABASE DEFINITIONS
 
-        public interface ICHANNEL
+        public interface ICHANNEL : ICloneable
         {
             Int64 oid { get; }
             String name { get; }
@@ -54,7 +55,7 @@
             String favourite_channel { get; }
             String display_name { get; }
         }
-        private class CHANNEL : ICHANNEL
+        private class CHANNEL : ICHANNEL, ICloneable
         {
             public CHANNEL()
             {
@@ -74,8 +75,15 @@
             public String favourite_channel { get; set; }
             public String display_name { get; set; }
             #endregion
+
+            public object Clone()
+            {
+                CHANNEL p = new CHANNEL();
+                BaseDatabaseDefinition<CHANNEL>.Create(this, ref p);
+                return p;   
+            }
         }
-        public interface IRECORDING_SCHEDULE
+        public interface IRECORDING_SCHEDULE : ICloneable
         {
             Int64 oid { get; }
             Int64 programme_oid { get; }
@@ -129,6 +137,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);
@@ -173,7 +198,7 @@
             }
         }
 
-        private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE
+        private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE, ICloneable
         {
             public RECORDING_SCHEDULE()
             {
@@ -185,7 +210,12 @@
             {
                 BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(ref instance, r, index);
             }
-
+            public object 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 +233,11 @@
             public Int32 priority { get; set; }
             public String conversion_profile { get; set; }
             #endregion
+
+            
         }
 
-        public interface IPROGRAMME : IEquatable<IPROGRAMME>
+        public interface IPROGRAMME : IEquatable<IPROGRAMME>, ICloneable
         {
             Int64 oid { get; set; }
             String name { get; set; }
@@ -220,8 +252,16 @@
             string ToString();
             IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite);
         }
-        private class PROGRAMME : IPROGRAMME
+        private class PROGRAMME : IPROGRAMME, ICloneable
         {
+
+            public object Clone()
+            {
+                PROGRAMME p = new PROGRAMME();
+                BaseDatabaseDefinition<PROGRAMME>.Create(this, ref p);
+                return p;   
+            }
+
             public PROGRAMME()
             {
                 BaseDatabaseDefinition<PROGRAMME>.CreateDefault(this);
@@ -292,6 +332,8 @@
             {
                 return this.ToString().GetHashCode();
             }
+
+            
         }
         #endregion
         private static class TABLES
@@ -347,10 +389,10 @@
             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();
@@ -376,8 +418,8 @@
                     // check if the xmltv entry has different data from the current program
                     if (!xmltv_entry.Equals(program_xmltv_entry))
                     {
-                        // data is different
-                        var updated_program = program;
+                        // data is different                        
+                        var updated_program = (IPROGRAMME)program.Clone();
 
                         if (program_xmltv_entry.Title != xmltv_entry.Title)
                         {
@@ -411,13 +453,13 @@
                             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));
                     }
                     else
                     {
                         // data is the same
                         //source_valid.Add(new OldNewProgram(program, program));
-                        source_valid.Add(program);
+                        source_valid.Add(new OldNewProgram(program));
                     }
                 }
                 index++;
@@ -823,34 +865,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 +880,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)
         {
+            gLog.Info.WriteLine("Updating {0} gbpvr programs.", programs.Count);
             int ChunkSize = 1024;
-            var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
+            var ChunkList = programs.Chunk<IOldNewProgram>(ChunkSize);
 
             double total = programs.Count;
             double index = 0;
@@ -881,20 +900,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 +993,7 @@
             }
             return result;
         }
-        private bool UpdateProgramEntryDatabase(IEnumerable<IPROGRAMME> list)
+        private bool UpdateProgramEntryDatabase(IEnumerable<IOldNewProgram> list)
         {
             bool result = false;
             try
@@ -1088,7 +1102,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 +1114,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();

 

  ViewVC Help
Powered by ViewVC 1.1.22