2020-10-07 08:53:19 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { SQLiteDB } from '@classes/sqlitedb';
|
2020-11-19 11:40:18 +00:00
|
|
|
import { SQLiteDBMock } from '@features/emulator/classes/sqlitedb';
|
2020-11-24 08:31:11 +00:00
|
|
|
import { makeSingleton, SQLite, Platform } from '@singletons';
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This service allows interacting with the local database to store and retrieve data.
|
|
|
|
*/
|
2020-11-19 15:35:17 +00:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-10-07 08:53:19 +00:00
|
|
|
export class CoreDbProvider {
|
|
|
|
|
2020-10-14 06:29:58 +00:00
|
|
|
protected dbInstances: {[name: string]: SQLiteDB} = {};
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get or create a database object.
|
|
|
|
*
|
|
|
|
* The database objects are cached statically.
|
|
|
|
*
|
|
|
|
* @param name DB name.
|
|
|
|
* @param forceNew True if it should always create a new instance.
|
|
|
|
* @return DB.
|
|
|
|
*/
|
|
|
|
getDB(name: string, forceNew?: boolean): SQLiteDB {
|
|
|
|
if (typeof this.dbInstances[name] === 'undefined' || forceNew) {
|
|
|
|
if (Platform.instance.is('cordova')) {
|
|
|
|
this.dbInstances[name] = new SQLiteDB(name);
|
|
|
|
} else {
|
|
|
|
this.dbInstances[name] = new SQLiteDBMock(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.dbInstances[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a DB.
|
|
|
|
*
|
|
|
|
* @param name DB name.
|
|
|
|
* @return Promise resolved when the DB is deleted.
|
|
|
|
*/
|
2020-10-14 06:29:58 +00:00
|
|
|
async deleteDB(name: string): Promise<void> {
|
2020-10-07 08:53:19 +00:00
|
|
|
if (typeof this.dbInstances[name] != 'undefined') {
|
|
|
|
// Close the database first.
|
2020-10-14 06:29:58 +00:00
|
|
|
await this.dbInstances[name].close();
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
const db = this.dbInstances[name];
|
|
|
|
delete this.dbInstances[name];
|
|
|
|
|
2020-10-14 06:29:58 +00:00
|
|
|
if (db instanceof SQLiteDBMock) {
|
|
|
|
// In WebSQL we cannot delete the database, just empty it.
|
|
|
|
return db.emptyDatabase();
|
|
|
|
} else {
|
2020-10-07 08:53:19 +00:00
|
|
|
return SQLite.instance.deleteDatabase({
|
|
|
|
name,
|
2020-10-14 06:29:58 +00:00
|
|
|
location: 'default',
|
2020-10-07 08:53:19 +00:00
|
|
|
});
|
|
|
|
}
|
2020-10-14 06:29:58 +00:00
|
|
|
} else if (Platform.instance.is('cordova')) {
|
|
|
|
return SQLite.instance.deleteDatabase({
|
|
|
|
name,
|
|
|
|
location: 'default',
|
|
|
|
});
|
|
|
|
}
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
2020-10-14 06:29:58 +00:00
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CoreDB extends makeSingleton(CoreDbProvider) {}
|