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

Annotation of /trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 228 - (hide annotations) (download)
Sun Mar 17 04:02:57 2013 UTC (7 years, 5 months ago) by william
File size: 57144 byte(s)
+ update rating when fixing gbpvr programs

1 william 156 //#define SQLLITE_CONNECTION_TEST
2     using System;
3     using System.Collections.Generic;
4     using System.Linq;
5     using System.Text;
6     using System.Data.SQLite;
7     using System.Diagnostics;
8     using Enterprise.Logging;
9 william 163 using libxmltv;
10 william 165 using libxmltv.Interfaces;
11     using libxmltv.Core;
12 william 156 namespace GBPVRProgramDatabaseFixer
13     {
14 william 224 public interface ICloneable<T>
15     {
16     T Clone();
17     }
18 william 182 public interface IOldNewProgram
19     {
20     SQLLITE.IPROGRAMME OldProgram { get; }
21     SQLLITE.IPROGRAMME NewProgram { get; }
22     }
23 william 163 public interface ISQLLITE
24 william 156 {
25 william 163 List<SQLLITE.IPROGRAMME> Programs { get; }
26     List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
27     List<SQLLITE.ICHANNEL> Channels { get; }
28     IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
29 william 215 List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
30 william 163
31 william 183 void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs);
32 william 215 void UpdateGBPVRPrograms(List<IOldNewProgram> programs);
33 william 183
34 william 163 }
35     public class SQLLITE : ISQLLITE
36     {
37 william 182 private class OldNewProgram : IOldNewProgram
38 william 176 {
39 william 215 public OldNewProgram() : this(new PROGRAMME()) { }
40     public OldNewProgram(IPROGRAMME _old) : this(_old,_old) { }
41 william 180 public OldNewProgram(IPROGRAMME _old, IPROGRAMME _new) { OldProgram = _old; NewProgram = _new; }
42    
43     public IPROGRAMME OldProgram { get; private set; }
44     public IPROGRAMME NewProgram { get; private set; }
45 william 176 }
46 william 163 public static ISQLLITE Create(string database, EventHandler<EventArgs> OnInstanceCreated)
47     {
48     return new SQLLITE(database, OnInstanceCreated);
49     }
50    
51 william 156 #region DATABASE DEFINITIONS
52 william 224 #region BaseDatabaseDefinition
53 william 156 private static class BaseDatabaseDefinition<T>
54     {
55     public static void CreateDefault(T instance)
56     {
57     try
58     {
59     Type t = typeof(T);
60     var props = t.GetProperties();
61     foreach (var prop in props)
62     {
63     Type prop_type = prop.PropertyType;
64     object field_value = null;
65     try
66     {
67     if (prop_type == typeof(string))
68     {
69     field_value = string.Empty;
70     }
71     else
72     {
73     field_value = Activator.CreateInstance(prop_type);
74     }
75     }
76     catch (Exception ex)
77     {
78     throw ex;
79     }
80     prop.SetValue(instance, field_value, null);
81     }
82     }
83     catch (Exception ex)
84     {
85     throw ex;
86     }
87     }
88 william 221 public static void Create(T source, ref T destination)
89     {
90     Type t = source.GetType();
91     var props = t.GetProperties();
92     foreach (var prop in props)
93     {
94     try
95     {
96     object value = prop.GetValue(source, null);
97     prop.SetValue(destination, value, null);
98     }
99     catch (Exception ex)
100     {
101     throw ex;
102     }
103     }
104     }
105 william 156 public static void Create(ref T instance, SQLiteDataReader r, int index)
106 william 184 {
107 william 156 string field_name = r.GetName(index);
108     Type field_type = r.GetFieldType(index);
109     object field_value = r.GetValue(index);
110     //gLog.Verbose.Debug.WriteLine("Name: '{0}' Type: '{1}' Value: '{2}'", field_name, field_type.Name, field_value == null ? "null" : field_value.ToString());
111    
112     Type t = typeof(T);
113     var props = t.GetProperties();
114     foreach (var prop in props)
115     {
116     if (prop.Name.ToLower() == field_name.ToLower())
117     {
118     if (prop.PropertyType == field_type)
119     {
120     Type db_type = field_value.GetType();
121     try
122     {
123     if (db_type == typeof(System.DBNull))
124     {
125     prop.SetValue(instance, null, null);
126     }
127     else
128     {
129     prop.SetValue(instance, field_value, null);
130     }
131 william 184
132 william 156 }
133 william 184 catch (Exception ex)
134 william 156 {
135     throw ex;
136     }
137     }
138     else
139     {
140     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);
141     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));
142     }
143     }
144     }
145    
146     }
147     }
148 william 224 #endregion
149     public interface ICHANNEL : ICloneable<ICHANNEL>
150     {
151     Int64 oid { get; }
152     String name { get; }
153     String channelID { get; }
154     Int64 channel_number { get; }
155     String favourite_channel { get; }
156     String display_name { get; }
157     }
158     private class CHANNEL : ICHANNEL, ICloneable<CHANNEL>
159     {
160     public CHANNEL()
161     {
162     BaseDatabaseDefinition<CHANNEL>.CreateDefault(this);
163     }
164     //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
165 william 156
166 william 224 public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index)
167     {
168     BaseDatabaseDefinition<CHANNEL>.Create(ref instance, r, index);
169     }
170     #region ICHANNEL members
171     public Int64 oid { get; set; }
172     public String name { get; set; }
173     public String channelID { get; set; }
174     public Int64 channel_number { get; set; }
175     public String favourite_channel { get; set; }
176     public String display_name { get; set; }
177     #endregion
178    
179     ICHANNEL ICloneable<ICHANNEL>.Clone()
180     {
181     return Clone();
182     }
183     public CHANNEL Clone()
184     {
185     CHANNEL p = new CHANNEL();
186     BaseDatabaseDefinition<CHANNEL>.Create(this, ref p);
187     return p;
188     }
189     }
190     public interface IRECORDING_SCHEDULE : ICloneable<IRECORDING_SCHEDULE>
191 william 156 {
192 william 224 Int64 oid { get; }
193     Int64 programme_oid { get; }
194     Int64 capture_source_oid { get; }
195     Int16 status { get; }
196     String filename { get; }
197     Int64 recording_type { get; }
198     Int64 recording_group { get; }
199     DateTime manual_start_time { get; }
200     DateTime manual_end_time { get; }
201     Int64 manual_channel_oid { get; }
202     Int64 quality_level { get; }
203     Int64 pre_pad_minutes { get; }
204     Int64 post_pad_minutes { get; }
205     Int32 priority { get; }
206     String conversion_profile { get; }
207     }
208     private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE, ICloneable<RECORDING_SCHEDULE>
209     {
210 william 184 public RECORDING_SCHEDULE()
211 william 156 {
212 william 184 BaseDatabaseDefinition<RECORDING_SCHEDULE>.CreateDefault(this);
213 william 156 }
214     //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
215    
216     public static void Create(ref RECORDING_SCHEDULE instance, SQLiteDataReader r, int index)
217     {
218 william 184 BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(ref instance, r, index);
219 william 156 }
220 william 224 IRECORDING_SCHEDULE ICloneable<IRECORDING_SCHEDULE>.Clone()
221 william 221 {
222 william 224 return Clone();
223     }
224     public RECORDING_SCHEDULE Clone()
225     {
226 william 221 RECORDING_SCHEDULE p = new RECORDING_SCHEDULE();
227     BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, ref p);
228 william 224 return p;
229 william 221 }
230 william 156 #region IRECORDING_SCHEDULE members
231 william 184 public Int64 oid { get; set; }
232     public Int64 programme_oid { get; set; }
233     public Int64 capture_source_oid { get; set; }
234 william 156 public Int16 status { get; set; }
235     public String filename { get; set; }
236 william 184 public Int64 recording_type { get; set; }
237     public Int64 recording_group { get; set; }
238     public DateTime manual_start_time { get; set; }
239 william 156 public DateTime manual_end_time { get; set; }
240     public Int64 manual_channel_oid { get; set; }
241     public Int64 quality_level { get; set; }
242     public Int64 pre_pad_minutes { get; set; }
243     public Int64 post_pad_minutes { get; set; }
244     public Int32 priority { get; set; }
245     public String conversion_profile { get; set; }
246     #endregion
247 william 221
248    
249 william 156 }
250 william 224 public interface IPROGRAMME : IEquatable<IPROGRAMME>, ICloneable<IPROGRAMME>
251 william 184 {
252 william 168 Int64 oid { get; set; }
253     String name { get; set; }
254     String sub_title { get; set; }
255     String description { get; set; }
256     DateTime start_time { get; set; }
257     DateTime end_time { get; set; }
258     Int64 channel_oid { get; set; }
259     String unique_identifier { get; set; }
260     String rating { get; set; }
261 william 165
262 william 182 string ToString();
263 william 165 IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite);
264 william 156 }
265 william 224 private class PROGRAMME : IPROGRAMME, ICloneable<PROGRAMME>
266 william 156 {
267 william 224
268     IPROGRAMME ICloneable<IPROGRAMME>.Clone()
269 william 221 {
270 william 224 return Clone();
271     }
272     public PROGRAMME Clone()
273     {
274 william 221 PROGRAMME p = new PROGRAMME();
275     BaseDatabaseDefinition<PROGRAMME>.Create(this, ref p);
276 william 224 return p;
277 william 221 }
278 william 184 public PROGRAMME()
279 william 156 {
280 william 184 BaseDatabaseDefinition<PROGRAMME>.CreateDefault(this);
281 william 156 }
282     //public PROGRAMME(SQLiteDataReader r, int index) : base(r, index) { }
283     public static void Create(ref PROGRAMME instance, SQLiteDataReader r, int index)
284     {
285     BaseDatabaseDefinition<PROGRAMME>.Create(ref instance, r, index);
286     }
287     #region IPROGRAMME members
288     public Int64 oid { get; set; }
289     public String name { get; set; }
290     public String sub_title { get; set; }
291     public String description { get; set; }
292     public DateTime start_time { get; set; }
293     public DateTime end_time { get; set; }
294     public Int64 channel_oid { get; set; }
295     public String unique_identifier { get; set; }
296     public String rating { get; set; }
297 william 165 public IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite)
298     {
299     ProgramList.ProgramDefintion definition = new ProgramList.ProgramDefintion();
300 william 181
301 william 165 var channel = sqllite.Channels.Find(s => s.oid == this.channel_oid);
302 william 181 var channelname = channel.display_name;
303     var split = channelname.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
304     definition.ChannelNumber = Convert.ToInt32(split.First());
305     definition.ChannelName = string.IsNullOrEmpty(split.Last()) ? string.Empty : split.Last().ToString();
306 william 165 definition.Description = this.description;
307 william 168 definition.Start = this.start_time.ToDateTimeString();
308     definition.Stop = this.end_time.ToDateTimeString();
309 william 165 definition.SubTitle = this.sub_title;
310     definition.Title = this.name;
311 william 228 definition.Rating = this.rating;
312 william 165 return definition;
313     }
314 william 156 #endregion
315 william 182
316     public bool Equals(IPROGRAMME other)
317     {
318     return this.ToString() == other.ToString();
319     }
320     public override string ToString()
321     {
322     StringBuilder builder = new StringBuilder();
323    
324     builder.AppendFormat("oid: '{0}' ", oid);
325     builder.AppendFormat("name: '{0}' ", name);
326     builder.AppendFormat("sub_title: '{0}' ", sub_title);
327     builder.AppendFormat("description: '{0}' ", description);
328     builder.AppendFormat("start_time: '{0}' ", start_time.ToDateTimeString());
329     builder.AppendFormat("end_time: '{0}' ", end_time.ToDateTimeString());
330     builder.AppendFormat("channel_oid: '{0}' ", channel_oid);
331     builder.AppendFormat("unique_identifier: '{0}' ", unique_identifier);
332     builder.AppendFormat("rating: '{0}'", rating);
333    
334     return builder.ToString();
335     }
336     public override bool Equals(object obj)
337     {
338     if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); }
339     if (obj.GetType().IsAssignableFrom(typeof(IPROGRAMME)))
340     {
341     return this.Equals((IPROGRAMME)obj);
342     }
343     return base.Equals(obj);
344     }
345     public override int GetHashCode()
346     {
347     return this.ToString().GetHashCode();
348     }
349 william 221
350 william 224
351    
352    
353    
354    
355    
356    
357 william 156 }
358     #endregion
359     private static class TABLES
360     {
361     public const string RECORDING_SCHEDULE = "RECORDING_SCHEDULE";
362     public const string PROGRAMME = "PROGRAMME";
363 william 159 public const string CHANNEL = "CHANNEL";
364 william 156 }
365 william 203 private string DATABASE_BACKUP_FILE = string.Empty;
366 william 156 //public SQLLite() { }
367 william 184 protected SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated)
368 william 156 {
369     this.OnInstanceCreated = OnInstanceCreated;
370     //CreateConnection(database);
371     this.Database = database;
372 william 157 if (!CreateDatabaseBackup())
373     {
374     gLog.Error.WriteLine("Failed to backup database.");
375     return;
376     }
377 william 156 ConnectionTest();
378 william 159 ReadChannelData();
379 william 156 ReadRecodringScheduleData();
380     ReadProgrammeData();
381     OnCreatedInstance(this, new EventArgs());
382     }
383    
384     [NonSerialized]
385     private EventHandler<EventArgs> _OnInstanceCreated;
386     private EventHandler<EventArgs> OnInstanceCreated { get { return _OnInstanceCreated; } set { _OnInstanceCreated = value; } }
387 william 184
388 william 156 private void OnCreatedInstance(object sender, EventArgs e)
389     {
390     if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
391     }
392    
393     private string Database;
394 william 184 #region ISQLLITE members
395 william 156 public List<IPROGRAMME> Programs { get; private set; }
396     public List<IRECORDING_SCHEDULE> Recordings { get; private set; }
397 william 159 public List<ICHANNEL> Channels { get; private set; }
398 william 163
399     public IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs)
400     {
401     var list = new List<IPROGRAMME>(programs.ToArray());
402     DateTime first = new DateTime();
403     DateTime last = new DateTime();
404 william 184 first = list.OrderBy(s => s.start_time).ToList().First().start_time;
405 william 163 last = list.OrderBy(s => s.start_time).ToList().Last().start_time;
406 william 167 gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
407     gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
408 william 163 var range = DateTimeRange.Create(first, last);
409     return range;
410     }
411    
412 william 215 public List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
413 william 163 {
414 william 169 source_invalid = new List<IPROGRAMME>();
415 william 215 List<IOldNewProgram> source_valid = new List<IOldNewProgram>();
416 william 180 gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();
417     xmltv_programs = xmltv_programs.OrderBy(s => DateTime.Parse(s.Start)).ToList();
418     gbpvr_programs.TrimExcess();
419     xmltv_programs.TrimExcess();
420 william 176
421 william 168 double total = gbpvr_programs.Count;
422 william 166 double index = 0;
423     double progress = 0;
424 william 168 foreach (var program in gbpvr_programs)
425 william 180 {
426 william 169 progress = 100.0 * (index / total);
427 william 184 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Filtering GBPVR Programs: {0:00}%", (int)progress)));
428 william 180 // find an entry in xmltv_programs by Channel Number & StartDate
429     var program_xmltv_entry = program.AsXMLTVProgramDefinition(this);
430     var xmltv_entry = xmltv_programs.Find(s => s.ChannelNumber == program_xmltv_entry.ChannelNumber && s.Start == program_xmltv_entry.Start);
431 william 168 if (xmltv_entry == null)
432 william 163 {
433 william 180 // xmltv entry was not found
434     source_invalid.Add(program);
435 william 163 }
436 william 164 else
437     {
438 william 180 // check if the xmltv entry has different data from the current program
439 william 184 if (!xmltv_entry.Equals(program_xmltv_entry))
440 william 172 {
441 william 225 // data is different
442 william 224 var updated_program = program.Clone();
443 william 182
444     if (program_xmltv_entry.Title != xmltv_entry.Title)
445 william 180 {
446 william 182 gLog.Verbose.Warn.WriteLine(" Program oid: '{0}' - replacing title", updated_program.oid);
447     updated_program.name = xmltv_entry.Title;
448 william 180 }
449 william 182 if (program_xmltv_entry.SubTitle != xmltv_entry.SubTitle)
450     {
451     gLog.Verbose.Warn.WriteLine(" Program oid: '{0}' - replacing sub_title", updated_program.oid);
452     updated_program.sub_title = xmltv_entry.SubTitle;
453     }
454     if (program_xmltv_entry.Stop != xmltv_entry.Stop)
455     {
456     gLog.Verbose.Warn.WriteLine(" Program oid: '{0}' - replacing end_time", updated_program.oid);
457     updated_program.end_time = DateTime.Parse(xmltv_entry.Stop);
458     }
459     if (program_xmltv_entry.Start != xmltv_entry.Start)
460     {
461     gLog.Verbose.Warn.WriteLine(" Program oid: '{0}' - replacing start_time", updated_program.oid);
462     updated_program.start_time = DateTime.Parse(xmltv_entry.Start);
463     }
464     if (program_xmltv_entry.Description != xmltv_entry.Description)
465     {
466     gLog.Verbose.Warn.WriteLine(" Program oid: '{0}' - replacing description", updated_program.oid);
467     updated_program.description = xmltv_entry.Description;
468     }
469    
470 william 228 if(program_xmltv_entry.Rating != xmltv_entry.Rating)
471     {
472     gLog.Verbose.Warn.WriteLine(" Program oid: '{0}' - replacing rating", updated_program.oid);
473     updated_program.rating = xmltv_entry.Rating;
474     }
475    
476 william 182 var updated_xmltv_entry = updated_program.AsXMLTVProgramDefinition(this);
477     if (!xmltv_entry.Equals(updated_xmltv_entry))
478     {
479     throw new Exception(string.Format("Program oid '{0}' was not properly updated.", updated_program.oid));
480     }
481 william 196 //source_valid.Add(new OldNewProgram(program, updated_program));
482 william 215 source_valid.Add(new OldNewProgram(program, updated_program));
483 william 172 }
484 william 176 else
485     {
486 william 180 // data is the same
487 william 196 //source_valid.Add(new OldNewProgram(program, program));
488 william 215 source_valid.Add(new OldNewProgram(program));
489 william 176 }
490     }
491 william 181 index++;
492 william 176 }
493 william 192 gLog.Info.WriteLine("Total XMLTV Programs: 0x{0:x8}", xmltv_programs.Count);
494     gLog.Info.WriteLine("Total Valid GB-PVR Programs: 0x{0:x8}", source_valid.Count);
495     gLog.Info.WriteLine("Total Invalid GB-PVR Programs: 0x{0:x8}", source_invalid.Count);
496     gLog.Info.WriteLine("Total GB-PVR Programs (Valid+Invalid): 0x{0:x8} == 0x{1:x8}", source_valid.Count + source_invalid.Count, gbpvr_programs.Count);
497 william 180 #region old-code
498     //List<OldNewProgram> source_update = new List<OldNewProgram>();
499     ////if (range == null)
500     ////{
501     //// gLog.Warn.WriteLine("The DateTimeRange passed in is null...returning the original program list");
502     //// return list;
503     ////}
504     ////gLog.Warn.WriteLine("FilterProgramsByDateRange has not been implemented");
505     //double total = gbpvr_programs.Count;
506     //double index = 0;
507     //double progress = 0;
508     //foreach (var program in gbpvr_programs)
509     //{
510     // progress = 100.0 * (index / total);
511     // gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Filtering GBPVR Programs: {0:00}%", (int)progress)));
512     // var channel_oid = program.channel_oid;
513     // var channel = this.Channels.Find(s => s.oid == channel_oid);
514     // var start_date = program.start_time;
515     // //var xmltv_entry_list = xmltv_programs.FindAll(s => s.ChannelNumber == channel.channel_number && s.Start == start_date.ToDateTimeString());
516     // var query = from c in xmltv_programs
517     // where
518     // c.ChannelNumber == channel.channel_number &&
519     // c.Start == start_date.ToDateTimeString()
520     // select c;
521     // IProgramDefinition xmltv_entry = null;
522     // if (query.Count()-1 > 0)
523     // {
524     // gLog.Verbose.Error.WriteLine("Found more than one entry: Matching channel='{0}' and start='{1}'", channel.channel_number, start_date.ToDateTimeString());
525     // gLog.Verbose.Error.WriteLine(" GB-PVR Program Data: oid='{0}' channel_oid='{1}' name='{2}' sub_title='{3}' description='{4}'", program.oid, program.channel_oid, program.name, program.sub_title, program.description);
526     // if (Debugger.IsAttached)
527     // {
528     // gLog.Error.WriteLine(" Found: {0} matching entries", query.Count());
529     // int k_index = 0;
530     // foreach (var k in query)
531     // {
532     // gLog.Verbose.Error.WriteLine(" query[{0}]: channel='{1}' start='{2}' ('{3}') title='{4}' subtitle='{5}' description='{6}'", k_index, k.ChannelNumber, k.Start, DateTime.Parse(k.Start).ToString("yyyy/MM/dd HH:mm:ss.fffffff"), k.Title, k.SubTitle, k.Description);
533     // k_index++;
534     // }
535     // Debugger.Break();
536     // }
537     // }
538     // else
539     // {
540     // xmltv_entry = query.FirstOrDefault();
541     // }
542     // if (xmltv_entry == null)
543     // {
544     // //gLog.Verbose.Warn.WriteLine("Invalidating GB-PVR Program: oid='{0}' channel_oid='{1}' title='{2}' start='{3}'", program.oid, program.channel_oid, program.name, program.start_time.ToDateTimeString());
545     // source_invalid.Add(program);
546     // }
547     // else
548     // {
549     // //gLog.Verbose.Warn.WriteLine("Updating GB-PVR Program (if needed): oid='{0}' channel_oid='{1}' title='{2}' subtitle='{3}' start='{4}'", program.oid, program.channel_oid, program.name, program.sub_title, program.start_time.ToDateTimeString());
550     // var updated_program = program;
551     // bool needsupdate = false;
552     // if (xmltv_entry.Title != program.name)
553     // {
554     // //gLog.Verbose.Warn.WriteLine(" Updating:");
555     // //gLog.Verbose.Warn.WriteLine(" Old Title: {0}", program.name);
556     // //gLog.Verbose.Warn.WriteLine(" New Title: {0}", xmltv_entry.Title);
557     // updated_program.name = xmltv_entry.Title;
558     // needsupdate = true;
559     // }
560     // if (xmltv_entry.SubTitle != program.sub_title)
561     // {
562     // //gLog.Verbose.Warn.WriteLine(" Updating:");
563     // //gLog.Verbose.Warn.WriteLine(" Old SubTile: {0}", program.sub_title);
564     // //gLog.Verbose.Warn.WriteLine(" New SubTile: {0}", xmltv_entry.SubTitle);
565     // updated_program.sub_title = xmltv_entry.SubTitle;
566     // needsupdate = true;
567     // }
568     // if (xmltv_entry.Description != program.description)
569     // {
570     // //gLog.Verbose.Warn.WriteLine(" Updating:");
571     // //gLog.Verbose.Warn.WriteLine(" Old Descption: {0}", program.description);
572     // //gLog.Verbose.Warn.WriteLine(" New Descption: {0}", xmltv_entry.Description);
573     // updated_program.description = xmltv_entry.Description;
574     // needsupdate = true;
575     // }
576     // if (DateTime.Parse(xmltv_entry.Start) != program.start_time)
577     // {
578     // //gLog.Verbose.Warn.WriteLine(" Updating:");
579     // //gLog.Verbose.Warn.WriteLine(" Old StartTime: {0}", program.start_time.ToDateTimeString());
580     // //gLog.Verbose.Warn.WriteLine(" New StartTime: {0}", DateTime.Parse(xmltv_entry.Start).ToDateTimeString());
581     // updated_program.start_time = DateTime.Parse(xmltv_entry.Start);
582     // needsupdate = true;
583     // }
584     // if (DateTime.Parse(xmltv_entry.Stop) != program.end_time)
585     // {
586     // //gLog.Verbose.Warn.WriteLine(" Updating:");
587     // //gLog.Verbose.Warn.WriteLine(" Old EndTime: {0}", program.end_time.ToDateTimeString());
588     // //gLog.Verbose.Warn.WriteLine(" New EndTime: {0}", DateTime.Parse(xmltv_entry.Stop).ToDateTimeString());
589     // updated_program.end_time = DateTime.Parse(xmltv_entry.Stop);
590     // needsupdate = true;
591     // }
592     // if (needsupdate)
593     // {
594     // OldNewProgram p = new OldNewProgram();
595     // p.OldProgram = program;
596     // p.NewProgram = updated_program;
597     // source_update.Add(p);
598     // }
599     // source_valid.Add(updated_program);
600     // }
601     // index++;
602     //}
603     //source_valid = source_valid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
604     //source_invalid = source_invalid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
605     //if (source_invalid.Count > 0)
606     //{
607     // double source_invalid_count = source_valid.Count;
608     // double source_invalid_index = 0;
609     // double source_invalid_progress = 0;
610     // foreach (var old_program in source_invalid)
611     // {
612     // source_invalid_progress = 100.0 * (source_invalid_index / source_invalid_count);
613     // gLog.ReportProgress(this, new ReportProgressEventArgs((int)source_invalid_progress, string.Format("Removing old GBPVR Programs: {0:00}%", (int)source_invalid_progress)));
614     // // remove database entry
615     // if (!RemoveProgramDatabaseEntry(old_program))
616     // {
617     // gLog.Error.WriteLine("Failed to remove program with oid: '{0}'", old_program.oid);
618     // }
619     // else
620     // {
621     // gLog.Verbose.Info.WriteLine("Removed program with oid: '{0}'", old_program.oid);
622     // }
623     // source_invalid_index++;
624     // }
625     //}
626     //else
627     //{
628     // gLog.Info.WriteLine("No old GB-PVR Programs needed to be removed.");
629     //}
630     //if (source_update.Count > 0)
631     //{
632     // double source_update_count = source_valid.Count;
633     // double source_update_index = 0;
634     // double source_update_progress = 0;
635     // foreach (var p in source_update)
636     // {
637     // source_update_progress = 100.0 * (source_update_index / source_update_count);
638     // gLog.ReportProgress(this, new ReportProgressEventArgs((int)source_update_progress, string.Format("Updating GBPVR Programs: {0:00}%", (int)source_update_progress)));
639     // // remove database entry
640     // if (!UpdateProgramDatabaseEntry(p.OldProgram,p.NewProgram))
641     // {
642     // gLog.Error.WriteLine("Failed to update program with oid: '{0}'", p.OldProgram.oid);
643     // }
644     // else
645     // {
646     // gLog.Verbose.Info.WriteLine("Upated program with oid: '{0}'", p.OldProgram.oid);
647     // }
648     // source_update_index++;
649     // }
650     //}
651     //else
652     //{
653     // gLog.Info.WriteLine("No GB-PVR Programs needed to be updated.");
654     //}
655     //gLog.Info.WriteLine("Total XMLTV Programs: 0x{0:x8}", xmltv_programs.Count);
656     //gLog.Info.WriteLine("Updated: 0x{0:x8} GB-PVR Programs", source_valid.Count);
657     //gLog.Info.WriteLine("Removed: 0x{0:x8} GB-PVR Programs", source_invalid.Count);
658     //gLog.Info.WriteLine("Total GB-PVR Programs (Updated & Removed): 0x{0:x8}", source_valid.Count + source_invalid.Count);
659     #endregion
660 william 176
661 william 166 return source_valid;
662 william 163 }
663 william 156 #endregion
664    
665    
666     private string CreateConnectionString()
667     {
668     string connection_string = string.Format("Data Source={0}", this.Database);
669     return connection_string;
670     }
671    
672     private SQLiteConnection CreateConnection() { SQLiteConnection connection = new SQLiteConnection(CreateConnectionString()); return connection; }
673    
674     [Conditional("SQLLITE_CONNECTION_TEST")]
675     private void ConnectionTest()
676     {
677     try
678     {
679     using (SQLiteConnection con = CreateConnection())
680     {
681     con.Open();
682 william 159 string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
683 william 156 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
684     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
685     {
686     using (SQLiteDataReader r = cmd.ExecuteReader())
687     {
688     if (!r.HasRows)
689     {
690     gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
691     }
692     else
693     {
694     while (r.Read())
695     {
696     for (int i = 0; i < r.FieldCount; i++)
697     {
698     string field_name = r.GetName(i);
699     Type field_type = r.GetFieldType(i);
700     object field_value = r.GetValue(i);
701     gLog.Verbose.Debug.WriteLine("Name: '{0}' Type: '{1}' Value: '{2}'", field_name, field_type.Name, field_value == null ? "null" : field_value.ToString());
702     }
703     break;
704     }
705     }
706     }
707     }
708 william 157 con.Clone();
709 william 156 }
710     OnCreatedInstance(this, new EventArgs());
711     }
712     catch (Exception ex)
713     {
714     gLog.Error.WriteLine(ex.ToString());
715     }
716     }
717    
718 william 157
719     private bool CreateDatabaseBackup()
720     {
721     try
722     {
723     string backup_file = string.Format("{0}.{1}", this.Database, DateTime.Now.ToString("yyyyMMddHHmmss"));
724     gLog.Info.WriteLine("Creating Database backup...");
725     gLog.Info.WriteLine("\tSource: {0}", this.Database);
726     gLog.Info.WriteLine("\tDestination: {0}", backup_file);
727    
728     System.IO.File.Copy(this.Database, backup_file);
729 william 203 DATABASE_BACKUP_FILE = backup_file;
730 william 157 return true;
731     }
732     catch (Exception ex)
733     {
734     gLog.Error.WriteLine(ex.ToString());
735     return false;
736     }
737     }
738 william 159 private void ReadChannelData()
739     {
740     try
741     {
742     List<ICHANNEL> channels = new List<ICHANNEL>();
743     using (SQLiteConnection con = CreateConnection())
744     {
745 william 184 try
746 william 159 {
747 william 184 con.Open();
748     string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
749     gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
750     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
751 william 159 {
752 william 184 using (SQLiteDataReader r = cmd.ExecuteReader())
753 william 159 {
754 william 184 if (!r.HasRows)
755 william 159 {
756 william 184 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
757     }
758     else
759     {
760     while (r.Read())
761 william 159 {
762 william 184 CHANNEL channel = new CHANNEL();
763     for (int i = 0; i < r.FieldCount; i++)
764     {
765     CHANNEL.Create(ref channel, r, i);
766     }
767     channels.Add(channel);
768 william 159 }
769     }
770     }
771     }
772     }
773 william 184 catch (SQLiteException ex)
774     {
775     gLog.Error.WriteLine(ex.ToString());
776     }
777     finally
778     {
779     con.Close();
780     }
781 william 159 }
782     this.Channels = channels;
783     }
784     catch (Exception ex)
785     {
786     gLog.Error.WriteLine(ex.ToString());
787     }
788     }
789 william 156 private void ReadProgrammeData()
790     {
791     try
792     {
793     List<IPROGRAMME> programs = new List<IPROGRAMME>();
794     using (SQLiteConnection con = CreateConnection())
795     {
796 william 184 try
797 william 156 {
798 william 184 con.Open();
799     string command_text = string.Format("select * from {0};", TABLES.PROGRAMME);
800     gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
801     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
802 william 156 {
803 william 184 using (SQLiteDataReader r = cmd.ExecuteReader())
804 william 156 {
805 william 184 if (!r.HasRows)
806 william 156 {
807 william 184 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
808     }
809     else
810     {
811     while (r.Read())
812 william 156 {
813 william 184 PROGRAMME program = new PROGRAMME();
814     for (int i = 0; i < r.FieldCount; i++)
815     {
816     PROGRAMME.Create(ref program, r, i);
817     }
818     programs.Add(program);
819 william 156 }
820     }
821     }
822     }
823     }
824 william 184 catch (SQLiteException ex)
825     {
826     gLog.Error.WriteLine(ex.ToString());
827     }
828     finally
829     {
830     con.Close();
831     }
832 william 156 }
833     this.Programs = programs;
834     }
835     catch (Exception ex)
836     {
837     gLog.Error.WriteLine(ex.ToString());
838     }
839     }
840     private void ReadRecodringScheduleData()
841     {
842     try
843     {
844     List<IRECORDING_SCHEDULE> recordings = new List<IRECORDING_SCHEDULE>();
845     using (SQLiteConnection con = CreateConnection())
846     {
847 william 184 try
848 william 156 {
849 william 184 con.Open();
850     string command_text = string.Format("select * from {0};", TABLES.RECORDING_SCHEDULE);
851     gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
852     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
853 william 156 {
854 william 184 using (SQLiteDataReader r = cmd.ExecuteReader())
855 william 156 {
856 william 184 if (!r.HasRows)
857 william 156 {
858 william 184 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
859     }
860     else
861     {
862     while (r.Read())
863 william 156 {
864 william 184 RECORDING_SCHEDULE recording = new RECORDING_SCHEDULE();
865     for (int i = 0; i < r.FieldCount; i++)
866     {
867     RECORDING_SCHEDULE.Create(ref recording, r, i);
868     }
869     recordings.Add(recording);
870 william 156 }
871     }
872     }
873     }
874     }
875 william 184 catch (SQLiteException ex)
876     {
877     gLog.Error.WriteLine(ex.ToString());
878     }
879     finally
880     {
881     con.Close();
882     }
883 william 156 }
884     this.Recordings = recordings;
885     }
886     catch (Exception ex)
887     {
888     gLog.Error.WriteLine(ex.ToString());
889     }
890     }
891 william 159
892 william 183 public void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs)
893 william 190 {
894 william 217 gLog.Info.WriteLine("Removing {0} gbpvr programs.", programs.Count);
895 william 220 int ChunkSize = 1024;
896 william 211 var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
897 william 208 double total = programs.Count;
898     double index = 0;
899     double progress = 0;
900 william 211 Stopwatch st1 = new Stopwatch();
901     st1.Start();
902     foreach (var p in ChunkList)
903 william 190 {
904 william 208 progress = 100.0 * (index / total);
905 william 211 if (!RemoveProgramDatabaseEntry(p))
906 william 208 {
907 william 211 gLog.Error.WriteLine("Failed to remove one or more program(s)");
908 william 208 }
909 william 218 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing old gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
910 william 211 index += (double)p.Count();
911 william 190 }
912 william 219 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing old gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
913 william 211 st1.Stop();
914     gLog.Warn.WriteLine(" operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
915 william 183 }
916 william 215 public void UpdateGBPVRPrograms(List<IOldNewProgram> programs)
917 william 183 {
918 william 217 gLog.Info.WriteLine("Updating {0} gbpvr programs.", programs.Count);
919 william 223 int ChunkSize = 4096;
920 william 215 var ChunkList = programs.Chunk<IOldNewProgram>(ChunkSize);
921 william 193
922 william 199 double total = programs.Count;
923     double index = 0;
924 william 208 double progress = 0;
925 william 199 Stopwatch st1 = new Stopwatch();
926     st1.Start();
927 william 196 foreach (var p in ChunkList)
928 william 189 {
929 william 208 progress = 100.0 * (index / total);
930     if (!UpdateProgramEntryDatabase(p))
931 william 193 {
932 william 208 gLog.Error.WriteLine("Failed to update one or more program(s)");
933 william 196 }
934 william 218 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
935 william 201 index += (double)p.Count();
936 william 195 }
937 william 219 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
938 william 199 st1.Stop();
939 william 208 gLog.Warn.WriteLine(" operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
940 william 183 }
941 william 174
942 william 186 private bool RemoveAllProgramDatabaseEntries()
943     {
944     bool result = false;
945     try
946     {
947     using (SQLiteConnection con = CreateConnection())
948     {
949     try
950     {
951     //gLog.Verbose.Info.WriteLine("Removing old program with oid: '{0}'", old_program.oid);
952     con.Open();
953     string command_text = string.Format(@"DELETE FROM [{0}];", TABLES.PROGRAMME);
954     //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
955     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
956     {
957     //cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
958     int rowsupdated = cmd.ExecuteNonQuery();
959     //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
960     }
961     result = true;
962     }
963     catch (SQLiteException ex)
964     {
965     gLog.Error.WriteLine(ex.ToString());
966     result = false;
967     }
968     finally
969     {
970     con.Close();
971     }
972     }
973     }
974     catch (Exception ex)
975     {
976     gLog.Error.WriteLine(ex.ToString());
977     result = false;
978     }
979     return result;
980     }
981 william 211 private bool RemoveProgramDatabaseEntry(IEnumerable<IPROGRAMME> list)
982 william 174 {
983     bool result = false;
984 william 183 try
985     {
986     using (SQLiteConnection con = CreateConnection())
987     {
988     try
989     {
990 william 211 //gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", new_program.oid);
991 william 183 con.Open();
992 william 211 //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
993     string command_text = string.Empty;
994     command_text = BuildGBPVRMultiDeleteCommand(list);
995 william 185 //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
996 william 183 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
997     {
998     int rowsupdated = cmd.ExecuteNonQuery();
999 william 185 //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
1000 william 183 }
1001 william 211
1002 william 183 result = true;
1003     }
1004     catch (SQLiteException ex)
1005     {
1006     gLog.Error.WriteLine(ex.ToString());
1007     result = false;
1008     }
1009 william 184 finally
1010     {
1011     con.Close();
1012     }
1013 william 183 }
1014     }
1015     catch (Exception ex)
1016     {
1017     gLog.Error.WriteLine(ex.ToString());
1018     result = false;
1019     }
1020 william 174 return result;
1021     }
1022 william 215 private bool UpdateProgramEntryDatabase(IEnumerable<IOldNewProgram> list)
1023 william 208 {
1024     bool result = false;
1025     try
1026     {
1027     using (SQLiteConnection con = CreateConnection())
1028     {
1029     try
1030     {
1031     //gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", new_program.oid);
1032     con.Open();
1033     //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
1034     string command_text = string.Empty;
1035     command_text = BuildGBPVRMultiUpdateCommand(list);
1036     //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
1037     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
1038     {
1039     int rowsupdated = cmd.ExecuteNonQuery();
1040     //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
1041     }
1042    
1043     result = true;
1044     }
1045     catch (SQLiteException ex)
1046     {
1047     gLog.Error.WriteLine(ex.ToString());
1048     result = false;
1049     }
1050     finally
1051     {
1052     con.Close();
1053     }
1054     }
1055     }
1056     catch (Exception ex)
1057     {
1058     gLog.Error.WriteLine(ex.ToString());
1059     result = false;
1060     }
1061     return result;
1062     }
1063 william 201 private bool InsertProgramEntryDatabase(IEnumerable<IPROGRAMME> list)
1064 william 186 {
1065     bool result = false;
1066     try
1067     {
1068     using (SQLiteConnection con = CreateConnection())
1069     {
1070     try
1071     {
1072     //gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", new_program.oid);
1073     con.Open();
1074     //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
1075     string command_text = string.Empty;
1076 william 189 command_text = BuildGBPVRMultiInsertCommand(list);
1077 william 186 //gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
1078     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
1079 william 189 {
1080 william 186 int rowsupdated = cmd.ExecuteNonQuery();
1081     //gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
1082     }
1083    
1084     result = true;
1085     }
1086     catch (SQLiteException ex)
1087     {
1088     gLog.Error.WriteLine(ex.ToString());
1089     result = false;
1090     }
1091     finally
1092     {
1093     con.Close();
1094     }
1095     }
1096     }
1097     catch (Exception ex)
1098     {
1099     gLog.Error.WriteLine(ex.ToString());
1100     result = false;
1101     }
1102     return result;
1103     }
1104 william 211
1105     #region Multi-Delete Command Support
1106     private string BuildGBPVRMultiDeleteCommand(IEnumerable<IPROGRAMME> list)
1107     {
1108     StringBuilder builder = new StringBuilder();
1109     /*
1110     insert into table1 (field1,field2) values (value1,value2);
1111     insert into table1 (field1,field2) values (value1,value2);
1112     insert into table1 (field1,field2) values (value1,value2);
1113     insert into table1 (field1,field2) values (value1,value2)
1114     */
1115     builder.AppendLine("begin transaction;");
1116     foreach (var t in list)
1117     {
1118     builder.AppendLine(BuildGBPVRSingleDeleteCommand(t));
1119     }
1120     builder.AppendLine("end transaction;");
1121     return builder.ToString();
1122     }
1123     private string BuildGBPVRSingleDeleteCommand(IPROGRAMME program)
1124     {
1125     StringBuilder builder = new StringBuilder();
1126     builder.AppendFormat("delete from {0} where oid={1};", TABLES.PROGRAMME, program.oid);
1127     return builder.ToString();
1128     }
1129     #endregion
1130 william 208 #region Multi-Update Command Support
1131 william 215 private string BuildGBPVRMultiUpdateCommand(IEnumerable<IOldNewProgram> list)
1132 william 208 {
1133     StringBuilder builder = new StringBuilder();
1134     /*
1135     insert into table1 (field1,field2) values (value1,value2);
1136     insert into table1 (field1,field2) values (value1,value2);
1137     insert into table1 (field1,field2) values (value1,value2);
1138     insert into table1 (field1,field2) values (value1,value2)
1139     */
1140     builder.AppendLine("begin transaction;");
1141     foreach (var t in list)
1142     {
1143 william 216 if (!t.OldProgram.Equals(t.NewProgram))
1144 william 215 {
1145 william 216 // only update the entry if it is different
1146 william 215 builder.AppendLine(BuildGBPVRSingleUpdateCommand(t.NewProgram));
1147     }
1148 william 208 }
1149     builder.AppendLine("end transaction;");
1150     return builder.ToString();
1151     }
1152     private string BuildGBPVRSingleUpdateCommand(IPROGRAMME program)
1153     {
1154     StringBuilder builder = new StringBuilder();
1155     builder.AppendFormat("update {0} SET ", TABLES.PROGRAMME);
1156     builder.AppendFormat("name=\"{0}\", ", program.name);
1157     builder.AppendFormat("sub_title=\"{0}\", ", program.sub_title);
1158     builder.AppendFormat("description=\"{0}\", ", program.description);
1159 william 226 builder.AppendFormat("start_time='{0}', ", program.start_time.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
1160     builder.AppendFormat("end_time='{0}', ", program.end_time.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
1161 william 208 builder.AppendFormat("channel_oid={0}, ", program.channel_oid);
1162     builder.AppendFormat("unique_identifier=\"{0}\", ", program.unique_identifier);
1163     builder.AppendFormat("rating=\"{0}\" ", program.rating);
1164 william 209 builder.AppendFormat("where oid={0};", program.oid);
1165 william 208 return builder.ToString();
1166     }
1167     #endregion
1168     #region Multi-Insert Command Support
1169 william 201 private string BuildGBPVRMultiInsertCommand(IEnumerable<IPROGRAMME> list)
1170 william 174 {
1171 william 189 StringBuilder builder = new StringBuilder();
1172     /*
1173     insert into table1 (field1,field2) values (value1,value2);
1174     insert into table1 (field1,field2) values (value1,value2);
1175     insert into table1 (field1,field2) values (value1,value2);
1176     insert into table1 (field1,field2) values (value1,value2)
1177     */
1178 william 194 builder.AppendLine("begin transaction;");
1179 william 189 foreach (var t in list)
1180 william 183 {
1181 william 189 builder.AppendLine(BuildGBPVRSingleInsertCommand(t));
1182 william 183 }
1183 william 194 builder.AppendLine("end transaction;");
1184 william 189 return builder.ToString();
1185 william 174 }
1186 william 208
1187 william 189 private string BuildGBPVRSingleInsertCommand(IPROGRAMME program)
1188     {
1189     StringBuilder builder = new StringBuilder();
1190     builder.AppendFormat("insert into {0} (oid,name,sub_title,description,start_time,end_time,channel_oid,unique_identifier,rating) values (", TABLES.PROGRAMME);
1191     builder.AppendFormat("{0},",program.oid);
1192     builder.AppendFormat("\"{0}\",", program.name);
1193     builder.AppendFormat("\"{0}\",", program.sub_title);
1194     builder.AppendFormat("\"{0}\",", program.description);
1195 william 206 builder.AppendFormat("'{0}',", program.start_time.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
1196     builder.AppendFormat("'{0}',", program.end_time.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
1197 william 189 builder.AppendFormat("{0},", program.channel_oid);
1198     builder.AppendFormat("\"{0}\",", program.unique_identifier);
1199     builder.AppendFormat("\"{0}\");", program.rating);
1200     return builder.ToString();
1201     }
1202 william 208 #endregion
1203    
1204 william 156 }
1205     }

  ViewVC Help
Powered by ViewVC 1.1.22