MOBILE-4270 db: Tweaks on sqlite

main
Pau Ferrer Ocaña 2022-11-30 11:35:02 +01:00
parent c22424c93e
commit 51bd21163a
1 changed files with 39 additions and 52 deletions

View File

@ -174,7 +174,8 @@ export class SQLiteDB {
sql = equal ? '= ?' : '<> ?'; sql = equal ? '= ?' : '<> ?';
params = Array.isArray(items) ? items : [items]; params = Array.isArray(items) ? items : [items];
} else { } else {
sql = (equal ? '' : 'NOT ') + 'IN (' + ',?'.repeat(items.length).substring(1) + ')'; const questionMarks = ',?'.repeat(items.length).substring(1);
sql = (equal ? '' : 'NOT ') + `IN (${questionMarks})`;
params = items; params = items;
} }
@ -237,7 +238,7 @@ export class SQLiteDB {
tableCheck?: string, tableCheck?: string,
): string { ): string {
const columnsSql: string[] = []; const columnsSql: string[] = [];
let sql = `CREATE TABLE IF NOT EXISTS ${name} (`; let tableStructureSQL = '';
// First define all the columns. // First define all the columns.
for (const index in columns) { for (const index in columns) {
@ -245,7 +246,7 @@ export class SQLiteDB {
let columnSql: string = column.name || ''; let columnSql: string = column.name || '';
if (column.type) { if (column.type) {
columnSql += ' ' + column.type; columnSql += ` ${column.type}`;
} }
if (column.primaryKey) { if (column.primaryKey) {
@ -273,25 +274,25 @@ export class SQLiteDB {
columnsSql.push(columnSql); columnsSql.push(columnSql);
} }
sql += columnsSql.join(', '); tableStructureSQL += columnsSql.join(', ');
// Now add the table constraints. // Now add the table constraints.
if (primaryKeys && primaryKeys.length) { if (primaryKeys && primaryKeys.length) {
sql += `, PRIMARY KEY (${primaryKeys.join(', ')})`; tableStructureSQL += `, PRIMARY KEY (${primaryKeys.join(', ')})`;
} }
if (uniqueKeys && uniqueKeys.length) { if (uniqueKeys && uniqueKeys.length) {
for (const index in uniqueKeys) { for (const index in uniqueKeys) {
const setOfKeys = uniqueKeys[index]; const setOfKeys = uniqueKeys[index];
if (setOfKeys && setOfKeys.length) { if (setOfKeys && setOfKeys.length) {
sql += `, UNIQUE (${setOfKeys.join(', ')})`; tableStructureSQL += `, UNIQUE (${setOfKeys.join(', ')})`;
} }
} }
} }
if (tableCheck) { if (tableCheck) {
sql += `, CHECK (${tableCheck})`; tableStructureSQL += `, CHECK (${tableCheck})`;
} }
for (const index in foreignKeys) { for (const index in foreignKeys) {
@ -301,18 +302,18 @@ export class SQLiteDB {
continue; continue;
} }
sql += `, FOREIGN KEY (${foreignKey.columns.join(', ')}) REFERENCES ${foreignKey.table} `; tableStructureSQL += `, FOREIGN KEY (${foreignKey.columns.join(', ')}) REFERENCES ${foreignKey.table} `;
if (foreignKey.foreignColumns && foreignKey.foreignColumns.length) { if (foreignKey.foreignColumns && foreignKey.foreignColumns.length) {
sql += `(${foreignKey.foreignColumns.join(', ')})`; tableStructureSQL += `(${foreignKey.foreignColumns.join(', ')})`;
} }
if (foreignKey.actions) { if (foreignKey.actions) {
sql += ` ${foreignKey.actions}`; tableStructureSQL += ` ${foreignKey.actions}`;
} }
} }
return sql + ')'; return `CREATE TABLE IF NOT EXISTS ${name} (${tableStructureSQL})`;
} }
/** /**
@ -355,7 +356,7 @@ export class SQLiteDB {
countItem: string = 'COUNT(\'x\')', countItem: string = 'COUNT(\'x\')',
): Promise<number> { ): Promise<number> {
if (select) { if (select) {
select = 'WHERE ' + select; select = `WHERE ${select}`;
} }
return this.countRecordsSql(`SELECT ${countItem} FROM ${table} ${select}`, params); return this.countRecordsSql(`SELECT ${countItem} FROM ${table} ${select}`, params);
@ -470,7 +471,7 @@ export class SQLiteDB {
*/ */
async deleteRecordsSelect(table: string, select: string = '', params?: SQLiteDBRecordValue[]): Promise<number> { async deleteRecordsSelect(table: string, select: string = '', params?: SQLiteDBRecordValue[]): Promise<number> {
if (select) { if (select) {
select = 'WHERE ' + select; select = `WHERE ${select}`;
} }
const result = await this.execute(`DELETE FROM ${table} ${select}`, params); const result = await this.execute(`DELETE FROM ${table} ${select}`, params);
@ -529,22 +530,8 @@ export class SQLiteDB {
return; return;
} }
// Remove undefined entries and convert null to "NULL". // Remove undefined entries.
for (const name in data) { Object.keys(data).forEach(key => data[key] === undefined && delete data[key]);
if (data[name] === undefined) {
delete data[name];
}
}
}
/**
* Format the data to where params.
*
* @param data Object data.
* @returns List of params.
*/
protected formatDataToSQLParams(data: SQLiteDBRecordValues): SQLiteDBRecordValue[] {
return Object.values(data).map((value) => value as SQLiteDBRecordValue);
} }
/** /**
@ -587,7 +574,7 @@ export class SQLiteDB {
params?: SQLiteDBRecordValue[], params?: SQLiteDBRecordValue[],
): Promise<SQLiteDBRecordValue> { ): Promise<SQLiteDBRecordValue> {
if (select) { if (select) {
select = 'WHERE ' + select; select = `WHERE ${select}`;
} }
return this.getFieldSql(`SELECT ${field} FROM ${table} ${select}`, params); return this.getFieldSql(`SELECT ${field} FROM ${table} ${select}`, params);
@ -648,7 +635,7 @@ export class SQLiteDB {
fields: string = '*', fields: string = '*',
): Promise<T> { ): Promise<T> {
if (select) { if (select) {
select = ' WHERE ' + select; select = ` WHERE ${select}`;
} }
return this.getRecordSql<T>(`SELECT ${fields} FROM ${table} ${select}`, params); return this.getRecordSql<T>(`SELECT ${fields} FROM ${table} ${select}`, params);
@ -746,10 +733,10 @@ export class SQLiteDB {
limitNum: number = 0, limitNum: number = 0,
): Promise<T[]> { ): Promise<T[]> {
if (select) { if (select) {
select = ' WHERE ' + select; select = ` WHERE ${select}`;
} }
if (sort) { if (sort) {
sort = ' ORDER BY ' + sort; sort = ` ORDER BY ${sort}`;
} }
const sql = `SELECT ${fields} FROM ${table} ${select} ${sort}`; const sql = `SELECT ${fields} FROM ${table} ${select} ${sort}`;
@ -778,7 +765,7 @@ export class SQLiteDB {
if (limits[1] < 1) { if (limits[1] < 1) {
limits[1] = Number.MAX_VALUE; limits[1] = Number.MAX_VALUE;
} }
sql += ' LIMIT ' + limits[0] + ', ' + limits[1]; sql += ` LIMIT ${limits[0]}, ${limits[1]}`;
} }
const result = await this.execute(sql, params); const result = await this.execute(sql, params);
@ -807,7 +794,7 @@ export class SQLiteDB {
return { return {
sql: `INSERT OR REPLACE INTO ${table} (${fields}) VALUES (${questionMarks})`, sql: `INSERT OR REPLACE INTO ${table} (${fields}) VALUES (${questionMarks})`,
params: this.formatDataToSQLParams(data), params: Object.values(data),
}; };
} }
@ -897,7 +884,7 @@ export class SQLiteDB {
): Promise<void> { ): Promise<void> {
try { try {
await this.tableExists(oldTable); await this.tableExists(oldTable);
} catch (error) { } catch {
// Old table does not exist, ignore. // Old table does not exist, ignore.
return; return;
} }
@ -919,7 +906,7 @@ export class SQLiteDB {
try { try {
await this.dropTable(oldTable); await this.dropTable(oldTable);
} catch (error) { } catch {
// Error deleting old table, ignore. // Error deleting old table, ignore.
} }
} }
@ -1066,7 +1053,7 @@ export class SQLiteDB {
} }
// Create the list of params using the "data" object and the params for the where clause. // Create the list of params using the "data" object and the params for the where clause.
let params = this.formatDataToSQLParams(data); let params = Object.values(data);
if (where && whereParams) { if (where && whereParams) {
params = params.concat(whereParams); params = params.concat(whereParams);
} }
@ -1090,19 +1077,19 @@ export class SQLiteDB {
}; };
} }
const where: string[] = [];
const params: SQLiteDBRecordValue[] = []; const params: SQLiteDBRecordValue[] = [];
for (const key in conditions) { const where = Object.keys(conditions).map((field) => {
const value = conditions[key]; const value = conditions[field];
if (value === undefined || value === null) { if (value === undefined || value === null) {
where.push(key + ' IS NULL'); return `${field} IS NULL`;
} else { }
where.push(key + ' = ?');
params.push(value); params.push(value);
}
} return `${field} = ?`;
});
return { return {
sql: where.join(' AND '), sql: where.join(' AND '),
@ -1130,7 +1117,7 @@ export class SQLiteDB {
values.forEach((value) => { values.forEach((value) => {
if (value === undefined || value === null) { if (value === undefined || value === null) {
sql = field + ' IS NULL'; sql = `${field} IS NULL`;
} else { } else {
params.push(value); params.push(value);
} }
@ -1138,14 +1125,14 @@ export class SQLiteDB {
if (params && params.length) { if (params && params.length) {
if (sql !== '') { if (sql !== '') {
sql = sql + ' OR '; sql += ' OR ';
} }
if (params.length == 1) { if (params.length == 1) {
sql = sql + field + ' = ?'; sql += `${field} = ?`;
} else { } else {
const questionMarks = ',?'.repeat(params.length).substring(1); const questionMarks = ',?'.repeat(params.length).substring(1);
sql = sql + field + ' IN (' + questionMarks + ')'; sql += ` ${field} IN (${questionMarks})`;
} }
} }
@ -1232,7 +1219,7 @@ export class SQLiteDB {
} }
export type SQLiteDBRecordValues = { export type SQLiteDBRecordValues = {
[key: string]: SQLiteDBRecordValue | undefined | null; [key: string]: SQLiteDBRecordValue;
}; };
export type SQLiteDBQueryParams = { export type SQLiteDBQueryParams = {
@ -1240,4 +1227,4 @@ export type SQLiteDBQueryParams = {
params: SQLiteDBRecordValue[]; params: SQLiteDBRecordValue[];
}; };
export type SQLiteDBRecordValue = number | string; export type SQLiteDBRecordValue = number | string | undefined | null;