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

54.1.1 by Roman Shchekin
"Create topic" removed, .pragma added to databasemodule_v2.js.
1
.pragma library // I hope this will prevent the waste of memory.
9.1.1 by Roman Shchekin
As always - start point for new database module by me.
2
.import QtQuick.LocalStorage 2.0 as SQL
3
4
/* For internal usage in module.
5
 */
6
var gDbCache = undefined
7
function openStdDataBase() {
8
    if (gDbCache === undefined)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
9
    {
18.2.2 by Roman Shchekin
Stable version of our app.
10
        gDbCache = SQL.LocalStorage.openDatabaseSync("RSS Reader", "1.0", "App main DB", 1000000)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
11
    }
12
13
    // check table exist after open the database
14
    gDbCache.transaction(function(tx){
15
        checkTableExists(tx/*, opts*/);
16
    }
17
    )
9.1.1 by Roman Shchekin
As always - start point for new database module by me.
18
    return gDbCache
19
}
20
23.2.6 by Roman Shchekin
List mode and network work via Google Feed API totally integrated to our
21
// Lol, guys, look here: http://www.sqlite.org/datatype3.html
22
// All VARCHAR(x) converted to TEXT :)
23
// I am currently working with 190 symbols length strings, all is ok :)
9.1.1 by Roman Shchekin
As always - start point for new database module by me.
24
function checkTableExists(transaction /* and additional string keys */) {
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
25
    transaction.executeSql('PRAGMA foreign_keys = ON;')   // enable foreign key support
80.1.1 by Joey Chan
optimization for gridview, articles, input method, edit-topic
26
    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);")
57.1.1 by Joey Chan
add "read" and "unread" states
27
    transaction.executeSql("CREATE TABLE IF NOT EXISTS tag  (id  INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,name  TEXT  NOT NULL UNIQUE );")
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
28
    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);")
80.1.1 by Joey Chan
optimization for gridview, articles, input method, edit-topic
29
    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);")
57.1.1 by Joey Chan
add "read" and "unread" states
30
    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');")
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
31
    var rs = transaction.executeSql("select * from settings")
32
    if (rs.rows.length == 0)
33
    {
34
        transaction.executeSql('INSERT INTO settings VALUES(?, ?, ?, ?, ?, ?)',
35
                               [1 , 1.0, "000000", '0', 0, '0'])
9.1.1 by Roman Shchekin
As always - start point for new database module by me.
36
    }
37
}
38
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
39
/* feed operations
40
 * include select, insert, update and delete operations
41
 */
42
// select
43
function loadFeeds()
44
{
45
    var db = openStdDataBase()
46
    var dbResult
47
//    var feeds
48
    db.transaction(function(tx) {
49
        dbResult = tx.executeSql("SELECT * FROM feed")
50
        console.log("feed SELECTED: ", dbResult.rows.length)
51
    }
52
    )
53
    return dbResult;  // I suggest that return the whole result in order to know if error occurs
54
}
55
56
// insert
57
function addFeed(title, source)  // from user input
58
{
59
    var dbResult
60
    var db = openStdDataBase()
61
    db.transaction(function (tx) {
62
        /* Check uniqueness.
63
         */
64
        dbResult = tx.executeSql("SELECT * FROM feed WHERE source=?", [source])
65
        if (dbResult.rows.length > 0) {
51.1.1 by Roman Shchekin
Fix for https://bugs.launchpad.net/ubuntu-rssreader-app/+bug/1217300.
66
            console.log("Database, addFeed: already exist feed with source: ", source, "ID", dbResult.rows.item(0).id)
13.1.12 by Roman Shchekin
Feeds management ready, topics to go!
67
            dbResult = {"error": true, "exist": true}
68
            return
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
69
        }
70
71
        dbResult = tx.executeSql('INSERT INTO feed (title, source) VALUES(?, ?)',
72
                                 [title , source])
73
        console.log("feed INSERT ID: ", dbResult.insertId)
12.1.1 by Roman Shchekin
Prepare for alpha.
74
75
        dbResult.feedId = tx.executeSql("SELECT * FROM feed WHERE source=?", [source]).rows.item(0).id
76
        console.log("dbResult.feedId", dbResult.feedId)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
77
    }
78
    )
79
    return dbResult;
80
}
81
12.1.1 by Roman Shchekin
Prepare for alpha.
82
// change confirmed
83
/* Update feed status.
84
 * 0 - default, 1 - good, 2 - bad url.
85
 */
