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

Contents of /trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 176 - (show annotations) (download)
Sat Mar 16 16:22:43 2013 UTC (7 years, 3 months ago) by william
File size: 35832 byte(s)

1 //#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 using libxmltv;
10 using libxmltv.Interfaces;
11 using libxmltv.Core;
12 namespace GBPVRProgramDatabaseFixer
13 {
14 public interface ISQLLITE
15 {
16 List<SQLLITE.IPROGRAMME> Programs { get; }
17 List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
18 List<SQLLITE.ICHANNEL> Channels { get; }
19 IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
20 List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
21
22 }
23 public class SQLLITE : ISQLLITE
24 {
25 private struct OldNewProgram
26 {
27 public IPROGRAMME OldProgram;
28 public IPROGRAMME NewProgram;
29 }
30 public static ISQLLITE Create(string database, EventHandler<EventArgs> OnInstanceCreated)
31 {
32 return new SQLLITE(database, OnInstanceCreated);
33 }
34
35 #region DATABASE DEFINITIONS
36
37 public interface ICHANNEL
38 {
39 Int64 oid { get; }
40 String name { get; }
41 String channelID { get; }
42 Int64 channel_number { get; }
43 String favourite_channel { get; }
44 String display_name { get; }
45 }
46 private class CHANNEL : ICHANNEL
47 {
48 public CHANNEL()
49 {
50 BaseDatabaseDefinition<CHANNEL>.CreateDefault(this);
51 }
52 //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
53
54 public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index)
55 {
56 BaseDatabaseDefinition<CHANNEL>.Create(ref instance, r, index);
57 }
58 #region ICHANNEL members
59 public Int64 oid { get; set; }
60 public String name { get; set; }
61 public String channelID { get; set; }
62 public Int64 channel_number { get; set; }
63 public String favourite_channel { get; set; }
64 public String display_name { get; set; }
65 #endregion
66 }
67 public interface IRECORDING_SCHEDULE
68 {
69 Int64 oid { get; }
70 Int64 programme_oid { get; }
71 Int64 capture_source_oid { get; }
72 Int16 status { get; }
73 String filename { get; }
74 Int64 recording_type { get; }
75 Int64 recording_group { get; }
76 DateTime manual_start_time { get; }
77 DateTime manual_end_time { get; }
78 Int64 manual_channel_oid { get; }
79 Int64 quality_level { get; }
80 Int64 pre_pad_minutes { get; }
81 Int64 post_pad_minutes { get;}
82 Int32 priority { get; }
83 String conversion_profile { get; }
84 }
85
86 private static class BaseDatabaseDefinition<T>
87 {
88 public static void CreateDefault(T instance)
89 {
90 try
91 {
92 Type t = typeof(T);
93 var props = t.GetProperties();
94 foreach (var prop in props)
95 {
96 Type prop_type = prop.PropertyType;
97 object field_value = null;
98 try
99 {
100 if (prop_type == typeof(string))
101 {
102 field_value = string.Empty;
103 }
104 else
105 {
106 field_value = Activator.CreateInstance(prop_type);
107 }
108 }
109 catch (Exception ex)
110 {
111 throw ex;
112 }
113 prop.SetValue(instance, field_value, null);
114 }
115 }
116 catch (Exception ex)
117 {
118 throw ex;
119 }
120 }
121 public static void Create(ref T instance, SQLiteDataReader r, int index)
122 {
123 string field_name = r.GetName(index);
124 Type field_type = r.GetFieldType(index);
125 object field_value = r.GetValue(index);
126 //gLog.Verbose.Debug.WriteLine("Name: '{0}' Type: '{1}' Value: '{2}'", field_name, field_type.Name, field_value == null ? "null" : field_value.ToString());
127
128 Type t = typeof(T);
129 var props = t.GetProperties();
130 foreach (var prop in props)
131 {
132 if (prop.Name.ToLower() == field_name.ToLower())
133 {
134 if (prop.PropertyType == field_type)
135 {
136 Type db_type = field_value.GetType();
137 try
138 {
139 if (db_type == typeof(System.DBNull))
140 {
141 prop.SetValue(instance, null, null);
142 }
143 else
144 {
145 prop.SetValue(instance, field_value, null);
146 }
147
148 }
149 catch (Exception ex)
150 {
151 throw ex;
152 }
153 }
154 else
155 {
156 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);
157 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));
158 }
159 }
160 }
161
162 }
163 }
164
165 private class RECORDING_SCHEDULE : IRECORDING_SCHEDULE
166 {
167 public RECORDING_SCHEDULE()
168 {
169 BaseDatabaseDefinition<RECORDING_SCHEDULE>.CreateDefault(this);
170 }
171 //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
172
173 public static void Create(ref RECORDING_SCHEDULE instance, SQLiteDataReader r, int index)
174 {
175 BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(ref instance, r, index);
176 }
177
178 #region IRECORDING_SCHEDULE members
179 public Int64 oid { get; set;}
180 public Int64 programme_oid { get; set;}
181 public Int64 capture_source_oid { get; set;}
182 public Int16 status { get; set; }
183 public String filename { get; set; }
184 public Int64 recording_type { get;set; }
185 public Int64 recording_group { get; set;}
186 public DateTime manual_start_time { get; set;}
187 public DateTime manual_end_time { get; set; }
188 public Int64 manual_channel_oid { get; set; }
189 public Int64 quality_level { get; set; }
190 public Int64 pre_pad_minutes { get; set; }
191 public Int64 post_pad_minutes { get; set; }
192 public Int32 priority { get; set; }
193 public String conversion_profile { get; set; }
194 #endregion
195 }
196
197 public interface IPROGRAMME
198 {
199 Int64 oid { get; set; }
200 String name { get; set; }
201 String sub_title { get; set; }
202 String description { get; set; }
203 DateTime start_time { get; set; }
204 DateTime end_time { get; set; }
205 Int64 channel_oid { get; set; }
206 String unique_identifier { get; set; }
207 String rating { get; set; }
208
209 IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite);
210 }
211 private class PROGRAMME : IPROGRAMME
212 {
213 public PROGRAMME()
214 {
215 BaseDatabaseDefinition<PROGRAMME>.CreateDefault(this);
216 }
217 //public PROGRAMME(SQLiteDataReader r, int index) : base(r, index) { }
218 public static void Create(ref PROGRAMME instance, SQLiteDataReader r, int index)
219 {
220 BaseDatabaseDefinition<PROGRAMME>.Create(ref instance, r, index);
221 }
222 #region IPROGRAMME members
223 public Int64 oid { get; set; }
224 public String name { get; set; }
225 public String sub_title { get; set; }
226 public String description { get; set; }
227 public DateTime start_time { get; set; }
228 public DateTime end_time { get; set; }
229 public Int64 channel_oid { get; set; }
230 public String unique_identifier { get; set; }
231 public String rating { get; set; }
232 public IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite)
233 {
234 ProgramList.ProgramDefintion definition = new ProgramList.ProgramDefintion();
235 var channel = sqllite.Channels.Find(s => s.oid == this.channel_oid);
236 definition.ChannelName = channel.display_name;
237 definition.ChannelNumber = (int)channel.channel_number;
238 definition.Description = this.description;
239 definition.Start = this.start_time.ToDateTimeString();
240 definition.Stop = this.end_time.ToDateTimeString();
241 definition.SubTitle = this.sub_title;
242 definition.Title = this.name;
243 return definition;
244 }
245 #endregion
246 }
247 #endregion
248 private static class TABLES
249 {
250 public const string RECORDING_SCHEDULE = "RECORDING_SCHEDULE";
251 public const string PROGRAMME = "PROGRAMME";
252 public const string CHANNEL = "CHANNEL";
253 }
254
255 //public SQLLite() { }
256 protected SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated)
257 {
258 this.OnInstanceCreated = OnInstanceCreated;
259 //CreateConnection(database);
260 this.Database = database;
261 if (!CreateDatabaseBackup())
262 {
263 gLog.Error.WriteLine("Failed to backup database.");
264 return;
265 }
266 ConnectionTest();
267 ReadChannelData();
268 ReadRecodringScheduleData();
269 ReadProgrammeData();
270 OnCreatedInstance(this, new EventArgs());
271 }
272
273 [NonSerialized]
274 private EventHandler<EventArgs> _OnInstanceCreated;
275 private EventHandler<EventArgs> OnInstanceCreated { get { return _OnInstanceCreated; } set { _OnInstanceCreated = value; } }
276
277 private void OnCreatedInstance(object sender, EventArgs e)
278 {
279 if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
280 }
281
282 private string Database;
283 #region ISQLLITE members
284 public List<IPROGRAMME> Programs { get; private set; }
285 public List<IRECORDING_SCHEDULE> Recordings { get; private set; }
286 public List<ICHANNEL> Channels { get; private set; }
287
288 public IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs)
289 {
290 var list = new List<IPROGRAMME>(programs.ToArray());
291 DateTime first = new DateTime();
292 DateTime last = new DateTime();
293 first = list.OrderBy(s => s.start_time).ToList().First().start_time;
294 last = list.OrderBy(s => s.start_time).ToList().Last().start_time;
295 gLog.Verbose.Debug.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
296 gLog.Verbose.Debug.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
297 var range = DateTimeRange.Create(first, last);
298 return range;
299 }
300
301 public List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
302 {
303 source_invalid = new List<IPROGRAMME>();
304 gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();
305 List<SQLLITE.IPROGRAMME> source_valid = new List<IPROGRAMME>();
306
307 List<OldNewProgram> source_update = new List<OldNewProgram>();
308 //if (range == null)
309 //{
310 // gLog.Warn.WriteLine("The DateTimeRange passed in is null...returning the original program list");
311 // return list;
312 //}
313 //gLog.Warn.WriteLine("FilterProgramsByDateRange has not been implemented");
314
315 double total = gbpvr_programs.Count;
316 double index = 0;
317 double progress = 0;
318 foreach (var program in gbpvr_programs)
319 {
320 progress = 100.0 * (index / total);
321 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Filtering GBPVR Programs: {0:00}%", (int)progress)));
322 var channel_oid = program.channel_oid;
323 var channel = this.Channels.Find(s => s.oid == channel_oid);
324 var start_date = program.start_time;
325 //var xmltv_entry_list = xmltv_programs.FindAll(s => s.ChannelNumber == channel.channel_number && s.Start == start_date.ToDateTimeString());
326 var query = from c in xmltv_programs
327 where
328 c.ChannelNumber == channel.channel_number &&
329 c.Start == start_date.ToDateTimeString()
330 select c;
331
332 IProgramDefinition xmltv_entry = null;
333 if (query.Count()-1 > 0)
334 {
335 gLog.Error.WriteLine("Found more than one entry: Matching channel='{0}' and start='{1}'", channel.channel_number, start_date.ToDateTimeString());
336 gLog.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);
337 if (Debugger.IsAttached)
338 {
339 gLog.Error.WriteLine(" Found: {0} matching entries", query.Count());
340 int k_index = 0;
341 foreach (var k in query)
342 {
343 gLog.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);
344 k_index++;
345 }
346 Debugger.Break();
347 }
348 }
349 else
350 {
351 xmltv_entry = query.FirstOrDefault();
352 }
353
354 if (xmltv_entry == null)
355 {
356 gLog.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());
357 source_invalid.Add(program);
358 }
359 else
360 {
361 gLog.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());
362 var updated_program = program;
363 bool needsupdate = false;
364 if (xmltv_entry.Title != program.name)
365 {
366 gLog.Warn.WriteLine(" Updating:");
367 gLog.Warn.WriteLine(" Old Title: {0}", program.name);
368 gLog.Warn.WriteLine(" New Title: {0}", xmltv_entry.Title);
369 updated_program.name = xmltv_entry.Title;
370 needsupdate = true;
371 }
372 if (xmltv_entry.SubTitle != program.sub_title)
373 {
374 gLog.Warn.WriteLine(" Updating:");
375 gLog.Warn.WriteLine(" Old SubTile: {0}", program.sub_title);
376 gLog.Warn.WriteLine(" New SubTile: {0}", xmltv_entry.SubTitle);
377 updated_program.sub_title = xmltv_entry.SubTitle;
378 needsupdate = true;
379 }
380 if (xmltv_entry.Description != program.description)
381 {
382 gLog.Warn.WriteLine(" Updating:");
383 gLog.Warn.WriteLine(" Old Descption: {0}", program.description);
384 gLog.Warn.WriteLine(" New Descption: {0}", xmltv_entry.Description);
385 updated_program.description = xmltv_entry.Description;
386 needsupdate = true;
387 }
388 if (DateTime.Parse(xmltv_entry.Start) != program.start_time)
389 {
390 gLog.Warn.WriteLine(" Updating:");
391 gLog.Warn.WriteLine(" Old StartTime: {0}", program.start_time.ToDateTimeString());
392 gLog.Warn.WriteLine(" New StartTime: {0}", DateTime.Parse(xmltv_entry.Start).ToDateTimeString());
393 updated_program.start_time = DateTime.Parse(xmltv_entry.Start);
394 needsupdate = true;
395 }
396 if (DateTime.Parse(xmltv_entry.Stop) != program.end_time)
397 {
398 gLog.Warn.WriteLine(" Updating:");
399 gLog.Warn.WriteLine(" Old EndTime: {0}", program.end_time.ToDateTimeString());
400 gLog.Warn.WriteLine(" New EndTime: {0}", DateTime.Parse(xmltv_entry.Stop).ToDateTimeString());
401 updated_program.end_time = DateTime.Parse(xmltv_entry.Stop);
402 needsupdate = true;
403 }
404 source_valid.Add(updated_program);
405 if (needsupdate)
406 {
407 //// update database entry
408 //if (!UpdateProgramDatabaseEntry(program, updated_program))
409 //{
410 // gLog.Error.WriteLine("Failed to update entry with oid: '{0}'", program.oid);
411 //}
412 OldNewProgram p = new OldNewProgram();
413 p.OldProgram = program;
414 p.NewProgram = updated_program;
415 source_update.Add(p);
416 }
417
418 }
419 index++;
420 }
421
422
423 source_valid = source_valid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
424 source_invalid = source_invalid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
425
426 if (source_invalid.Count > 0)
427 {
428 foreach (var old_program in source_invalid)
429 {
430 // remove database entry
431 if (!RemoveProgramDatabaseEntry(old_program))
432 {
433 gLog.Error.WriteLine("Failed to remove program with oid: '{0}'", old_program.oid);
434 }
435 else
436 {
437 gLog.Info.WriteLine("Removed program with oid: '{0}'", old_program.oid);
438 }
439 }
440 }
441 else
442 {
443 gLog.Info.WriteLine("No old GB-PVR Programs needed to be removed.");
444 }
445
446 if (source_update.Count > 0)
447 {
448 foreach (var p in source_update)
449 {
450 // remove database entry
451 if (!UpdateProgramDatabaseEntry(p.OldProgram,p.NewProgram))
452 {
453 gLog.Error.WriteLine("Failed to update program with oid: '{0}'", p.OldProgram.oid);
454 }
455 else
456 {
457 gLog.Info.WriteLine("Upated program with oid: '{0}'", p.OldProgram.oid);
458 }
459 }
460 }
461 else
462 {
463 gLog.Info.WriteLine("No GB-PVR Programs needed to be updated.");
464 }
465
466 gLog.Info.WriteLine("Total XMLTV Programs: 0x{0:x8}", xmltv_programs.Count);
467 gLog.Info.WriteLine("Updated: 0x{0:x8} GB-PVR Programs", source_valid.Count);
468 gLog.Info.WriteLine("Removed: 0x{0:x8} GB-PVR Programs", source_invalid.Count);
469 gLog.Info.WriteLine("Total GB-PVR Programs (Updated & Removed): 0x{0:x8}", source_valid.Count + source_invalid.Count);
470
471 return source_valid;
472 }
473 #endregion
474
475
476 private string CreateConnectionString()
477 {
478 string connection_string = string.Format("Data Source={0}", this.Database);
479 return connection_string;
480 }
481
482 private SQLiteConnection CreateConnection() { SQLiteConnection connection = new SQLiteConnection(CreateConnectionString()); return connection; }
483
484 [Conditional("SQLLITE_CONNECTION_TEST")]
485 private void ConnectionTest()
486 {
487 try
488 {
489 using (SQLiteConnection con = CreateConnection())
490 {
491 con.Open();
492 string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
493 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
494 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
495 {
496 using (SQLiteDataReader r = cmd.ExecuteReader())
497 {
498 if (!r.HasRows)
499 {
500 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
501 }
502 else
503 {
504 while (r.Read())
505 {
506 for (int i = 0; i < r.FieldCount; i++)
507 {
508 string field_name = r.GetName(i);
509 Type field_type = r.GetFieldType(i);
510 object field_value = r.GetValue(i);
511 gLog.Verbose.Debug.WriteLine("Name: '{0}' Type: '{1}' Value: '{2}'", field_name, field_type.Name, field_value == null ? "null" : field_value.ToString());
512 }
513 break;
514 }
515 }
516 }
517 }
518 con.Clone();
519 }
520 OnCreatedInstance(this, new EventArgs());
521 }
522 catch (Exception ex)
523 {
524 gLog.Error.WriteLine(ex.ToString());
525 }
526 }
527
528
529 private bool CreateDatabaseBackup()
530 {
531 try
532 {
533 string backup_file = string.Format("{0}.{1}", this.Database, DateTime.Now.ToString("yyyyMMddHHmmss"));
534 gLog.Info.WriteLine("Creating Database backup...");
535 gLog.Info.WriteLine("\tSource: {0}", this.Database);
536 gLog.Info.WriteLine("\tDestination: {0}", backup_file);
537
538 System.IO.File.Copy(this.Database, backup_file);
539 return true;
540 }
541 catch (Exception ex)
542 {
543 gLog.Error.WriteLine(ex.ToString());
544 return false;
545 }
546 }
547 private void ReadChannelData()
548 {
549 try
550 {
551 List<ICHANNEL> channels = new List<ICHANNEL>();
552 using (SQLiteConnection con = CreateConnection())
553 {
554 con.Open();
555 string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
556 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
557 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
558 {
559 using (SQLiteDataReader r = cmd.ExecuteReader())
560 {
561 if (!r.HasRows)
562 {
563 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
564 }
565 else
566 {
567 while (r.Read())
568 {
569 CHANNEL channel = new CHANNEL();
570 for (int i = 0; i < r.FieldCount; i++)
571 {
572 CHANNEL.Create(ref channel, r, i);
573 }
574 channels.Add(channel);
575 }
576 }
577 }
578 }
579 con.Clone();
580 }
581 this.Channels = channels;
582 }
583 catch (Exception ex)
584 {
585 gLog.Error.WriteLine(ex.ToString());
586 }
587 }
588 private void ReadProgrammeData()
589 {
590 try
591 {
592 List<IPROGRAMME> programs = new List<IPROGRAMME>();
593 using (SQLiteConnection con = CreateConnection())
594 {
595 con.Open();
596 string command_text = string.Format("select * from {0};", TABLES.PROGRAMME);
597 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
598 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
599 {
600 using (SQLiteDataReader r = cmd.ExecuteReader())
601 {
602 if (!r.HasRows)
603 {
604 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
605 }
606 else
607 {
608 while (r.Read())
609 {
610 PROGRAMME program = new PROGRAMME();
611 for (int i = 0; i < r.FieldCount; i++)
612 {
613 PROGRAMME.Create(ref program, r, i);
614 }
615 programs.Add(program);
616 }
617 }
618 }
619 }
620 con.Clone();
621 }
622 this.Programs = programs;
623 }
624 catch (Exception ex)
625 {
626 gLog.Error.WriteLine(ex.ToString());
627 }
628 }
629 private void ReadRecodringScheduleData()
630 {
631 try
632 {
633 List<IRECORDING_SCHEDULE> recordings = new List<IRECORDING_SCHEDULE>();
634 using (SQLiteConnection con = CreateConnection())
635 {
636 con.Open();
637 string command_text = string.Format("select * from {0};", TABLES.RECORDING_SCHEDULE);
638 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
639 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
640 {
641 using (SQLiteDataReader r = cmd.ExecuteReader())
642 {
643 if (!r.HasRows)
644 {
645 gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
646 }
647 else
648 {
649 while (r.Read())
650 {
651 RECORDING_SCHEDULE recording = new RECORDING_SCHEDULE();
652 for (int i = 0; i < r.FieldCount; i++)
653 {
654 RECORDING_SCHEDULE.Create(ref recording, r, i);
655 }
656 recordings.Add(recording);
657 }
658 }
659 }
660 }
661 con.Clone();
662 }
663 this.Recordings = recordings;
664 }
665 catch (Exception ex)
666 {
667 gLog.Error.WriteLine(ex.ToString());
668 }
669 }
670
671
672
673 private bool RemoveProgramDatabaseEntry(IPROGRAMME old_program)
674 {
675 bool result = false;
676 try
677 {
678 using (SQLiteConnection con = CreateConnection())
679 {
680 try
681 {
682 gLog.Verbose.Info.WriteLine("Removing old program with oid: '{0}'", old_program.oid);
683 con.Open();
684 string command_text = string.Format(@"DELETE FROM [{0}] WHERE [oid] = @oid;", TABLES.PROGRAMME);
685 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
686 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
687 {
688 cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
689 int rowsupdated = cmd.ExecuteNonQuery();
690 gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
691 }
692 con.Clone();
693 result = true;
694 }
695 catch (SQLiteException ex)
696 {
697 gLog.Error.WriteLine(ex.ToString());
698 result = false;
699 }
700 }
701 }
702 catch (Exception ex)
703 {
704 gLog.Error.WriteLine(ex.ToString());
705 result = false;
706 }
707 return result;
708 }
709 private bool UpdateProgramDatabaseEntry(IPROGRAMME old_program, IPROGRAMME new_program)
710 {
711 bool result = false;
712 try
713 {
714 using (SQLiteConnection con = CreateConnection())
715 {
716 try
717 {
718 gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", old_program.oid);
719 con.Open();
720 //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
721 string command_text = string.Empty;
722 StringBuilder builder = new StringBuilder();
723
724 builder.AppendLine(string.Format("UPDATE [{0}]", TABLES.PROGRAMME));
725 builder.Append("SET ");
726 builder.AppendFormat("[name]=@name,");
727 builder.AppendFormat("[sub_title]=@sub_title,");
728 builder.AppendFormat("[description]=@description,");
729 builder.AppendFormat("[start_time]=@start_time,");
730 builder.AppendFormat("[end_time]=@end_time,");
731 builder.AppendFormat("[channel_oid]=@channel_oid,");
732 builder.AppendFormat("[unique_identifier]=@unique_identifier,");
733 builder.AppendFormat("[rating]=@rating");
734 builder.AppendLine(" WHERE [oid] = @oid");
735
736 command_text = builder.ToString();
737 gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
738 using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
739 {
740 cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
741 cmd.Parameters.Add(new SQLiteParameter("name", new_program.name));
742 cmd.Parameters.Add(new SQLiteParameter("sub_title", new_program.sub_title));
743 cmd.Parameters.Add(new SQLiteParameter("description", new_program.description));
744 cmd.Parameters.Add(new SQLiteParameter("start_time", new_program.start_time));
745 cmd.Parameters.Add(new SQLiteParameter("end_time", new_program.end_time));
746 cmd.Parameters.Add(new SQLiteParameter("channel_oid", new_program.channel_oid));
747 cmd.Parameters.Add(new SQLiteParameter("unique_identifier", new_program.unique_identifier));
748 cmd.Parameters.Add(new SQLiteParameter("rating", new_program.rating));
749 int rowsupdated = cmd.ExecuteNonQuery();
750 gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
751 }
752 con.Clone();
753 result = true;
754 }
755 catch (SQLiteException ex)
756 {
757 gLog.Error.WriteLine(ex.ToString());
758 result = false;
759 }
760 }
761 }
762 catch (Exception ex)
763 {
764 gLog.Error.WriteLine(ex.ToString());
765 result = false;
766 }
767 return result;
768 }
769 }
770 }

  ViewVC Help
Powered by ViewVC 1.1.22