Debian Codegraph @

src/app/qml/data/Storage.qml

raw
1 /*
2 * Copyright (C) 2013, 2014, 2015 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Martin Borho <[email protected]>
17 */
18 import QtQuick.LocalStorage 2.0
19 import QtQuick 2.12
20
21 Item {
22 property var db: null
23
24 function openDB() {
25 if(db !== null) return;
26
27 db = LocalStorage.openDatabaseSync("weather.ubports", "", "Default Lomiri weather app", 100000);
28
29 if (db.version === "") {
30 db.changeVersion("", "0.1",
31 function(tx) {
32 tx.executeSql('CREATE TABLE IF NOT EXISTS Locations(id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT, date TEXT)');
33 console.log('Database created');
34 });
35 // reopen database with new version number
36 db = LocalStorage.openDatabaseSync("weather.ubports", "", "Default Lomiri weather app", 100000);
37 }
38
39 if(db.version === "0.1") {
40 db.changeVersion("0.1", "0.2",
41 function(tx) {
42 tx.executeSql('CREATE TABLE IF NOT EXISTS settings(key TEXT UNIQUE, value TEXT)');
43 console.log('Settings table added, Database upgraded to v0.2');
44 });
45 // reopen database with new version number
46 db = LocalStorage.openDatabaseSync("weather.ubports", "", "Default Lomiri weather app", 100000);
47 }
48
49 if(db.version === "0.2") {
50 db.changeVersion("0.2", "0.3",
51 function(tx) {
52 tx.executeSql('DELETE FROM Locations WHERE 1');
53 console.log('Removed old locations, Database upgraded to v0.3');
54 });
55 }
56
57 if (!settings.migrated) {
58 try { // attempt to read the old settings
59 var oldSettings = {};
60
61 // Load old settings
62 db.readTransaction( function(tx) {
63 var rs = tx.executeSql("SELECT * FROM settings")
64
65 for(var i = 0; i < rs.rows.length; i++) {
66 var row = rs.rows.item(i);
67 oldSettings[row.key] = row.value;
68 }
69 });
70
71 console.debug("Migrating old data:", JSON.stringify(oldSettings))
72
73 // Move to new Settings API
74 settings.migrated = true
75 settings.service = oldSettings["service"] === undefined ? "openmeteo" : oldSettings["service"]
76 settings.precipUnit = oldSettings["precipUnit"] === undefined ? "mm" : oldSettings["precipUnit"]
77 settings.tempScale = oldSettings["tempUnit"] === undefined ? "°C" : oldSettings["tempUnit"]
78 settings.windUnit = oldSettings["windUnit"] === undefined ? "m/s" : oldSettings["windUnit"]
79
80 /*
81 TODO: uncomment when reboot is ready to replace existing app
82 db.transaction( function(tx) {
83 tx.executeSql("DROP TABLE IF EXISTS settings")
84 });
85 */
86 } catch (e) { // likely table did not exist
87 console.debug("No old data to migrate.")
88 settings.migrated = true
89 }
90 }
91 }
92
93 function insertLocation(data) {
94 openDB();
95 var res;
96
97 db.transaction( function(tx){
98 var r = tx.executeSql('INSERT INTO Locations(data, date) VALUES(?, ?)', [JSON.stringify(data), new Date().getTime()]);
99 res = r.insertId;
100 });
101 return res;
102 }
103
104 function insertLocationAtStart(data) {
105 var res = insertLocation(data);
106 reorder(res, 0);
107 return 0;
108 }
109
110 function updateLocation(dbId, data) {
111 openDB();
112 db.transaction( function(tx){
113 var r = tx.executeSql('UPDATE Locations SET data = ?, date=? WHERE id = ?', [JSON.stringify(data), new Date().getTime(), dbId])
114 });
115 }
116
117 function getLocations(callback) {
118 openDB();
119 db.readTransaction(
120 function(tx){
121 var locations = [];
122 var rs = tx.executeSql('SELECT * FROM Locations');
123 for(var i = 0; i < rs.rows.length; i++) {
124 var row = rs.rows.item(i),
125 locData = JSON.parse(row.data);
126 locData["updated"] = parseInt(row.date, 10);
127 locData["db"] = {id: row.id, updated: new Date(parseInt(row.date, 10))};
128 locations.push(locData);
129 }
130 callback(locations);
131 }
132 );
133 }
134
135 function clearLocation(location_id) {
136 openDB();
137 db.transaction(function(tx){
138 tx.executeSql('DELETE FROM Locations WHERE id = ?', [location_id]);
139 });
140 }
141
142 function clearMultiLocation(locations) {
143 openDB();
144
145 db.transaction(function (tx) {
146 // Remove all the deleted indexes
147 for (var i=0; i < locations.length; i++) {
148 tx.executeSql('DELETE FROM Locations WHERE id=?;', [locations[i]])
149 }
150
151 // Rebuild locations in order
152 var rs = tx.executeSql('SELECT id FROM Locations ORDER BY id ASC')
153
154 for (i=0; i < rs.rows.length; i++) {
155 tx.executeSql('UPDATE Locations SET id=? WHERE id=?;',
156 [i, rs.rows.item(i).id])
157 }
158 })
159 }
160
161 function clearDB() { // for dev purposes
162 openDB();
163 db.transaction(function(tx){
164 tx.executeSql('DELETE FROM Locations WHERE 1');
165 });
166 }
167
168 function reorder(from, to) {
169 openDB();
170
171 db.transaction(function(tx) {
172 // Track to move put as -1 for now
173 tx.executeSql('UPDATE Locations SET id=? WHERE id=?;',
174 [-1, from])
175
176 // Shuffle locations inbetween from->to
177 if (from > to) {
178 for (var i = from-1; i >= to; i--) {
179 tx.executeSql('UPDATE Locations SET id=? WHERE id=?;',
180 [i+1, i])
181 }
182 } else {
183 for (var j = from+1; j <= to; j++) {
184 tx.executeSql('UPDATE Locations SET id=? WHERE id=?;',
185 [j-1, j])
186 }
187 }
188
189 // Switch moving location to its new position
190 tx.executeSql('UPDATE Locations SET id=? WHERE id=?;',
191 [to, -1])
192 })
193 }
194 }