-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathStorage.js
292 lines (264 loc) · 9.33 KB
/
Storage.js
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
/****************************************************************************
Copyright 2013-2014 Michał Prędotka
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
function getDatabase(){
return openDatabaseSync("Zeegaree-lite-devel", "1.0", "zeegaree-lite-devel", 1000000);
}
/*============== CHECKING DB VERSION ===================*/
// Create special table for DB version number
function createDBVersionTable()
{
var db = getDatabase();
db.transaction(
function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS dbversion' +
'(dbversionvalue REAL)');
}
);
}
// Check if DB value exist
function checkIfDBVersionExist(db_version)
{
var db = getDatabase();
var result = ""
db.transaction(
function(tx) {
var rs = tx.executeSql('SELECT * FROM dbversion WHERE dbversionvalue = ?',
[db_version]);
if (rs.rows.length > 0 ) {
result = "true";
}
else {
result = "false";
}
}
);
return result
}
function checkIfDBVersionTableExists()
{
var db = getDatabase();
var result = ""
db.transaction(
function(tx) {
var rs = tx.executeSql('SELECT name FROM sqlite_master WHERE name="dbversion" AND type="table"');
if (rs.rows.length > 0 ) {
result = "exist";
}
else {
result = "not_exist";
}
}
);
return result
}
// Save DB version to DB
function saveDBVersion(db_version)
{
var db = getDatabase();
var result = "";
db.transaction(
function(tx) {
var rs = tx.executeSql('INSERT INTO dbversion VALUES(?)',
[db_version]);
if (rs.rowsAffected > 0) {
result = "DB version saved as: " + db_version;
} else {
result = "Error with saving DB version: " + db_version;
}
}
);
console.log(result)
}
/*============== SETTINGS ===================*/
// Create settings table for Work&Play
function createSettingsTable()
{
var db = getDatabase();
db.transaction(
function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS worksettings' +
'(settingname TEXT,' +
'settingvalue TEXT)');
}
);
}
// Check if setting exist for Work&Play
function checkIfSettingExist(setting)
{
var db = getDatabase();
var result = ""
db.transaction(
function(tx) {
var rs = tx.executeSql('SELECT * FROM worksettings WHERE settingname = ?',
[setting]);
if (rs.rows.length > 0 ) {
result = "true";
}
else {
result = "false";
}
}
);
return result
}
// Save setting to DB
function saveSetting(setting, value)
{
var db = getDatabase();
var result = "";
db.transaction(
function(tx) {
var rs = tx.executeSql('INSERT INTO worksettings VALUES(?,?)',
[setting, value]);
if (rs.rowsAffected > 0) {
result = "Setting: " + setting + " saved as: " + value;
} else {
result = "Error with saving setting: " + setting;
}
}
);
console.log(result)
}
// Get setting from DB
function getSetting(setting)
{
var db = getDatabase();
var result = "";
db.transaction(
function(tx){
var rs = tx.executeSql('SELECT settingvalue ' +
'FROM worksettings ' +
'WHERE settingname = ?',
[setting]);
if (rs.rows.length > 0) {
result = rs.rows.item(0).settingvalue;
}
else {
result = "Unknown setting";
}
}
);
return result
}
// Update setting in DB
function updateSettings(setting, value)
{
var db = getDatabase();
var result = "";
db.transaction(
function(tx) {
var rs = tx.executeSql('UPDATE worksettings '+
'SET settingvalue = ?'+
'WHERE settingname = ?',
[value, setting])
if (rs.rowsAffected > 0) {
result = "Setting: " + setting + " updated to: " + value;
} else {
result = "Error with updating setting: " + setting;
}
}
);
console.log(result)
}
// Get specific settings for Timer
function getSettingTimer()
{
repeat_alarm_setting.isSelected = checkIfSettingExist("repeat_alarm") == "true" ? getSetting("repeat_alarm") : true
}
/*============== FAVOURITES FOR TIMER ===================*/
// Create favourites table for Timer
function createFavsTable(){
var db = getDatabase();
db.transaction(
function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS favs' +
'(fname TEXT,' +
'ftime TEXT,' +
'tstamp TEXT)')
}
);
}
// Save favourites for Timer
function saveFavs(favname, favtime, favtimestamp){
var db = getDatabase();
var result = "";
db.transaction(
function(tx) {
var rs = tx.executeSql('INSERT INTO favs VALUES(?, ?, ?)',
[favname, favtime, favtimestamp]);
if (rs.rowsAffected > 0) {
result = "Favourite: " + favname + " saved with time: " + favtime;
}
else {
result = "Error with saving favourite: " + favname;
}
}
);
console.log(result);
}
// Update favourites for Timer
function updateFavs(favname, favtime, favtimestamp){
var db = getDatabase();
var result = ""
db.transaction(
function(tx) {
var rs = tx.executeSql('UPDATE favs '+
'SET fname = ?, ftime = ? '+
'WHERE tstamp = ?',
[favname, favtime, favtimestamp]);
if (rs.rowsAffected > 0) {
result = "Favourite: " + favname + " updated to: " + favtime;
}
else {
result = "Error with updating favourite: " + favname;
}
}
);
console.log(result);
}
// Delete favourites for Timer
function deleteFavs(favtimestamp){
var db = getDatabase();
var result = ""
db.transaction(
function(tx) {
var rs = tx.executeSql('DELETE FROM favs WHERE tstamp = ?',[favtimestamp]);
if (rs.rowsAffected > 0) {
result = "Favourite with timestamp: " + favtimestamp + " deleted";
}
else {
result = "Error with deleting favourite with timestamp: " + favtimestamp;
}
}
);
console.log(result);
}
// Get favourites from DB to ListView for Timer
function getFavs(favlistmodel)
{
var db = getDatabase();
db.transaction(
function(tx) {
var rs = tx.executeSql('SELECT * FROM favs');
var fname = "";
var ftime = "";
var tstamp = "";
for(var i = 0; i < rs.rows.length; i++) {
fname = rs.rows.item(i).fname;
ftime = rs.rows.item(i).ftime;
tstamp = rs.rows.item(i).tstamp;
favlistmodel.append({"nameoffav": fname, "timeoffav": ftime, "favtimestamp": tstamp});
}
}
);
}