/[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/16 18:23:11	182
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/17 01:01:10	213
@@ -22,7 +22,10 @@
         List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
         List<SQLLITE.ICHANNEL> Channels { get; }
         IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
-        List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
+        List<SQLLITE.IPROGRAMME> 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);
 
     }
     public class SQLLITE : ISQLLITE
@@ -53,9 +56,9 @@
         }
         private class CHANNEL : ICHANNEL
         {
-            public CHANNEL() 
+            public CHANNEL()
             {
-                BaseDatabaseDefinition<CHANNEL>.CreateDefault(this); 
+                BaseDatabaseDefinition<CHANNEL>.CreateDefault(this);
             }
             //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
 
@@ -86,7 +89,7 @@
             Int64 manual_channel_oid { get; }
             Int64 quality_level { get; }
             Int64 pre_pad_minutes { get; }
-            Int64 post_pad_minutes { get;}
+            Int64 post_pad_minutes { get; }
             Int32 priority { get; }
             String conversion_profile { get; }
         }
@@ -127,7 +130,7 @@
                 }
             }
             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);
@@ -152,9 +155,9 @@
                                 {
                                     prop.SetValue(instance, field_value, null);
                                 }
-                                
+
                             }
-                            catch (Exception ex) 
+                            catch (Exception ex)
                             {
                                 throw ex;
                             }
@@ -172,26 +175,26 @@
 
         private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE
         {
-            public RECORDING_SCHEDULE() 
+            public RECORDING_SCHEDULE()
             {
-                BaseDatabaseDefinition<RECORDING_SCHEDULE>.CreateDefault(this); 
+                BaseDatabaseDefinition<RECORDING_SCHEDULE>.CreateDefault(this);
             }
             //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
 
             public static void Create(ref RECORDING_SCHEDULE instance, SQLiteDataReader r, int index)
             {
-                BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(ref instance, r, index); 
+                BaseDatabaseDefinition<RECORDING_SCHEDULE>.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 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 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; }
@@ -203,7 +206,7 @@
         }
 
         public interface IPROGRAMME : IEquatable<IPROGRAMME>
-            {
+        {
             Int64 oid { get; set; }
             String name { get; set; }
             String sub_title { get; set; }
@@ -219,9 +222,9 @@
         }
         private class PROGRAMME : IPROGRAMME
         {
-            public PROGRAMME() 
+            public PROGRAMME()
             {
-                BaseDatabaseDefinition<PROGRAMME>.CreateDefault(this); 
+                BaseDatabaseDefinition<PROGRAMME>.CreateDefault(this);
             }
             //public PROGRAMME(SQLiteDataReader r, int index) : base(r, index) { }
             public static void Create(ref PROGRAMME instance, SQLiteDataReader r, int index)
@@ -297,9 +300,9 @@
             public const string PROGRAMME = "PROGRAMME";
             public const string CHANNEL = "CHANNEL";
         }
-
+        private string DATABASE_BACKUP_FILE = string.Empty;
         //public SQLLite() { }
-        protected SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated) 
+        protected SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated)
         {
             this.OnInstanceCreated = OnInstanceCreated;
             //CreateConnection(database);
@@ -319,14 +322,14 @@
         [NonSerialized]
         private EventHandler<EventArgs> _OnInstanceCreated;
         private EventHandler<EventArgs> OnInstanceCreated { get { return _OnInstanceCreated; } set { _OnInstanceCreated = value; } }
-        
+
         private void OnCreatedInstance(object sender, EventArgs e)
         {
             if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
         }
 
         private string Database;
-        #region ISQLLITE members        
+        #region ISQLLITE members
         public List<IPROGRAMME> Programs { get; private set; }
         public List<IRECORDING_SCHEDULE> Recordings { get; private set; }
         public List<ICHANNEL> Channels { get; private set; }
@@ -336,7 +339,7 @@
             var list = new List<IPROGRAMME>(programs.ToArray());
             DateTime first = new DateTime();
             DateTime last = new DateTime();
-            first = list.OrderBy(s => s.start_time).ToList().First().start_time;            
+            first = list.OrderBy(s => s.start_time).ToList().First().start_time;
             last = list.OrderBy(s => s.start_time).ToList().Last().start_time;
             gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
             gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
@@ -344,10 +347,10 @@
             return range;
         }
 
