From db46635262581b1896aa294f8fd851d00e3dbbac Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Wed, 30 Jan 2019 11:03:27 +0100 Subject: [PATCH] MOBILE-2842 core: New SQLiteDB methods --- src/classes/sqlitedb.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/classes/sqlitedb.ts b/src/classes/sqlitedb.ts index fcd319045..9bc24272b 100644 --- a/src/classes/sqlitedb.ts +++ b/src/classes/sqlitedb.ts @@ -407,6 +407,16 @@ export class SQLiteDB { return this.execute(`DELETE FROM ${table} ${select}`, params); } + /** + * Drop a table if it exists. + * + * @param {string} name The table name. + * @return {Promise} Promise resolved when success. + */ + dropTable(name: string): Promise { + return this.execute(`DROP TABLE IF EXISTS ${name}`); + } + /** * Execute a SQL query. * IMPORTANT: Use this function only if you cannot use any of the other functions in this API. Please take into account that @@ -783,6 +793,23 @@ export class SQLiteDB { return this.executeBatch(statements); } + /** + * Insert multiple records into database from another table. + * + * @param {string} table The database table to be inserted into. + * @param {string} source The database table to get the records from. + * @param {object} [conditions] The conditions to build the where clause. Must not contain numeric indexes. + * @param {string} [fields='*'] A comma separated list of fields to return. + * @return {Promise} Promise resolved when done. + */ + insertRecordsFrom(table: string, source: string, conditions?: object, fields: string = '*'): Promise { + const selectAndParams = this.whereClause(conditions); + const select = selectAndParams[0] ? 'WHERE ' + selectAndParams[0] : ''; + const params = selectAndParams[1]; + + return this.execute(`INSERT INTO ${table} SELECT ${fields} FROM ${source} ${select}`, params); + } + /** * Ensures that limit params are numeric and positive integers, to be passed to the database. * We explicitly treat null, '' and -1 as 0 in order to provide compatibility with how limit @@ -875,6 +902,16 @@ export class SQLiteDB { }); } + /** + * Test whether a table exists.. + * + * @param {string} name The table name. + * @return {Promise} Promise resolved if exists, rejected otherwise. + */ + tableExists(name: string): Promise { + return this.recordExists('sqlite_master', {type: 'table', tbl_name: name}); + } + /** * Update one or more records in a table. *