MOBILE-2842 core: New SQLiteDB methods

main
Albert Gasset 2019-01-30 11:03:27 +01:00
parent fb29050627
commit db46635262
1 changed files with 37 additions and 0 deletions

View File

@ -407,6 +407,16 @@ export class SQLiteDB {
return this.execute(`DELETE FROM ${table} ${select}`, params); return this.execute(`DELETE FROM ${table} ${select}`, params);
} }
/**
* Drop a table if it exists.
*
* @param {string} name The table name.
* @return {Promise<any>} Promise resolved when success.
*/
dropTable(name: string): Promise<any> {
return this.execute(`DROP TABLE IF EXISTS ${name}`);
}
/** /**
* Execute a SQL query. * 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 * 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); 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<any>} Promise resolved when done.
*/
insertRecordsFrom(table: string, source: string, conditions?: object, fields: string = '*'): Promise<any> {
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. * 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 * 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<void>} Promise resolved if exists, rejected otherwise.
*/
tableExists(name: string): Promise<void> {
return this.recordExists('sqlite_master', {type: 'table', tbl_name: name});
}
/** /**
* Update one or more records in a table. * Update one or more records in a table.
* *