forked from brianleroux/lawnchair
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebkitSQLiteAdaptor.js
166 lines (156 loc) · 4.08 KB
/
WebkitSQLiteAdaptor.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
/**
* WebkitSQLiteAdaptor
* ===================
* Sqlite implementation for Lawnchair.
*
*/
var WebkitSQLiteAdaptor = function(options) {
for (var i in LawnchairAdaptorHelpers) {
this[i] = LawnchairAdaptorHelpers[i];
}
this.init(options);
};
WebkitSQLiteAdaptor.prototype = {
init:function(options) {
var that = this;
var merge = that.merge;
var opts = (typeof arguments[0] == 'string') ? {table:options} : options;
// default properties
this.name = merge('Lawnchair', opts.name );
this.version = merge('1.0', opts.version );
this.table = merge('field', opts.table );
this.display = merge('shed', opts.display );
this.max = merge(65536, opts.max );
this.db = merge(null, opts.db );
// default sqlite callbacks
this.onError = function(){};
this.onData = function(){};
if("onError" in opts) {
this.onError = opts.onError;
}
// error out on shit browsers
if (!window.openDatabase)
throw('Lawnchair, "This browser does not support sqlite storage."');
// instantiate the store
this.db = openDatabase(this.name, this.version, this.display, this.max);
// create a default database and table if one does not exist
this.db.transaction(function(tx) {
tx.executeSql("SELECT COUNT(*) FROM " + that.table, [], function(){}, function(tx, error) {
that.db.transaction(function(tx) {
tx.executeSql("CREATE TABLE "+ that.table + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)", [], function(){}, that.onError);
});
});
});
},
save:function(obj, callback) {
var that = this;
var update = function(id, obj, callback) {
that.db.transaction(function(t) {
t.executeSql(
"UPDATE " + that.table + " SET value=?, timestamp=? WHERE id=?",
[that.serialize(obj), that.now(), id],
function() {
if (callback != undefined) {
obj.key = id;
callback(obj);
}
},
that.onError
);
});
};
var insert = function(obj, callback) {
that.db.transaction(function(t) {
var id = (obj.key == undefined) ? that.uuid() : obj.key;
delete(obj.key);
t.executeSql(
"INSERT INTO " + that.table + " (id, value,timestamp) VALUES (?,?,?)",
[id, that.serialize(obj), that.now()],
function() {
if (callback != undefined) {
obj.key = id;
callback(obj);
}
},
that.onError
);
});
};
if (obj.key == undefined) {
insert(obj, callback);
} else {
this.get(obj.key, function(r) {
var isUpdate = (r != null);
if (isUpdate) {
var id = obj.key;
delete(obj.key);
update(id, obj, callback);
} else {
insert(obj, callback);
}
});
}
},
get:function(key, callback) {
var that = this;
this.db.transaction(function(t) {
t.executeSql(
"SELECT value FROM " + that.table + " WHERE id = ?",
[key],
function(tx, results) {
if (results.rows.length == 0) {
callback(null);
} else {
var o = that.deserialize(results.rows.item(0).value);
o.key = key;
callback(o);
}
},
this.onError
);
});
},
all:function(callback) {
var cb = this.terseToVerboseCallback(callback);
var that = this;
this.db.transaction(function(t) {
t.executeSql("SELECT * FROM " + that.table, [], function(tx, results) {
if (results.rows.length == 0 ) {
cb([]);
} else {
var r = [];
for (var i = 0, l = results.rows.length; i < l; i++) {
var raw = results.rows.item(i).value;
var obj = that.deserialize(raw);
obj.key = results.rows.item(i).id;
r.push(obj);
}
cb(r);
}
},
that.onError);
});
},
remove:function(keyOrObj, callback) {
var that = this;
this.db.transaction(function(t) {
t.executeSql(
"DELETE FROM " + that.table + " WHERE id = ?",
[(typeof keyOrObj == 'string') ? keyOrObj : keyOrObj.key],
callback || that.onData,
that.onError
);
});
},
nuke:function(callback) {
var that = this;
this.db.transaction(function(tx) {
tx.executeSql(
"DELETE FROM " + that.table,
[],
callback || that.onData,
that.onError
);
});
}
};