MOBILE-4304 core: Remove getInOrEqual database helper

main
Noel De Martin 2024-01-17 17:09:18 +01:00
parent ef88336a2d
commit 26bf15496d
5 changed files with 12 additions and 77 deletions

View File

@ -879,14 +879,9 @@ export class CoreSite extends CoreAuthenticatedSite {
return await this.lastViewedTable.getMany({ component });
}
const whereAndParams = SQLiteDB.getInOrEqual(ids);
whereAndParams.sql = 'id ' + whereAndParams.sql + ' AND component = ?';
whereAndParams.params.push(component);
return await this.lastViewedTable.getManyWhere({
sql: whereAndParams.sql,
sqlParams: whereAndParams.params,
sql: `id IN (${ids.map(() => '?').join(', ')}) AND component = ?`,
sqlParams: [...ids, component],
js: (record) => record.component === component && ids.includes(record.id),
});
} catch {

View File

@ -137,51 +137,6 @@ export interface SQLiteDBForeignKeySchema {
*/
export class SQLiteDB {
/**
* Constructs 'IN()' or '=' sql fragment
*
* @param items A single value or array of values for the expression. It doesn't accept objects.
* @param equal True means we want to equate to the constructed expression.
* @param onEmptyItems This defines the behavior when the array of items provided is empty. Defaults to false,
* meaning return empty. Other values will become part of the returned SQL fragment.
* @returns A list containing the constructed sql fragment and an array of parameters.
*/
static getInOrEqual(
items: SQLiteDBRecordValue | SQLiteDBRecordValue[],
equal: boolean = true,
onEmptyItems?: SQLiteDBRecordValue | null,
): SQLiteDBQueryParams {
let sql = '';
let params: SQLiteDBRecordValue[];
// Default behavior, return empty data on empty array.
if (Array.isArray(items) && !items.length && onEmptyItems === undefined) {
return { sql: '', params: [] };
}
// Handle onEmptyItems on empty array of items.
if (Array.isArray(items) && !items.length) {
if (onEmptyItems === null) { // Special case, NULL value.
sql = equal ? ' IS NULL' : ' IS NOT NULL';
return { sql, params: [] };
} else {
items = [onEmptyItems as SQLiteDBRecordValue]; // Rest of cases, prepare items for processing.
}
}
if (!Array.isArray(items) || items.length == 1) {
sql = equal ? '= ?' : '<> ?';
params = Array.isArray(items) ? items : [items];
} else {
const questionMarks = ',?'.repeat(items.length).substring(1);
sql = (equal ? '' : 'NOT ') + `IN (${questionMarks})`;
params = items;
}
return { sql, params };
}
db?: SQLiteObject;
promise!: Promise<void>;

View File

@ -56,7 +56,6 @@ import { lazyMap, LazyMap } from '@/core/utils/lazy-map';
import { asyncInstance, AsyncInstance } from '@/core/utils/async-instance';
import { CoreDatabaseTable } from '@classes/database/database-table';
import { CoreDatabaseCachingStrategy } from '@classes/database/database-table-proxy';
import { SQLiteDB } from '@classes/sqlitedb';
import { CorePlatform } from '@services/platform';
import { asyncObservable } from '@/core/utils/rxjs';
import { firstValueFrom } from 'rxjs';
@ -391,12 +390,9 @@ export class CoreCourseProvider {
}
const site = await CoreSites.getSite(siteId);
const whereAndParams = SQLiteDB.getInOrEqual(ids);
const entries = await this.viewedModulesTables[site.getId()].getManyWhere({
sql: 'cmId ' + whereAndParams.sql,
sqlParams: whereAndParams.params,
sql: `cmId IN (${ids.map(() => '?').join(', ')})`,
sqlParams: ids,
js: (record) => ids.includes(record.cmId),
});

View File

@ -43,7 +43,6 @@ import { CoreH5PContentBeingSaved, CoreH5PLibraryBeingSaved } from './storage';
import { CoreH5PLibraryAddTo, CoreH5PLibraryMetadataSettings } from './validator';
import { CoreH5PMetadata } from './metadata';
import { Translate } from '@singletons';
import { SQLiteDB } from '@classes/sqlitedb';
import { AsyncInstance, asyncInstance } from '@/core/utils/async-instance';
import { LazyMap, lazyMap } from '@/core/utils/lazy-map';
import { CoreDatabaseTable } from '@classes/database/database-table';
@ -138,14 +137,11 @@ export class CoreH5PFramework {
siteId ??= CoreSites.getCurrentSiteId();
const whereAndParams = SQLiteDB.getInOrEqual(libraryIds);
whereAndParams.sql = 'mainlibraryid ' + whereAndParams.sql;
await this.contentTables[siteId].updateWhere(
{ filtered: null },
{
sql: whereAndParams.sql,
sqlParams: whereAndParams.params,
sql: `mainlibraryid IN (${libraryIds.map(() => '?').join(', ')})`,
sqlParams: libraryIds,
js: record => libraryIds.includes(record.mainlibraryid),
},
);

View File

@ -28,7 +28,6 @@ import { CoreTextUtils } from '@services/utils/text';
import { CoreTimeUtils } from '@services/utils/time';
import { CoreUrlUtils } from '@services/utils/url';
import { CoreUtils, CoreUtilsOpenFileOptions } from '@services/utils/utils';
import { SQLiteDB } from '@classes/sqlitedb';
import { CoreError } from '@classes/errors/error';
import { CoreConstants } from '@/core/constants';
import { ApplicationInit, makeSingleton, NgZone, Translate } from '@singletons';
@ -2270,6 +2269,8 @@ export class CoreFilepoolProvider {
componentId?: string | number,
onlyUnknown: boolean = true,
): Promise<void> {
siteId = siteId ?? CoreSites.getCurrentSiteId();
const items = await this.getComponentFiles(siteId, component, componentId);
if (!items.length) {
@ -2277,23 +2278,15 @@ export class CoreFilepoolProvider {
return;
}
siteId = siteId ?? CoreSites.getCurrentSiteId();
const fileIds = items.map((item) => item.fileId);
const whereAndParams = SQLiteDB.getInOrEqual(fileIds);
whereAndParams.sql = 'fileId ' + whereAndParams.sql;
if (onlyUnknown) {
whereAndParams.sql += ' AND (' + CoreFilepoolProvider.FILE_IS_UNKNOWN_SQL + ')';
}
await this.filesTables[siteId].updateWhere(
{ stale: 1 },
{
sql: whereAndParams.sql,
sqlParams: whereAndParams.params,
sql: onlyUnknown
? `fileId IN (${fileIds.map(() => '?').join(', ')}) AND (${CoreFilepoolProvider.FILE_IS_UNKNOWN_SQL})`
: `fileId IN (${fileIds.map(() => '?').join(', ')})`,
sqlParams: fileIds,
js: record => fileIds.includes(record.fileId) && (
!onlyUnknown || CoreFilepoolProvider.FILE_IS_UNKNOWN_JS(record)
),