forked from AlaSQL/alasql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha1.html
120 lines (107 loc) · 2.48 KB
/
a1.html
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
<h1>Test database</h1>
<script>
/*
var request = window.indexedDB.open("agbase",2);
request.onsuccess = function(event) {
var db = event.target.result;
};
request.onupgradeneeded = function(event) {
var db = event.target.result;
};
*/
function showDatabases(cb) {
var request = indexedDB.webkitGetDatabaseNames();
request.onsuccess = function(event){
var dblist = event.target.result;
var res = [];
for(var i=0;i<dblist.length;i++){
res.push({databaseid: dblist[i]});
}
cb(res);
};
}
function showTables(dbid,cb) {
var request = window.indexedDB.open(dbid);
request.onsuccess = function(event) {
var db = event.target.result;
var res = [];
var tblist = db.objectStoreNames;
for(var i=0;i<tblist.length;i++){
res.push({tableid: tblist[i]});
}
cb(res);
};
}
/*
showDatabases(function(data){
var dbs = data;
console.log(dbs[1].databaseid);
showTables(dbs[1].databaseid, function(data){
console.log(data[0].tableid);
getAll('mydb','mycollection',function(data){
console.table(data);
});
});
});
*/
function getAll(dbid, tableid, cb) {
var request = window.indexedDB.open(dbid);
request.onsuccess = function(event) {
var res = [];
var db = event.target.result;
db.transaction([tableid]).objectStore(tableid).openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
res.push(cursor.value);
cursor.continue();
} else {
cb(res);
}
}
}
}
function openIndexedDBStream(dbid, tableid) {
var stream = {};
stream.databaseid = dbid;
stream.tableid = tableid;
stream.cursor = undefined;
stream.next = function(cb) {
if(!stream.cursor) {
var request = window.indexedDB.open(stream.databaseid);
request.onsuccess = function(event) {
var res = [];
var db = event.target.result;
db.transaction([stream.tableid]).objectStore(stream.tableid).openCursor().onsuccess = function(event) {
stream.cursor = event.target.result;
if(stream.cursor) {
cb(stream.cursor.value);
} else {
cb();
}
};
}
} else {
stream.cursor.continue();
}
};
return stream;
};
if(false) {
var tm = Date.now();
var st1 = openIndexedDBStream('mydb', 'mycollection');
st1.next(function(data) {
if(data) {
// console.log(data);
st1.next();
} else {
console.log(Date.now()-tm);
// console.log('finish');
}
});
}
var tm1 = Date.now();
getAll('ag155','one',function(data){
console.log(data);
console.log(Date.now()-tm1);
});
</script>