summaryrefslogtreecommitdiff
path: root/database/database.h
blob: 5bb595fb4272ab43d41c54ddf20b8eb6869d2ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
/* 
 * File:   database.h
 * Author: savop
 *
 * Created on 3. September 2009, 22:20
 */

#ifndef _DATABASE_H
#define	_DATABASE_H

#include <sqlite3.h>
#include <vdr/tools.h>
#include "../common.h"

#define SQLITE_CASCADE_DELETES

#define PK_OBJECTS                      TOSTRING(1)
#define PK_RESOURCES                    TOSTRING(2)
#define PK_SEARCHCLASSES                TOSTRING(3)

#define SQLITE_FIRST_CUSTOMID           TOSTRING(100)

#define SQLITE_COLUMN_NAME_LENGTH       64

#define SQLITE_TABLE_RESOURCES          "Resources"
#define SQLITE_TABLE_OBJECTS            "Objects"
#define SQLITE_TABLE_ITEMS              "Items"
#define SQLITE_TABLE_CONTAINERS         "Containers"
#define SQLITE_TABLE_VIDEOITEMS         "VideoItems"
#define SQLITE_TABLE_AUDIOITEMS         "AudioItems"
#define SQLITE_TABLE_IMAGEITEMS         "ImageItems"
#define SQLITE_TABLE_VIDEOBROADCASTS    "VideoBroadcasts"
#define SQLITE_TABLE_AUDIOBROADCASTS    "AudioBroadcasts"
#define SQLITE_TABLE_MOVIES             "Movies"
#define SQLITE_TABLE_PHOTOS             "Photos"
#define SQLITE_TABLE_ALBUMS             "Albums"
#define SQLITE_TABLE_PLAYLISTS          "Playlists"
#define SQLITE_TABLE_SEARCHCLASS        "SearchClass"
#define SQLITE_TABLE_PRIMARY_KEYS       "PrimaryKeys"
#define SQLITE_TABLE_SYSTEM             "System"
#define SQLITE_TABLE_ITEMFINDER         "ItemFinder"

#define SQLITE_TYPE_TEXT                "TEXT"
#define SQLITE_TYPE_INTEGER             "INTEGER"
#define SQLITE_TYPE_BOOL                SQLITE_TYPE_INTEGER
#define SQLITE_TYPE_DATE                SQLITE_TYPE_TEXT
#define SQLITE_TYPE_ULONG               SQLITE_TYPE_INTEGER
#define SQLITE_TYPE_LONG                SQLITE_TYPE_INTEGER
#define SQLITE_TYPE_UINTEGER            SQLITE_TYPE_INTEGER

#define SQLITE_TRANSACTION_BEGIN        "BEGIN IMMEDIATE TRANSACTION "
#define SQLITE_TRANSACTION_END          "COMMIT TRANSACTION"
#define SQLITE_TRANSACTION_TYPE         "ROLLBACK"

#define SQLITE_CONFLICT_CLAUSE          "ON CONFLICT " SQLITE_TRANSACTION_TYPE
#define SQLITE_PRIMARY_KEY              SQLITE_TYPE_INTEGER " PRIMARY KEY"
#define SQLITE_NOT_NULL                 "NOT NULL"
#define SQLITE_UNIQUE                   "UNIQUE"

#define SQLITE_COL_OBJECTID             "ObjectID"
#define SQLITE_COL_PARENTID             "ParentID"
#define SQLITE_COL_TITLE                "Title"
#define SQLITE_COL_CREATOR              "Creator"
#define SQLITE_COL_CLASS                "Class"
#define SQLITE_COL_RESTRICTED           "Restricted"
#define SQLITE_COL_WRITESTATUS          "WriteStatus"
#define SQLITE_COL_REFERENCEID          "RefID"
#define SQLITE_COL_CLASSDERIVED         "IncludeDerived"
#define SQLITE_COL_SEARCHABLE           "Searchable"
#define SQLITE_COL_CONTAINER_UID        "UpdateID"
#define SQLITE_COL_RESOURCEID           "ResourceID"
#define SQLITE_COL_PROTOCOLINFO         "ProtocolInfo"
#define SQLITE_COL_CONTENTTYPE          "ContentType"
#define SQLITE_COL_RESOURCETYPE         "ResourceType"
#define SQLITE_COL_RESOURCE             "Resource"
#define SQLITE_COL_SIZE                 "Size"
#define SQLITE_COL_DURATION             "Duration"
#define SQLITE_COL_BITRATE              "Bitrate"
#define SQLITE_COL_SAMPLEFREQUENCE      "SampleFreq"
#define SQLITE_COL_BITSPERSAMPLE        "BitsPerSample"
#define SQLITE_COL_NOAUDIOCHANNELS      "NoAudioChannels"
#define SQLITE_COL_COLORDEPTH           "ColorDepth"
#define SQLITE_COL_RESOLUTION           "Resolution"
#define SQLITE_COL_GENRE                "Genre"
#define SQLITE_COL_LONGDESCRIPTION      "LongDescription"
#define SQLITE_COL_PRODUCER             "Producer"
#define SQLITE_COL_RATING               "Rating"
#define SQLITE_COL_ACTOR                "Actor"
#define SQLITE_COL_DIRECTOR             "Director"
#define SQLITE_COL_DESCRIPTION          "Description"
#define SQLITE_COL_PUBLISHER            "Publisher"
#define SQLITE_COL_LANGUAGE             "Language"
#define SQLITE_COL_RELATION             "Relation"
#define SQLITE_COL_STORAGEMEDIUM        "StorageMedium"
#define SQLITE_COL_DVDREGIONCODE        "DVDRegionCode"
#define SQLITE_COL_CHANNELNAME          "Channelname"
#define SQLITE_COL_SCHEDULEDSTARTTIME   "ScheduledStartTime"
#define SQLITE_COL_SCHEDULEDENDTIME     "ScheduledEndTime"
#define SQLITE_COL_ICON                 "Icon"
#define SQLITE_COL_REGION               "Region"
#define SQLITE_COL_CHANNELNR            "ChannelNr"
#define SQLITE_COL_RIGHTS               "Rights"
#define SQLITE_COL_RADIOCALLSIGN        "CallSign"
#define SQLITE_COL_RADIOSTATIONID       "StationID"
#define SQLITE_COL_RADIOBAND            "Band"
#define SQLITE_COL_CONTRIBUTOR          "Contributor"
#define SQLITE_COL_DATE                 "Date"
#define SQLITE_COL_ALBUM                "Album"
#define SQLITE_COL_ARTIST               "Artist"
#define SQLITE_COL_DLNA_CONTAINERTYPE   "DLNAContainer"
#define SQLITE_COL_CHILDCOUNT           "ChildCount"
#define SQLITE_COL_ITEMFINDER           "ItemFastID"

