From fb29050627fb243d07e57bd2e6704f3e81d196ae Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Wed, 30 Jan 2019 10:29:01 +0100 Subject: [PATCH] MOBILE-2842 core: Interfaces for DB schemas --- src/addon/calendar/providers/calendar.ts | 3 +- .../messages/providers/messages-offline.ts | 3 +- .../mod/assign/providers/assign-offline.ts | 3 +- src/addon/mod/choice/providers/offline.ts | 3 +- src/addon/mod/data/providers/offline.ts | 3 +- src/addon/mod/feedback/providers/offline.ts | 3 +- src/addon/mod/forum/providers/offline.ts | 3 +- src/addon/mod/glossary/providers/offline.ts | 3 +- .../mod/lesson/providers/lesson-offline.ts | 3 +- src/addon/mod/lesson/providers/lesson-sync.ts | 3 +- src/addon/mod/lesson/providers/lesson.ts | 3 +- .../accessrules/password/providers/handler.ts | 3 +- src/addon/mod/quiz/providers/quiz-offline.ts | 4 +- .../mod/scorm/providers/scorm-offline.ts | 4 +- src/addon/mod/survey/providers/offline.ts | 3 +- src/addon/mod/wiki/providers/wiki-offline.ts | 3 +- src/addon/mod/workshop/providers/offline.ts | 3 +- src/addon/notes/providers/notes-offline.ts | 3 +- .../providers/pushnotifications.ts | 3 +- src/classes/site.ts | 4 +- src/classes/sqlitedb.ts | 171 ++++++++++++++---- src/core/course/providers/course-offline.ts | 3 +- src/core/course/providers/course.ts | 3 +- .../providers/module-prefetch-delegate.ts | 3 +- src/core/emulator/providers/helper.ts | 3 +- .../emulator/providers/local-notifications.ts | 4 +- src/core/question/providers/question.ts | 3 +- src/core/sharedfiles/providers/sharedfiles.ts | 4 +- src/core/user/providers/user.ts | 3 +- src/providers/config.ts | 4 +- src/providers/cron.ts | 4 +- src/providers/filepool.ts | 6 +- src/providers/local-notifications.ts | 4 +- src/providers/sites.ts | 12 +- src/providers/sync.ts | 3 +- 35 files changed, 208 insertions(+), 85 deletions(-) diff --git a/src/addon/calendar/providers/calendar.ts b/src/addon/calendar/providers/calendar.ts index d59bd9ce8..fda684ad5 100644 --- a/src/addon/calendar/providers/calendar.ts +++ b/src/addon/calendar/providers/calendar.ts @@ -23,6 +23,7 @@ import { CoreConstants } from '@core/constants'; import { CoreLocalNotificationsProvider } from '@providers/local-notifications'; import { CoreConfigProvider } from '@providers/config'; import { ILocalNotification } from '@ionic-native/local-notifications'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle calendar events. @@ -38,7 +39,7 @@ export class AddonCalendarProvider { // Variables for database. static EVENTS_TABLE = 'addon_calendar_events'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonCalendarProvider.EVENTS_TABLE, columns: [ diff --git a/src/addon/messages/providers/messages-offline.ts b/src/addon/messages/providers/messages-offline.ts index f340c7e17..bca52ffd7 100644 --- a/src/addon/messages/providers/messages-offline.ts +++ b/src/addon/messages/providers/messages-offline.ts @@ -17,6 +17,7 @@ import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreAppProvider } from '@providers/app'; import { CoreTextUtilsProvider } from '@providers/utils/text'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle Offline messages. @@ -29,7 +30,7 @@ export class AddonMessagesOfflineProvider { // Variables for database. static MESSAGES_TABLE = 'addon_messages_offline_messages'; // When group messaging isn't available or a new conversation starts. static CONVERSATION_MESSAGES_TABLE = 'addon_messages_offline_conversation_messages'; // Conversation messages. - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonMessagesOfflineProvider.MESSAGES_TABLE, columns: [ diff --git a/src/addon/mod/assign/providers/assign-offline.ts b/src/addon/mod/assign/providers/assign-offline.ts index 6c54503b7..39b32baf6 100644 --- a/src/addon/mod/assign/providers/assign-offline.ts +++ b/src/addon/mod/assign/providers/assign-offline.ts @@ -18,6 +18,7 @@ import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline assign. @@ -30,7 +31,7 @@ export class AddonModAssignOfflineProvider { // Variables for database. static SUBMISSIONS_TABLE = 'addon_mod_assign_submissions'; static SUBMISSIONS_GRADES_TABLE = 'addon_mod_assign_submissions_grading'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModAssignOfflineProvider.SUBMISSIONS_TABLE, columns: [ diff --git a/src/addon/mod/choice/providers/offline.ts b/src/addon/mod/choice/providers/offline.ts index f17e1d2ed..dc8afd2fd 100644 --- a/src/addon/mod/choice/providers/offline.ts +++ b/src/addon/mod/choice/providers/offline.ts @@ -14,6 +14,7 @@ import { Injectable } from '@angular/core'; import { CoreSitesProvider } from '@providers/sites'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline choices. @@ -23,7 +24,7 @@ export class AddonModChoiceOfflineProvider { // Variables for database. static CHOICE_TABLE = 'addon_mod_choice_responses'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModChoiceOfflineProvider.CHOICE_TABLE, columns: [ diff --git a/src/addon/mod/data/providers/offline.ts b/src/addon/mod/data/providers/offline.ts index df54530ff..460b053cf 100644 --- a/src/addon/mod/data/providers/offline.ts +++ b/src/addon/mod/data/providers/offline.ts @@ -18,6 +18,7 @@ import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreFileProvider } from '@providers/file'; import { CoreFileUploaderProvider } from '@core/fileuploader/providers/fileuploader'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle Offline data. @@ -29,7 +30,7 @@ export class AddonModDataOfflineProvider { // Variables for database. static DATA_ENTRY_TABLE = 'addon_mod_data_entry'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModDataOfflineProvider.DATA_ENTRY_TABLE, columns: [ diff --git a/src/addon/mod/feedback/providers/offline.ts b/src/addon/mod/feedback/providers/offline.ts index d8c26f1c4..80ea785f4 100644 --- a/src/addon/mod/feedback/providers/offline.ts +++ b/src/addon/mod/feedback/providers/offline.ts @@ -17,6 +17,7 @@ import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle Offline feedback. @@ -28,7 +29,7 @@ export class AddonModFeedbackOfflineProvider { // Variables for database. static FEEDBACK_TABLE = 'addon_mod_feedback_answers'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModFeedbackOfflineProvider.FEEDBACK_TABLE, columns: [ diff --git a/src/addon/mod/forum/providers/offline.ts b/src/addon/mod/forum/providers/offline.ts index 4ba0c0e86..4140897d4 100644 --- a/src/addon/mod/forum/providers/offline.ts +++ b/src/addon/mod/forum/providers/offline.ts @@ -16,6 +16,7 @@ import { Injectable } from '@angular/core'; import { CoreFileProvider } from '@providers/file'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline forum. @@ -27,7 +28,7 @@ export class AddonModForumOfflineProvider { static DISCUSSIONS_TABLE = 'addon_mod_forum_discussions'; static REPLIES_TABLE = 'addon_mod_forum_replies'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModForumOfflineProvider.DISCUSSIONS_TABLE, columns: [ diff --git a/src/addon/mod/glossary/providers/offline.ts b/src/addon/mod/glossary/providers/offline.ts index 0adccdb0b..20b0ef53d 100644 --- a/src/addon/mod/glossary/providers/offline.ts +++ b/src/addon/mod/glossary/providers/offline.ts @@ -17,6 +17,7 @@ import { CoreFileProvider } from '@providers/file'; import { CoreSitesProvider } from '@providers/sites'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreTextUtilsProvider } from '@providers/utils/text'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline glossary. @@ -27,7 +28,7 @@ export class AddonModGlossaryOfflineProvider { // Variables for database. static ENTRIES_TABLE = 'addon_mod_glossary_entrues'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModGlossaryOfflineProvider.ENTRIES_TABLE, columns: [ diff --git a/src/addon/mod/lesson/providers/lesson-offline.ts b/src/addon/mod/lesson/providers/lesson-offline.ts index cf037fadb..32dcdc74c 100644 --- a/src/addon/mod/lesson/providers/lesson-offline.ts +++ b/src/addon/mod/lesson/providers/lesson-offline.ts @@ -18,6 +18,7 @@ import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; import { CoreUtilsProvider } from '@providers/utils/utils'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; import { AddonModLessonProvider } from './lesson'; /** @@ -31,7 +32,7 @@ export class AddonModLessonOfflineProvider { // Variables for database. We use lowercase in the names to match the WS responses. static RETAKES_TABLE = 'addon_mod_lesson_retakes'; static PAGE_ATTEMPTS_TABLE = 'addon_mod_lesson_page_attempts'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModLessonOfflineProvider.RETAKES_TABLE, columns: [ diff --git a/src/addon/mod/lesson/providers/lesson-sync.ts b/src/addon/mod/lesson/providers/lesson-sync.ts index de7e2ef7c..1e250f951 100644 --- a/src/addon/mod/lesson/providers/lesson-sync.ts +++ b/src/addon/mod/lesson/providers/lesson-sync.ts @@ -25,6 +25,7 @@ import { CoreUrlUtilsProvider } from '@providers/utils/url'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreSyncBaseProvider } from '@classes/base-sync'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; import { AddonModLessonProvider } from './lesson'; import { AddonModLessonOfflineProvider } from './lesson-offline'; import { AddonModLessonPrefetchHandler } from './prefetch-handler'; @@ -58,7 +59,7 @@ export class AddonModLessonSyncProvider extends CoreSyncBaseProvider { // Variables for database. static RETAKES_FINISHED_TABLE = 'addon_mod_lesson_retakes_finished_sync'; - protected tablesSchema = { + protected tablesSchema: SQLiteDBTableSchema = { name: AddonModLessonSyncProvider.RETAKES_FINISHED_TABLE, columns: [ { diff --git a/src/addon/mod/lesson/providers/lesson.ts b/src/addon/mod/lesson/providers/lesson.ts index 6af1f19d1..e2343fbcb 100644 --- a/src/addon/mod/lesson/providers/lesson.ts +++ b/src/addon/mod/lesson/providers/lesson.ts @@ -21,6 +21,7 @@ import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreGradesProvider } from '@core/grades/providers/grades'; import { CoreSiteWSPreSets } from '@classes/site'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; import { AddonModLessonOfflineProvider } from './lesson-offline'; /** @@ -155,7 +156,7 @@ export class AddonModLessonProvider { // Variables for database. static PASSWORD_TABLE = 'addon_mod_lesson_password'; - protected tablesSchema = { + protected tablesSchema: SQLiteDBTableSchema = { name: AddonModLessonProvider.PASSWORD_TABLE, columns: [ { diff --git a/src/addon/mod/quiz/accessrules/password/providers/handler.ts b/src/addon/mod/quiz/accessrules/password/providers/handler.ts index eea3286b2..52b482eea 100644 --- a/src/addon/mod/quiz/accessrules/password/providers/handler.ts +++ b/src/addon/mod/quiz/accessrules/password/providers/handler.ts @@ -17,6 +17,7 @@ import { Injectable, Injector } from '@angular/core'; import { CoreSitesProvider } from '@providers/sites'; import { AddonModQuizAccessRuleHandler } from '../../../providers/access-rules-delegate'; import { AddonModQuizAccessPasswordComponent } from '../component/password'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Handler to support password access rule. @@ -25,7 +26,7 @@ import { AddonModQuizAccessPasswordComponent } from '../component/password'; export class AddonModQuizAccessPasswordHandler implements AddonModQuizAccessRuleHandler { // Variables for database. static PASSWORD_TABLE = 'addon_mod_quiz_access_password'; - protected tableSchema = { + protected tableSchema: SQLiteDBTableSchema = { name: AddonModQuizAccessPasswordHandler.PASSWORD_TABLE, columns: [ { diff --git a/src/addon/mod/quiz/providers/quiz-offline.ts b/src/addon/mod/quiz/providers/quiz-offline.ts index 76f7a2631..5d21446d1 100644 --- a/src/addon/mod/quiz/providers/quiz-offline.ts +++ b/src/addon/mod/quiz/providers/quiz-offline.ts @@ -21,7 +21,7 @@ import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreQuestionProvider } from '@core/question/providers/question'; import { CoreQuestionBehaviourDelegate } from '@core/question/providers/behaviour-delegate'; import { AddonModQuizProvider } from './quiz'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline quiz. @@ -33,7 +33,7 @@ export class AddonModQuizOfflineProvider { // Variables for database. static ATTEMPTS_TABLE = 'addon_mod_quiz_attempts'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModQuizOfflineProvider.ATTEMPTS_TABLE, columns: [ diff --git a/src/addon/mod/scorm/providers/scorm-offline.ts b/src/addon/mod/scorm/providers/scorm-offline.ts index b8cdc1d43..9bfdd2c79 100644 --- a/src/addon/mod/scorm/providers/scorm-offline.ts +++ b/src/addon/mod/scorm/providers/scorm-offline.ts @@ -21,7 +21,7 @@ import { CoreTimeUtilsProvider } from '@providers/utils/time'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreUserProvider } from '@core/user/providers/user'; import { AddonModScormProvider } from './scorm'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline SCORM. @@ -34,7 +34,7 @@ export class AddonModScormOfflineProvider { // Variables for database. static ATTEMPTS_TABLE = 'addon_mod_scorm_offline_attempts'; static TRACKS_TABLE = 'addon_mod_scorm_offline_scos_tracks'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModScormOfflineProvider.ATTEMPTS_TABLE, columns: [ diff --git a/src/addon/mod/survey/providers/offline.ts b/src/addon/mod/survey/providers/offline.ts index 6e3ce13a4..61d04a315 100644 --- a/src/addon/mod/survey/providers/offline.ts +++ b/src/addon/mod/survey/providers/offline.ts @@ -16,6 +16,7 @@ import { Injectable } from '@angular/core'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle Offline survey. @@ -27,7 +28,7 @@ export class AddonModSurveyOfflineProvider { // Variables for database. static SURVEY_TABLE = 'addon_mod_survey_answers'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModSurveyOfflineProvider.SURVEY_TABLE, columns: [ diff --git a/src/addon/mod/wiki/providers/wiki-offline.ts b/src/addon/mod/wiki/providers/wiki-offline.ts index 6ae9935d4..d9407e2dc 100644 --- a/src/addon/mod/wiki/providers/wiki-offline.ts +++ b/src/addon/mod/wiki/providers/wiki-offline.ts @@ -15,6 +15,7 @@ import { Injectable } from '@angular/core'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline wiki. @@ -26,7 +27,7 @@ export class AddonModWikiOfflineProvider { // Variables for database. static NEW_PAGES_TABLE = 'addon_mod_wiki_new_pages_store'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModWikiOfflineProvider.NEW_PAGES_TABLE, columns: [ diff --git a/src/addon/mod/workshop/providers/offline.ts b/src/addon/mod/workshop/providers/offline.ts index 63ed454a8..c5c8c78d8 100644 --- a/src/addon/mod/workshop/providers/offline.ts +++ b/src/addon/mod/workshop/providers/offline.ts @@ -17,6 +17,7 @@ import { CoreFileProvider } from '@providers/file'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline workshop. @@ -30,7 +31,7 @@ export class AddonModWorkshopOfflineProvider { static EVALUATE_SUBMISSIONS_TABLE = 'addon_mod_workshop_evaluate_submissions'; static EVALUATE_ASSESSMENTS_TABLE = 'addon_mod_workshop_evaluate_assessments'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonModWorkshopOfflineProvider.SUBMISSIONS_TABLE, columns: [ diff --git a/src/addon/notes/providers/notes-offline.ts b/src/addon/notes/providers/notes-offline.ts index dd9fbf50b..1572c6559 100644 --- a/src/addon/notes/providers/notes-offline.ts +++ b/src/addon/notes/providers/notes-offline.ts @@ -16,6 +16,7 @@ import { Injectable } from '@angular/core'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline notes. @@ -26,7 +27,7 @@ export class AddonNotesOfflineProvider { // Variables for database. static NOTES_TABLE = 'addon_notes_offline_notes'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonNotesOfflineProvider.NOTES_TABLE, columns: [ diff --git a/src/addon/pushnotifications/providers/pushnotifications.ts b/src/addon/pushnotifications/providers/pushnotifications.ts index 01ac2318c..cc36a99a5 100644 --- a/src/addon/pushnotifications/providers/pushnotifications.ts +++ b/src/addon/pushnotifications/providers/pushnotifications.ts @@ -28,6 +28,7 @@ import { CoreConfigProvider } from '@providers/config'; import { CoreConstants } from '@core/constants'; import { CoreConfigConstants } from '../../../configconstants'; import { ILocalNotification } from '@ionic-native/local-notifications'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle push notifications. @@ -41,7 +42,7 @@ export class AddonPushNotificationsProvider { // Variables for database. static BADGE_TABLE = 'addon_pushnotifications_badge'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: AddonPushNotificationsProvider.BADGE_TABLE, columns: [ diff --git a/src/classes/site.ts b/src/classes/site.ts index 08b3512da..3c84bcd8a 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -15,7 +15,7 @@ import { Injector } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { HttpClient } from '@angular/common/http'; -import { SQLiteDB } from './sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from './sqlitedb'; import { CoreAppProvider } from '@providers/app'; import { CoreDbProvider } from '@providers/db'; import { CoreEventsProvider } from '@providers/events'; @@ -168,7 +168,7 @@ export class CoreSite { // Variables for the database. protected WS_CACHE_TABLE = 'wscache'; protected CONFIG_TABLE = 'core_site_config'; - protected tableSchemas = [ + protected tableSchemas: SQLiteDBTableSchema[] = [ { name: this.WS_CACHE_TABLE, columns: [ diff --git a/src/classes/sqlitedb.ts b/src/classes/sqlitedb.ts index 8e650767b..fcd319045 100644 --- a/src/classes/sqlitedb.ts +++ b/src/classes/sqlitedb.ts @@ -15,6 +15,129 @@ import { SQLite, SQLiteObject } from '@ionic-native/sqlite'; import { Platform } from 'ionic-angular'; +/** + * Schema of a table. + */ +export interface SQLiteDBTableSchema { + /** + * The table name. + * @type {string} + */ + name: string; + + /** + * The columns to create in the table. + * @type {SQLiteDBColumnSchema[]} + */ + columns: SQLiteDBColumnSchema[]; + + /** + * Names of columns that are primary key. Use it for compound primary keys. + * @type {string[]} + */ + primaryKeys?: string[]; + + /** + * List of sets of unique columns. E.g: [['section', 'title'], ['author', 'title']]. + * @type {string[][]} + */ + uniqueKeys?: string[][]; + + /** + * List of foreign keys. + * @type {SQLiteDBForeignKeySchema[]} + */ + foreignKeys?: SQLiteDBForeignKeySchema[]; + + /** + * Check constraint for the table. + * @type {string} + */ + tableCheck?: string; +} + +/** + * Schema of a column. + */ +export interface SQLiteDBColumnSchema { + /** + * Column's name. + * @type {string} + */ + name: string; + + /** + * Column's type. + * @type {string} + */ + type?: 'INTEGER' | 'REAL' | 'TEXT' | 'BLOB'; + + /** + * Whether the column is a primary key. Use it only if primary key is a single column. + * @type {boolean} + */ + primaryKey?: boolean; + + /** + * Whether it should be autoincremented. Only if primaryKey is true. + * @type {boolean} + */ + autoIncrement?: boolean; + + /** + * True if column shouldn't be null. + * @type {boolean} + */ + notNull?: boolean; + + /** + * WWhether the column is unique. + * @type {boolean} + */ + unique?: boolean; + + /** + * Check constraint for the column. + * @type {string} + */ + check?: string; + + /** + * Default value for the column. + * @type {string} + */ + default?: string; +} + +/** + * Schema of a foreign key. + */ +export interface SQLiteDBForeignKeySchema { + /** + * Columns to include in this foreign key. + * @type {string[]} + */ + columns: string[]; + + /** + * The external table referenced by this key. + * @type {string} + */ + table: string; + + /** + * List of referenced columns from the referenced table. + * @type {string[]} + */ + foreignColumns?: string[]; + + /** + * Text with the actions to apply to the foreign key. + * @type {string} + */ + actions?: string; +} + /** * Class to interact with the local database. * @@ -43,27 +166,15 @@ export class SQLiteDB { * Helper function to create a table if it doesn't exist. * * @param {string} name The table name. - * @param {any[]} columns The columns to create in the table. Each column can have: - * * {string} name Column's name. - * * {string} [type] Column's type. - * * {boolean} [primaryKey] If column is primary key. Use it only if primary key is a single column. - * * {boolean} [autoIncrement] Whether it should be autoincremented. Only if primaryKey is true. - * * {boolean} [notNull] True if column shouldn't be null. - * * {boolean} [unique] Whether the column is unique. - * * {string} [check] Check constraint for the column. - * * {string} [default] Default value for the column. + * @param {SQLiteDBColumnSchema[]} columns The columns to create in the table. * @param {string[]} [primaryKeys] Names of columns that are primary key. Use it for compound primary keys. * @param {string[][]} [uniqueKeys] List of sets of unique columns. E.g: [['section', 'title'], ['author', 'title']]. - * @param {any[]} [foreignKeys] List of foreign keys. Each key can have: - * * {string[]} columns Columns to include in this foreign key. - * * {string} table The external table referenced by this key. - * * {string[]} [foreignColumns] List of referenced columns from the referenced table. - * * {string} [actions] Text with the actions to apply to the foreign key. + * @param {SQLiteDBForeignKeySchema[]} [foreignKeys] List of foreign keys. * @param {string} [tableCheck] Check constraint for the table. * @return SQL query. */ - buildCreateTableSql(name: string, columns: any[], primaryKeys?: string[], uniqueKeys?: string[][], foreignKeys?: any[], - tableCheck?: string): string { + buildCreateTableSql(name: string, columns: SQLiteDBColumnSchema[], primaryKeys?: string[], uniqueKeys?: string[][], + foreignKeys?: SQLiteDBForeignKeySchema[], tableCheck?: string): string { const columnsSql = []; let sql = `CREATE TABLE IF NOT EXISTS ${name} (`; @@ -207,27 +318,15 @@ export class SQLiteDB { * Create a table if it doesn't exist. * * @param {string} name The table name. - * @param {any[]} columns The columns to create in the table. Each column can have: - * * {string} name Column's name. - * * {string} [type] Column's type. - * * {boolean} [primaryKey] If column is primary key. Use it only if primary key is a single column. - * * {boolean} [autoIncrement] Whether it should be autoincremented. Only if primaryKey is true. - * * {boolean} [notNull] True if column shouldn't be null. - * * {boolean} [unique] Whether the column is unique. - * * {string} [check] Check constraint for the column. - * * {string} [default] Default value for the column. + * @param {SQLiteDBColumnSchema[]} columns The columns to create in the table. * @param {string[]} [primaryKeys] Names of columns that are primary key. Use it for compound primary keys. * @param {string[][]} [uniqueKeys] List of sets of unique columns. E.g: [['section', 'title'], ['author', 'title']]. - * @param {any[]} [foreignKeys] List of foreign keys. Each key can have: - * * {string[]} columns Columns to include in this foreign key. - * * {string} table The external table referenced by this key. - * * {string[]} [foreignColumns] List of referenced columns from the referenced table. - * * {string} [actions] Text with the actions to apply to the foreign key. + * @param {SQLiteDBForeignKeySchema[]} [foreignKeys] List of foreign keys. * @param {string} [tableCheck] Check constraint for the table. * @return {Promise} Promise resolved when success. */ - createTable(name: string, columns: any[], primaryKeys?: string[], uniqueKeys?: string[][], foreignKeys?: any[], - tableCheck?: string): Promise { + createTable(name: string, columns: SQLiteDBColumnSchema[], primaryKeys?: string[], uniqueKeys?: string[][], + foreignKeys?: SQLiteDBForeignKeySchema[], tableCheck?: string): Promise { const sql = this.buildCreateTableSql(name, columns, primaryKeys, uniqueKeys, foreignKeys, tableCheck); return this.execute(sql); @@ -236,10 +335,10 @@ export class SQLiteDB { /** * Create a table if it doesn't exist from a schema. * - * @param {any} table Table schema. + * @param {SQLiteDBTableSchema} table Table schema. * @return {Promise} Promise resolved when success. */ - createTableFromSchema(table: any): Promise { + createTableFromSchema(table: SQLiteDBTableSchema): Promise { return this.createTable(table.name, table.columns, table.primaryKeys, table.uniqueKeys, table.foreignKeys, table.tableCheck); } @@ -247,10 +346,10 @@ export class SQLiteDB { /** * Create several tables if they don't exist from a list of schemas. * - * @param {any[]} tables List of table schema. + * @param {SQLiteDBTableSchema[]} tables List of table schema. * @return {Promise} Promise resolved when success. */ - createTablesFromSchema(tables: any[]): Promise { + createTablesFromSchema(tables: SQLiteDBTableSchema[]): Promise { const promises = []; tables.forEach((table) => { promises.push(this.createTableFromSchema(table)); diff --git a/src/core/course/providers/course-offline.ts b/src/core/course/providers/course-offline.ts index fadb65463..af28b5cbf 100644 --- a/src/core/course/providers/course-offline.ts +++ b/src/core/course/providers/course-offline.ts @@ -14,6 +14,7 @@ import { Injectable } from '@angular/core'; import { CoreSitesProvider } from '@providers/sites'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to handle offline data for courses. @@ -23,7 +24,7 @@ export class CoreCourseOfflineProvider { // Variables for database. static MANUAL_COMPLETION_TABLE = 'course_manual_completion'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: CoreCourseOfflineProvider.MANUAL_COMPLETION_TABLE, columns: [ diff --git a/src/core/course/providers/course.ts b/src/core/course/providers/course.ts index f69b5063a..59b7dfe15 100644 --- a/src/core/course/providers/course.ts +++ b/src/core/course/providers/course.ts @@ -23,6 +23,7 @@ import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSiteWSPreSets, CoreSite } from '@classes/site'; import { CoreConstants } from '../../constants'; import { CoreCourseOfflineProvider } from './course-offline'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service that provides some features regarding a course. @@ -47,7 +48,7 @@ export class CoreCourseProvider { // Variables for database. protected COURSE_STATUS_TABLE = 'course_status'; - protected courseStatusTableSchema = { + protected courseStatusTableSchema: SQLiteDBTableSchema = { name: this.COURSE_STATUS_TABLE, columns: [ { diff --git a/src/core/course/providers/module-prefetch-delegate.ts b/src/core/course/providers/module-prefetch-delegate.ts index e69b04036..61e228345 100644 --- a/src/core/course/providers/module-prefetch-delegate.ts +++ b/src/core/course/providers/module-prefetch-delegate.ts @@ -27,6 +27,7 @@ import { CoreConstants } from '../../constants'; import { Md5 } from 'ts-md5/dist/md5'; import { Subject, BehaviorSubject, Subscription } from 'rxjs'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Progress of downloading a list of modules. @@ -202,7 +203,7 @@ export interface CoreCourseModulePrefetchHandler extends CoreDelegateHandler { export class CoreCourseModulePrefetchDelegate extends CoreDelegate { // Variables for database. protected CHECK_UPDATES_TIMES_TABLE = 'check_updates_times'; - protected checkUpdatesTableSchema = { + protected checkUpdatesTableSchema: SQLiteDBTableSchema = { name: this.CHECK_UPDATES_TIMES_TABLE, columns: [ { diff --git a/src/core/emulator/providers/helper.ts b/src/core/emulator/providers/helper.ts index 4c866ebc8..e3eb05776 100644 --- a/src/core/emulator/providers/helper.ts +++ b/src/core/emulator/providers/helper.ts @@ -26,6 +26,7 @@ import { CoreTimeUtilsProvider } from '@providers/utils/time'; import { FileTransferErrorMock } from './file-transfer'; import { CoreEmulatorCaptureHelperProvider } from './capture-helper'; import { CoreConstants } from '../../constants'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Helper service for the emulator feature. It also acts as an init handler. @@ -40,7 +41,7 @@ export class CoreEmulatorHelperProvider implements CoreInitHandler { // Variables for database. protected LAST_RECEIVED_NOTIFICATION_TABLE = 'core_emulator_last_received_notification'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: this.LAST_RECEIVED_NOTIFICATION_TABLE, columns: [ diff --git a/src/core/emulator/providers/local-notifications.ts b/src/core/emulator/providers/local-notifications.ts index 0936960e7..f8e4f0f65 100644 --- a/src/core/emulator/providers/local-notifications.ts +++ b/src/core/emulator/providers/local-notifications.ts @@ -17,7 +17,7 @@ import { LocalNotifications, ILocalNotification, ILocalNotificationAction } from import { CoreAppProvider } from '@providers/app'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreUtilsProvider } from '@providers/utils/utils'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; import { CoreConstants } from '@core/constants'; import { CoreConfigConstants } from '../../../configconstants'; import * as moment from 'moment'; @@ -43,7 +43,7 @@ export class LocalNotificationsMock extends LocalNotifications { // Variables for database. protected DESKTOP_NOTIFS_TABLE = 'desktop_local_notifications'; - protected tableSchema = { + protected tableSchema: SQLiteDBTableSchema = { name: this.DESKTOP_NOTIFS_TABLE, columns: [ { diff --git a/src/core/question/providers/question.ts b/src/core/question/providers/question.ts index 31a132cae..4c1e56e0e 100644 --- a/src/core/question/providers/question.ts +++ b/src/core/question/providers/question.ts @@ -17,6 +17,7 @@ import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; import { CoreUtilsProvider } from '@providers/utils/utils'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * An object to represent a question state. @@ -63,7 +64,7 @@ export class CoreQuestionProvider { // Variables for database. protected QUESTION_TABLE = 'questions'; protected QUESTION_ANSWERS_TABLE = 'question_answers'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: this.QUESTION_TABLE, columns: [ diff --git a/src/core/sharedfiles/providers/sharedfiles.ts b/src/core/sharedfiles/providers/sharedfiles.ts index 407c572ae..1938c1ec4 100644 --- a/src/core/sharedfiles/providers/sharedfiles.ts +++ b/src/core/sharedfiles/providers/sharedfiles.ts @@ -21,7 +21,7 @@ import { CoreSitesProvider } from '@providers/sites'; import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { Md5 } from 'ts-md5/dist/md5'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to share files with the app. @@ -32,7 +32,7 @@ export class CoreSharedFilesProvider { // Variables for the database. protected SHARED_FILES_TABLE = 'shared_files'; - protected tableSchema = { + protected tableSchema: SQLiteDBTableSchema = { name: this.SHARED_FILES_TABLE, columns: [ { diff --git a/src/core/user/providers/user.ts b/src/core/user/providers/user.ts index 242c586d6..96c1fa0f5 100644 --- a/src/core/user/providers/user.ts +++ b/src/core/user/providers/user.ts @@ -18,6 +18,7 @@ import { CoreLoggerProvider } from '@providers/logger'; import { CoreSite } from '@classes/site'; import { CoreSitesProvider } from '@providers/sites'; import { CoreUtilsProvider } from '@providers/utils/utils'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Service to provide user functionalities. @@ -31,7 +32,7 @@ export class CoreUserProvider { // Variables for database. protected USERS_TABLE = 'users'; - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: this.USERS_TABLE, columns: [ diff --git a/src/providers/config.ts b/src/providers/config.ts index 05e9a6a0e..e368c073b 100644 --- a/src/providers/config.ts +++ b/src/providers/config.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; import { CoreAppProvider } from './app'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Factory to provide access to dynamic and permanent config and settings. @@ -24,7 +24,7 @@ import { SQLiteDB } from '@classes/sqlitedb'; export class CoreConfigProvider { protected appDB: SQLiteDB; protected TABLE_NAME = 'core_config'; - protected tableSchema = { + protected tableSchema: SQLiteDBTableSchema = { name: this.TABLE_NAME, columns: [ { diff --git a/src/providers/cron.ts b/src/providers/cron.ts index b82335476..99d12b95d 100644 --- a/src/providers/cron.ts +++ b/src/providers/cron.ts @@ -19,7 +19,7 @@ import { CoreConfigProvider } from './config'; import { CoreLoggerProvider } from './logger'; import { CoreUtilsProvider } from './utils/utils'; import { CoreConstants } from '@core/constants'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; /** * Interface that all cron handlers must implement. @@ -94,7 +94,7 @@ export class CoreCronDelegate { // Variables for database. protected CRON_TABLE = 'cron'; - protected tableSchema = { + protected tableSchema: SQLiteDBTableSchema = { name: this.CRON_TABLE, columns: [ { diff --git a/src/providers/filepool.ts b/src/providers/filepool.ts index e01deba92..08fbb85a6 100644 --- a/src/providers/filepool.ts +++ b/src/providers/filepool.ts @@ -28,7 +28,7 @@ import { CoreTextUtilsProvider } from './utils/text'; import { CoreTimeUtilsProvider } from './utils/time'; import { CoreUrlUtilsProvider } from './utils/url'; import { CoreUtilsProvider } from './utils/utils'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; import { CoreConstants } from '@core/constants'; import { Md5 } from 'ts-md5/dist/md5'; @@ -254,7 +254,7 @@ export class CoreFilepoolProvider { protected FILES_TABLE = 'filepool_files'; // Downloaded files. protected LINKS_TABLE = 'filepool_files_links'; // Links between downloaded files and components. protected PACKAGES_TABLE = 'filepool_packages'; // Downloaded packages (sets of files). - protected appTablesSchema = [ + protected appTablesSchema: SQLiteDBTableSchema[] = [ { name: this.QUEUE_TABLE, columns: [ @@ -306,7 +306,7 @@ export class CoreFilepoolProvider { primaryKeys: ['siteId', 'fileId'] } ]; - protected sitesTablesSchema = [ + protected sitesTablesSchema: SQLiteDBTableSchema[] = [ { name: this.FILES_TABLE, columns: [ diff --git a/src/providers/local-notifications.ts b/src/providers/local-notifications.ts index 5b64a5f64..877ee7d48 100644 --- a/src/providers/local-notifications.ts +++ b/src/providers/local-notifications.ts @@ -22,7 +22,7 @@ import { CoreEventsProvider } from './events'; import { CoreLoggerProvider } from './logger'; import { CoreTextUtilsProvider } from './utils/text'; import { CoreUtilsProvider } from './utils/utils'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; import { CoreConstants } from '@core/constants'; import { Subject, Subscription } from 'rxjs'; @@ -38,7 +38,7 @@ export class CoreLocalNotificationsProvider { protected SITES_TABLE = 'notification_sites'; // Store to asigne unique codes to each site. protected COMPONENTS_TABLE = 'notification_components'; // Store to asigne unique codes to each component. protected TRIGGERED_TABLE = 'notifications_triggered'; // Store to prevent re-triggering notifications. - protected tablesSchema = [ + protected tablesSchema: SQLiteDBTableSchema[] = [ { name: this.SITES_TABLE, columns: [ diff --git a/src/providers/sites.ts b/src/providers/sites.ts index d1abf0bc9..226efe33a 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -25,7 +25,7 @@ import { CoreUtilsProvider } from './utils/utils'; import { CoreConstants } from '@core/constants'; import { CoreConfigConstants } from '../configconstants'; import { CoreSite } from '@classes/site'; -import { SQLiteDB } from '@classes/sqlitedb'; +import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb'; import { Md5 } from 'ts-md5/dist/md5'; /** @@ -143,7 +143,7 @@ export class CoreSitesProvider { // Variables for the database. protected SITES_TABLE = 'sites'; protected CURRENT_SITE_TABLE = 'current_site'; - protected appTablesSchema = [ + protected appTablesSchema: SQLiteDBTableSchema[] = [ { name: this.SITES_TABLE, columns: [ @@ -1251,18 +1251,18 @@ export class CoreSitesProvider { /** * Create a table in all the sites databases. * - * @param {any} table Table schema. + * @param {SQLiteDBTamableSchema} table Table schema. */ - createTableFromSchema(table: any): void { + createTableFromSchema(table: SQLiteDBTableSchema): void { this.createTablesFromSchema([table]); } /** * Create several tables in all the sites databases. * - * @param {any[]} tables List of tables schema. + * @param {SQLiteDBTamableSchema[]} tables List of tables schema. */ - createTablesFromSchema(tables: any[]): void { + createTablesFromSchema(tables: SQLiteDBTableSchema[]): void { // Add the tables to the list of schemas. This list is to create all the tables in new sites. this.siteTablesSchemas = this.siteTablesSchemas.concat(tables); diff --git a/src/providers/sync.ts b/src/providers/sync.ts index 510fa7be5..e1329d8e0 100644 --- a/src/providers/sync.ts +++ b/src/providers/sync.ts @@ -15,6 +15,7 @@ import { Injectable } from '@angular/core'; import { CoreEventsProvider } from './events'; import { CoreSitesProvider } from './sites'; +import { SQLiteDBTableSchema } from '@classes/sqlitedb'; /* * Service that provides some features regarding synchronization. @@ -24,7 +25,7 @@ export class CoreSyncProvider { // Variables for the database. protected SYNC_TABLE = 'sync'; - protected tableSchema = { + protected tableSchema: SQLiteDBTableSchema = { name: this.SYNC_TABLE, columns: [ {