-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookmarks.js
183 lines (149 loc) · 5.38 KB
/
bookmarks.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
var constants = {
'mybookmarks':'mybookmarks',
'DAY_AS_MILLISECONDS':1000 * 60 * 60 * 24
}
var db
function exportSQLite(db) {
var bin2String = function(array) {
return String.fromCharCode.apply(String, array)
}
return simpleStorage.set(constants.mybookmarks, bin2String(db.export()))
/* true, false, or error object, according to API docs */
}
function importLocalStorage() {
var string2Bin = function(str) {
return str.split("").map(function(val) {
return val.charCodeAt(0)
})
}
var datastring = simpleStorage.get(constants.mybookmarks)
return ('undefined' === typeof datastring ? datastring : string2Bin(datastring))
/* either undefined or array of binary */
} // importLocalStorage
function createDB() {
if (!simpleStorage.canUse())
throw { name:'BookmarkException', msg:'local storage not an option' }
var db = new SQL.Database()
db.run('CREATE TABLE bookmarks (url TEXT UNIQUE PRIMARY KEY NOT NULL, \
creationDate INTEGER, \
tags TEXT, \
expirationDate INTEGER);')
db.run('CREATE INDEX idx_creation ON bookmarks(creationDate);')
db.run('CREATE INDEX idx_expiration ON bookmarks(expirationDate);')
db.run('CREATE TABLE tags (tag TEXT UNIQUE PRIMARY KEY NOT NULL);')
// schema ready. empty database exists in memory.
var result = exportSQLite(db)
if (false === result)
throw { name:'BookmarkException', msg:'failed to write to local storage' }
if ('Object' === typeof result)
throw result // something very bad happened
// database now written to local storage
return db
} // createDB
function saveDB(db) {
if (!simpleStorage.canUse())
throw { name:'BookmarkException', msg:'local storage not an option' }
var result = exportSQLite(db)
if (false === result)
throw { name:'BookmarkException', msg:'failed to write to local storage' }
if ('Object' === typeof result)
throw result // something very bad happened
// successfully written to local storage
} // saveDB
function restoreDB() {
if (!simpleStorage.canUse())
throw { name:'BookmarkException', msg:'local storage not an option' }
var data = importLocalStorage()
return ('undefined' === typeof data ? createDB() : new SQL.Database(data))
/* return reference to SQLite database in memory */
}
function insertBookmark(db, bookmark) {
var setColumns = function(bookmark) {
var cols = 'url, creationDate' // required
if (0 < bookmark.tags.length)
cols += ', tags'
if ('undefined' !== typeof bookmark.expirationDate &&
null !== bookmark.expirationDate)
cols += ', expirationDate'
return cols
} // setColumns
var setValues = function(bookmark) {
var delimited_string = '\'_val_\''
var vals = ''
vals += delimited_string.replace('_val_', bookmark.url)
vals += ', '
vals += Date.now()
if (0 < bookmark.tags.length) {
vals += ', '
vals += delimited_string.replace('_val_', bookmark.tags.toString())
}
if ('undefined' !== typeof bookmark.expirationDate &&
null !== bookmark.expirationDate) {
vals += ', '
vals += bookmark.expirationDate
}
return vals
} // setValues
var sql = 'INSERT INTO bookmarks(_cols_) VALUES(_vals_);'
sql = sql.replace('_cols_', setColumns(bookmark))
sql = sql.replace('_vals_', setValues(bookmark))
console.log(sql)
console.log(db.run(sql).exec('SELECT * FROM bookmarks ORDER BY creationDate'))
} // insertBookmark
function insertTag(db, name) {
var delimited_string = '\'_val_\''
var sql = 'INSERT INTO tags(tag) VALUES(_val_);'
sql = sql.replace('_val_', delimited_string.replace('_val_', name))
console.log(sql)
try { console.log(db.run(sql).exec('SELECT * FROM tags ORDER BY tag')) }
catch (ignored) { console.error(ignored) }
}
function queryTags(db) {
var sql = 'SELECT * FROM tags ORDER BY tag;'
var result = db.exec(sql)
var tags = []
var result
console.log(sql)
try { result = db.exec(sql) }
catch(ignored) { console.error(ignored) }
finally {
if ('undefined' !== typeof result[0]) tags = result[0].values
return tags
}
}
function queryBookmarks(db, options) {
var sql = 'SELECT url, creationDate, expirationDate, tags \
FROM bookmarks _where_ _orderby_'
var where = ''
var orderBy = 'ORDER BY ' + options.orderBy
if (0 < options.where.length) {
where = 'WHERE tags LIKE \'%_pattern_%\''
where = where.replace('_pattern_', options.where)
}
sql = sql.replace('_where_', where)
sql = sql.replace('_orderby_', orderBy)
var bookmarks = []
var result
console.log(sql)
try { result = db.exec(sql) }
catch(ignored) { console.error(ignored) }
finally {
if ('undefined' !== typeof result[0]) bookmarks = result[0].values
return bookmarks
}
} // queryBookmarks
function deleteBookmark(db, url) {
var sql = 'DELETE FROM bookmarks WHERE url=\'_url_\';'
sql = sql.replace('_url_', url)
console.log(sql)
console.log(db.run(sql).exec('SELECT * FROM bookmarks ORDER BY creationDate'))
}
(function() {
try {
db = restoreDB()
}
catch(e) {
console.error(e)
}
console.log('SQLite is ready.')
})()