/**
 * sql工具模块
 * @date 2022-03
 * @author 何华东
 */

export default class SqlUtils {
    /**
      * 生成insert into sql语句
      */
    static 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语句
     */
    static 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(',')})`;
    }
}

export { SqlUtils }