#define SQLITE_UPNP_OBJECTID            SQLITE_COL_OBJECTID " " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL " " SQLITE_CONFLICT_CLAUSE " "\
                                                                SQLITE_UNIQUE " " SQLITE_CONFLICT_CLAUSE

#define SQLITE_INSERT_TRIGGER(TableA,TableB,Class)      "CREATE TRIGGER IF NOT EXISTS "\
                                                        TableA "_I_" TableB " "\
                                                        "BEFORE INSERT ON "\
                                                        TableB " "\
                                                        "FOR EACH ROW BEGIN "\
                                                        "SELECT CASE "\
                                                        "WHEN ("\
                                                        "((SELECT " SQLITE_COL_OBJECTID " FROM " TableA " "\
                                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID " "\
                                                        ") IS NULL) "\
                                                        "OR "\
                                                        "((SELECT " SQLITE_COL_OBJECTID " FROM " SQLITE_TABLE_OBJECTS " "\
                                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID " "\
                                                        "AND " SQLITE_COL_CLASS " LIKE '" Class "%%') IS NULL) "\
                                                        ") THEN "\
                                                        "RAISE(" SQLITE_TRANSACTION_TYPE ", "\
                                                        "'INSERT on table " TableB " failed due constraint violation "\
                                                        "on foreign key " SQLITE_COL_OBJECTID "'"\
                                                        ") "\
                                                        "END; END;"

#define SQLITE_UPDATE_TRIGGER(TableA,TableB,Class)      "CREATE TRIGGER IF NOT EXISTS "\
                                                        TableA "_U_" TableB " "\
                                                        "BEFORE UPDATE ON "\
                                                        TableB " "\
                                                        "FOR EACH ROW BEGIN "\
                                                        "SELECT CASE "\
                                                        "WHEN ("\
                                                        "((SELECT " SQLITE_COL_OBJECTID " FROM " SQLITE_TABLE_OBJECTS " "\
                                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID " "\
                                                        "AND " SQLITE_COL_CLASS " LIKE '" Class "%%') IS NULL)"\
                                                        ") THEN "\
                                                        "RAISE(" SQLITE_TRANSACTION_TYPE ", "\
                                                        "'UPDATE on table " TableB " failed due constraint violation "\
                                                        "on foreign key " SQLITE_COL_OBJECTID "'"\
                                                        ") "\
                                                        "END; END;"

#define SQLITE_INSERT_REFERENCE_TRIGGER(Table,Column)   "CREATE TRIGGER IF NOT EXISTS "\
                                                        Table "_I_" Table " "\
                                                        "BEFORE INSERT ON " \
                                                        Table " " \
                                                        "FOR EACH ROW BEGIN "\
                                                        "SELECT CASE "\
                                                        "WHEN ( "\
                                                        "((SELECT " SQLITE_COL_OBJECTID " FROM " Table " "\
                                                        "WHERE " SQLITE_COL_OBJECTID " = NEW." Column ") IS NULL) "\
                                                        "AND "\
                                                        "(NEW." Column "!=-1)"\
                                                        ")THEN "\
                                                        "RAISE(" SQLITE_TRANSACTION_TYPE ", 'INSERT on table " Table " "\
                                                        "violates foreign key \"" Column "\"') "\
                                                        "END; END;"

#define SQLITE_UPDATE_REFERENCE_TRIGGER(Table,Column)   "CREATE TRIGGER IF NOT EXISTS "\
                                                        Table "_U_" Table " "\
                                                        "BEFORE INSERT ON " \
                                                        Table " " \
                                                        "FOR EACH ROW BEGIN "\
                                                        "SELECT CASE "\
                                                        "WHEN ( "\
                                                        "((SELECT " SQLITE_COL_OBJECTID " FROM " Table " "\
                                                        "WHERE " SQLITE_COL_OBJECTID " = NEW." Column ") IS NULL) "\
                                                        "AND "\
                                                        "(NEW." Column "!=-1)"\
                                                        ")THEN "\
                                                        "RAISE(" SQLITE_TRANSACTION_TYPE ", 'UPDATE on table " Table " "\
                                                        "violates foreign key \"" Column "\"') "\
                                                        "END; END;"

#define SQLITE_DELETE_REFERENCE_TRIGGER(Table,Column)   "CREATE TRIGGER IF NOT EXISTS "\
                                                        Table "_D_" Table " " \
                                                        "BEFORE DELETE ON " \
                                                        Table " " \
                                                        "FOR EACH ROW BEGIN "\
                                                        "SELECT CASE "\
                                                        "WHEN ("\
                                                        "(SELECT " Column " FROM " Table " "\
                                                        "WHERE " Column " = OLD." SQLITE_COL_OBJECTID ") IS NOT NULL"\
                                                        ")THEN "\
                                                        "RAISE(" SQLITE_TRANSACTION_TYPE ", 'DELETE on table " Table " "\
                                                        "violates foreign key \"" Column "\"') "\
                                                        "END; END;"