-        public List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
+        public List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
         {
             source_invalid = new List<IPROGRAMME>();
-            List<IOldNewProgram> source_valid = new List<IOldNewProgram>();
+            List<IPROGRAMME> source_valid = new List<IPROGRAMME>();
             gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();
             xmltv_programs = xmltv_programs.OrderBy(s => DateTime.Parse(s.Start)).ToList();
             gbpvr_programs.TrimExcess();
@@ -359,7 +362,7 @@
             foreach (var program in gbpvr_programs)
             {
                 progress = 100.0 * (index / total);
-                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Filtering GBPVR Programs: {0:00}%", (int)progress)));                
+                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Filtering GBPVR Programs: {0:00}%", (int)progress)));
                 // find an entry in xmltv_programs by Channel Number & StartDate                
                 var program_xmltv_entry = program.AsXMLTVProgramDefinition(this);
                 var xmltv_entry = xmltv_programs.Find(s => s.ChannelNumber == program_xmltv_entry.ChannelNumber && s.Start == program_xmltv_entry.Start);
@@ -371,7 +374,7 @@
                 else
                 {
                     // check if the xmltv entry has different data from the current program
-                    if(!xmltv_entry.Equals(program_xmltv_entry))
+                    if (!xmltv_entry.Equals(program_xmltv_entry))
                     {
                         // data is different
                         var updated_program = program;
@@ -407,17 +410,22 @@
                         {
                             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(new OldNewProgram(program, updated_program));
+                        source_valid.Add(updated_program);
                     }
                     else
                     {
                         // data is the same
-                        source_valid.Add(new OldNewProgram(program, program));
+                        //source_valid.Add(new OldNewProgram(program, program));
+                        source_valid.Add(program);
                     }
                 }
                 index++;
             }
-
+            gLog.Info.WriteLine("Total XMLTV Programs: 0x{0:x8}", xmltv_programs.Count);
+            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);
             #region old-code
             //List<OldNewProgram> source_update = new List<OldNewProgram>();
             ////if (range == null)
@@ -650,6 +658,7 @@
                 gLog.Info.WriteLine("\tDestination: {0}", backup_file);
 
                 System.IO.File.Copy(this.Database, backup_file);
+                DATABASE_BACKUP_FILE = backup_file;
                 return true;
             }
             catch (Exception ex)
@@ -665,32 +674,42 @@
                 List<ICHANNEL> channels = new List<ICHANNEL>();
                 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))
+                    try
                     {
-                        using (SQLiteDataReader r = cmd.ExecuteReader())
+                        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))
                         {
-                            if (!r.HasRows)
+                            using (SQLiteDataReader r = cmd.ExecuteReader())
                             {
-                                gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
-                            }
-                            else
-                            {
-                                while (r.Read())
+                                if (!r.HasRows)
                                 {
-                                    CHANNEL channel = new CHANNEL();
-                                    for (int i = 0; i < r.FieldCount; i++)
+                                    gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
+                                }
+                                else
+                                {
+                                    while (r.Read())
                                     {
-                                        CHANNEL.Create(ref channel, r, i);
+                                        CHANNEL channel = new CHANNEL();
+                                        for (int i = 0; i < r.FieldCount; i++)
+                                        {
+                                            CHANNEL.Create(ref channel, r, i);
+                                        }
+                                        channels.Add(channel);
                                     }
-                                    channels.Add(channel);
                                 }
                             }
                         }
                     }
-                    con.Clone();
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
                 }
                 this.Channels = channels;
             }
@@ -706,32 +725,42 @@
                 List<IPROGRAMME> programs = new List<IPROGRAMME>();
                 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))
+                    try
                     {
-                        using (SQLiteDataReader r = cmd.ExecuteReader())
+                        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))
                         {
-                            if (!r.HasRows)
+                            using (SQLiteDataReader r = cmd.ExecuteReader())
                             {
-                                gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
-                            }
-                            else
-                            {
-                                while (r.Read())
+                                if (!r.HasRows)
                                 {
-                                    PROGRAMME program = new PROGRAMME();
-                                    for (int i = 0; i < r.FieldCount; i++)
+                                    gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
+                                }
+                                else
+                                {
+                                    while (r.Read())
                                     {
-                                        PROGRAMME.Create(ref program, r, i);
+                                        PROGRAMME program = new PROGRAMME();
+                                        for (int i = 0; i < r.FieldCount; i++)
+                                        {
+                                            PROGRAMME.Create(ref program, r, i);
+                                        }
+                                        programs.Add(program);
                                     }
-                                    programs.Add(program);
                                 }
                             }
                         }
                     }
