package com.engine.organization.util.db;


import com.engine.organization.exception.OrganizationRunTimeException;
import weaver.conn.RecordSet;

public enum DBType implements DBOperateAdapter {
    /**
     * 根据数据库类型，匹配不通的函数
     */
    MYSQL("mysql") {
        @Override
        public String like(String some) {
            return " like '%" + some + "%' ";
        }

        @Override
        public String concat(String some) {
            return " concat(','," + some + ",',') ";
        }

        @Override
        public String concat(String concatStr, String fieldName) {
            return " concat('" + concatStr + "'," + fieldName + ") ";
        }

        @Override
        public String currentDate() {
            return "now()";
        }

        @Override
        public String ifNull(String some, String defaultValue) {
            return " ifnull(" + some + ",'" + defaultValue + "')";
        }
    },
    SQLSERVER("sqlserver") {
        @Override
        public String like(String some) {
            return " like '%" + some + "%' ";
        }

        @Override
        public String concat(String some) {
            return " ',cast('+" + some + "as varchar)+',' ";
        }

        @Override
        public String concat(String concatStr, String fieldName) {
            return " '" + concatStr + "' + cast(" + fieldName + " as varchar) ";
        }

        @Override
        public String currentDate() {
            return "GETDATE()";
        }

        @Override
        public String ifNull(String some, String defaultValue) {
            return " isnull(" + some + ",'" + defaultValue + "')";
        }
    },
    ORACLE("oracle") {
        @Override
        public String like(String some) {
            return " like '%" + some + "%' ";
        }

        @Override
        public String concat(String some) {
            return " ',' ||" + some + "|| ',' ";
        }

        @Override
        public String concat(String concatStr, String fieldName) {
            return " '" + concatStr + "' ||" + fieldName + " ";
        }

        @Override
        public String currentDate() {
            return "SYSDATE";
        }

        @Override
        public String ifNull(String some, String defaultValue) {
            return " NVL(" + some + ",'" + defaultValue + "')";
        }
    },
    POSTGRESQL("postgresql") {
        @Override
        public String like(String some) {
            return " like '%" + some + "%' ";
        }

        @Override
        public String concat(String some) {
            return " ',' ||" + some + "|| ',' ";
        }

        @Override
        public String concat(String concatStr, String fieldName) {
            return " '" + concatStr + "' ||" + fieldName + " ";
        }

        @Override
        public String currentDate() {
            return "now()";
        }

        @Override
        public String ifNull(String some, String defaultValue) {
            return " COALESCE(" + some + ",'" + defaultValue + "')";
        }
    };

    private String dbtype;

    DBType(String dbtype) {
        this.dbtype = dbtype;
    }

    public static DBType get(String dbtype) {
        for (DBType t : DBType.values()) {
            if (t.dbtype.equalsIgnoreCase(dbtype)) {
                return t;
            }
        }
        throw new OrganizationRunTimeException("不支持的数据库类型");
    }

    public static boolean isOracle() {
        return DBType.get(new RecordSet().getDBType()).equals(DBType.ORACLE);
    }


}