#ifdef SQLITE_CASCADE_DELETES
#define SQLITE_DELETE_TRIGGER(TableA,TableB)            "CREATE TRIGGER IF NOT EXISTS "\
                                                        TableA "_D_" TableB " "\
                                                        "BEFORE DELETE ON "\
                                                        TableA " "\
                                                        "FOR EACH ROW BEGIN "\
                                                        "DELETE FROM " TableB " "\
                                                        "WHERE " SQLITE_COL_OBJECTID "=OLD." SQLITE_COL_OBJECTID "; "\
                                                        "END;"

#define SQLITE_DELETE_PARENT_TRIGGER                    "CREATE TRIGGER IF NOT EXISTS "\
                                                        SQLITE_TABLE_OBJECTS "_D_" SQLITE_TABLE_OBJECTS " " \
                                                        "BEFORE DELETE ON " \
                                                        SQLITE_TABLE_OBJECTS " " \
                                                        "FOR EACH ROW BEGIN "\
                                                        "DELETE FROM " SQLITE_TABLE_OBJECTS " "\
                                                        "WHERE " SQLITE_COL_PARENTID "=OLD." SQLITE_COL_OBJECTID "; "\
                                                        "END;"
#else
#define SQLITE_DELETE_TRIGGER(TableA,TableB)            "CREATE TRIGGER IF NOT EXISTS "\
                                                        TableA "_D_" TableB " "\
                                                        "BEFORE DELETE ON "\
                                                        TableA " "\
                                                        "FOR EACH ROW BEGIN "\
                                                        "SELECT CASE "\
                                                        "WHEN ("\
                                                        "(SELECT " SQLITE_COL_OBJECTID " FROM " TableB " "\
                                                        "WHERE " SQLITE_COL_OBJECTID "=OLD." SQLITE_COL_OBJECTID ") IS NOT NULL"\
                                                        ") THEN "\
                                                        "RAISE(" SQLITE_TRANSACTION_TYPE ", "\
                                                        "'DELETE on table " TableA " failed due constraint violation "\
                                                        "on foreign key " SQLITE_COL_OBJECTID "'"\
                                                        ") "\
                                                        "END; END;"

#define SQLITE_DELETE_PARENT_TRIGGER                    SQLITE_DELETE_REFERENCE_TRIGGER(SQLITE_TABLE_OBJECTS, SQLITE_COL_PARENTID)
#endif