86
function setFeedStatus(id, status)  // from user input
87
{
88
    var db = openStdDataBase()
89
    var dbResult
90
    db.transaction(function (tx) {
91
        dbResult = tx.executeSql('UPDATE feed SET status=? WHERE id=?',
92
                                 [status, id])
93
        console.log("feed setFeedStatus, AFFECTED ROWS: ", dbResult.rowsAffected)
94
    }
95
    )
96
    return dbResult
97
}
98
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
99
// update
100
function updateFeedByUser(id, title, source)  // from user input
101
{
102
    var db = openStdDataBase()
103
    var dbResult
104
    db.transaction(function (tx) {
105
//        ensureFeedTableExists(tx)
106
        dbResult = tx.executeSql('UPDATE feed SET title=?, source=? WHERE id=?',
107
                                 [title, source, id])
12.1.1 by Roman Shchekin
Prepare for alpha.
108
        console.log("feed updateFeedByUser, AFFECTED ROWS: ", dbResult.rowsAffected)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
109
    }
110
    )
111
    return dbResult
112
}
113
23.2.3 by Roman Shchekin
Partially ported to google feed api.
114
function updateFeedByXml(id, link, description, title)   // from xml file
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
115
{
116
    var db = openStdDataBase()
117
    var dbResult
118
    db.transaction(function (tx) {
23.2.3 by Roman Shchekin
Partially ported to google feed api.
119
        dbResult = tx.executeSql('UPDATE feed SET link=?, description=?, title=? WHERE id=?',
120
                                 [link, description, title, id])
12.1.1 by Roman Shchekin
Prepare for alpha.
121
        console.log("feed updateFeedByXml, AFFECTED ROWS: ", dbResult.rowsAffected)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
122
    }
123
    )
124
    return dbResult
125
}
126
127
function updateFeedImage(id, image)  //  offline image path
128
{
129
    var db = openStdDataBase()
130
    var dbResult
131
    db.transaction(function (tx) {
132
//        ensureFeedTableExists(tx)
133
        dbResult = tx.executeSql('UPDATE feed SET image=? WHERE id=?',
134
                                 [image, id])
135
        console.log("feed UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
136
    }
137
    )
138
    return dbResult
139
}
140
141
function updateFeedCount(id, count)  //
142
{
143
    var db = openStdDataBase()
144
    var dbResult
145
    db.transaction(function (tx) {
146
//        ensureFeedTableExists(tx)
147
        dbResult = tx.executeSql('UPDATE feed SET count=? WHERE id=?',
148
                                 [count, id])
149
        console.log("feed UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
150
    }
151
    )
152
    return dbResult
153
}
154
155
// delete
156
function deleteFeed(id)
157
{
158
    var db = openStdDataBase()
159
    var dbResult
160
    db.transaction(function (tx) {
161
//        ensureFeedTableExists(tx)
162
        tx.executeSql('PRAGMA foreign_keys = ON;')   // enable foreign key support
163
        dbResult = tx.executeSql('delete from feed WHERE id=?',
164
                                 [id])
165
        console.log("feed delete, AFFECTED ROWS: ", dbResult.rowsAffected)
166
    }
167
    )
168
    return dbResult
169
}
170
43.1.1 by Joey Chan
new feature: editable topic name
171
function deleteFeedByTagId(tagId)
172
{
173
    var db = openStdDataBase()
174
    var dbResult
175
    db.transaction(function (tx) {
176
//        ensureFeedTableExists(tx)
177
        tx.executeSql('PRAGMA foreign_keys = ON;')   // enable foreign key support
178
        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 = ?)',
179
                                 [tagId])
180
        console.log("feed delete by tag id, AFFECTED ROWS: ", dbResult.rowsAffected)
181
    }
182
    )
183
    return dbResult
184
}
185
18.2.2 by Roman Shchekin
Stable version of our app.
186
// select feeds without of topic (tag).
187
function loadFeedsWithoutTopic()
188
{
189
    var db = openStdDataBase()
190
    var dbResult
191
192
    db.transaction(function(tx) {
193
        dbResult = tx.executeSql("SELECT * FROM feed WHERE id NOT IN (SELECT feed_id FROM feed_tag)")
194
        console.log("loadFeedsWithoutTopic SELECTED: ", dbResult.rows.length)
195
    }
196
    )
197
    return dbResult;  // I suggest that return the whole result in order to know if error occurs
198
}
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
199
69.2.6 by Roman Shchekin
Memory usage optimization. No need to load whole table to get it's
200
function feedsCount() {
201
    var db = openStdDataBase()
202
    var dbResult
203
204
    db.transaction(function(tx) {
205
        dbResult = tx.executeSql("SELECT Count(*) FROM feed_tag")
206
        var obj = dbResult.rows.item(0)
207
        dbResult.count = obj["Count(*)"]
208
    }
209
    )
210
    return dbResult
211
}
212
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
213
214
/* article operations
215
 * include select, insert, update and delete operations
216
 *
217
 *
218
 */
