/**
 * sql帮助模块
 * @date 2022-03
 * @author 何华东
 */

/**
  * 生成insert into sql语句
  * 
  * 注：排除字段值为undefined、null的字段
  * @param type 
  * @param entity 
  * @returns 
  */
export function generateInsertSql(tableName: string, entity: object) {
    let keys = Reflect.ownKeys(entity).filter(s => { return entity[s] !== undefined && entity[s] !== null; });

    return `INSERT INTO ${tableName}(${keys.join(',')}) VALUES (${keys.map(s => {
        return typeof entity[s] === 'number' ? entity[s] : `'${entity[s]}'`;
    }).join(',')})`;
}

/**
 * 生成replace into sql语句
 * 
 * 注：排除字段值为undefined、null的字段
 * @param type 
 * @param entity 
 * @returns 
 */
export function generateReplaceSql(tableName: string, entity: object) {
    let keys = Reflect.ownKeys(entity).filter(s => { return entity[s] !== undefined && entity[s] !== null; });

    return `REPLACE INTO ${tableName}(${keys.join(',')}) VALUES (${keys.map(s => {
        return typeof entity[s] === 'number' ? entity[s] : `'${entity[s]}'`;
    }).join(',')})`;
}