/**********************************************\
*                                              *
*  Primary keys                                *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_PRIMARY_KEYS    "CREATE TABLE IF NOT EXISTS "\
                                            SQLITE_TABLE_PRIMARY_KEYS \
                                            "("\
                                            "KeyID " SQLITE_PRIMARY_KEY " " SQLITE_NOT_NULL ","\
                                            "Key " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL\
                                            ");"\
                                            "INSERT OR IGNORE INTO "\
                                            SQLITE_TABLE_PRIMARY_KEYS \
                                            "(KeyID, Key) VALUES ("\
                                            PK_OBJECTS "," SQLITE_FIRST_CUSTOMID\
                                            ");"\
                                            "INSERT OR IGNORE INTO "\
                                            SQLITE_TABLE_PRIMARY_KEYS \
                                            "(KeyID, Key) VALUES ("\
                                            PK_RESOURCES ",0"\
                                            ");"\
                                            "INSERT OR IGNORE INTO "\
                                            SQLITE_TABLE_PRIMARY_KEYS \
                                            "(KeyID, Key) VALUES ("\
                                            PK_SEARCHCLASSES ",0"\
                                            ");"

#define SQLITE_TRIGGER_UPDATE_OBJECTID      "CREATE TRIGGER IF NOT EXISTS "\
                                            SQLITE_TABLE_OBJECTS "_PK_UPDATE "\
                                            "AFTER INSERT ON "\
                                            SQLITE_TABLE_OBJECTS " "\
                                            "BEGIN "\
                                            "UPDATE " SQLITE_TABLE_PRIMARY_KEYS " SET Key=Key+1 WHERE KeyID=" PK_OBJECTS "; "\
                                            "END;"

/**********************************************\
*                                              *
*  System settings                             *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_SYSTEM          "CREATE TABLE IF NOT EXISTS "\
                                            SQLITE_TABLE_SYSTEM " "\
                                            "("\
                                            "Key " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL " " SQLITE_UNIQUE ","\
                                            "Value " SQLITE_TYPE_TEXT " "\
                                            ");"

#define SQLITE_TRIGGER_UPDATE_SYSTEM        "CREATE TRIGGER IF NOT EXISTS "\
                                            SQLITE_TABLE_SYSTEM "_VALUE_UPDATE "\
                                            "BEFORE UPDATE "\
                                            "ON " SQLITE_TABLE_SYSTEM " "\
                                            "WHEN ((SELECT Key FROM " SQLITE_TABLE_SYSTEM " WHERE Key=NEW.Key) IS NULL) "\
                                            "BEGIN INSERT INTO " SQLITE_TABLE_SYSTEM " (Key) VALUES (NEW.Key); END;"

/**********************************************\
*                                              *
*  Fast item finder                            *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_ITEMFINDER      "CREATE TABLE IF NOT EXISTS "\
                                            SQLITE_TABLE_ITEMFINDER " "\
                                            "("\
                                            SQLITE_UPNP_OBJECTID ","\
                                            SQLITE_COL_ITEMFINDER " " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL " " SQLITE_UNIQUE \
                                            ");"

#define SQLITE_TRIGGER_D_OBJECTS_ITEMFINDER SQLITE_DELETE_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                                  SQLITE_TABLE_ITEMFINDER)

/**********************************************\
*                                              *
*  Objects                                     *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_OBJECTS     "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_OBJECTS \
                                        "(" \
                                        SQLITE_COL_OBJECTID " " SQLITE_PRIMARY_KEY " " SQLITE_NOT_NULL " " SQLITE_CONFLICT_CLAUSE "," \
                                        SQLITE_COL_PARENTID " " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL " " SQLITE_CONFLICT_CLAUSE "," \
                                        SQLITE_COL_TITLE    " " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_CREATOR  " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_CLASS    " " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_RESTRICTED " " SQLITE_TYPE_BOOL " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_WRITESTATUS " " SQLITE_TYPE_INTEGER \
                                        ");"

// Trigger for foreign key ParentID

#define SQLITE_TRIGGER_D_OBJECTS_OBJECTS SQLITE_DELETE_PARENT_TRIGGER

#define SQLITE_TRIGGER_I_OBJECTS_OBJECTS SQLITE_INSERT_REFERENCE_TRIGGER(SQLITE_TABLE_OBJECTS, SQLITE_COL_PARENTID)\
                                         "CREATE TRIGGER IF NOT EXISTS "\
                                         SQLITE_TABLE_OBJECTS "_PI_" SQLITE_TABLE_OBJECTS " "\
                                         "BEFORE INSERT ON "\
                                         SQLITE_TABLE_OBJECTS " " \
                                         "FOR EACH ROW BEGIN "\
                                         "SELECT CASE "\
                                         "WHEN ("\
                                         "((SELECT " SQLITE_COL_PARENTID " FROM " SQLITE_TABLE_OBJECTS " "\
                                         "WHERE " SQLITE_COL_PARENTID "=-1) IS NOT NULL) "\
                                         "AND "\
                                         "(NEW." SQLITE_COL_PARENTID "=-1)"\
                                         ") THEN "\
                                         "RAISE(" SQLITE_TRANSACTION_TYPE ","\
                                         "'INSERT on table " SQLITE_TABLE_OBJECTS " violates constraint. "\
                                         SQLITE_COL_PARENTID " must uniquely be -1') "\
                                         "END; END;"

#define SQLITE_TRIGGER_U_OBJECTS_OBJECTS SQLITE_UPDATE_REFERENCE_TRIGGER(SQLITE_TABLE_OBJECTS, SQLITE_COL_PARENTID)\
                                         "CREATE TRIGGER IF NOT EXISTS "\
                                         SQLITE_TABLE_OBJECTS "_PU_" SQLITE_TABLE_OBJECTS " "\
                                         "BEFORE UPDATE ON "\
                                         SQLITE_TABLE_OBJECTS " " \
                                         "FOR EACH ROW BEGIN "\
                                         "SELECT CASE "\
                                         "WHEN ("\
                                         "((SELECT " SQLITE_COL_PARENTID " FROM " SQLITE_TABLE_OBJECTS " "\
                                         "WHERE " SQLITE_COL_PARENTID "=-1 "\
                                         "AND " SQLITE_COL_OBJECTID "!=NEW." SQLITE_COL_OBJECTID " ) IS NOT NULL) "\
                                         "AND "\
                                         "(NEW." SQLITE_COL_PARENTID "=-1) AND (OLD." SQLITE_COL_PARENTID "!=-1) "\
                                         ") THEN "\
                                         "RAISE(" SQLITE_TRANSACTION_TYPE ","\
                                         "'UPDATE on table " SQLITE_TABLE_OBJECTS " violates constraint. "\
                                         SQLITE_COL_PARENTID " must uniquely be -1') "\
                                         "END; END;"

/**********************************************\
*                                              *
*  Items                                       *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_ITEMS       "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_ITEMS \
                                        "(" \
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_REFERENCEID " " SQLITE_TYPE_INTEGER " DEFAULT -1" \
                                        ");"

// Trigger for foreign key ObjectID

#define SQLITE_TRIGGER_D_OBJECT_ITEMS   SQLITE_DELETE_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                              SQLITE_TABLE_ITEMS)

#define SQLITE_TRIGGER_I_OBJECT_ITEMS   SQLITE_INSERT_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                              SQLITE_TABLE_ITEMS,\
                                                              UPNP_CLASS_ITEM)

#define SQLITE_TRIGGER_U_OBJECT_ITEMS   SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                              SQLITE_TABLE_ITEMS,\
                                                              UPNP_CLASS_ITEM)

// Trigger for Reference items

#define SQLITE_TRIGGER_I_ITEMS_ITEMS    SQLITE_INSERT_REFERENCE_TRIGGER(SQLITE_TABLE_ITEMS, SQLITE_COL_REFERENCEID)

#define SQLITE_TRIGGER_U_ITEMS_ITEMS    SQLITE_UPDATE_REFERENCE_TRIGGER(SQLITE_TABLE_ITEMS, SQLITE_COL_REFERENCEID)

#define SQLITE_TRIGGER_D_ITEMS_ITEMS    SQLITE_DELETE_REFERENCE_TRIGGER(SQLITE_TABLE_ITEMS, SQLITE_COL_REFERENCEID)

/**********************************************\
*                                              *
*  Containers                                  *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_CONTAINER   "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_CONTAINERS \
                                        "(" \
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_SEARCHABLE " " SQLITE_TYPE_INTEGER ","\
                                        SQLITE_COL_CONTAINER_UID " " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL ","\
                                        SQLITE_COL_DLNA_CONTAINERTYPE " " SQLITE_TYPE_TEXT \
                                        ");"

#define SQLITE_TRIGGER_D_OBJECT_CONTAINERS  SQLITE_DELETE_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                                  SQLITE_TABLE_CONTAINERS)

#define SQLITE_TRIGGER_I_OBJECT_CONTAINERS  SQLITE_INSERT_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                                  SQLITE_TABLE_CONTAINERS,\
                                                                  UPNP_CLASS_CONTAINER)

#define SQLITE_TRIGGER_U_OBJECT_CONTAINERS  SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_OBJECTS,\
                                                                  SQLITE_TABLE_CONTAINERS,\
                                                                  UPNP_CLASS_CONTAINER)

/**********************************************\
*                                              *
*  Video items                                 *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_VIDEOITEMS  "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_VIDEOITEMS \
                                        "(" \
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_GENRE " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_LONGDESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_PRODUCER " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_RATING " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_ACTOR " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DIRECTOR " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_PUBLISHER " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_LANGUAGE " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_RELATION " " SQLITE_TYPE_TEXT \
                                        ");"

#define SQLITE_TRIGGER_D_ITEMS_VIDEOITEMS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_ITEMS, SQLITE_TABLE_VIDEOITEMS)

#define SQLITE_TRIGGER_U_ITEMS_VIDEOITEMS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_ITEMS, \
                                                                SQLITE_TABLE_VIDEOITEMS, \
                                                                UPNP_CLASS_VIDEO)

#define SQLITE_TRIGGER_I_ITEMS_VIDEOITEMS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_ITEMS, \
                                                                SQLITE_TABLE_VIDEOITEMS, \
                                                                UPNP_CLASS_VIDEO)

/**********************************************\
*                                              *
*  Audio items                                 *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_AUDIOITEMS  "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_AUDIOITEMS \
                                        "(" \
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_GENRE " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_LONGDESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_PUBLISHER " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_RELATION " " SQLITE_TYPE_TEXT \
                                        ");"

#define SQLITE_TRIGGER_D_ITEMS_AUDIOITEMS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_ITEMS, SQLITE_TABLE_AUDIOITEMS)

#define SQLITE_TRIGGER_U_ITEMS_AUDIOITEMS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_ITEMS, \
                                                                SQLITE_TABLE_AUDIOITEMS, \
                                                                UPNP_CLASS_AUDIO)

#define SQLITE_TRIGGER_I_ITEMS_AUDIOITEMS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_ITEMS, \
                                                                SQLITE_TABLE_AUDIOITEMS, \
                                                                UPNP_CLASS_AUDIO)

/**********************************************\
*                                              *
*  Image items                                 *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_IMAGEITEMS  "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_IMAGEITEMS \
                                        "("\
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_LONGDESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_PUBLISHER " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_STORAGEMEDIUM " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_RATING " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_DATE " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_RIGHTS " " SQLITE_TYPE_TEXT\
                                        ");"

#define SQLITE_TRIGGER_D_ITEMS_IMAGEITEMS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_ITEMS, SQLITE_TABLE_IMAGEITEMS)

#define SQLITE_TRIGGER_U_ITEMS_IMAGEITEMS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_ITEMS, \
                                                                SQLITE_TABLE_IMAGEITEMS, \
                                                                UPNP_CLASS_IMAGE)

#define SQLITE_TRIGGER_I_ITEMS_IMAGEITEMS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_ITEMS, \
                                                                SQLITE_TABLE_IMAGEITEMS, \
                                                                UPNP_CLASS_IMAGE)

/**********************************************\
*                                              *
*  Video broadcasts                            *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_VIDEOBROADCASTS "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_VIDEOBROADCASTS \
                                        "("\
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_ICON " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_REGION " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_CHANNELNR " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_CHANNELNAME " " SQLITE_TYPE_TEXT " " SQLITE_UNIQUE \
                                        ");"

#define SQLITE_TRIGGER_D_VIDEOITEMS_VIDEOBROADCASTS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_VIDEOITEMS, SQLITE_TABLE_VIDEOBROADCASTS)

#define SQLITE_TRIGGER_U_VIDEOITEMS_VIDEOBROADCASTS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_VIDEOITEMS,\
                                                                          SQLITE_TABLE_VIDEOBROADCASTS,\
                                                                          UPNP_CLASS_VIDEOBC)

#define SQLITE_TRIGGER_I_VIDEOITEMS_VIDEOBROADCASTS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_VIDEOITEMS,\
                                                                          SQLITE_TABLE_VIDEOBROADCASTS,\
                                                                          UPNP_CLASS_VIDEOBC)

/**********************************************\
*                                              *
*  Audio broadcasts                            *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_AUDIOBROADCASTS "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_AUDIOBROADCASTS \
                                        "("\
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_REGION " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_RADIOCALLSIGN " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_RADIOSTATIONID " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_RADIOBAND " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_CHANNELNR " " SQLITE_TYPE_INTEGER \
                                        ");"

#define SQLITE_TRIGGER_D_AUDIOITEMS_AUDIOBROADCASTS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_AUDIOITEMS, SQLITE_TABLE_AUDIOBROADCASTS)

#define SQLITE_TRIGGER_I_AUDIOITEMS_AUDIOBROADCASTS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_AUDIOITEMS,\
                                                                          SQLITE_TABLE_AUDIOBROADCASTS,\
                                                                          UPNP_CLASS_AUDIOBC)

#define SQLITE_TRIGGER_U_AUDIOITEMS_AUDIOBROADCASTS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_AUDIOITEMS,\
                                                                          SQLITE_TABLE_AUDIOBROADCASTS,\
                                                                          UPNP_CLASS_AUDIOBC)

/**********************************************\
*                                              *
*  Movies                                      *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_MOVIES      "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_MOVIES \
                                        "("\
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_STORAGEMEDIUM " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DVDREGIONCODE " " SQLITE_TYPE_INTEGER "," \
                                        SQLITE_COL_CHANNELNAME " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_SCHEDULEDSTARTTIME " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_SCHEDULEDENDTIME " " SQLITE_TYPE_TEXT\
                                        ");"

#define SQLITE_TRIGGER_D_VIDEOITEMS_MOVIES SQLITE_DELETE_TRIGGER(SQLITE_TABLE_VIDEOITEMS, SQLITE_TABLE_MOVIES)


#define SQLITE_TRIGGER_I_VIDEOITEMS_MOVIES SQLITE_INSERT_TRIGGER(SQLITE_TABLE_VIDEOITEMS,\
                                                                 SQLITE_TABLE_MOVIES,\
                                                                 UPNP_CLASS_MOVIE)

#define SQLITE_TRIGGER_U_VIDEOITEMS_MOVIES SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_VIDEOITEMS,\
                                                                 SQLITE_TABLE_MOVIES,\
                                                                 UPNP_CLASS_MOVIE)

/**********************************************\
*                                              *
*  Photos                                      *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_PHOTOS      "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_PHOTOS \
                                        "("\
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_ALBUM " " SQLITE_TYPE_TEXT\
                                        ");"

#define SQLITE_TRIGGER_D_IMAGEITEMS_PHOTOS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_IMAGEITEMS, SQLITE_TABLE_PHOTOS)

#define SQLITE_TRIGGER_I_IMAGEITEMS_PHOTOS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_IMAGEITEMS,\
                                                                 SQLITE_TABLE_PHOTOS,\
                                                                 UPNP_CLASS_PHOTO)

#define SQLITE_TRIGGER_U_IMAGEITEMS_PHOTOS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_IMAGEITEMS,\
                                                                 SQLITE_TABLE_PHOTOS,\
                                                                 UPNP_CLASS_PHOTO)

/**********************************************\
*                                              *
*  Albums                                      *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_ALBUMS      "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_ALBUMS \
                                        "("\
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_STORAGEMEDIUM " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_LONGDESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_PUBLISHER " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_CONTRIBUTOR " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_DATE " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_RELATION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_RIGHTS " " SQLITE_TYPE_TEXT \
                                        ");"

#define SQLITE_TRIGGER_D_CONTAINERS_ALBUMS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_CONTAINERS, SQLITE_TABLE_ALBUMS)

#define SQLITE_TRIGGER_U_CONTAINERS_ALBUMS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_CONTAINERS,\
                                                                 SQLITE_TABLE_ALBUMS,\
                                                                 UPNP_CLASS_ALBUM)

#define SQLITE_TRIGGER_I_CONTAINERS_ALBUMS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_CONTAINERS,\
                                                                 SQLITE_TABLE_ALBUMS,\
                                                                 UPNP_CLASS_ALBUM)

/**********************************************\
*                                              *
*  Playlists                                   *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_PLAYLISTS   "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_PLAYLISTS \
                                        "(" \
                                        SQLITE_UPNP_OBJECTID "," \
                                        SQLITE_COL_ARTIST " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_GENRE " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_LONGDESCRIPTION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DESCRIPTION " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_PRODUCER " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_STORAGEMEDIUM " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_CONTRIBUTOR " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_DATE " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_LANGUAGE " " SQLITE_TYPE_TEXT ","\
                                        SQLITE_COL_RIGHTS " " SQLITE_TYPE_TEXT\
                                        ");"

#define SQLITE_TRIGGER_D_CONTAINERS_PLAYLISTS SQLITE_DELETE_TRIGGER(SQLITE_TABLE_CONTAINERS, SQLITE_TABLE_PLAYLISTS)

#define SQLITE_TRIGGER_I_CONTAINERS_PLAYLISTS SQLITE_INSERT_TRIGGER(SQLITE_TABLE_CONTAINERS,\
                                                                    SQLITE_TABLE_PLAYLISTS,\
                                                                    UPNP_CLASS_PLAYLISTCONT)

#define SQLITE_TRIGGER_U_CONTAINERS_PLAYLISTS SQLITE_UPDATE_TRIGGER(SQLITE_TABLE_CONTAINERS,\
                                                                    SQLITE_TABLE_PLAYLISTS,\
                                                                    UPNP_CLASS_PLAYLISTCONT)

/**********************************************\
*                                              *
*  Search classes                              *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_SEARCHCLASS "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_SEARCHCLASS \
                                        "(" \
                                        SQLITE_COL_OBJECTID " " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_CLASS " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_CLASSDERIVED " " SQLITE_TYPE_BOOL \
                                        ");"

#define SQLITE_TRIGGER_D_CONTAINERS_SEARCHCLASSES "CREATE TRIGGER IF NOT EXISTS " \
                                        SQLITE_TABLE_CONTAINERS "_D_" SQLITE_TABLE_SEARCHCLASS " " \
                                        "BEFORE DELETE ON " \
                                        SQLITE_TABLE_CONTAINERS " " \
                                        "FOR EACH ROW BEGIN "\
                                        "DELETE FROM " SQLITE_TABLE_SEARCHCLASS " "\
                                        "WHERE " SQLITE_COL_OBJECTID "= OLD." SQLITE_COL_OBJECTID "; " \
                                        "END;"

#define SQLITE_TRIGGER_U_CONTAINERS_SEARCHCLASSES "CREATE TRIGGER IF NOT EXISTS " \
                                        SQLITE_TABLE_CONTAINERS "_U_" SQLITE_TABLE_SEARCHCLASS " " \
                                        "BEFORE UPDATE ON " \
                                        SQLITE_TABLE_SEARCHCLASS " " \
                                        "FOR EACH ROW BEGIN "\
                                        "SELECT CASE "\
                                        "WHEN ("\
                                        "(SELECT " SQLITE_COL_OBJECTID " FROM " SQLITE_TABLE_CONTAINERS " "\
                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID ") IS NULL "\
                                        ") THEN "\
                                        "RAISE (" SQLITE_TRANSACTION_TYPE ", 'UPDATE on table " SQLITE_TABLE_SEARCHCLASS " "\
                                        "violates foreign key constraint \"" SQLITE_COL_OBJECTID "\"') " \
                                        "END; END;"

#define SQLITE_TRIGGER_I_CONTAINERS_SEARCHCLASSES "CREATE TRIGGER IF NOT EXISTS " \
                                        SQLITE_TABLE_CONTAINERS "_I_" SQLITE_TABLE_SEARCHCLASS " " \
                                        "BEFORE INSERT ON " \
                                        SQLITE_TABLE_SEARCHCLASS " " \
                                        "FOR EACH ROW BEGIN "\
                                        "SELECT CASE "\
                                        "WHEN ("\
                                        "(SELECT " SQLITE_COL_OBJECTID " FROM " SQLITE_TABLE_CONTAINERS " "\
                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID ") IS NULL "\
                                        ") THEN "\
                                        "RAISE (" SQLITE_TRANSACTION_TYPE ", 'INSERT on table " SQLITE_TABLE_SEARCHCLASS " "\
                                        "violates foreign key constraint \"" SQLITE_COL_OBJECTID "\"') " \
                                        "END; END;"

/**********************************************\
*                                              *
*  Resources                                   *
*                                              *
\**********************************************/