219
// select
30.1.1 by Roman Shchekin
List mode partially implemented.
220
function loadArticles(params)   // params = {"isAll": true/false, "feedId": id | "tagId" : id}
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
221
{
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
222
    var db = openStdDataBase()
223
    var dbResult
224
225
    db.transaction(function(tx) {
30.1.1 by Roman Shchekin
List mode partially implemented.
226
        if (params == undefined || params.isAll) // miss params
57.1.2 by Joey Chan
all grid items now sort by time value
227
            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')
30.1.1 by Roman Shchekin
List mode partially implemented.
228
        else if (params.feedId)
57.1.2 by Joey Chan
all grid items now sort by time value
229
            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])
30.1.1 by Roman Shchekin
List mode partially implemented.
230
        else if (params.tagId)
57.1.2 by Joey Chan
all grid items now sort by time value
231
            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])
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
232
    }
233
    )
234
    return dbResult;
235
}
236
106.1.6 by Roman Shchekin
Almost working fav.
237
// UNUSED
238
function loadArticle(articleId)   // params = {"isAll": true/false, "feedId": id | "tagId" : id}
239
{
240
    var db = openStdDataBase()
241
    var dbResult
242
243
    db.transaction(function(tx) {
244
        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])
245
        console.assert(dbResult.rows.length !== 0, "ERROR loadArticle, " + articleId)
246
    }
247
    )
248
    return dbResult;
249
}
250
57.1.1 by Joey Chan
add "read" and "unread" states
251
// load needed properties of every article, like status, favourite, etc..
252
function preloadArticlesProperties(feedId)
253
{
254
    var db = openStdDataBase()
255
    var dbResult
256
257
    db.transaction(function(tx) {
258
        if (feedId == undefined) // miss feedId
88.2.1 by Roman Shchekin
Read/unread for list, new app name.
259
            dbResult = tx.executeSql('SELECT guid, status FROM article WHERE status = "1" ') // guid, status
57.1.1 by Joey Chan
add "read" and "unread" states
260
        else
88.2.1 by Roman Shchekin
Read/unread for list, new app name.
261
            dbResult = tx.executeSql('SELECT guid, status FROM article WHERE feed_id = ? AND status = "1" ', [feedId])
57.1.1 by Joey Chan
add "read" and "unread" states
262
    }
263
    )
264
    console.log("preloadArticlesProperties: ", dbResult.rows.length)
265
    return dbResult;
266
}
267
64.1.1 by Joey Chan
New feature: "Saved" simply implemented, include "Saved" tab and related gridview, listview will be implemented by Roman soon
268
// load all favourite articles
269
function loadFavouriteArticles()
270
{
271
    var db = openStdDataBase()
272
    var dbResult
273
274
    db.transaction(function(tx) {
275
        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' )
276
    }
277
    )
106.1.9 by Roman Shchekin
Yeah, it works! Need to test it!
278
    //console.log("loadFavouriteArticles", dbResult.rows.length)
106.1.11 by Roman Shchekin
Little bug fixed.
279
    //console.assert(dbResult.rows.length !== 0,  "ERROR: There are no saved articles")
64.1.1 by Joey Chan
New feature: "Saved" simply implemented, include "Saved" tab and related gridview, listview will be implemented by Roman soon
280
    return dbResult;
281
}
282
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
283
// insert
12.1.2 by Roman Shchekin
Alpha version. ListView used as main view, logic based on
284
function addArticle(title, content, link, description, pubdate, guid, feed_id)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
285
{
286
    var dbResult
287
    var db = openStdDataBase()
288
    db.transaction(function (tx) {
289
//        ensureFeedTableExists(tx)
290
291
        /* Check uniqueness.
292
         */
293
        dbResult = tx.executeSql("SELECT * FROM article WHERE guid=?", [guid])
294
        if (dbResult.rows.length > 0) {
12.1.2 by Roman Shchekin
Alpha version. ListView used as main view, logic based on
295
            console.log("Database, add article: already exist article with guid: ", guid)
13.1.12 by Roman Shchekin
Feeds management ready, topics to go!
296
            dbResult = {"error": true, "exist": true}
297
            return
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
298
        }
12.1.2 by Roman Shchekin
Alpha version. ListView used as main view, logic based on
299
        dbResult = tx.executeSql('INSERT INTO article (title, content, link, description, pubdate, guid, feed_id) VALUES(?, ?, ?, ?, ?, ?, ?)',
300
                                 [title, content, link, description, pubdate, guid, feed_id])
23.2.5 by Roman Shchekin
Some little changes...
301
302
        dbResult.articleId = tx.executeSql("SELECT * FROM article WHERE guid=?", [guid]).rows.item(0).id
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
303
    }
304
    )
305
    return dbResult;
306
}
307
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
308
/*
309
  this function is for avoiding hard drive performance issue,
310
  pass model and feed id as parameters to this function, it will automaticly insert all the articles into database
57.1.1 by Joey Chan
add "read" and "unread" states
311
  add third pamams which is for restoring articles' properties
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
312
 */
