|
1 | 1 | (function () {
|
2 | 2 | "use strict";
|
3 | 3 |
|
4 |
| - var Statement, Database; |
| 4 | + var Statement, Database, ItemDataSource, GroupDataSource; |
5 | 5 |
|
6 | 6 | // Alternative typeof implementation yielding more meaningful results,
|
7 | 7 | // see http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
|
|
166 | 166 | prepare: function (sql, args) {
|
167 | 167 | return new Statement(this, sql, args);
|
168 | 168 | },
|
| 169 | + itemDataSource: function (sql, args, keyColumnName, groupKeyColumnName) { |
| 170 | + if (type(args) === 'string') { |
| 171 | + groupKeyColumnName = keyColumnName; |
| 172 | + keyColumnName = args; |
| 173 | + args = undefined; |
| 174 | + } |
| 175 | + |
| 176 | + return new ItemDataSource(this, sql, args, keyColumnName, groupKeyColumnName); |
| 177 | + }, |
| 178 | + groupDataSource: function (sql, args, keyColumnName, sizeColumnName) { |
| 179 | + if (type(args) === 'string') { |
| 180 | + sizeColumnName = keyColumnName; |
| 181 | + keyColumnName = args; |
| 182 | + args = undefined; |
| 183 | + } |
| 184 | + |
| 185 | + return new GroupDataSource(this, sql, args, keyColumnName, sizeColumnName); |
| 186 | + }, |
169 | 187 | close: function () {
|
170 | 188 | this.connection.close();
|
171 | 189 | }
|
172 | 190 | });
|
173 | 191 |
|
| 192 | + ItemDataSource = WinJS.Class.derive(WinJS.UI.VirtualizedDataSource, |
| 193 | + function (db, sql, args, keyColumnName, groupKeyColumnName) { |
| 194 | + var dataAdapter = { |
| 195 | + getCount: function () { |
| 196 | + var row = db.one('SELECT COUNT(*) AS cnt FROM (' + sql + ')', args); |
| 197 | + return WinJS.Promise.wrap(row.cnt); |
| 198 | + }, |
| 199 | + itemsFromIndex: function (requestIndex, countBefore, countAfter) { |
| 200 | + var items, |
| 201 | + limit = countBefore + 1 + countAfter, |
| 202 | + offset = requestIndex - countBefore; |
| 203 | + |
| 204 | + items = db.map( |
| 205 | + 'SELECT * FROM (' + sql + ') LIMIT ' + limit + ' OFFSET ' + offset, |
| 206 | + function (row) { |
| 207 | + var item = { |
| 208 | + key: row[keyColumnName].toString(), |
| 209 | + data: row |
| 210 | + }; |
| 211 | + if (groupKeyColumnName) { |
| 212 | + item.groupKey = row[groupKeyColumnName].toString(); |
| 213 | + } |
| 214 | + return item; |
| 215 | + }); |
| 216 | + |
| 217 | + return WinJS.Promise.wrap({ |
| 218 | + items: items, |
| 219 | + offset: countBefore, |
| 220 | + atEnd: items.length < limit |
| 221 | + }); |
| 222 | + } |
| 223 | + }; |
| 224 | + |
| 225 | + this._baseDataSourceConstructor(dataAdapter); |
| 226 | + } |
| 227 | + ); |
| 228 | + |
| 229 | + GroupDataSource = WinJS.Class.derive(WinJS.UI.VirtualizedDataSource, |
| 230 | + function (db, sql, args, keyColumnName, sizeColumnName) { |
| 231 | + var groups, |
| 232 | + dataAdapter, |
| 233 | + keyIndexMap = {}, |
| 234 | + groupIndex = 0, |
| 235 | + firstItemIndex = 0; |
| 236 | + |
| 237 | + groups = db.map(sql, args, function (row) { |
| 238 | + var item = { |
| 239 | + key: row[keyColumnName].toString(), |
| 240 | + groupSize: row[sizeColumnName], |
| 241 | + firstItemIndexHint: firstItemIndex, |
| 242 | + data: row |
| 243 | + }; |
| 244 | + |
| 245 | + keyIndexMap[item.key] = groupIndex; |
| 246 | + groupIndex += 1; |
| 247 | + firstItemIndex += item.groupSize; |
| 248 | + |
| 249 | + return item; |
| 250 | + }); |
| 251 | + |
| 252 | + dataAdapter = { |
| 253 | + getCount: function () { |
| 254 | + return WinJS.Promise.wrap(groups.length); |
| 255 | + }, |
| 256 | + itemsFromIndex: function (requestIndex, countBefore, countAfter) { |
| 257 | + return WinJS.Promise.wrap({ |
| 258 | + items: groups.slice(), |
| 259 | + offset: requestIndex, |
| 260 | + absoluteIndex: requestIndex, |
| 261 | + atStart: true, |
| 262 | + atEnd: true |
| 263 | + }); |
| 264 | + }, |
| 265 | + itemsFromKey: function (key, countBefore, countAfter) { |
| 266 | + return this.itemsFromIndex(keyIndexMap[key], countBefore, countAfter); |
| 267 | + } |
| 268 | + }; |
| 269 | + |
| 270 | + this._baseDataSourceConstructor(dataAdapter); |
| 271 | + } |
| 272 | + ); |
| 273 | + |
174 | 274 | WinJS.Namespace.define('SQLite3JS', {
|
175 | 275 | Database: Database
|
176 | 276 | });
|
|
0 commit comments