~mhall119/+junk/rss-gridview-demo

« back to all changes in this revision

Viewing changes to databasemodule.js

  • Committer: Tarmac
  • Author(s): Roman Shchekin, mrqtros
  • Date: 2013-03-12 09:16:28 UTC
  • mfrom: (2.1.4 ubuntu-rssreader-app)
  • Revision ID: tarmac-20130312091628-57wst0qv8mp3xlxo
In this version you can find list of feeds and ability to append and remove items from it. Works on XMLHttpRequest.

Approved by Roman Shchekin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file will include set of functions
 
2
 * to store, retrieve and modify data from database.
 
3
 */
 
4
 
 
5
.import QtQuick.LocalStorage 2.0 as SQL
 
6
 
 
7
 
 
8
/* For internal usage in module.
 
9
 */
 
10
function openStdDataBase() {
 
11
    return SQL.LocalStorage.openDatabaseSync("RSS Reader", "1.0", "Feed DB", 10000)
 
12
}
 
13
 
 
14
function ensureTableExists(transaction) {
 
15
    transaction.executeSql('CREATE TABLE IF NOT EXISTS UserFeeds(link STRING PRIMARY KEY, displayName STRING, siteLink STRING, description STRING)')
 
16
}
 
17
/* Internal module usage end. */
 
18
 
 
19
/* This variable will store result of any operation with database.
 
20
 */
 
21
var dbResult
 
22
 
 
23
/* Load all feed from database.
 
24
 * Mb later we will need some criteria, such as tag, date and so on!
 
25
 */
 
26
function loadFeeds() {
 
27
    var db = openStdDataBase()
 
28
 
 
29
    db.transaction(function(tx) {
 
30
        ensureTableExists(tx)
 
31
        dbResult = tx.executeSql("SELECT * FROM UserFeeds")
 
32
        console.log("SELECTED: ", dbResult.rows.length)
 
33
    }
 
34
    )
 
35
}
 
36
 
 
37
/* Add feed to database.
 
38
 * Now only 3 fields, later - more (date, tag and etc.)
 
39
 */
 
40
function addFeed(link, displayName, siteLink, description) {
 
41
    displayName = displayName || ""
 
42
    siteLink = siteLink || ""
 
43
    description = description || ""
 
44
 
 
45
    var db = openStdDataBase()
 
46
 
 
47
    db.transaction(function (tx) {
 
48
        ensureTableExists(tx)
 
49
        dbResult = tx.executeSql('INSERT INTO UserFeeds VALUES(?, ?, ?, ?)', [link , displayName, siteLink, description])
 
50
        console.log("INSERT ID", dbResult.insertId)
 
51
    }
 
52
    )
 
53
}
 
54
 
 
55
function updateWithInfo(link, displayName, siteLink, description) {
 
56
    var db = openStdDataBase()
 
57
 
 
58
    db.transaction(function (tx) {
 
59
        ensureTableExists(tx)
 
60
        dbResult = tx.executeSql('UPDATE UserFeeds SET displayName=?, siteLink=?, description=? WHERE link=?', [displayName, siteLink, description, link])
 
61
        console.log("UPDATE, AFFECTED ROWS", dbResult.rowsAffected)
 
62
    }
 
63
    )
 
64
}
 
65
 
 
66
function deleteFeed(link) {
 
67
    var db = openStdDataBase()
 
68
 
 
69
    db.transaction(function (tx) {
 
70
        ensureTableExists(tx)
 
71
        dbResult = tx.executeSql('DELETE FROM UserFeeds WHERE link = ?', [link])
 
72
        console.log("DELETE, AFFECTED ROWS", dbResult.rowsAffected)
 
73
    }
 
74
    )
 
75
}
 
76
 
 
77
/* Only for debug purposes.
 
78
 */
 
79
function dropTable() {
 
80
    var db = openStdDataBase()
 
81
 
 
82
    db.transaction(function(tx) {
 
83
        tx.executeSql("DROP TABLE IF EXISTS UserFeeds")
 
84
        console.log("DATABASE DELETED")
 
85
    }
 
86
    )
 
87
}
 
88
 
 
89