57.1.1 by Joey Chan
add "read" and "unread" states
313
function addArticles(model, feed_id, restoreArray)
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
314
{
315
    var dbResult
316
317
    var db = openStdDataBase()
318
    db.transaction(function (tx) {
319
320
        var article;
321
        for (var i = 0; i < model.count; i++) {
322
323
            article = model.get(i)
324
            var title =  article.title == undefined ? "" : article.title
325
            var guid =  article.guid == undefined ? Qt.md5(title) : article.guid
326
            var link =  article.link == undefined ? "" : article.link
327
            var pubDate =  article.pubDate == undefined ? "" : article.pubDate
328
            var description =  article.description == undefined ? "" : article.description
329
            var content =  article.content == undefined ? "" : article.content
29.1.1 by Roman Shchekin
A lot of little fixes of careless mistakes.
330
            var image =  article.image == undefined ? "" : article.image
93.1.1 by Roman Shchekin
Media groups now in DB too.
331
            var media_groups = article.media_groups == undefined ? "" : JSON.stringify(article.media_groups)
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
332
333
            /* Check uniqueness.
334
             */
335
            dbResult = tx.executeSql("SELECT * FROM article WHERE guid=? AND feed_id=?", [guid, feed_id])
336
            if (dbResult.rows.length > 0) {
337
                console.log("Database, add article: already exist article with source: ", guid)
338
                continue;
339
            }
93.1.1 by Roman Shchekin
Media groups now in DB too.
340
            dbResult = tx.executeSql('INSERT INTO article (title, content, link, description, pubdate, guid, feed_id, image, media_groups) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)',
341
                                     [title, content, link, description, pubDate, guid, feed_id, image, media_groups])
31.1.1 by Roman Shchekin
Added dialog for "Refresh" operation!
342
            // console.log("article INSERT ID: ", JSON.stringify(dbResult) )
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
343
        }
57.1.1 by Joey Chan
add "read" and "unread" states
344
345
        if (restoreArray != undefined) {
346
            for (var j = 0; j < restoreArray.rows.length; j++) {
347
                dbResult = tx.executeSql('UPDATE article SET status=? WHERE guid=? AND status="0"',
348
                                         [restoreArray.rows[j].status, restoreArray.rows[j].guid])
349
            }
350
        }
13.1.1 by Joey Chan
bugs fixed, especially add a function "addArticles" to avoid the hard drive performance issue
351
    }
352
    )
353
    return dbResult;
354
}
355
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
356
// update
357
function updateArticleStatus(id, status)
358
{
359
    var db = openStdDataBase()
360
    var dbResult
361
    db.transaction(function (tx) {
362
//        ensureFeedTableExists(tx)
10.2.2 by Joey Chan
bugs fixed for database module
363
        dbResult = tx.executeSql('UPDATE article SET status=? WHERE id=?',
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
364
                                 [status, id])
64.1.1 by Joey Chan
New feature: "Saved" simply implemented, include "Saved" tab and related gridview, listview will be implemented by Roman soon
365
        console.log("article status UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
366
    }
367
    )
368
    return dbResult
369
}
370
371
function updateArticleFavourite(id, favourite)
372
{
373
    var db = openStdDataBase()
374
    var dbResult
375
    db.transaction(function (tx) {
376
//        ensureFeedTableExists(tx)
10.2.2 by Joey Chan
bugs fixed for database module
377
        dbResult = tx.executeSql('UPDATE article SET favourite=? WHERE id=?',
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
378
                                 [favourite, id])
64.1.1 by Joey Chan
New feature: "Saved" simply implemented, include "Saved" tab and related gridview, listview will be implemented by Roman soon
379
        console.log("article favourite UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
380
    }
381
    )
382
    return dbResult
383
}
384
385
function updateArticleImage(id, image)  //  offline image path
386
{
387
    var db = openStdDataBase()
388
    var dbResult
389
    db.transaction(function (tx) {
390
//        ensureFeedTableExists(tx)
10.2.2 by Joey Chan
bugs fixed for database module
391
        dbResult = tx.executeSql('UPDATE article SET image=? WHERE id=?',
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
392
                                 [image, id])
23.2.5 by Roman Shchekin
Some little changes...
393
        //console.log("article UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
394
    }
395
    )
396
    return dbResult
397
}
398
399
400
// delete
401
function deleteArticle(id)
402
{
403
    var db = openStdDataBase()
404
    var dbResult
405
    db.transaction(function (tx) {
406
//        ensureFeedTableExists(tx)
407
        dbResult = tx.executeSql('delete from article WHERE id=?',
408
                                 [id])
409
        console.log("article delete, AFFECTED ROWS: ", dbResult.rowsAffected)
410
    }
411
    )
412
    return dbResult
413
}
414
10.2.2 by Joey Chan
bugs fixed for database module
415
// clear article table, only status='2' and favourite='1' remain
12.1.2 by Roman Shchekin
Alpha version. ListView used as main view, logic based on
416
function clearArticles(feed_id)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
417
{
418
    var db = openStdDataBase()
419
    var dbResult
420
    db.transaction(function (tx) {
421
//        ensureFeedTableExists(tx)
10.2.2 by Joey Chan
bugs fixed for database module
422
        dbResult = tx.executeSql("delete from article WHERE (status='0' OR status='1') AND favourite='0' AND feed_id=?", [feed_id])
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
423
        console.log("article delete, AFFECTED ROWS: ", dbResult.rowsAffected)
424
    }
425
    )
426
    return dbResult
427
}
428
429
430
/* tag operations
431
 * include select, insert, update and delete operations
432
 *
433
 *
434
 */
435
// select
436
function loadTags()
437
{
438
    var db = openStdDataBase()
439
    var dbResult
440
441
    db.transaction(function(tx) {
442
        dbResult = tx.executeSql("SELECT * FROM tag")
29.1.1 by Roman Shchekin
A lot of little fixes of careless mistakes.
443
        console.assert(dbResult.rows.length !== 0, "ERROR: NO TAGS DATABASE")
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
444
    }
445
    )
446
    return dbResult;
447
}
448
449
// insert
450
function addTag(name)
451
{
452
    var dbResult
453
    var db = openStdDataBase()
454
    db.transaction(function (tx) {
455
//        ensureFeedTableExists(tx)
456
457
        /* Check uniqueness.
458
         */
459
        dbResult = tx.executeSql("SELECT * FROM tag WHERE name=?", [name])
460
        if (dbResult.rows.length > 0) {
10.2.2 by Joey Chan
bugs fixed for database module
461
            console.log("Database, add tag: already exist tag with source: ", name)
13.1.12 by Roman Shchekin
Feeds management ready, topics to go!
462
            dbResult = {"error": true, "exist": true}
463
            return
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
464
        }
465
466
        dbResult = tx.executeSql('INSERT INTO tag (name) VALUES(?)',
467
                                 [name])
13.1.7 by Joey Chan
final version demo of RSS reader, includes topic view, single article view
468
        console.log("tag INSERT ID: ", dbResult.insertId)
13.1.13 by Roman Shchekin
Moving on - now can add topic.
469
470
        dbResult.tagId = tx.executeSql("SELECT * FROM tag WHERE name=?", [name]).rows.item(0).id
471
        console.log("dbResult.tagId", dbResult.tagId)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
472
    }
473
    )
474
    return dbResult;
475
}
476
477
// update
478
function updateTag(id, name)
479
{
480
    var db = openStdDataBase()
481
    var dbResult
482
    db.transaction(function (tx) {
483
//        ensureFeedTableExists(tx)
484
        dbResult = tx.executeSql('UPDATE tag SET name=? WHERE id=?',
485
                                 [name, id])
486
        console.log("tag UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
487
    }
488
    )
489
    return dbResult
490
}
491
492
// delete
493
function deleteTag(id)
494
{
495
    var db = openStdDataBase()
496
    var dbResult
497
    db.transaction(function (tx) {
498
//        ensureFeedTableExists(tx)
499
        dbResult = tx.executeSql('delete from tag WHERE id=?',
500
                                 [id])
501
        console.log("tag delete, AFFECTED ROWS: ", dbResult.rowsAffected)
502
    }
503
    )
504
    return dbResult
505
}
506
507
508
/* feed_tag operations
509
 * include select, insert and delete operations
510
 *
511
 *
512
 */
513
// select
514
function loadFeedTags()
515
{
516
    var db = openStdDataBase()
517
    var dbResult
518
519
    db.transaction(function(tx) {
520
        dbResult = tx.executeSql("SELECT * FROM feed_tag")
521
    }
522
    )
523
    return dbResult;
524
}
525
13.1.7 by Joey Chan
final version demo of RSS reader, includes topic view, single article view
526
function loadFeedsFromTag(tag_id)
527
{
528
    var db = openStdDataBase()
529
    var dbResult
530
531
    db.transaction(function(tx) {
30.1.1 by Roman Shchekin
List mode partially implemented.
532
        dbResult = tx.executeSql("SELECT t1.* FROM feed t1 INNER JOIN feed_tag t2 ON t1.id = t2.feed_id WHERE tag_id =?", [tag_id])
68.1.2 by Roman Shchekin
My speedups. I've got between 150 and 500 % performance improvement depending on amount of articles in Tab.
533
        // console.log("loadFeedsFromTag:", tag_id, "SELECTED: ", dbResult.rows.length)
13.1.7 by Joey Chan
final version demo of RSS reader, includes topic view, single article view
534
    }
535
    )
536
    return dbResult;
537
}
538
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
539
// insert
540
function addFeedTag(feed_id, tag_id)
541
{
542
    var dbResult
543
    var db = openStdDataBase()
544
    db.transaction(function (tx) {
545
//        ensureFeedTableExists(tx)
546
547
        /* Check uniqueness.
548
         */
549
        dbResult = tx.executeSql("SELECT * FROM feed_tag WHERE feed_id=? AND tag_id=? ", [feed_id, tag_id])
550
        if (dbResult.rows.length > 0) {
10.2.2 by Joey Chan
bugs fixed for database module
551
            console.log("Database, add feed_tag: already exist feed_tag with source: ", feed_id, tag_id)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
552
            return {"error": true, "exist": true};
553
        }
554
555
        dbResult = tx.executeSql('INSERT INTO feed_tag (feed_id, tag_id) VALUES(?, ?)',
556
                                 [feed_id, tag_id])
10.2.2 by Joey Chan
bugs fixed for database module
557
        console.log("feed_tag INSERT ID: ", dbResult.insertId)
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
558
    }
559
    )
560
    return dbResult;
561
}
562
563
// delete
564
function deleteFeedTag(id)
565
{
566
    var db = openStdDataBase()
567
    var dbResult
568
    db.transaction(function (tx) {
569
//        ensureFeedTableExists(tx)
570
        dbResult = tx.executeSql('delete from feed_tag WHERE id=?',
571
                                 [id])
572
        console.log("feed_tag delete, AFFECTED ROWS: ", dbResult.rowsAffected)
573
    }
574
    )
575
    return dbResult
576
}
577
13.1.14 by Roman Shchekin
2 pages to go.
578
// delete
579
function deleteFeedTagsByTagId(tagId)
580
{
581
    var db = openStdDataBase()
582
    var dbResult
583
    db.transaction(function (tx) {
584
//        ensureFeedTableExists(tx)
585
        dbResult = tx.executeSql('delete from feed_tag WHERE tag_id=?',
586
                                 [tagId])
587
        console.log("feed_tag delete, AFFECTED ROWS: ", dbResult.rowsAffected)
588
    }
589
    )
590
    return dbResult
591
}
592
13.1.11 by Roman Shchekin
Basis of topics management.
593
function deleteFeedTag(feedId, tagId)
594
{
595
    var db = openStdDataBase()
596
    var dbResult
597
598
    db.transaction(function(tx) {
599
        dbResult = tx.executeSql("DELETE FROM feed_tag WHERE feed_id = ? AND tag_id = ?", [feedId, tagId])
43.1.1 by Joey Chan
new feature: editable topic name
600
        console.log("feed_tag delete by feedId and tagId: ", dbResult.rowsAffected)
13.1.11 by Roman Shchekin
Basis of topics management.
601
    }
602
    )
603
    return dbResult;
604
}
605
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
606
607
/* settings operations
608
 * include select and update operations
609
 *
610
 *
611
 */
612
// select  //for each setting
613
function getDBVersion()
614
{
615
    var db = openStdDataBase()
616
    var dbResult
617
    var dbVersion = ""
618
619
    db.transaction(function(tx) {
620
        dbResult = tx.executeSql("SELECT current_database_version FROM settings where id=1")
621
        if (dbResult.rows.length > 0)
622
        {
623
            dbVersion = dbResult.rows.item(0).current_database_version ;
624
        }
625
626
    }
627
    )
628
    return dbVersion;
629
}
630
631
function getDBLastUpdate()
632
{
633
    var db = openStdDataBase()
634
    var dbResult
635
    var dbLastUpdate = ""
636
637
    db.transaction(function(tx) {
638
        dbResult = tx.executeSql("SELECT database_last_updated FROM settings where id=1")
639
        if (dbResult.rows.length > 0)
640
        {
641
            dbLastUpdate = dbResult.rows.item(0).database_last_updated ;
642
        }
643
644
    }
645
    )
646
    return dbLastUpdate;
647
}
648
69.2.1 by Roman Shchekin
"Reading options" popup implemented, new interface for options. Team, I encourage you to use it in your code too.
649
/* I want to use update_interval field for storing
650
 * font and light/dark theme.
651
 * INTEGER BYTES: 00 00 <useDark 1 | 0> <fontSize value>
652
 */
653
function getFontSize()
654
{
655
    var db = openStdDataBase()
656
    var dbResult
657
    var dbValue = 0
658
659
    db.transaction(function(tx) {
660
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
661
        if (dbResult.rows.length > 0)
662
        {
663
            dbValue = dbResult.rows.item(0).update_interval
664
            dbValue = 0xFF & dbValue
665
        }
666
    }
667
    )
668
    return dbValue;
669
}
670
671
function getUseDarkTheme()
672
{
673
    var db = openStdDataBase()
674
    var dbResult
675
    var result
676
677
    db.transaction(function(tx) {
678
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
679
        if (dbResult.rows.length > 0)
680
        {
681
            var dbValue = dbResult.rows.item(0).update_interval
682
            dbValue = 0xFF00 & dbValue
683
            result = (dbValue !== 0)
684
        }
685
    }
686
    )
687
    return result;
688
}
689
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
690
function getViewMode()
691
{
692
    var db = openStdDataBase()
693
    var dbResult
694
    var dbViewMode = ""
695
696
    db.transaction(function(tx) {
697
        dbResult = tx.executeSql("SELECT view_mode FROM settings where id=1")
698
        if (dbResult.rows.length > 0)
699
        {
700
            dbViewMode = dbResult.rows.item(0).view_mode ;
701
        }
702
703
    }
704
    )
705
    return dbViewMode;
706
}
707
708
function getUpdateInterval()
709
{
710
    var db = openStdDataBase()
711
    var dbResult
10.2.2 by Joey Chan
bugs fixed for database module
712
    var dbUpdateInterval = 0
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
713
714
    db.transaction(function(tx) {
715
        dbResult = tx.executeSql("SELECT update_interval FROM settings where id=1")
716
        if (dbResult.rows.length > 0)
717
        {
718
            dbUpdateInterval = dbResult.rows.item(0).update_interval ;
719
        }
720
721
    }
722
    )
723
    return dbUpdateInterval;
724
}
725
726
function getNetworkMode()
727
{
728
    var db = openStdDataBase()
729
    var dbResult
730
    var dbNetworkMode = ""
731
732
    db.transaction(function(tx) {
733
        dbResult = tx.executeSql("SELECT network_mode FROM settings where id=1")
734
        if (dbResult.rows.length > 0)
735
        {
736
            dbNetworkMode = dbResult.rows.item(0).network_mode ;
737
        }
738
739
    }
740
    )
741
    return dbNetworkMode;
742
}
743
744
// update  //for each setting
745
function setDBVersion(current_database_version)
746
{
747
    var db = openStdDataBase()
748
    var dbResult
749
    db.transaction(function (tx) {
750
//        ensureFeedTableExists(tx)
751
        dbResult = tx.executeSql('UPDATE settings SET current_database_version=? WHERE id=1',
752
                                 [current_database_version])
753
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
754
    }
755
    )
756
    return dbResult
757
}
758
759
function setDBLastUpdate(database_last_updated)
760
{
761
    var db = openStdDataBase()
762
    var dbResult
763
    db.transaction(function (tx) {
764
//        ensureFeedTableExists(tx)
765
        dbResult = tx.executeSql('UPDATE settings SET database_last_updated=? WHERE id=1',
766
                                 [database_last_updated])
767
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
768
    }
769
    )
770
    return dbResult
771
}
772
69.2.1 by Roman Shchekin
"Reading options" popup implemented, new interface for options. Team, I encourage you to use it in your code too.
773
function setFontSize(fontSize)
774
{
775
    var db = openStdDataBase()
776
    var dbResult
777
778
    db.transaction(function (tx) {
779
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
780
        if (dbResult.rows.length > 0)
781
        {
782
            var dbValue = dbResult.rows.item(0).update_interval
783
            var newValue = (0xFF & fontSize) | (dbValue & 0xFFFFFF00)
784
785
            dbResult = tx.executeSql('UPDATE settings SET update_interval=? WHERE id=1',
786
                                     [newValue])
787
        }
788
    }
789
    )
790
    return dbResult
791
}
792
793
function setUseDarkTheme(useDarkTheme)
794
{
795
    useDarkTheme = useDarkTheme ? 1 : 0
796
    var db = openStdDataBase()
797
    var dbResult
798
    db.transaction(function (tx) {
799
        dbResult = tx.executeSql("SELECT update_interval FROM settings WHERE id=1")
800
        if (dbResult.rows.length > 0)
801
        {
802
            var dbValue = dbResult.rows.item(0).update_interval
803
            var newValue = (/*0xFF & */useDarkTheme << 8) | (dbValue & 0xFFFF00FF)
804
805
            dbResult = tx.executeSql('UPDATE settings SET update_interval=? WHERE id=1',
806
                                     [newValue])
807
        }
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
808
    }
809
    )
810
    return dbResult
811
}
812
69.2.2 by Roman Shchekin
Added saving state of view: "list" or "grid".
813
function setViewMode(value)
814
{
815
    var db = openStdDataBase()
816
    var dbResult
817
818
    db.transaction(function(tx) {
819
        dbResult = tx.executeSql("SELECT view_mode FROM settings where id=1")
820
        if (dbResult.rows.length > 0)
821
        {
822
            dbResult = tx.executeSql('UPDATE settings SET view_mode=? WHERE id=1',
823
                                     [value])
824
        }
825
826
    }
827
    )
828
    return dbResult;
829
}
830
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
831
function setUpdateInterval(update_interval)
832
{
833
    var db = openStdDataBase()
834
    var dbResult
835
    db.transaction(function (tx) {
836
//        ensureFeedTableExists(tx)
837
        dbResult = tx.executeSql('UPDATE settings SET update_interval=? WHERE id=1',
838
                                 [update_interval])
839
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
840
    }
841
    )
842
    return dbResult
843
}
844
845
function setNetworkMode(network_mode)
846
{
847
    var db = openStdDataBase()
848
    var dbResult
849
    db.transaction(function (tx) {
850
//        ensureFeedTableExists(tx)
851
        dbResult = tx.executeSql('UPDATE settings SET network_mode=? WHERE id=1',
852
                                 [network_mode])
853
        console.log("settings UPDATE, AFFECTED ROWS: ", dbResult.rowsAffected)
854
    }
855
    )
856
    return dbResult
857
}
858
859
860
861
/* operations for testing
862
 * include clear and drop operations
863
 * not completed yet
864
 *
865
 */
866
// clear
867
function clearData(table)
868
{
869
    var db = openStdDataBase()
870
10.2.2 by Joey Chan
bugs fixed for database module
871
    switch(table)
872
    {
873
    case "feed":
874
        db.transaction(function(tx) {
875
            tx.executeSql("delete from feed")
876
            console.log("feed clear")
877
        }
878
        )
879
        break;
880
    case "article":
881
        db.transaction(function(tx) {
882
            tx.executeSql("delete from article")
883
            console.log("article clear")
884
        }
885
        )
886
        break;
887
    case "tag":
888
        db.transaction(function(tx) {
889
            tx.executeSql("delete from tag")
890
            console.log("tag clear")
891
        }
892
        )
893
        break;
894
    case "feed_tag":
895
        db.transaction(function(tx) {
896
            tx.executeSql("delete from feed_tag")
897
            console.log("feed_tag clear")
898
        }
899
        )
900
        break;
901
    case "settings":
902
        db.transaction(function(tx) {
903
            tx.executeSql("delete from settings")
904
            console.log("settings clear")
905
        }
906
        )
907
        break;
908
    default:
909
        db.transaction(function(tx) {
910
            tx.executeSql("delete from feed_tag")
911
            tx.executeSql("delete from feed")
912
            tx.executeSql("delete from tag")
913
            tx.executeSql("delete from article")
914
            tx.executeSql("delete from settings")
915
            console.log("DATABASE clear")
916
        }
917
        )
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
918
    }
919
}
920
921
// drop
922
function dropTable(table)
923
{
924
    var db = openStdDataBase()
925
10.2.2 by Joey Chan
bugs fixed for database module
926
    switch(table)
927
    {
928
    case "feed":
929
        db.transaction(function(tx) {
930
            tx.executeSql("DROP TABLE IF EXISTS feed")
931
            console.log("feed deleted")
932
        }
933
        )
934
        break;
935
    case "article":
936
        db.transaction(function(tx) {
937
            tx.executeSql("DROP TABLE IF EXISTS article")
938
            console.log("article deleted")
939
        }
940
        )
941
        break;
942
    case "tag":
943
        db.transaction(function(tx) {
944
            tx.executeSql("DROP TABLE IF EXISTS tag")
945
            console.log("tag deleted")
946
        }
947
        )
948
        break;
949
    case "feed_tag":
950
        db.transaction(function(tx) {
951
            tx.executeSql("DROP TABLE IF EXISTS feed_tag")
952
            console.log("feed_tag deleted")
953
        }
954
        )
955
        break;
956
    case "settings":
957
        db.transaction(function(tx) {
958
            tx.executeSql("DROP TABLE IF EXISTS settings")
959
            console.log("settings deleted")
960
        }
961
        )
962
        break;
963
    default:
964
        db.transaction(function(tx) {
965
            tx.executeSql("DROP TABLE IF EXISTS feed")
966
            tx.executeSql("DROP TABLE IF EXISTS article")
967
            tx.executeSql("DROP TABLE IF EXISTS tag")
968
            tx.executeSql("DROP TABLE IF EXISTS feed_tag")
969
            tx.executeSql("DROP TABLE IF EXISTS settings")
970
            console.log("DATABASE deleted")
971
        }
972
        )
10.2.1 by Joey Chan
first complete version of database, feel free to find bugs or help us to improve it :)
973
    }
974
}