~marcinello/ubuntu-rssreader-app/fix-1297463

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
.pragma library // I hope this will prevent the waste of memory.
.import QtQuick.LocalStorage 2.0 as SQL

/* For internal usage in module.
 */
var gDbCache = undefined
function openStdDataBase() {
    if (gDbCache === undefined)
    {
        gDbCache = SQL.LocalStorage.openDatabaseSync("RSS Reader", "1.0", "App main DB", 1000000)
    }

    // check table exist after open the database
    gDbCache.transaction(function(tx){
        checkTableExists(tx/*, opts*/);
    }
    )
    return gDbCache
}

// Lol, guys, look here: http://www.sqlite.org/datatype3.html
// All VARCHAR(x) converted to TEXT :)
// I am currently working with 190 symbols length strings, all is ok :)
function checkTableExists(transaction /* and additional string keys */) {
    transaction.executeSql('PRAGMA foreign_keys = ON;')   // enable foreign key support
    transaction.executeSql("CREATE TABLE IF NOT EXISTS feed  (id  INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,source  TEXT  NULL,title  TEXT  NULL,link  TEXT  NULL, description  TEXT  NULL, status  char(1)  NULL DEFAULT '0', pubdate  INTEGER  NULL,image  TEXT  NULL, count INTEGER NULL DEFAULT 0);")
    transaction.executeSql("CREATE TABLE IF NOT EXISTS tag  (id  INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,name  TEXT  NOT NULL UNIQUE );")
    transaction.executeSql("CREATE TABLE IF NOT EXISTS feed_tag  (id  INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,feed_id  INTEGER  NULL,tag_id  INTEGER  NULL,FOREIGN KEY(feed_id) REFERENCES feed(id) on delete cascade);")
    transaction.executeSql("CREATE TABLE IF NOT EXISTS article ( id  INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL, title  TEXT  NULL, content TEXT NULL, link  TEXT  NULL, description  TEXT  NULL, pubdate  INTEGER  NULL, status  char(1)  NULL DEFAULT '0', favourite  char(1)  NULL DEFAULT '0', image  TEXT  NULL, guid TEXT NULL, feed_id  INTEGER  NULL,count INTEGER NULL DEFAULT 0, media_groups TEXT NULL);")
    transaction.executeSql("CREATE TABLE IF NOT EXISTS settings  ( id INTEGER, current_database_version TEXT NULL, database_last_updated  TEXT  NULL, view_mode char(1) NULL DEFAULT '0', update_interval INTEGER NULL DEFAULT 0, network_mode char(1) NULL DEFAULT '0');")
    var rs = transaction.executeSql("select * from settings")
    if (rs.rows.length == 0)
    {
        transaction.executeSql('INSERT INTO settings VALUES(?, ?, ?, ?, ?, ?)',
                               [1 , 1.0, "000000", '0', 0, '0'])
    }
}

/* feed operations
 * include select, insert, update and delete operations
 */
// select
function loadFeeds()
{
    var db = openStdDataBase()
    var dbResult
//    var feeds
    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT * FROM feed")
        console.log("feed SELECTED: ", dbResult.rows.length)
    }
    )
    return dbResult;  // I suggest that return the whole result in order to know if error occurs
}

// insert
function addFeed(title, source)  // from user input
{
    var dbResult
    var db = openStdDataBase()
    db.transaction(function (tx) {
        /* Check uniqueness.
         */
        dbResult = tx.executeSql("SELECT * FROM feed WHERE source=?", [source])
        if (dbResult.rows.length > 0) {
            console.log("Database, addFeed: already exist feed with source: ", source, "ID", dbResult.rows.item(0).id)
            dbResult = {"error": true, "exist": true}
            return
        }

        dbResult = tx.executeSql('INSERT INTO feed (title, source) VALUES(?, ?)',
                                 [title , source])
        console.log("feed INSERT ID: ", dbResult.insertId)

        dbResult.feedId = tx.executeSql("SELECT * FROM feed WHERE source=?", [source]).rows.item(0).id
        console.log("dbResult.feedId", dbResult.feedId)
    }
    )
    return dbResult;
}

// change confirmed
/* Update feed status.
 * 0 - default, 1 - good, 2 - bad url.
 */
