MOBILE-3565 core: Fix SQL params
parent
dd636e61fd
commit
8fef21ff55
|
@ -156,8 +156,14 @@ export class SQLiteDB {
|
||||||
* @param tableCheck Check constraint for the table.
|
* @param tableCheck Check constraint for the table.
|
||||||
* @return SQL query.
|
* @return SQL query.
|
||||||
*/
|
*/
|
||||||
buildCreateTableSql(name: string, columns: SQLiteDBColumnSchema[], primaryKeys?: string[], uniqueKeys?: string[][],
|
buildCreateTableSql(
|
||||||
foreignKeys?: SQLiteDBForeignKeySchema[], tableCheck?: string): string {
|
name: string,
|
||||||
|
columns: SQLiteDBColumnSchema[],
|
||||||
|
primaryKeys?: string[],
|
||||||
|
uniqueKeys?: string[][],
|
||||||
|
foreignKeys?: SQLiteDBForeignKeySchema[],
|
||||||
|
tableCheck?: string,
|
||||||
|
): string {
|
||||||
const columnsSql = [];
|
const columnsSql = [];
|
||||||
let sql = `CREATE TABLE IF NOT EXISTS ${name} (`;
|
let sql = `CREATE TABLE IF NOT EXISTS ${name} (`;
|
||||||
|
|
||||||
|
@ -258,7 +264,7 @@ export class SQLiteDB {
|
||||||
async countRecords(table: string, conditions?: SQLiteDBRecordValues): Promise<number> {
|
async countRecords(table: string, conditions?: SQLiteDBRecordValues): Promise<number> {
|
||||||
const selectAndParams = this.whereClause(conditions);
|
const selectAndParams = this.whereClause(conditions);
|
||||||
|
|
||||||
return this.countRecordsSelect(table, selectAndParams[0], selectAndParams[1]);
|
return this.countRecordsSelect(table, selectAndParams.sql, selectAndParams.params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -270,8 +276,12 @@ export class SQLiteDB {
|
||||||
* @param countItem The count string to be used in the SQL call. Default is COUNT('x').
|
* @param countItem The count string to be used in the SQL call. Default is COUNT('x').
|
||||||
* @return Promise resolved with the count of records returned from the specified criteria.
|
* @return Promise resolved with the count of records returned from the specified criteria.
|
||||||
*/
|
*/
|
||||||
async countRecordsSelect(table: string, select: string = '', params?: SQLiteDBRecordValue[],
|
async countRecordsSelect(
|
||||||
countItem: string = 'COUNT(\'x\')'): Promise<number> {
|
table: string,
|
||||||
|
select: string = '',
|
||||||
|
params?: SQLiteDBRecordValue[],
|
||||||
|
countItem: string = 'COUNT(\'x\')',
|
||||||
|
): Promise<number> {
|
||||||
if (select) {
|
if (select) {
|
||||||
select = 'WHERE ' + select;
|
select = 'WHERE ' + select;
|
||||||
}
|
}
|
||||||
|
@ -308,8 +318,14 @@ export class SQLiteDB {
|
||||||
* @param tableCheck Check constraint for the table.
|
* @param tableCheck Check constraint for the table.
|
||||||
* @return Promise resolved when success.
|
* @return Promise resolved when success.
|
||||||
*/
|
*/
|
||||||
async createTable(name: string, columns: SQLiteDBColumnSchema[], primaryKeys?: string[], uniqueKeys?: string[][],
|
async createTable(
|
||||||
foreignKeys?: SQLiteDBForeignKeySchema[], tableCheck?: string): Promise<void> {
|
name: string,
|
||||||
|
columns: SQLiteDBColumnSchema[],
|
||||||
|
primaryKeys?: string[],
|
||||||
|
uniqueKeys?: string[][],
|
||||||
|
foreignKeys?: SQLiteDBForeignKeySchema[],
|
||||||
|
tableCheck?: string,
|
||||||
|
): Promise<void> {
|
||||||
const sql = this.buildCreateTableSql(name, columns, primaryKeys, uniqueKeys, foreignKeys, tableCheck);
|
const sql = this.buildCreateTableSql(name, columns, primaryKeys, uniqueKeys, foreignKeys, tableCheck);
|
||||||
|
|
||||||
await this.execute(sql);
|
await this.execute(sql);
|
||||||
|
@ -358,7 +374,7 @@ export class SQLiteDB {
|
||||||
|
|
||||||
const selectAndParams = this.whereClause(conditions);
|
const selectAndParams = this.whereClause(conditions);
|
||||||
|
|
||||||
return this.deleteRecordsSelect(table, selectAndParams[0], selectAndParams[1]);
|
return this.deleteRecordsSelect(table, selectAndParams.sql, selectAndParams.params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -372,7 +388,7 @@ export class SQLiteDB {
|
||||||
async deleteRecordsList(table: string, field: string, values: SQLiteDBRecordValue[]): Promise<number> {
|
async deleteRecordsList(table: string, field: string, values: SQLiteDBRecordValue[]): Promise<number> {
|
||||||
const selectAndParams = this.whereClauseList(field, values);
|
const selectAndParams = this.whereClauseList(field, values);
|
||||||
|
|
||||||
return this.deleteRecordsSelect(table, selectAndParams[0], selectAndParams[1]);
|
return this.deleteRecordsSelect(table, selectAndParams.sql, selectAndParams.params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -483,7 +499,7 @@ export class SQLiteDB {
|
||||||
async getField(table: string, field: string, conditions?: SQLiteDBRecordValues): Promise<SQLiteDBRecordValue> {
|
async getField(table: string, field: string, conditions?: SQLiteDBRecordValues): Promise<SQLiteDBRecordValue> {
|
||||||
const selectAndParams = this.whereClause(conditions);
|
const selectAndParams = this.whereClause(conditions);
|
||||||
|
|
||||||
return this.getFieldSelect(table, field, selectAndParams[0], selectAndParams[1]);
|
return this.getFieldSelect(table, field, selectAndParams.sql, selectAndParams.params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -495,8 +511,12 @@ export class SQLiteDB {
|
||||||
* @param params Array of sql parameters.
|
* @param params Array of sql parameters.
|
||||||
* @return Promise resolved with the field's value.
|
* @return Promise resolved with the field's value.
|
||||||
*/
|
*/
|
||||||
async getFieldSelect(table: string, field: string, select: string = '', params?: SQLiteDBRecordValue[]):
|
async getFieldSelect(
|
||||||
Promise<SQLiteDBRecordValue> {
|
table: string,
|
||||||
|
field: string,
|
||||||
|
select: string = '',
|
||||||
|
params?: SQLiteDBRecordValue[],
|
||||||
|
): Promise<SQLiteDBRecordValue> {
|
||||||
if (select) {
|
if (select) {
|
||||||
select = 'WHERE ' + select;
|
select = 'WHERE ' + select;
|
||||||
}
|
}
|
||||||
|
@ -529,8 +549,11 @@ export class SQLiteDB {
|
||||||
* meaning return empty. Other values will become part of the returned SQL fragment.
|
* meaning return empty. Other values will become part of the returned SQL fragment.
|
||||||
* @return A list containing the constructed sql fragment and an array of parameters.
|
* @return A list containing the constructed sql fragment and an array of parameters.
|
||||||
*/
|
*/
|
||||||
getInOrEqual(items: SQLiteDBRecordValue | SQLiteDBRecordValue[], equal: boolean = true, onEmptyItems?: SQLiteDBRecordValue):
|
getInOrEqual(
|
||||||
SQLiteDBQueryParams {
|
items: SQLiteDBRecordValue | SQLiteDBRecordValue[],
|
||||||
|
equal: boolean = true,
|
||||||
|
onEmptyItems?: SQLiteDBRecordValue,
|
||||||
|
): SQLiteDBQueryParams {
|
||||||
let sql = '';
|
let sql = '';
|
||||||
let params: SQLiteDBRecordValue[];
|
let params: SQLiteDBRecordValue[];
|
||||||
|
|
||||||
|
@ -581,7 +604,7 @@ export class SQLiteDB {
|
||||||
getRecord<T = unknown>(table: string, conditions?: SQLiteDBRecordValues, fields: string = '*'): Promise<T> {
|
getRecord<T = unknown>(table: string, conditions?: SQLiteDBRecordValues, fields: string = '*'): Promise<T> {
|
||||||
const selectAndParams = this.whereClause(conditions);
|
const selectAndParams = this.whereClause(conditions);
|
||||||
|
|
||||||
return this.getRecordSelect<T>(table, selectAndParams[0], selectAndParams[1], fields);
|
return this.getRecordSelect<T>(table, selectAndParams.sql, selectAndParams.params, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -593,8 +616,12 @@ export class SQLiteDB {
|
||||||
* @param fields A comma separated list of fields to return.
|
* @param fields A comma separated list of fields to return.
|
||||||
* @return Promise resolved with the record, rejected if not found.
|
* @return Promise resolved with the record, rejected if not found.
|
||||||
*/
|
*/
|
||||||
getRecordSelect<T = unknown>(table: string, select: string = '', params: SQLiteDBRecordValue[] = [], fields: string = '*'):
|
getRecordSelect<T = unknown>(
|
||||||
Promise<T> {
|
table: string,
|
||||||
|
select: string = '',
|
||||||
|
params: SQLiteDBRecordValue[] = [],
|
||||||
|
fields: string = '*',
|
||||||
|
): Promise<T> {
|
||||||
if (select) {
|
if (select) {
|
||||||
select = ' WHERE ' + select;
|
select = ' WHERE ' + select;
|
||||||
}
|
}
|
||||||
|
@ -633,11 +660,17 @@ export class SQLiteDB {
|
||||||
* @param limitNum Return a subset comprising this many records in total.
|
* @param limitNum Return a subset comprising this many records in total.
|
||||||
* @return Promise resolved with the records.
|
* @return Promise resolved with the records.
|
||||||
*/
|
*/
|
||||||
getRecords<T = unknown>(table: string, conditions?: SQLiteDBRecordValues, sort: string = '', fields: string = '*',
|
getRecords<T = unknown>(
|
||||||
limitFrom: number = 0, limitNum: number = 0): Promise<T[]> {
|
table: string,
|
||||||
|
conditions?: SQLiteDBRecordValues,
|
||||||
|
sort: string = '',
|
||||||
|
fields: string = '*',
|
||||||
|
limitFrom: number = 0,
|
||||||
|
limitNum: number = 0,
|
||||||
|
): Promise<T[]> {
|
||||||
const selectAndParams = this.whereClause(conditions);
|
const selectAndParams = this.whereClause(conditions);
|
||||||
|
|
||||||
return this.getRecordsSelect<T>(table, selectAndParams[0], selectAndParams[1], sort, fields, limitFrom, limitNum);
|
return this.getRecordsSelect<T>(table, selectAndParams.sql, selectAndParams.params, sort, fields, limitFrom, limitNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -652,11 +685,18 @@ export class SQLiteDB {
|
||||||
* @param limitNum Return a subset comprising this many records in total.
|
* @param limitNum Return a subset comprising this many records in total.
|
||||||
* @return Promise resolved with the records.
|
* @return Promise resolved with the records.
|
||||||
*/
|
*/
|
||||||
getRecordsList<T = unknown>(table: string, field: string, values: SQLiteDBRecordValue[], sort: string = '',
|
getRecordsList<T = unknown>(
|
||||||
fields: string = '*', limitFrom: number = 0, limitNum: number = 0): Promise<T[]> {
|
table: string,
|
||||||
|
field: string,
|
||||||
|
values: SQLiteDBRecordValue[],
|
||||||
|
sort: string = '',
|
||||||
|
fields: string = '*',
|
||||||
|
limitFrom: number = 0,
|
||||||
|
limitNum: number = 0,
|
||||||
|
): Promise<T[]> {
|
||||||
const selectAndParams = this.whereClauseList(field, values);
|
const selectAndParams = this.whereClauseList(field, values);
|
||||||
|
|
||||||
return this.getRecordsSelect<T>(table, selectAndParams[0], selectAndParams[1], sort, fields, limitFrom, limitNum);
|
return this.getRecordsSelect<T>(table, selectAndParams.sql, selectAndParams.params, sort, fields, limitFrom, limitNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -671,8 +711,15 @@ export class SQLiteDB {
|
||||||
* @param limitNum Return a subset comprising this many records in total.
|
* @param limitNum Return a subset comprising this many records in total.
|
||||||
* @return Promise resolved with the records.
|
* @return Promise resolved with the records.
|
||||||
*/
|
*/
|
||||||
getRecordsSelect<T = unknown>(table: string, select: string = '', params: SQLiteDBRecordValue[] = [], sort: string = '',
|
getRecordsSelect<T = unknown>(
|
||||||
fields: string = '*', limitFrom: number = 0, limitNum: number = 0): Promise<T[]> {
|
table: string,
|
||||||
|
select: string = '',
|
||||||
|
params: SQLiteDBRecordValue[] = [],
|
||||||
|
sort: string = '',
|
||||||
|
fields: string = '*',
|
||||||
|
limitFrom: number = 0,
|
||||||
|
limitNum: number = 0,
|
||||||
|
): Promise<T[]> {
|
||||||
if (select) {
|
if (select) {
|
||||||
select = ' WHERE ' + select;
|
select = ' WHERE ' + select;
|
||||||
}
|
}
|
||||||
|
@ -694,8 +741,12 @@ export class SQLiteDB {
|
||||||
* @param limitNum Return a subset comprising this many records.
|
* @param limitNum Return a subset comprising this many records.
|
||||||
* @return Promise resolved with the records.
|
* @return Promise resolved with the records.
|
||||||
*/
|
*/
|
||||||
async getRecordsSql<T = unknown>(sql: string, params?: SQLiteDBRecordValue[], limitFrom?: number, limitNum?: number):
|
async getRecordsSql<T = unknown>(
|
||||||
Promise<T[]> {
|
sql: string,
|
||||||
|
params?: SQLiteDBRecordValue[],
|
||||||
|
limitFrom?: number,
|
||||||
|
limitNum?: number,
|
||||||
|
): Promise<T[]> {
|
||||||
const limits = this.normaliseLimitFromNum(limitFrom, limitNum);
|
const limits = this.normaliseLimitFromNum(limitFrom, limitNum);
|
||||||
|
|
||||||
if (limits[0] || limits[1]) {
|
if (limits[0] || limits[1]) {
|
||||||
|
@ -746,6 +797,8 @@ export class SQLiteDB {
|
||||||
}))
|
}))
|
||||||
.then((db: SQLiteObject) => {
|
.then((db: SQLiteObject) => {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
|
|
||||||
|
return;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -758,7 +811,7 @@ export class SQLiteDB {
|
||||||
*/
|
*/
|
||||||
async insertRecord(table: string, data: SQLiteDBRecordValues): Promise<number> {
|
async insertRecord(table: string, data: SQLiteDBRecordValues): Promise<number> {
|
||||||
const sqlAndParams = this.getSqlInsertQuery(table, data);
|
const sqlAndParams = this.getSqlInsertQuery(table, data);
|
||||||
const result = await this.execute(sqlAndParams[0], sqlAndParams[1]);
|
const result = await this.execute(sqlAndParams.sql, sqlAndParams.params);
|
||||||
|
|
||||||
return result.insertId;
|
return result.insertId;
|
||||||
}
|
}
|
||||||
|
@ -793,11 +846,15 @@ export class SQLiteDB {
|
||||||
* @param fields A comma separated list of fields to return.
|
* @param fields A comma separated list of fields to return.
|
||||||
* @return Promise resolved when done.
|
* @return Promise resolved when done.
|
||||||
*/
|
*/
|
||||||
async insertRecordsFrom(table: string, source: string, conditions?: SQLiteDBRecordValues, fields: string = '*'):
|
async insertRecordsFrom(
|
||||||
Promise<void> {
|
table: string,
|
||||||
|
source: string,
|
||||||
|
conditions?: SQLiteDBRecordValues,
|
||||||
|
fields: string = '*',
|
||||||
|
): Promise<void> {
|
||||||
const selectAndParams = this.whereClause(conditions);
|
const selectAndParams = this.whereClause(conditions);
|
||||||
const select = selectAndParams[0] ? 'WHERE ' + selectAndParams[0] : '';
|
const select = selectAndParams.sql ? 'WHERE ' + selectAndParams.sql : '';
|
||||||
const params = selectAndParams[1];
|
const params = selectAndParams.params;
|
||||||
|
|
||||||
await this.execute(`INSERT INTO ${table} SELECT ${fields} FROM ${source} ${select}`, params);
|
await this.execute(`INSERT INTO ${table} SELECT ${fields} FROM ${source} ${select}`, params);
|
||||||
}
|
}
|
||||||
|
@ -913,7 +970,7 @@ export class SQLiteDB {
|
||||||
async updateRecords(table: string, data: SQLiteDBRecordValues, conditions?: SQLiteDBRecordValues): Promise<number> {
|
async updateRecords(table: string, data: SQLiteDBRecordValues, conditions?: SQLiteDBRecordValues): Promise<number> {
|
||||||
const whereAndParams = this.whereClause(conditions);
|
const whereAndParams = this.whereClause(conditions);
|
||||||
|
|
||||||
return this.updateRecordsWhere(table, data, whereAndParams[0], whereAndParams[1]);
|
return this.updateRecordsWhere(table, data, whereAndParams.sql, whereAndParams.params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -925,8 +982,12 @@ export class SQLiteDB {
|
||||||
* @param whereParams Params for the where clause.
|
* @param whereParams Params for the where clause.
|
||||||
* @return Promise resolved with the number of affected rows.
|
* @return Promise resolved with the number of affected rows.
|
||||||
*/
|
*/
|
||||||
async updateRecordsWhere(table: string, data: SQLiteDBRecordValues, where?: string, whereParams?: SQLiteDBRecordValue[]):
|
async updateRecordsWhere(
|
||||||
Promise<number> {
|
table: string,
|
||||||
|
data: SQLiteDBRecordValues,
|
||||||
|
where?: string,
|
||||||
|
whereParams?: SQLiteDBRecordValue[],
|
||||||
|
): Promise<number> {
|
||||||
this.formatDataToInsert(data);
|
this.formatDataToInsert(data);
|
||||||
if (!data || !Object.keys(data).length) {
|
if (!data || !Object.keys(data).length) {
|
||||||
// No fields to update, consider it's done.
|
// No fields to update, consider it's done.
|
||||||
|
|
Loading…
Reference in New Issue