package com.liangjian.dataplatform.member.constant;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/10/23
 */
public enum DatabaseUrls {
    MySQL{
        @Override
        public String getUrl(String host, String dbName) {
            return String.format(urlBone,"mysql",host,dbName) + "?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        }
    },
    MsSql{
        @Override
        public String getUrl(String host, String dbName) {
            return String.format(urlBone,"mssql",host,dbName);
        }
    },
    Hive {
        @Override
        public String getUrl(String host, String dbName) {
            return String.format(urlBone,"hive2",host,dbName);
        }
    },
    ClickHouse {
        @Override
        public String getUrl(String host, String dbName) {
            return String.format(urlBone,"clickhouse",host,dbName);
        }
    }
    ;

    public static DatabaseUrls getByType(String type) {
        List<DatabaseUrls> urls = Arrays.asList(values());
        for (DatabaseUrls url : urls) {
            String lowerName = url.name().toLowerCase();
            if(type.toLowerCase().contains(lowerName)) {
                return url;
            }
        }
        throw new RuntimeException("Enum not found.");
    }
    public static String urlBone = "jdbc:%s://%s/%s";
    public abstract String getUrl(String host, String dbName);
}
