-
Notifications
You must be signed in to change notification settings - Fork 1
/
trackdb.js
61 lines (53 loc) · 1.58 KB
/
trackdb.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
TrackDB = {
// Constants
__dbname: "tracks",
__dbversion: "1.0",
__dbdesc: "",
__dbsize: 1024*1024*5, // 5MB
// sql
__sql_createtable: "CREATE TABLE IF NOT EXISTS tracks(tracknum INTEGER PRIMARY KEY ASC, completed TIMESTAMP DEFAULT NULL)",
__sql_gettrack: "SELECT * FROM tracks WHERE tracknum = ?",
__sql_addtrack: "INSERT INTO tracks (tracknum) VALUES (?)",
__sql_completetrack: "UPDATE tracks SET completed = datetime() WHERE tracknum = ?",
_db: null,
_dberror: function(tx,err) {
console.log(err);
},
_ensuredb: function() {
var t = this;
if (t._db == null) {
t._db = openDatabase(t.__dbname, t.__dbversion, t.__dbdesc, t.__dbsize);
t._db.transaction(function(tx){
tx.executeSql(t.__sql_createtable,[],null,t._dberror);
});
}
},
getTrack: function(tracknum,cb) {
var t = this;
t._ensuredb();
t._db.transaction(function(tx){
tx.executeSql(t.__sql_gettrack,[tracknum],function(tx,rs){
var res = 0;
if (rs.rows.length > 0) {
res = (rs.rows.item(0).completed != null) ? 2 : 1;
} else {
tx.executeSql(t.__sql_addtrack,[tracknum],null,t._dberror);
}
if (cb != null) {
cb(res);
}
},t._dberror);
});
},
completeTrack: function(tracknum,cb) {
var t = this;
t._ensuredb();
t._db.transaction(function(tx){
tx.executeSql(t.__sql_completetrack,[tracknum],function(tx){
if (cb != null) {
cb();
}
},t._dberror);
});
},
};