#define SQLITE_CREATE_TABLE_RESOURCES   "CREATE TABLE IF NOT EXISTS "\
                                        SQLITE_TABLE_RESOURCES \
                                        "(" \
                                        SQLITE_COL_RESOURCEID " " SQLITE_PRIMARY_KEY " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_OBJECTID " " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_PROTOCOLINFO " " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_CONTENTTYPE " " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_RESOURCETYPE " " SQLITE_TYPE_INTEGER " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_RESOURCE " " SQLITE_TYPE_TEXT " " SQLITE_NOT_NULL "," \
                                        SQLITE_COL_SIZE " " SQLITE_TYPE_ULONG "," \
                                        SQLITE_COL_DURATION " " SQLITE_TYPE_TEXT "," \
                                        SQLITE_COL_BITRATE " " SQLITE_TYPE_UINTEGER "," \
                                        SQLITE_COL_SAMPLEFREQUENCE " " SQLITE_TYPE_UINTEGER "," \
                                        SQLITE_COL_BITSPERSAMPLE " " SQLITE_TYPE_UINTEGER "," \
                                        SQLITE_COL_NOAUDIOCHANNELS " " SQLITE_TYPE_UINTEGER "," \
                                        SQLITE_COL_COLORDEPTH " " SQLITE_TYPE_UINTEGER "," \
                                        SQLITE_COL_RESOLUTION " " SQLITE_TYPE_TEXT \
                                        ");"