function setFeedStatus(id, status)  // from user input
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
        dbResult = tx.executeSql('UPDATE feed SET status=? WHERE id=?',
                                 [status, id])
        console.log("feed setFeedStatus, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

// update
function updateFeedByUser(id, title, source)  // from user input
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE feed SET title=?, source=? WHERE id=?',
                                 [title, source, id])
        console.log("feed updateFeedByUser, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function updateFeedByXml(id, link, description, title)   // from xml file
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
        dbResult = tx.executeSql('UPDATE feed SET link=?, description=?, title=? WHERE id=?',
                                 [link, description, title, id])
        console.log("feed updateFeedByXml, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function updateFeedImage(id, image)  //  offline image path
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE feed SET image=? WHERE id=?',
                                 [image, id])
        console.log("feed UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function updateFeedCount(id, count)  //
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE feed SET count=? WHERE id=?',
                                 [count, id])
        console.log("feed UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

// delete
function deleteFeed(id)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        tx.executeSql('PRAGMA foreign_keys = ON;')   // enable foreign key support
        dbResult = tx.executeSql('delete from feed WHERE id=?',
                                 [id])
        console.log("feed delete, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function deleteFeedByTagId(tagId)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        tx.executeSql('PRAGMA foreign_keys = ON;')   // enable foreign key support
        dbResult = tx.executeSql('delete from feed where exists (select 1 from feed_tag where feed_tag.feed_id = feed.id and feed_tag.tag_id = ?)',
                                 [tagId])
        console.log("feed delete by tag id, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

// select feeds without of topic (tag).
function loadFeedsWithoutTopic()
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT * FROM feed WHERE id NOT IN (SELECT feed_id FROM feed_tag)")
        console.log("loadFeedsWithoutTopic SELECTED: ", dbResult.rows.length)
    }
    )
    return dbResult;  // I suggest that return the whole result in order to know if error occurs
}

function feedsCount() {
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT Count(*) FROM feed_tag")
        var obj = dbResult.rows.item(0)
        dbResult.count = obj["Count(*)"]
    }
    )
    return dbResult
}


/* article operations
 * include select, insert, update and delete operations
 *
 *
 */
// select
function loadArticles(params)   // params = {"isAll": true/false, "feedId": id | "tagId" : id}
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        if (params == undefined || params.isAll) // miss params
            dbResult = tx.executeSql('SELECT article.*, feed.title as feed_name FROM article inner join feed on article.feed_id = feed.id ORDER BY article.pubdate DESC')
        else if (params.feedId)
            dbResult = tx.executeSql('SELECT article.*, feed.title as feed_name FROM article inner join feed on article.feed_id = feed.id WHERE article.feed_id = ? ORDER BY article.pubdate DESC', [params.feedId])
        else if (params.tagId)
            dbResult = tx.executeSql('SELECT article.*, feed.title as feed_name FROM article inner join feed on article.feed_id = feed.id WHERE article.feed_id IN (SELECT feed_id FROM feed_tag WHERE tag_id = ?) ORDER BY article.pubdate DESC', [params.tagId])
    }
    )
    return dbResult;
}

// UNUSED
function loadArticle(articleId)   // params = {"isAll": true/false, "feedId": id | "tagId" : id}
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql('SELECT article.*, feed.title as feed_name FROM article inner join feed on article.feed_id = feed.id WHERE article.id = ?', [articleId])
        console.assert(dbResult.rows.length !== 0, "ERROR loadArticle, " + articleId)
    }
    )
    return dbResult;
}

// load needed properties of every article, like status, favourite, etc..
function preloadArticlesProperties(feedId)
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        if (feedId == undefined) // miss feedId
            dbResult = tx.executeSql('SELECT guid, status FROM article WHERE status = "1" ') // guid, status
        else
            dbResult = tx.executeSql('SELECT guid, status FROM article WHERE feed_id = ? AND status = "1" ', [feedId])
    }
    )
    console.log("preloadArticlesProperties: ", dbResult.rows.length)
    return dbResult;
}

// load all favourite articles
function loadFavouriteArticles()
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql('SELECT article.*, feed.title as feed_name FROM article inner join feed on article.feed_id = feed.id WHERE article.favourite = "1" ORDER BY article.pubdate DESC' )
    }
    )
    //console.log("loadFavouriteArticles", dbResult.rows.length)
    //console.assert(dbResult.rows.length !== 0,  "ERROR: There are no saved articles")
    return dbResult;
}

