~rpadovani/ubuntu-calculator-app/4Sep2013

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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This file is part of ubuntu-calculator-app.
 *
 * ubuntu-calculator-app is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * ubuntu-calculator-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.0
import QtQuick.LocalStorage 2.0
import "./dateutils.js" as DateUtils

Item {
    property var db: null

    function openDB() {
        if(db !== null) return;

        db = LocalStorage.openDatabaseSync("ubuntu-calculator-app", "", "Default Ubuntu touch calculator", 100000);
        upgradeDB(db.version);
    }

    /* We need this function because db.version is in the .ini file, that is update only by Javascript Garbage Collection.
     * So, we have to upgrade from actual version to the last version using only once db.changeVersion.
     * To avoid to have a lot of switch and spaghetti-code, this function allow to add new db version without modify old sql
     *
     * IMPORTANT: NUMBER OF VERSION HAVE TO BE INT (e.g. 0.1, not 0.1.1)
     */
    function upgradeDB() {
        // This is the array with all the sql code, insert your update at the last
        var sqlcode = [
            'CREATE TABLE IF NOT EXISTS Calculations(id INTEGER PRIMARY KEY, calc TEXT)',
            'ALTER TABLE Calculations ADD insertDate INTEGER NOT NULL DEFAULT 0'
        ]

        // This is the last version of the DB, remember to update when you insert a new version
        var lastVersion = "0.2";

        // Hack for change old numeration with new one
        if (db.version == "0.1.1") {
            db.changeVersion("0.1.1", "0.2");
            console.log("Fixed DB!");
        }

        // So, let's start the version change...
        db.changeVersion(db.version, lastVersion,
            function(tx) {
                if (db.version < 0.1) {
                    tx.executeSql(sqlcode[0]);
                    console.log('Database upgraded to 0.1');
                }
                if (db.version < 0.2) {
                    tx.executeSql(sqlcode[1]);
                    console.log('Database upgraded to 0.2');
                }
                
                /* This is the structure of the update:
                 * n is the number of version that sql update to
                 * m is the number of the sql element in the array. Remember that the number of the first element of array is 0 ;)
                if (db.version < n) {
                    tx.executeSql(sqlcode[m]);
                    console.log('Database upgraded to n');
                }
                */
            }); // Finish db.changeVersion
    }

    function getCalculations(callback){
        openDB();
        db.transaction(
                    function(tx){
                        var res = tx.executeSql('SELECT * FROM Calculations');
                        for(var i=res.rows.length - 1; i >= 0; i--){
                            var obj = JSON.parse(res.rows.item(i).calc);
                            obj.dbId = res.rows.item(i).id;
                            obj.dateText  = DateUtils.formatRelativeTime(i18n, res.rows.item(i).insertDate);

                            if (!('mainLabel' in obj))
                                obj.mainLabel = ''

                            callback(obj);
                        }
                    }
                    );
    }

    function saveCalculations(calculations){
        openDB();
        var res;
        db.transaction( function(tx){
            var r = tx.executeSql('INSERT INTO Calculations(calc, insertDate) VALUES(?, ?)', [JSON.stringify(calculations[0].calc), calculations[0].date]);
            res = r.insertId;
        });
        return res;
    }

    function removeCalculation(calc){
        openDB();
        db.transaction(function(tx){
            tx.executeSql('DELETE FROM Calculations WHERE id = ?', [calc.dbId]);
        });
    }

    // Function to have time from an ID
    function fromIdToDate (dbId) {
        var time;
        openDB();
        db.transaction(
            function(tx){
                var res = tx.executeSql('SELECT insertDate FROM Calculations WHERE id = ?', dbId);
                time = res.rows.item(0).insertDate;
             }
        );
        return time;
    }
}