-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsql.js
More file actions
222 lines (187 loc) · 5.82 KB
/
sql.js
File metadata and controls
222 lines (187 loc) · 5.82 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/** There are, naturally, dragons below. **/
function Sqlite3Reader() {
this.file = null;
this.pagesize = 0;
this.tables = [];
this.table_lookup = {};
var rootnode = null;
basereader = this;
this.loadfile = function(reader) {
var that = this;
//console.log('what is this')
return function(e) {
that.file = new Parser(reader.result);
var magic = that.file.bytes(15);
if(("SQLite format 3"!=magic)||(0!=that.file.bytes(1).charCodeAt(0)))
throw "location db has bad magic ("+magic+")";
that.header();
pagesize = that.page_size;
that.rootnode = new Sqlite3BTree(that.file);
lilog('root node read')
MASTER = new Sqlite3Table(that.rootnode, true);
lilog('sqlite_master loaded')
that.tables = [];
$.each(MASTER.rows(), function(i, schema) {
var r = {
type: read_column(schema.columns[0], schema.payload),
name: read_column(schema.columns[1], schema.payload),
tbl_name: read_column(schema.columns[2], schema.payload),
rootpage: read_column(schema.columns[3], schema.payload),
sql: read_column(schema.columns[4], schema.payload)
}
if(r['type']=='table') {
that.tables.push(r);
that.table_lookup[r.name] = r;
}
});
$(document).trigger('sqlready');
}
}
this.read_table = function(table_name) {
var table = SQR.table_lookup[table_name];
var root = table['rootpage'];
this.file.seek(0);
this.file.seek(page_size * (root-1))
oldpos = this.file.pos();
var node = new Sqlite3BTree(this.file);
this.file.seek(oldpos);
tbl = new Sqlite3Table(node);
var data = [];
$.map(tbl.rows(), function(r) {
row = $.map(r.columns, function(col) { return read_column(col, r.payload) })
////console.log('new row')
//console.log(row)
data.push(row)
return row
});
return data
}
this.header = function() {
// we only care about the page_size for this right now.
page_size = this.page_size = this.file.get_int(2); // global whatuuupppp
lilog('page size: ' + this.page_size);
this.file.seek(100);
};
function Sqlite3Table(_node, root) {
var node = _node;
var isRoot = root;
var passthrough = null;
if(node.header['type']==5) {
passthrough = new Sqlite3ITable(_node, root)
}else{
passthrough = new Sqlite3LTable(_node, root)
}
this.rows = function() {
return passthrough.rows();
}
}
function Sqlite3ITable(_node, root) {
lilog("starting inner table");
that = this;
var isRoot = root;
var node = _node;
this.file = _node.file;
this.pos = this.file.pos();
var header = node.header;
var cells = node.cells;
this.read_children = function() {
var kids = [];
var pages = [];
//console.log('VV header VV')
//console.log(cells)
if(header['rightmost'] != null) { pages.push(header['rightmost']) }
$.each(cells, function(i, value) {
if(isRoot) { that.file.seek(value) }else{ that.file.seek(value + that.pos) }
//console.log('cell ' + value + ' @ ' + that.file.pos() + ' // ' + that.pos);
var page_number = that.file.get_int(4) - 1;
//console.log('page found: ' + page_number)
int_key = that.file.varint(); // need to impliment varint asap;
//console.log('key number: ' + int_key)
pages.push(page_number)
//console.log(pages)
//lilog('^^ points to page ' + page_number);
that.file.seek(that.pos)
});
$.each(pages.sort().reverse(), function(i, page) {
that.file.seek(page_size * page);
//lilog('reading child tree @ ' + that.file.pos() + '// page: ' + page)
var tree = new Sqlite3BTree(that.file);
that.file.seek(page_size * page);
var table = new Sqlite3LTable(tree)
kids.push(table)
});
return kids;
}
this.rows = function() {
_r = []
$.each(children, function(i, kid) {
$.each(kid.rows(), function(i, x) { count = count + 1; _r.push(x) });
});
return _r;
}
var children = this.read_children();
}
function Sqlite3LTable(_node, root) {
//lilog("starting leaf table");
var that = this;
that.isRoot = root;
var node = _node;
this.file = _node.file;
this.pos = that.file.pos();
this.header = node.header;
var cells = _node.cells.sort();
this.rows = function() {
var _internal = []
$.each(cells, function(i, value) {
if(that.root) { that.file.seek(value) }else{ that.file.seek(value + that.pos) }
record = new Sqlite3Record(that.file);
_internal.push(record)
})
return _internal;
}
}
function Sqlite3Record(file) {
var payload_length = file.varint();
var rowid = file.varint();
var oldpos = file.pos();
var header_length = file.varint();
file.seek(oldpos);
var header = file.bytes(header_length);
var payload = new Parser(header);
var header_length = payload.varint(); //shutup
this.columns = [];
while(payload.more()) {
this.columns.push(payload.varint());
}
//this.columns = this.columns.reverse();
this.payload = new Parser(file.bytes(payload_length - header_length));
//console.log('length: ' + this.payload.length)
}
function Sqlite3BTree(_file) {
this.file = _file;
var types = {'2': 'IIndex', '5': 'ITable', '10': 'LIndex', '13': 'LTable'};
this.startpos = this.file.pos();
this.parse_header = function() {
//lilog('parsing btree at ' + this.file.pos())
type = this.file.get_int(1);
ffree = this.file.get_int(2);
cells = this.file.get_int(2);
content = this.file.get_int(2);
this.file.skip(1);
rightmost = null;
if(type==5) {
rightmost = this.file.get_int(4) - 1;
}
return {rightmost: rightmost, type: type, cells: cells, content: content, string_type: types[type]}
}
this.parse_cells = function() {
cells = [];
for(i = 0; i < this.header['cells']; i++) {
cells.push(this.file.get_int(2));
}
return cells;
}
this.header = this.parse_header();
this.cells = this.parse_cells();
}
}