// insert
function addArticle(title, content, link, description, pubdate, guid, feed_id)
{
    var dbResult
    var db = openStdDataBase()
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)

        /* Check uniqueness.
         */
        dbResult = tx.executeSql("SELECT * FROM article WHERE guid=?", [guid])
        if (dbResult.rows.length > 0) {
            console.log("Database, add article: already exist article with guid: ", guid)
            dbResult = {"error": true, "exist": true}
            return
        }
        dbResult = tx.executeSql('INSERT INTO article (title, content, link, description, pubdate, guid, feed_id) VALUES(?, ?, ?, ?, ?, ?, ?)',
                                 [title, content, link, description, pubdate, guid, feed_id])

        dbResult.articleId = tx.executeSql("SELECT * FROM article WHERE guid=?", [guid]).rows.item(0).id
    }
    )
    return dbResult;
}

/*
  this function is for avoiding hard drive performance issue,
  pass model and feed id as parameters to this function, it will automaticly insert all the articles into database
  add third pamams which is for restoring articles' properties
 */
function addArticles(model, feed_id, restoreArray)
{
    var dbResult

    var db = openStdDataBase()
    db.transaction(function (tx) {

        var article;
        for (var i = 0; i < model.count; i++) {

            article = model.get(i)
            var title =  article.title == undefined ? "" : article.title
            var guid =  article.guid == undefined ? Qt.md5(title) : article.guid
            var link =  article.link == undefined ? "" : article.link
            var pubDate =  article.pubDate == undefined ? "" : article.pubDate
            var description =  article.description == undefined ? "" : article.description
            var content =  article.content == undefined ? "" : article.content
            var image =  article.image == undefined ? "" : article.image
            var media_groups = article.media_groups == undefined ? "" : JSON.stringify(article.media_groups)

            /* Check uniqueness.
             */
            dbResult = tx.executeSql("SELECT * FROM article WHERE guid=? AND feed_id=?", [guid, feed_id])
            if (dbResult.rows.length > 0) {
                console.log("Database, add article: already exist article with source: ", guid)
                continue;
            }
            dbResult = tx.executeSql('INSERT INTO article (title, content, link, description, pubdate, guid, feed_id, image, media_groups) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)',
                                     [title, content, link, description, pubDate, guid, feed_id, image, media_groups])
            // console.log("article INSERT ID: ", JSON.stringify(dbResult) )
        }

        if (restoreArray != undefined) {
            for (var j = 0; j < restoreArray.rows.length; j++) {
                dbResult = tx.executeSql('UPDATE article SET status=? WHERE guid=? AND status="0"',
                                         [restoreArray.rows[j].status, restoreArray.rows[j].guid])
            }
        }
    }
    )
    return dbResult;
}

// update
function updateArticleStatus(id, status)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE article SET status=? WHERE id=?',
                                 [status, id])
        console.log("article status UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function updateArticleFavourite(id, favourite)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE article SET favourite=? WHERE id=?',
                                 [favourite, id])
        console.log("article favourite UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function updateArticleImage(id, image)  //  offline image path
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE article SET image=? WHERE id=?',
                                 [image, id])
        //console.log("article UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}


// delete
function deleteArticle(id)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('delete from article WHERE id=?',
                                 [id])
        console.log("article delete, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

// clear article table, only status='2' and favourite='1' remain
function clearArticles(feed_id)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql("delete from article WHERE (status='0' OR status='1') AND favourite='0' AND feed_id=?", [feed_id])
        console.log("article delete, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}


/* tag operations
 * include select, insert, update and delete operations
 *
 *
 */
// select
function loadTags()
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT * FROM tag")
        console.assert(dbResult.rows.length !== 0, "ERROR: NO TAGS DATABASE")
    }
    )
    return dbResult;
}

// insert
function addTag(name)
{
    var dbResult
    var db = openStdDataBase()
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)

        /* Check uniqueness.
         */
        dbResult = tx.executeSql("SELECT * FROM tag WHERE name=?", [name])
        if (dbResult.rows.length > 0) {
            console.log("Database, add tag: already exist tag with source: ", name)
            dbResult = {"error": true, "exist": true}
            return
        }

        dbResult = tx.executeSql('INSERT INTO tag (name) VALUES(?)',
                                 [name])
        console.log("tag INSERT ID: ", dbResult.insertId)

        dbResult.tagId = tx.executeSql("SELECT * FROM tag WHERE name=?", [name]).rows.item(0).id
        console.log("dbResult.tagId", dbResult.tagId)
    }
    )
    return dbResult;
}

// update
function updateTag(id, name)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE tag SET name=? WHERE id=?',
                                 [name, id])
        console.log("tag UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

// delete
function deleteTag(id)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('delete from tag WHERE id=?',
                                 [id])
        console.log("tag delete, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}


/* feed_tag operations
 * include select, insert and delete operations
 *
 *
 */
// select
function loadFeedTags()
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT * FROM feed_tag")
    }
    )
    return dbResult;
}

