/[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 23:07:46	207
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/16 23:35:42	208
@@ -824,56 +824,59 @@
         public void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs)
         {            
             gLog.Info.WriteLine("Removing all gbpvr programs.");
-            //double total = programs.Count;
-            //double index = 0;
-            //double progress = 0;
-            //foreach (var program in programs)
-            //{
-            //    progress = 100.0 * (index / total);
-            //    gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing GBPVR Programs: {0:00}%", (int)progress)));
-            //    if (!RemoveProgramDatabaseEntry(program))
-            //    {
-            //        gLog.Error.WriteLine("Failed to remove program with oid: {0}", program.oid);
-            //    }
-            //    index++;
-            //}
+            double total = programs.Count;
+            double index = 0;
+            double progress = 0;
             Stopwatch st = new Stopwatch();
             st.Start();
-            if (!RemoveAllProgramDatabaseEntries())
+            foreach (var program in programs)
             {
-                gLog.Error.WriteLine("Failed to remove one or more program(s)");
+                progress = 100.0 * (index / total);
+                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing GBPVR Program oid='{0}' ({1:00}%)", program.oid, (int)progress)));
+                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} minutes", st.Elapsed.TotalMinutes);
+            gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds", st.Elapsed.TotalSeconds);
         }
         public void UpdateGBPVRPrograms(List<IPROGRAMME> programs)
         {
-            int ChunkSize = 4096;
+            int ChunkSize = 1024;
             var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
 
             double total = programs.Count;
             double index = 0;
-            //double progress = 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);
+                progress = 100.0 * (index / total);
                 //gLog.Warn.WriteLine("Inserting {0} gbpvr programs ({1} of {2})", p.Count(), index, total);   
-                if (!InsertProgramEntryDatabase(p))
+                if (!UpdateProgramEntryDatabase(p))
                 {
-                    gLog.Error.WriteLine("Failed to insert one or more program(s)");
+                    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(0, string.Format("Inserting {0} gbpvr programs ({1} of {2})", p.Count(), index, total)));
+                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating {0} gbpvr programs ({1} of {2}) {0:00}%", p.Count(), index, total, (int)progress)));
                 index += (double)p.Count();                
             }
             st1.Stop();
-            gLog.Warn.WriteLine("    operation took: {0:0.00000} minutes overall", st1.Elapsed.TotalMinutes);
+            gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
         }
 
         private bool RemoveAllProgramDatabaseEntries()
@@ -954,6 +957,47 @@
             }
             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;
@@ -995,7 +1039,41 @@
             }
             return result;
         }
-
+        #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();
@@ -1013,7 +1091,7 @@
             builder.AppendLine("end transaction;");
             return builder.ToString();
         }
-
+        
         private string BuildGBPVRSingleInsertCommand(IPROGRAMME program)
         {
             StringBuilder builder = new StringBuilder();
@@ -1029,6 +1107,7 @@
             builder.AppendFormat("\"{0}\");", program.rating);
             return builder.ToString();
         }
-      
+        #endregion
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22