-                    con.Clone();
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
                 }
                 this.Programs = programs;
             }
@@ -747,32 +776,42 @@
                 List<IRECORDING_SCHEDULE> recordings = new List<IRECORDING_SCHEDULE>();
                 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))
+                    try
                     {
-                        using (SQLiteDataReader r = cmd.ExecuteReader())
+                        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))
                         {
-                            if (!r.HasRows)
+                            using (SQLiteDataReader r = cmd.ExecuteReader())
                             {
-                                gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
-                            }
-                            else
-                            {
-                                while (r.Read())
+                                if (!r.HasRows)
                                 {
-                                    RECORDING_SCHEDULE recording = new RECORDING_SCHEDULE();
-                                    for (int i = 0; i < r.FieldCount; i++)
+                                    gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
+                                }
+                                else
+                                {
+                                    while (r.Read())
                                     {
-                                        RECORDING_SCHEDULE.Create(ref recording, r, i);
+                                        RECORDING_SCHEDULE recording = new RECORDING_SCHEDULE();
+                                        for (int i = 0; i < r.FieldCount; i++)
+                                        {
+                                            RECORDING_SCHEDULE.Create(ref recording, r, i);
+                                        }
+                                        recordings.Add(recording);
                                     }
-                                    recordings.Add(recording);
                                 }
                             }
                         }
                     }
-                    con.Clone();
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
                 }
                 this.Recordings = recordings;
             }
@@ -782,101 +821,341 @@
             }
         }
 
+        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);
+
+            int ChunkSize = 1024;
+            var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
+
+            double total = programs.Count;
+            double index = 0;
+            double progress = 0;
+            Stopwatch st1 = new Stopwatch();
+            st1.Start();
+            foreach (var p in ChunkList)
+            {
+                progress = 100.0 * (index / total);
+                if (!RemoveProgramDatabaseEntry(p))
+                {
+                    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)));
+                index += (double)p.Count(); 
+            }
+            st1.Stop();
+            gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
+        }
+        public void UpdateGBPVRPrograms(List<IPROGRAMME> programs)
+        {
+            int ChunkSize = 1024;
+            var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
 
+            double total = programs.Count;
+            double index = 0;
+            double progress = 0;
+            Stopwatch st1 = new Stopwatch();
+            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)));
+                index += (double)p.Count();                
+            }
+            st1.Stop();
+            gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
+        }
 