function loadFeedsFromTag(tag_id)
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT t1.* FROM feed t1 INNER JOIN feed_tag t2 ON t1.id = t2.feed_id WHERE tag_id =?", [tag_id])
        // console.log("loadFeedsFromTag:", tag_id, "SELECTED: ", dbResult.rows.length)
    }
    )
    return dbResult;
}

// insert
function addFeedTag(feed_id, tag_id)
{
    var dbResult
    var db = openStdDataBase()
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)

        /* Check uniqueness.
         */
        dbResult = tx.executeSql("SELECT * FROM feed_tag WHERE feed_id=? AND tag_id=? ", [feed_id, tag_id])
        if (dbResult.rows.length > 0) {
            console.log("Database, add feed_tag: already exist feed_tag with source: ", feed_id, tag_id)
            return {"error": true, "exist": true};
        }

        dbResult = tx.executeSql('INSERT INTO feed_tag (feed_id, tag_id) VALUES(?, ?)',
                                 [feed_id, tag_id])
        console.log("feed_tag INSERT ID: ", dbResult.insertId)
    }
    )
    return dbResult;
}

// delete
function deleteFeedTag(id)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('delete from feed_tag WHERE id=?',
                                 [id])
        console.log("feed_tag delete, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

// delete
function deleteFeedTagsByTagId(tagId)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('delete from feed_tag WHERE tag_id=?',
                                 [tagId])
        console.log("feed_tag delete, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function deleteFeedTag(feedId, tagId)
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("DELETE FROM feed_tag WHERE feed_id = ? AND tag_id = ?", [feedId, tagId])
        console.log("feed_tag delete by feedId and tagId: ", dbResult.rowsAffected)
    }
    )
    return dbResult;
}


/* settings operations
 * include select and update operations
 *
 *
 */
// select  //for each setting
function getDBVersion()
{
    var db = openStdDataBase()
    var dbResult
    var dbVersion = ""

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT current_database_version FROM settings where id=1")
        if (dbResult.rows.length > 0)
        {
            dbVersion = dbResult.rows.item(0).current_database_version ;
        }

    }
    )
    return dbVersion;
}

function getDBLastUpdate()
{
    var db = openStdDataBase()
    var dbResult
    var dbLastUpdate = ""

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT database_last_updated FROM settings where id=1")
        if (dbResult.rows.length > 0)
        {
            dbLastUpdate = dbResult.rows.item(0).database_last_updated ;
        }

    }
    )
    return dbLastUpdate;
}

/* I want to use update_interval field for storing
 * font and light/dark theme.
 * INTEGER BYTES: 00 00 <useDark 1 | 0> <fontSize value>
 */
function getFontSize()
{
    var db = openStdDataBase()
    var dbResult
    var dbValue = 0

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
        if (dbResult.rows.length > 0)
        {
            dbValue = dbResult.rows.item(0).update_interval
            dbValue = 0xFF & dbValue
        }
    }
    )
    return dbValue;
}

function getUseDarkTheme()
{
    var db = openStdDataBase()
    var dbResult
    var result

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
        if (dbResult.rows.length > 0)
        {
            var dbValue = dbResult.rows.item(0).update_interval
            dbValue = 0xFF00 & dbValue
            result = (dbValue !== 0)
        }
    }
    )
    return result;
}

function getViewMode()
{
    var db = openStdDataBase()
    var dbResult
    var dbViewMode = ""

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT view_mode FROM settings where id=1")
        if (dbResult.rows.length > 0)
        {
            dbViewMode = dbResult.rows.item(0).view_mode ;
        }

    }
    )
    return dbViewMode;
}

function getUpdateInterval()
{
    var db = openStdDataBase()
    var dbResult
    var dbUpdateInterval = 0

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT update_interval FROM settings where id=1")
        if (dbResult.rows.length > 0)
        {
            dbUpdateInterval = dbResult.rows.item(0).update_interval ;
        }

    }
    )
    return dbUpdateInterval;
}

function getNetworkMode()
{
    var db = openStdDataBase()
    var dbResult
    var dbNetworkMode = ""

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT network_mode FROM settings where id=1")
        if (dbResult.rows.length > 0)
        {
            dbNetworkMode = dbResult.rows.item(0).network_mode ;
        }

    }
    )
    return dbNetworkMode;
}