#define SQLITE_TRIGGER_D_OBJECT_RESOURCES "CREATE TRIGGER IF NOT EXISTS " \
                                        SQLITE_TABLE_OBJECTS "_D_" SQLITE_TABLE_RESOURCES " " \
                                        "BEFORE DELETE ON " \
                                        SQLITE_TABLE_OBJECTS " " \
                                        "FOR EACH ROW BEGIN "\
                                        "DELETE FROM " SQLITE_TABLE_RESOURCES " "\
                                        "WHERE " SQLITE_COL_OBJECTID "= OLD." SQLITE_COL_OBJECTID "; " \
                                        "END;"

#define SQLITE_TRIGGER_I_OBJECT_RESOURCES "CREATE TRIGGER IF NOT EXISTS " \
                                        SQLITE_TABLE_OBJECTS "_I_" SQLITE_TABLE_RESOURCES " " \
                                        "BEFORE INSERT ON " \
                                        SQLITE_TABLE_RESOURCES " " \
                                        "FOR EACH ROW BEGIN "\
                                        "SELECT CASE "\
                                        "WHEN ("\
                                        "(SELECT " SQLITE_COL_OBJECTID " FROM " SQLITE_TABLE_OBJECTS " "\
                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID ") IS NULL"\
                                        ") THEN "\
                                        "RAISE (" SQLITE_TRANSACTION_TYPE ", 'INSERT on table " SQLITE_TABLE_RESOURCES " "\
                                        "violates foreign key constraint \"" SQLITE_COL_OBJECTID "\"') " \
                                        "END; END;"

