forked from indexeddbshim/IndexedDBShim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IDBIndex.js
659 lines (610 loc) · 28.1 KB
/
IDBIndex.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import {createDOMException} from './DOMException';
import {IDBCursor, IDBCursorWithValue} from './IDBCursor';
import * as util from './util';
import * as Key from './Key';
import {setSQLForKeyRange, IDBKeyRange, convertValueToKeyRange} from './IDBKeyRange';
import IDBTransaction from './IDBTransaction';
import * as Sca from './Sca';
import CFG from './CFG';
import IDBObjectStore from './IDBObjectStore';
import SyncPromise from 'sync-promise';
const readonlyProperties = ['objectStore', 'keyPath', 'multiEntry', 'unique'];
/**
* IDB Index
* http://www.w3.org/TR/IndexedDB/#idl-def-IDBIndex
* @param {IDBObjectStore} store
* @param {IDBIndexProperties} indexProperties
* @constructor
*/
function IDBIndex () {
throw new TypeError('Illegal constructor');
}
const IDBIndexAlias = IDBIndex;
IDBIndex.__createInstance = function (store, indexProperties) {
function IDBIndex () {
const me = this;
me[Symbol.toStringTag] = 'IDBIndex';
util.defineReadonlyProperties(me, readonlyProperties);
me.__objectStore = store;
me.__name = me.__originalName = indexProperties.columnName;
me.__keyPath = Array.isArray(indexProperties.keyPath) ? indexProperties.keyPath.slice() : indexProperties.keyPath;
const optionalParams = indexProperties.optionalParams;
me.__multiEntry = !!(optionalParams && optionalParams.multiEntry);
me.__unique = !!(optionalParams && optionalParams.unique);
me.__deleted = !!indexProperties.__deleted;
me.__objectStore.__cursors = indexProperties.cursors || [];
Object.defineProperty(me, '__currentName', {
get: function () {
return '__pendingName' in me ? me.__pendingName : me.name;
}
});
Object.defineProperty(me, 'name', {
enumerable: false,
configurable: false,
get: function () {
return this.__name;
},
set: function (newName) {
const me = this;
newName = util.convertToDOMString(newName);
const oldName = me.name;
IDBTransaction.__assertVersionChange(me.objectStore.transaction);
IDBTransaction.__assertActive(me.objectStore.transaction);
IDBIndexAlias.__invalidStateIfDeleted(me);
IDBObjectStore.__invalidStateIfDeleted(me);
if (newName === oldName) {
return;
}
if (me.objectStore.__indexes[newName] && !me.objectStore.__indexes[newName].__deleted &&
!me.objectStore.__indexes[newName].__pendingDelete) {
throw createDOMException('ConstraintError', 'Index "' + newName + '" already exists on ' + me.objectStore.__currentName);
}
me.__name = newName;
const objectStore = me.objectStore;
delete objectStore.__indexes[oldName];
objectStore.__indexes[newName] = me;
objectStore.indexNames.splice(objectStore.indexNames.indexOf(oldName), 1, newName);
const storeHandle = objectStore.transaction.__storeHandles[objectStore.name];
const oldIndexHandle = storeHandle.__indexHandles[oldName];
oldIndexHandle.__name = newName; // Fix old references
storeHandle.__indexHandles[newName] = oldIndexHandle; // Ensure new reference accessible
me.__pendingName = oldName;
const colInfoToPreserveArr = [
['key', 'BLOB ' + (objectStore.autoIncrement ? 'UNIQUE, inc INTEGER PRIMARY KEY AUTOINCREMENT' : 'PRIMARY KEY')],
['value', 'BLOB']
].concat(
[...objectStore.indexNames]
.filter((indexName) => indexName !== newName)
.map((indexName) => [util.escapeIndexNameForSQL(indexName), 'BLOB'])
);
me.__renameIndex(objectStore, oldName, newName, colInfoToPreserveArr, function (tx, success) {
IDBIndexAlias.__updateIndexList(store, tx, function (store) {
delete storeHandle.__pendingName;
success(store);
});
});
}
});
}
IDBIndex.prototype = IDBIndexAlias.prototype;
return new IDBIndex();
};
IDBIndex.__invalidStateIfDeleted = function (index, msg) {
if (index.__deleted || index.__pendingDelete || (
index.__pendingCreate && index.objectStore.transaction && index.objectStore.transaction.__errored
)) {
throw createDOMException('InvalidStateError', msg || 'This index has been deleted');
}
};
/**
* Clones an IDBIndex instance for a different IDBObjectStore instance.
* @param {IDBIndex} index
* @param {IDBObjectStore} store
* @protected
*/
IDBIndex.__clone = function (index, store) {
const idx = IDBIndex.__createInstance(store, {
columnName: index.name,
keyPath: index.keyPath,
optionalParams: {
multiEntry: index.multiEntry,
unique: index.unique
}
});
['__pendingCreate', '__pendingDelete', '__deleted', '__originalName', '__recreated'].forEach((p) => {
idx[p] = index[p];
});
return idx;
};
/**
* Creates a new index on an object store.
* @param {IDBObjectStore} store
* @param {IDBIndex} index
* @returns {IDBIndex}
* @protected
*/
IDBIndex.__createIndex = function (store, index) {
const indexName = index.name;
const storeName = store.__currentName;
const idx = store.__indexes[indexName];
index.__pendingCreate = true;
// Add the index to the IDBObjectStore
store.indexNames.push(indexName);
store.__indexes[indexName] = index; // We add to indexes as needs to be available, e.g., if there is a subsequent deleteIndex call
let indexHandle = store.__indexHandles[indexName];
if (!indexHandle ||
index.__pendingDelete ||
index.__deleted ||
indexHandle.__pendingDelete ||
indexHandle.__deleted
) {
indexHandle = store.__indexHandles[indexName] = IDBIndex.__clone(index, store);
}
// Create the index in WebSQL
const transaction = store.transaction;
transaction.__addNonRequestToTransactionQueue(function createIndex (tx, args, success, failure) {
const columnExists = idx && (idx.__deleted || idx.__recreated); // This check must occur here rather than earlier as properties may not have been set yet otherwise
let indexValues = {};
function error (tx, err) {
failure(createDOMException('UnknownError', 'Could not create index "' + indexName + '"' + err.code + '::' + err.message, err));
}
function applyIndex (tx) {
// Update the object store's index list
IDBIndex.__updateIndexList(store, tx, function () {
// Add index entries for all existing records
tx.executeSql('SELECT "key", "value" FROM ' + util.escapeStoreNameForSQL(storeName), [], function (tx, data) {
CFG.DEBUG && console.log('Adding existing ' + storeName + ' records to the ' + indexName + ' index');
addIndexEntry(0);
function addIndexEntry (i) {
if (i < data.rows.length) {
try {
const value = Sca.decode(util.unescapeSQLiteResponse(data.rows.item(i).value));
let indexKey = Key.extractKeyValueDecodedFromValueUsingKeyPath(value, index.keyPath, index.multiEntry); // Todo: Do we need this stricter error checking?
if (indexKey.invalid || indexKey.failure) { // Todo: Do we need invalid checks and should we instead treat these as being duplicates?
throw new Error('Go to catch; ignore bad indexKey');
}
indexKey = Key.encode(indexKey.value, index.multiEntry);
if (index.unique) {
if (indexValues[indexKey]) {
indexValues = {};
failure(createDOMException(
'ConstraintError',
'Duplicate values already exist within the store'
));
return;
}
indexValues[indexKey] = true;
}
tx.executeSql(
'UPDATE ' + util.escapeStoreNameForSQL(storeName) + ' SET ' +
util.escapeIndexNameForSQL(indexName) + ' = ? WHERE "key" = ?',
[util.escapeSQLiteStatement(indexKey), data.rows.item(i).key],
function (tx, data) {
addIndexEntry(i + 1);
}, error
);
} catch (e) {
// Not a valid value to insert into index, so just continue
addIndexEntry(i + 1);
}
} else {
delete index.__pendingCreate;
delete indexHandle.__pendingCreate;
if (index.__deleted) {
delete index.__deleted;
delete indexHandle.__deleted;
index.__recreated = true;
indexHandle.__recreated = true;
}
indexValues = {};
success(store);
}
}
}, error);
}, error);
}
const escapedStoreNameSQL = util.escapeStoreNameForSQL(storeName);
const escapedIndexNameSQL = util.escapeIndexNameForSQL(index.name);
function addIndexSQL (tx) {
if (!CFG.useSQLiteIndexes) {
applyIndex(tx);
return;
}
tx.executeSql(
'CREATE INDEX IF NOT EXISTS "' +
// The escaped index name must be unique among indexes in the whole database;
// so we prefix with store name; as prefixed, will also not conflict with
// index on `key`
// Avoid quotes and separate with special escape sequence
escapedStoreNameSQL.slice(1, -1) + '^5' + escapedIndexNameSQL.slice(1, -1) +
'" ON ' + escapedStoreNameSQL + '(' + escapedIndexNameSQL + ')',
[],
applyIndex,
error
);
}
if (columnExists) {
// For a previously existing index, just update the index entries in the existing column;
// no need to add SQLite index to it either as should already exist
applyIndex(tx);
} else {
// For a new index, add a new column to the object store, then apply the index
const sql = ['ALTER TABLE', escapedStoreNameSQL, 'ADD', escapedIndexNameSQL, 'BLOB'].join(' ');
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], addIndexSQL, error);
}
}, undefined, store);
};
/**
* Deletes an index from an object store.
* @param {IDBObjectStore} store
* @param {IDBIndex} index
* @protected
*/
IDBIndex.__deleteIndex = function (store, index) {
// Remove the index from the IDBObjectStore
index.__pendingDelete = true;
const indexHandle = store.__indexHandles[index.name];
if (indexHandle) {
indexHandle.__pendingDelete = true;
}
store.indexNames.splice(store.indexNames.indexOf(index.name), 1);
// Remove the index in WebSQL
const transaction = store.transaction;
transaction.__addNonRequestToTransactionQueue(function deleteIndex (tx, args, success, failure) {
function error (tx, err) {
failure(createDOMException('UnknownError', 'Could not delete index "' + index.name + '"', err));
}
function finishDeleteIndex () {
// Update the object store's index list
IDBIndex.__updateIndexList(store, tx, function (store) {
delete index.__pendingDelete;
delete index.__recreated;
index.__deleted = true;
if (indexHandle) {
indexHandle.__deleted = true;
delete indexHandle.__pendingDelete;
}
success(store);
}, error);
}
if (!CFG.useSQLiteIndexes) {
finishDeleteIndex();
return;
}
tx.executeSql(
'DROP INDEX IF EXISTS ' +
util.sqlQuote(
util.escapeStoreNameForSQL(store.name).slice(1, -1) + '^5' +
util.escapeIndexNameForSQL(index.name).slice(1, -1)
),
[],
finishDeleteIndex,
error
);
}, undefined, store);
};
/**
* Updates index list for the given object store.
* @param {IDBObjectStore} store
* @param {object} tx
* @param {function} success
* @param {function} failure
*/
IDBIndex.__updateIndexList = function (store, tx, success, failure) {
const indexList = {};
for (let i = 0; i < store.indexNames.length; i++) {
const idx = store.__indexes[store.indexNames[i]];
/** @type {IDBIndexProperties} **/
indexList[idx.name] = {
columnName: idx.name,
keyPath: idx.keyPath,
optionalParams: {
unique: idx.unique,
multiEntry: idx.multiEntry
},
deleted: !!idx.deleted
};
}
CFG.DEBUG && console.log('Updating the index list for ' + store.__currentName, indexList);
tx.executeSql('UPDATE __sys__ SET "indexList" = ? WHERE "name" = ?', [JSON.stringify(indexList), util.escapeSQLiteStatement(store.__currentName)], function () {
success(store);
}, failure);
};
/**
* Retrieves index data for the given key
* @param {*|IDBKeyRange} range
* @param {string} opType
* @param {boolean} nullDisallowed
* @param {number} count
* @returns {IDBRequest}
* @private
*/
IDBIndex.prototype.__fetchIndexData = function (range, opType, nullDisallowed, count) {
const me = this;
if (count !== undefined) {
count = util.enforceRange(count, 'unsigned long');
}
IDBIndex.__invalidStateIfDeleted(me);
IDBObjectStore.__invalidStateIfDeleted(me.objectStore);
if (me.objectStore.__deleted) {
throw createDOMException('InvalidStateError', "This index's object store has been deleted");
}
IDBTransaction.__assertActive(me.objectStore.transaction);
if (nullDisallowed && range == null) {
throw createDOMException('DataError', 'No key or range was specified');
}
const fetchArgs = buildFetchIndexDataSQL(nullDisallowed, me, range, opType, false);
return me.objectStore.transaction.__addToTransactionQueue(function (...args) {
executeFetchIndexData(count, ...fetchArgs, ...args);
}, undefined, me);
};
/**
* Opens a cursor over the given key range.
* @param {*|IDBKeyRange} query
* @param {string} direction
* @returns {IDBRequest}
*/
IDBIndex.prototype.openCursor = function (/* query, direction */) {
const me = this;
const [query, direction] = arguments;
const cursor = IDBCursorWithValue.__createInstance(query, direction, me.objectStore, me, util.escapeIndexNameForSQLKeyColumn(me.name), 'value');
me.__objectStore.__cursors.push(cursor);
return cursor.__req;
};
/**
* Opens a cursor over the given key range. The cursor only includes key values, not data.
* @param {*|IDBKeyRange} query
* @param {string} direction
* @returns {IDBRequest}
*/
IDBIndex.prototype.openKeyCursor = function (/* query, direction */) {
const me = this;
const [query, direction] = arguments;
const cursor = IDBCursor.__createInstance(query, direction, me.objectStore, me, util.escapeIndexNameForSQLKeyColumn(me.name), 'key');
me.__objectStore.__cursors.push(cursor);
return cursor.__req;
};
IDBIndex.prototype.get = function (query) {
if (!arguments.length) { // Per https://heycam.github.io/webidl/
throw new TypeError('A parameter was missing for `IDBIndex.get`.');
}
return this.__fetchIndexData(query, 'value', true);
};
IDBIndex.prototype.getKey = function (query) {
if (!arguments.length) { // Per https://heycam.github.io/webidl/
throw new TypeError('A parameter was missing for `IDBIndex.getKey`.');
}
return this.__fetchIndexData(query, 'key', true);
};
IDBIndex.prototype.getAll = function (/* query, count */) {
const [query, count] = arguments;
return this.__fetchIndexData(query, 'value', false, count);
};
IDBIndex.prototype.getAllKeys = function (/* query, count */) {
const [query, count] = arguments;
return this.__fetchIndexData(query, 'key', false, count);
};
IDBIndex.prototype.count = function (/* query */) {
const me = this;
const query = arguments[0];
// With the exception of needing to check whether the index has been
// deleted, we could, for greater spec parity (if not accuracy),
// just call:
// `return me.__objectStore.count(query);`
if (util.instanceOf(query, IDBKeyRange)) { // Todo: Do we need this block?
// We don't need to add to cursors array since has the count parameter which won't cache
return IDBCursorWithValue.__createInstance(query, 'next', me.objectStore, me, util.escapeIndexNameForSQLKeyColumn(me.name), 'value', true).__req;
}
return me.__fetchIndexData(query, 'count', false);
};
IDBIndex.prototype.__renameIndex = function (store, oldName, newName, colInfoToPreserveArr = [], cb = null) {
const newNameType = 'BLOB';
const storeName = store.__currentName;
const escapedStoreNameSQL = util.escapeStoreNameForSQL(storeName);
const escapedNewIndexNameSQL = util.escapeIndexNameForSQL(newName);
const escapedTmpStoreNameSQL = util.sqlQuote('tmp_' + util.escapeStoreNameForSQL(storeName).slice(1, -1));
const colNamesToPreserve = colInfoToPreserveArr.map((colInfo) => colInfo[0]);
const colInfoToPreserve = colInfoToPreserveArr.map((colInfo) => colInfo.join(' '));
const listColInfoToPreserve = (colInfoToPreserve.length ? (colInfoToPreserve.join(', ') + ', ') : '');
const listColsToPreserve = (colNamesToPreserve.length ? (colNamesToPreserve.join(', ') + ', ') : '');
// We could adapt the approach at http://stackoverflow.com/a/8430746/271577
// to make the approach reusable without passing column names, but it is a bit fragile
store.transaction.__addNonRequestToTransactionQueue(function renameIndex (tx, args, success, error) {
function sqlError (tx, err) {
error(err);
}
function finish () {
if (cb) {
cb(tx, success);
return;
}
success();
}
// See https://www.sqlite.org/lang_altertable.html#otheralter
// We don't query for indexes as we already have the info
// This approach has the advantage of auto-deleting indexes via the DROP TABLE
const sql = 'CREATE TABLE ' + escapedTmpStoreNameSQL +
'(' + listColInfoToPreserve + escapedNewIndexNameSQL + ' ' + newNameType + ')';
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function () {
const sql = 'INSERT INTO ' + escapedTmpStoreNameSQL + '(' +
listColsToPreserve + escapedNewIndexNameSQL +
') SELECT ' + listColsToPreserve + util.escapeIndexNameForSQL(oldName) + ' FROM ' + escapedStoreNameSQL;
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function () {
const sql = 'DROP TABLE ' + escapedStoreNameSQL;
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function () {
const sql = 'ALTER TABLE ' + escapedTmpStoreNameSQL + ' RENAME TO ' + escapedStoreNameSQL;
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function (tx, data) {
if (!CFG.useSQLiteIndexes) {
finish();
return;
}
const indexCreations = colNamesToPreserve
.slice(2) // Doing `key` separately and no need for index on `value`
.map((escapedIndexNameSQL) => new SyncPromise(function (resolve, reject) {
const escapedIndexToRecreate = util.sqlQuote(
escapedStoreNameSQL.slice(1, -1) + '^5' + escapedIndexNameSQL.slice(1, -1)
);
// const sql = 'DROP INDEX IF EXISTS ' + escapedIndexToRecreate;
// CFG.DEBUG && console.log(sql);
// tx.executeSql(sql, [], function () {
const sql = 'CREATE INDEX ' +
escapedIndexToRecreate + ' ON ' + escapedStoreNameSQL + '(' + escapedIndexNameSQL + ')';
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], resolve, function (tx, err) {
reject(err);
});
// }, function (tx, err) {
// reject(err);
// });
}));
indexCreations.push(
new SyncPromise(function (resolve, reject) {
const escapedIndexToRecreate = util.sqlQuote('sk_' + escapedStoreNameSQL.slice(1, -1));
// Chrome erring here if not dropped first; Node does not
const sql = 'DROP INDEX IF EXISTS ' + escapedIndexToRecreate;
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], function () {
const sql = 'CREATE INDEX ' + escapedIndexToRecreate +
' ON ' + escapedStoreNameSQL + '("key")';
CFG.DEBUG && console.log(sql);
tx.executeSql(sql, [], resolve, function (tx, err) {
reject(err);
});
}, function (tx, err) {
reject(err);
});
})
);
SyncPromise.all(indexCreations).then(finish, error);
}, sqlError);
}, sqlError);
}, sqlError);
}, sqlError);
});
};
Object.defineProperty(IDBIndex, Symbol.hasInstance, {
value: function (obj) {
return util.isObj(obj) && typeof obj.openCursor === 'function' && typeof obj.multiEntry === 'boolean';
}
});
readonlyProperties.forEach((prop) => {
Object.defineProperty(IDBIndex.prototype, prop, {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
}
});
});
Object.defineProperty(IDBIndex.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
throw new TypeError('Illegal invocation');
},
set: function (val) {
throw new TypeError('Illegal invocation');
}
});
IDBIndex.prototype[Symbol.toStringTag] = 'IDBIndexPrototype';
Object.defineProperty(IDBIndex, 'prototype', {
writable: false
});
function executeFetchIndexData (count, unboundedDisallowed, index, hasKey, range, opType, multiChecks, sql, sqlValues, tx, args, success, error) {
if (unboundedDisallowed) {
count = 1;
}
if (count) {
sql.push('LIMIT', count);
}
const isCount = opType === 'count';
CFG.DEBUG && console.log('Trying to fetch data for Index', sql.join(' '), sqlValues);
tx.executeSql(sql.join(' '), sqlValues, function (tx, data) {
const records = [];
let recordCount = 0;
const decode = isCount ? () => {} : (opType === 'key' ? (record) => {
// Key.convertValueToKey(record.key); // Already validated before storage
return Key.decode(util.unescapeSQLiteResponse(record.key));
} : (record) => { // when opType is value
return Sca.decode(util.unescapeSQLiteResponse(record.value));
});
if (index.multiEntry) {
const escapedIndexNameForKeyCol = util.escapeIndexNameForSQLKeyColumn(index.name);
const encodedKey = Key.encode(range, index.multiEntry);
for (let i = 0; i < data.rows.length; i++) {
const row = data.rows.item(i);
const rowKey = Key.decode(row[escapedIndexNameForKeyCol]);
let record;
if (hasKey && (
(multiChecks && range.some((check) => rowKey.includes(check))) || // More precise than our SQL
Key.isMultiEntryMatch(encodedKey, row[escapedIndexNameForKeyCol])
)) {
recordCount++;
record = row;
} else if (!hasKey && !multiChecks) {
if (rowKey !== undefined) {
recordCount += (Array.isArray(rowKey) ? rowKey.length : 1);
record = row;
}
}
if (record) {
records.push(decode(record));
if (unboundedDisallowed) {
break;
}
}
}
} else {
for (let i = 0; i < data.rows.length; i++) {
const record = data.rows.item(i);
if (record) {
records.push(decode(record));
}
}
recordCount = records.length;
}
if (isCount) {
success(recordCount);
} else if (recordCount === 0) {
success(unboundedDisallowed ? undefined : []);
} else {
success(unboundedDisallowed ? records[0] : records);
}
}, error);
}
function buildFetchIndexDataSQL (nullDisallowed, index, range, opType, multiChecks) {
const hasRange = nullDisallowed || range != null;
const col = opType === 'count' ? 'key' : opType; // It doesn't matter which column we use for 'count' as long as it is valid
const sql = [
'SELECT', util.sqlQuote(col) + (
index.multiEntry ? ', ' + util.escapeIndexNameForSQL(index.name) : ''
),
'FROM', util.escapeStoreNameForSQL(index.objectStore.__currentName),
'WHERE', util.escapeIndexNameForSQL(index.name), 'NOT NULL'
];
const sqlValues = [];
if (hasRange) {
if (multiChecks) {
sql.push('AND (');
range.forEach((innerKey, i) => {
if (i > 0) sql.push('OR');
sql.push(util.escapeIndexNameForSQL(index.name), "LIKE ? ESCAPE '^' ");
sqlValues.push('%' + util.sqlLIKEEscape(Key.encode(innerKey, index.multiEntry)) + '%');
});
sql.push(')');
} else if (index.multiEntry) {
sql.push('AND', util.escapeIndexNameForSQL(index.name), "LIKE ? ESCAPE '^'");
sqlValues.push('%' + util.sqlLIKEEscape(Key.encode(range, index.multiEntry)) + '%');
} else {
const convertedRange = convertValueToKeyRange(range, nullDisallowed);
setSQLForKeyRange(convertedRange, util.escapeIndexNameForSQL(index.name), sql, sqlValues, true, false);
}
}
return [nullDisallowed, index, hasRange, range, opType, multiChecks, sql, sqlValues];
}
export {buildFetchIndexDataSQL, executeFetchIndexData, IDBIndex, IDBIndex as default};