// update  //for each setting
function setDBVersion(current_database_version)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE settings SET current_database_version=? WHERE id=1',
                                 [current_database_version])
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function setDBLastUpdate(database_last_updated)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE settings SET database_last_updated=? WHERE id=1',
                                 [database_last_updated])
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function setFontSize(fontSize)
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function (tx) {
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
        if (dbResult.rows.length > 0)
        {
            var dbValue = dbResult.rows.item(0).update_interval
            var newValue = (0xFF & fontSize) | (dbValue & 0xFFFFFF00)

            dbResult = tx.executeSql('UPDATE settings SET update_interval=? WHERE id=1',
                                     [newValue])
        }
    }
    )
    return dbResult
}

function setUseDarkTheme(useDarkTheme)
{
    useDarkTheme = useDarkTheme ? 1 : 0
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
        if (dbResult.rows.length > 0)
        {
            var dbValue = dbResult.rows.item(0).update_interval
            var newValue = (/*0xFF & */useDarkTheme << 8) | (dbValue & 0xFFFF00FF)

            dbResult = tx.executeSql('UPDATE settings SET update_interval=? WHERE id=1',
                                     [newValue])
        }
    }
    )
    return dbResult
}

function setViewMode(value)
{
    var db = openStdDataBase()
    var dbResult

    db.transaction(function(tx) {
        dbResult = tx.executeSql("SELECT view_mode FROM settings where id=1")
        if (dbResult.rows.length > 0)
        {
            dbResult = tx.executeSql('UPDATE settings SET view_mode=? WHERE id=1',
                                     [value])
        }

    }
    )
    return dbResult;
}

function setUpdateInterval(update_interval)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE settings SET update_interval=? WHERE id=1',
                                 [update_interval])
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}

function setNetworkMode(network_mode)
{
    var db = openStdDataBase()
    var dbResult
    db.transaction(function (tx) {
//        ensureFeedTableExists(tx)
        dbResult = tx.executeSql('UPDATE settings SET network_mode=? WHERE id=1',
                                 [network_mode])
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
    }
    )
    return dbResult
}



/* operations for testing
 * include clear and drop operations
 * not completed yet
 *
 */
// clear
function clearData(table)
{
    var db = openStdDataBase()

    switch(table)
    {
    case "feed":
        db.transaction(function(tx) {
            tx.executeSql("delete from feed")
            console.log("feed clear")
        }
        )
        break;
    case "article":
        db.transaction(function(tx) {
            tx.executeSql("delete from article")
            console.log("article clear")
        }
        )
        break;
    case "tag":
        db.transaction(function(tx) {
            tx.executeSql("delete from tag")
            console.log("tag clear")
        }
        )
        break;
    case "feed_tag":
        db.transaction(function(tx) {
            tx.executeSql("delete from feed_tag")
            console.log("feed_tag clear")
        }
        )
        break;
    case "settings":
        db.transaction(function(tx) {
            tx.executeSql("delete from settings")
            console.log("settings clear")
        }
        )
        break;
    default:
        db.transaction(function(tx) {
            tx.executeSql("delete from feed_tag")
            tx.executeSql("delete from feed")
            tx.executeSql("delete from tag")
            tx.executeSql("delete from article")
            tx.executeSql("delete from settings")
            console.log("DATABASE clear")
        }
        )
    }
}

// drop
function dropTable(table)
{
    var db = openStdDataBase()

    switch(table)
    {
    case "feed":
        db.transaction(function(tx) {
            tx.executeSql("DROP TABLE IF EXISTS feed")
            console.log("feed deleted")
        }
        )
        break;
    case "article":
        db.transaction(function(tx) {
            tx.executeSql("DROP TABLE IF EXISTS article")
            console.log("article deleted")
        }
        )
        break;
    case "tag":
        db.transaction(function(tx) {
            tx.executeSql("DROP TABLE IF EXISTS tag")
            console.log("tag deleted")
        }
        )
        break;
    case "feed_tag":
        db.transaction(function(tx) {
            tx.executeSql("DROP TABLE IF EXISTS feed_tag")
            console.log("feed_tag deleted")
        }
        )
        break;
    case "settings":
        db.transaction(function(tx) {
            tx.executeSql("DROP TABLE IF EXISTS settings")
            console.log("settings deleted")
        }
        )
        break;
    default:
        db.transaction(function(tx) {
            tx.executeSql("DROP TABLE IF EXISTS feed")
            tx.executeSql("DROP TABLE IF EXISTS article")
            tx.executeSql("DROP TABLE IF EXISTS tag")
            tx.executeSql("DROP TABLE IF EXISTS feed_tag")
            tx.executeSql("DROP TABLE IF EXISTS settings")
            console.log("DATABASE deleted")
        }
        )
    }
}