-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyValueStore.qml
91 lines (81 loc) · 2.49 KB
/
KeyValueStore.qml
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
import QbSql 1.0
import QtQuick 2.0
QbSql {
id: objKeyValueStore
function setup(){
var vmap = objKeyValueStore.query("CREATE TABLE IF NOT EXISTS KeyValueStore(kid INTEGER PRIMARY KEY AUTOINCREMENT, key VARCHAR(250) NOT NULL UNIQUE, value TEXT DEFAULT '')");
var isOK = false;
try{
if(vmap["status"] === "OK"){
isOK = true;
}
}
catch(e){
}
return isOK;
}
function forcedSet(key,value){
if(isKeyExists(key)){
return update(key,value);
}
else{
return set(key,value);
}
}
function set(key,value){
if(key === undefined || key === null || key==="") return false;
if(value === undefined || value === null) value = "";
var vmap = objKeyValueStore.preparedQuery("INSERT INTO KeyValueStore(key,value) VALUES(:key,:value)",[[":key",key],[":value",value]]);
var isOK = false;
try{
if(vmap["status"] === "OK"){
isOK = true;
}
}
catch(e){
}
return isOK;
}
function get(key,value){
if(value === undefined || value === null) value = "";
var vmap = objKeyValueStore.preparedQuery("SELECT * FROM KeyValueStore WHERE key=:key",[[":key",key]]);
var dval = "";
var isFound = false;
try{
if(vmap["status"] === "OK"){
dval = vmap["data"][0]["value"];
}
}
catch(e){
}
if(dval === "") return value;
else return dval;
}
function update(key,value){
if(key === undefined || key === null || key==="") return false;
if(value === undefined || value === null) value = "";
var vmap = objKeyValueStore.preparedQuery("UPDATE KeyValueStore SET value=:value WHERE key=:key",[[":key",key],[":value",value]]);
var isOK = false;
try{
if(vmap["status"] === "OK"){
isOK = true;
}
}
catch(e){
}
return isOK;
}
function isKeyExists(key){
var vmap = objKeyValueStore.preparedQuery("SELECT COUNT(*) FROM KeyValueStore WHERE key=:key",[[":key",key]]);
//console.log(JSON.stringify(vmap));
var isFound = false;
try{
if(vmap["status"] === "OK"){
if(vmap["data"][0]["COUNT(*)"] === 1) isFound = true;
}
}
catch(e){
}
return isFound;
}
}