-        private bool RemoveProgramDatabaseEntry(IPROGRAMME old_program)
+        private bool RemoveAllProgramDatabaseEntries()
         {
             bool result = false;
-            //try
-            //{                
-            //    using (SQLiteConnection con = CreateConnection())
-            //    {
-            //        try
-            //        {
-            //            gLog.Verbose.Info.WriteLine("Removing old program with oid: '{0}'", old_program.oid);
-            //            con.Open();
-            //            string command_text = string.Format(@"DELETE FROM [{0}] WHERE [oid] = @oid;", TABLES.PROGRAMME);
-            //            gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
-            //            using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
-            //            {
-            //                cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
-            //                int rowsupdated = cmd.ExecuteNonQuery();
-            //                gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
-            //            }
-            //            con.Clone();
-            //            result = true;
-            //        }
-            //        catch (SQLiteException ex)
-            //        {
-            //            gLog.Error.WriteLine(ex.ToString());
-            //            result = false;
-            //        }
-            //    }
-            //}
-            //catch (Exception ex)
-            //{
-            //    gLog.Error.WriteLine(ex.ToString());
-            //    result = false;
-            //}
+            try
+            {
+                using (SQLiteConnection con = CreateConnection())
+                {
+                    try
+                    {
+                        //gLog.Verbose.Info.WriteLine("Removing old program with oid: '{0}'", old_program.oid);
+                        con.Open();
+                        string command_text = string.Format(@"DELETE FROM [{0}];", TABLES.PROGRAMME);
+                        //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
+                        using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
+                        {
+                            //cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
+                            int rowsupdated = cmd.ExecuteNonQuery();
+                            //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
+                        }
+                        result = true;
+                    }
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                        result = false;
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+                result = false;
+            }
             return result;
         }
-        private bool UpdateProgramDatabaseEntry(IPROGRAMME old_program, IPROGRAMME new_program)
+        private bool RemoveProgramDatabaseEntry(IEnumerable<IPROGRAMME> list)
         {
             bool result = false;
-            //try
-            //{
-            //    using (SQLiteConnection con = CreateConnection())
-            //    {
-            //        try
-            //        {
-            //            gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", old_program.oid);
-            //            con.Open();
-            //            //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
-            //            string command_text = string.Empty;
-            //            StringBuilder builder = new StringBuilder();
-            //            builder.AppendLine(string.Format("UPDATE [{0}]", TABLES.PROGRAMME));
-            //            builder.Append("SET ");
-            //            builder.AppendFormat("[name]=@name,");
-            //            builder.AppendFormat("[sub_title]=@sub_title,");
-            //            builder.AppendFormat("[description]=@description,");
-            //            builder.AppendFormat("[start_time]=@start_time,");
-            //            builder.AppendFormat("[end_time]=@end_time,");
-            //            builder.AppendFormat("[channel_oid]=@channel_oid,");
-            //            builder.AppendFormat("[unique_identifier]=@unique_identifier,");
-            //            builder.AppendFormat("[rating]=@rating");
-            //            builder.AppendLine(" WHERE [oid] = @oid");
-            //            command_text = builder.ToString();
-            //            gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
-            //            using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
-            //            {
-            //                cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
-            //                cmd.Parameters.Add(new SQLiteParameter("name", new_program.name));
-            //                cmd.Parameters.Add(new SQLiteParameter("sub_title", new_program.sub_title));
-            //                cmd.Parameters.Add(new SQLiteParameter("description", new_program.description));
-            //                cmd.Parameters.Add(new SQLiteParameter("start_time", new_program.start_time));
-            //                cmd.Parameters.Add(new SQLiteParameter("end_time", new_program.end_time));
-            //                cmd.Parameters.Add(new SQLiteParameter("channel_oid", new_program.channel_oid));
-            //                cmd.Parameters.Add(new SQLiteParameter("unique_identifier", new_program.unique_identifier));
-            //                cmd.Parameters.Add(new SQLiteParameter("rating", new_program.rating));
-            //                int rowsupdated = cmd.ExecuteNonQuery();
-            //                gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
-            //            }
-            //            con.Clone();
-            //            result = true;
-            //        }
-            //        catch (SQLiteException ex)
-            //        {
-            //            gLog.Error.WriteLine(ex.ToString());
-            //            result = false;
-            //        }
-            //    }
-            //}
-            //catch (Exception ex)
-            //{
-            //    gLog.Error.WriteLine(ex.ToString());
-            //    result = false;
-            //}
+            try
+            {
+                using (SQLiteConnection con = CreateConnection())
+                {
+                    try
+                    {
+                        //gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", new_program.oid);
+                        con.Open();
+                        //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
+                        string command_text = string.Empty;
+                        command_text = BuildGBPVRMultiDeleteCommand(list);
+                        //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
+                        using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
+                        {
+                            int rowsupdated = cmd.ExecuteNonQuery();
+                            //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
+                        }
+
+                        result = true;
+                    }
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                        result = false;
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+                result = false;
+            }
             return result;
         }
+        private bool UpdateProgramEntryDatabase(IEnumerable<IPROGRAMME> list)
+        {
+            bool result = false;
+            try
+            {
+                using (SQLiteConnection con = CreateConnection())
+                {
+                    try
+                    {
+                        //gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", new_program.oid);
+                        con.Open();
+                        //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
+                        string command_text = string.Empty;
+                        command_text = BuildGBPVRMultiUpdateCommand(list);
+                        //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
+                        using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
+                        {
+                            int rowsupdated = cmd.ExecuteNonQuery();
+                            //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
+                        }
+
+                        result = true;
+                    }
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                        result = false;
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+                result = false;
+            }
+            return result;
+        }
+        private bool InsertProgramEntryDatabase(IEnumerable<IPROGRAMME> list)
+        {
+            bool result = false;
+            try
+            {
+                using (SQLiteConnection con = CreateConnection())
+                {
+                    try
+                    {
+                        //gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", new_program.oid);
+                        con.Open();
+                        //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
+                        string command_text = string.Empty;
+                        command_text = BuildGBPVRMultiInsertCommand(list);
+                        //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
+                        using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
+                        {                          
+                            int rowsupdated = cmd.ExecuteNonQuery();
+                            //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
+                        }
+
+                        result = true;
+                    }
+                    catch (SQLiteException ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                        result = false;
+                    }
+                    finally
+                    {
+                        con.Close();
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+                result = false;
+            }
+            return result;
+        }
+
+        #region Multi-Delete Command Support
+        private string BuildGBPVRMultiDeleteCommand(IEnumerable<IPROGRAMME> list)
+        {
+            StringBuilder builder = new StringBuilder();
+            /*
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2)
+             */
+            builder.AppendLine("begin transaction;");
+            foreach (var t in list)
+            {
+                builder.AppendLine(BuildGBPVRSingleDeleteCommand(t));
+            }
+            builder.AppendLine("end transaction;");
+            return builder.ToString();
+        }
+        private string BuildGBPVRSingleDeleteCommand(IPROGRAMME program)
+        {
+            StringBuilder builder = new StringBuilder();
+            builder.AppendFormat("delete from {0} where oid={1};", TABLES.PROGRAMME, program.oid);
+            return builder.ToString();
+        }
+        #endregion
+        #region Multi-Update Command Support
+        private string BuildGBPVRMultiUpdateCommand(IEnumerable<IPROGRAMME> list)
+        {
+            StringBuilder builder = new StringBuilder();
+            /*
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2)
+             */
+            builder.AppendLine("begin transaction;");
+            foreach (var t in list)
+            {
+                builder.AppendLine(BuildGBPVRSingleInsertCommand(t));
+            }
+            builder.AppendLine("end transaction;");
+            return builder.ToString();
+        }
+        private string BuildGBPVRSingleUpdateCommand(IPROGRAMME program)
+        {
+            StringBuilder builder = new StringBuilder();
+            builder.AppendFormat("update {0} SET ", TABLES.PROGRAMME);
+            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("channel_oid={0}, ", program.channel_oid);
+            builder.AppendFormat("unique_identifier=\"{0}\", ", program.unique_identifier);
+            builder.AppendFormat("rating=\"{0}\" ", program.rating);
+            builder.AppendFormat("where oid={0};", program.oid);
+            return builder.ToString();
+        }
+        #endregion
+        #region Multi-Insert Command Support
+        private string BuildGBPVRMultiInsertCommand(IEnumerable<IPROGRAMME> list)
+        {
+            StringBuilder builder = new StringBuilder();
+            /*
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2);
+                insert into table1 (field1,field2) values (value1,value2)
+             */
+            builder.AppendLine("begin transaction;");
+            foreach (var t in list)
+            {
+                builder.AppendLine(BuildGBPVRSingleInsertCommand(t));
+            }
+            builder.AppendLine("end transaction;");
+            return builder.ToString();
+        }
+        
+        private string BuildGBPVRSingleInsertCommand(IPROGRAMME program)
+        {
+            StringBuilder builder = new StringBuilder();
+            builder.AppendFormat("insert into {0} (oid,name,sub_title,description,start_time,end_time,channel_oid,unique_identifier,rating) values (", TABLES.PROGRAMME);
+            builder.AppendFormat("{0},",program.oid);
+            builder.AppendFormat("\"{0}\",", program.name);
+            builder.AppendFormat("\"{0}\",", program.sub_title);
+            builder.AppendFormat("\"{0}\",", program.description);
+            builder.AppendFormat("'{0}',", program.start_time.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
+            builder.AppendFormat("'{0}',", program.end_time.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
+            builder.AppendFormat("{0},", program.channel_oid);
+            builder.AppendFormat("\"{0}\",", program.unique_identifier);
+            builder.AppendFormat("\"{0}\");", program.rating);
+            return builder.ToString();
+        }
+        #endregion
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22