#define SQLITE_TRIGGER_U_OBJECT_RESOURCES "CREATE TRIGGER IF NOT EXISTS " \
                                        SQLITE_TABLE_OBJECTS "_U_" SQLITE_TABLE_RESOURCES " " \
                                        "BEFORE UPDATE ON " \
                                        SQLITE_TABLE_RESOURCES " " \
                                        "FOR EACH ROW BEGIN "\
                                        "SELECT CASE "\
                                        "WHEN ("\
                                        "(SELECT " SQLITE_COL_OBJECTID " FROM " SQLITE_TABLE_OBJECTS " "\
                                        "WHERE " SQLITE_COL_OBJECTID "=NEW." SQLITE_COL_OBJECTID ") IS NULL"\
                                        ") THEN "\
                                        "RAISE (" SQLITE_TRANSACTION_TYPE ", 'INSERT on table " SQLITE_TABLE_RESOURCES " "\
                                        "violates foreign key constraint \"" SQLITE_COL_OBJECTID "\"') " \
                                        "END; END;"

class cSQLiteDatabase;

/**
 * Result row of a SQL SELECT request
 *
 * This is a single row of a {\c SQL SELECT} request.
 *
 * @see cRows
 */
class cRow : public cListObject {
    friend class cSQLiteDatabase;
private:
    int     currentCol;
    int     ColCount;
    char**  Columns;
    char**  Values;
    cRow();
public:
    virtual ~cRow();
    /**
     * Number of columns in this row
     *
     * @return the number of rows
     */
    int  Count(){ return this->ColCount; }
    /**
     * Fetches a Column
     *
     * This will fetch a column of this row and stores the name of the column
     * in the first parameter and the value in the second parameter.
     *
     * @return returns
     * - \bc true, if more columns to come
     * - \bc false, if the column is its last in this row.
     */
    bool fetchColumn(
        cString* Column, /**< The name of the current column */
        cString* Value /**< The value of the current value */
    );
    
