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

/**
 * `sql`工具类
 */
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语句，固定排除字段`syncTime`
     */
    static generateReplaceSql(tableName: string, entity: object, excludeFields?: string[]) {
        let keys = Reflect.ownKeys(entity).filter(s => { return entity[s] !== undefined && entity[s] !== null && s !== 'syncTime' && !excludeFields?.includes(s.toString()); });

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

/**
  * 生成`insert into` sql语句
  */
export function generateInsertSql(tableName: string, entity: object) {
    return SqlUtils.generateInsertSql(tableName, entity);
}

/**
 * 生成`replace into` sql语句，固定排除字段`syncTime`
 */
export function generateReplaceSql(tableName: string, entity: object, excludeFields?: string[]) {
    return SqlUtils.generateReplaceSql(tableName, entity, excludeFields);
}

export { SqlUtils }