    /**
     * Fetches a Column
     *
     * This will fetch a column of this row and stores the name of the column
     * in the first parameter and the value in the second parameter.
     *
     * @return returns
     * - \bc true, if more columns to come
     * - \bc false, if the column is its last in this row.
     */
    bool fetchColumn(
        char** Column, /**< The name of the current column */
        char** Value /**< The value of the current column */
    );
};

/**
 * Result rows of a SQL SELECT request
 *
 * Contains the rows of a SQL SELECT request
 *
 * @see cRow
 */
class cRows : public cList<cRow> {
    friend class cSQLiteDatabase;
private:
    cRow*   mLastRow;
    cRows();
public:
    virtual ~cRows();
    /**
     * Fetches a row from the result
     *
     * This fetches the next row in the resultset by storing the contents of
     * that row in the first parameter.
     *
     * @return returns
     * - \bc true, if more rows to come
     * - \bc false, if the row is its last in this resultset.
     */
    bool fetchRow(
        cRow** Row /**< The Pointer of the row */
    );
};

/**
 * SQLite Database
 *
 * This is a wrapper class for a SQLite3 database connection
 * It supports simple execution functions.
 *
 * On requests with returns any results a instance of \c cRows* will be created.
 */
class cSQLiteDatabase {
    friend class cStatement;
private:
    bool        mAutoCommit;
    bool        mActiveTransaction;
    cRow*       mLastRow;
    cRows*      mRows;
    sqlite3*    mDatabase;
    static cSQLiteDatabase* mInstance;
    cSQLiteDatabase();
    int initialize();
    int initializeTables();
    int initializeTriggers();
    static int getResultRow(void* DB, int NumCols, char** Values, char** ColNames);
    int exec(const char* Statement);
public:
    /**
     * Prints a SQLite escaped text
     *
     * Returns a formated text with special characters to escape SQLite special
     * characters like "'". Additionally to the well known characters of \a printf
     * the following are allowed:
     *
     * - \bc q, like s, escapes single quotes in strings
     * - \bc Q, like q, surrounds the escaped string with additional
     *             single quotes
     * - \bc z, frees the string after reading and coping it
     *
     * @see sprintf()
     * @return the formated string
     */
    static const char* sprintf(
        const char* Format, /**< The format string */
        ... /**< optional properties which will be passed to sprintf */
    );
    virtual ~cSQLiteDatabase();
    /**
     * Returns the instance of the database
     *
     * Returns the instance of the SQLite database. This will create a single
     * instance of none is existing on the very first call. A subsequent call
     * will return the same instance.
     *
     * @return the database instance
     */
    static cSQLiteDatabase* getInstance();
    /**
     * Row count of the last result
     *
     * Returns the row count of the last {\c SQL SELECT} request.
     *
     * @see cRows
     * @return the result row count
     */
    int getResultCount() const { return this->mRows->Count(); }
    /**
     * The last \c INSERT RowID
     *
     * Returns the primary key of the last inserted row.
     * This will only work if there are no successive calls to the database.
     *
     * @return the last insert RowID
     */
    long getLastInsertRowID() const;
    /**
     * Result set of the last request
     *
     * Returns the result rows of the SQL SELECT request.
     * This might be NULL, if the last statement was not a SELECT.
     *
     * @see cRows
     * @return the result rows of the last \c SELECT statement.
     */
    cRows* getResultRows() const { return this->mRows; }
    /**
     * Executes a SQL statement
     *
     * This will execute the statement in the first parameter. If it is followed
     * by any optional parameters it will be formated using the same function as
     * in \c cSQLiteDatabase::sprintf().
     *
     * \sa cSQLiteDatabase::sprintf().
     *
     * @return returns an integer representing
     * - \bc -1, in case of an error
     * - \bc 0, when the statement was executed successfuly
     */
    int execStatement(
        const char* Statement , /**< Statement to be executed */
        ... /**< optional parameters passed to the format string */
    );
    /**
     * Starts a transaction
     *
     * This starts a new transaction and commits or rolls back a previous.
     *
     * @see cSQLiteDatabase::setAutoCommit
     * @see cSQLiteDatabase::commitTransaction
     */
    void startTransaction();
    /**
     * Commits a transaction
     *
     * This function commits the transaction and writes all changes to the
     * database
     *
     * @see cSQLiteDatabase::startTransaction
     */
    void commitTransaction();
    /**
     * Performs a rollback on a transaction
     *
     * This function performs a rollback. No changes will be made to the
     * database
     *
     * @see cSQLiteDatabase::rollbackTransaction
     */
    void rollbackTransaction();
    /**
     * Set the commit behavior
     *
     * This function sets the auto commit behavior on new transactions with
     * \sa cSQLiteDatabase::startTransaction.
     *
     * - \bc true, commits the last transaction before starting a
     *              new one
     * - \bc false, performs a rollback on the old transaction
     *
     */
    void setAutoCommit(
        bool Commit=true /**< Switches the behavior of auto commit */
    ){ this->mAutoCommit = Commit; }
};

#endif	/* _DATABASE_H */