package com.tofly.codegen.util;

/**
 * @author tuonbed
 * @date 2020/3/13  9:25
 * 数据库基本信息的创建工厂
 */
public class DataTypeSqlInfoFactory {

    /**
     * 根据传入的url地址，判断是哪个数据库，获取相关信息
     * @param url
     * @return
     */
    public static DataTypeSqlInfo getDataTypeSqlInfo(String url){
        String dataType=getDbType(url,null);
        switch (dataType){
            case "oracle":
                return new OracleDataTypeSqlInfo();
            case "mysql":
                return new MySQLDataTypeSqlInfo();
            default:
                return null;
        }
    }

    /**
     * 根据传入的url地址，返回对应的驱动信息
     * @param url
     * @return
     */
    public static String  getClassname(String url){
        String dataType=url.split(":")[1];
        switch (dataType){
            case "oracle":
                return "oracle.jdbc.OracleDriver";
            case "mysql":
                return "com.mysql.cj.jdbc.Driver";
            default:
                return null;
        }
    }


    public static String getDbType(String rawUrl, String driverClassName) {
        if (rawUrl == null) {
            return null;
        } else if (!rawUrl.startsWith("jdbc:derby:") && !rawUrl.startsWith("jdbc:log4jdbc:derby:")) {
            if (!rawUrl.startsWith("jdbc:mysql:") && !rawUrl.startsWith("jdbc:cobar:") && !rawUrl.startsWith("jdbc:log4jdbc:mysql:")) {
                if (rawUrl.startsWith("jdbc:mariadb:")) {
                    return "mariadb";
                } else if (!rawUrl.startsWith("jdbc:oracle:") && !rawUrl.startsWith("jdbc:log4jdbc:oracle:")) {
                    if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
                        return "AliOracle";
                    } else if (!rawUrl.startsWith("jdbc:microsoft:") && !rawUrl.startsWith("jdbc:log4jdbc:microsoft:")) {
                        if (!rawUrl.startsWith("jdbc:sqlserver:") && !rawUrl.startsWith("jdbc:log4jdbc:sqlserver:")) {
                            if (!rawUrl.startsWith("jdbc:sybase:Tds:") && !rawUrl.startsWith("jdbc:log4jdbc:sybase:")) {
                                if (!rawUrl.startsWith("jdbc:jtds:") && !rawUrl.startsWith("jdbc:log4jdbc:jtds:")) {
                                    if (!rawUrl.startsWith("jdbc:fake:") && !rawUrl.startsWith("jdbc:mock:")) {
                                        if (!rawUrl.startsWith("jdbc:postgresql:") && !rawUrl.startsWith("jdbc:log4jdbc:postgresql:")) {
                                            if (rawUrl.startsWith("jdbc:edb:")) {
                                                return "edb";
                                            } else if (!rawUrl.startsWith("jdbc:hsqldb:") && !rawUrl.startsWith("jdbc:log4jdbc:hsqldb:")) {
                                                if (rawUrl.startsWith("jdbc:odps:")) {
                                                    return "odps";
                                                } else if (rawUrl.startsWith("jdbc:db2:")) {
                                                    return "db2";
                                                } else if (rawUrl.startsWith("jdbc:sqlite:")) {
                                                    return "sqlite";
                                                } else if (rawUrl.startsWith("jdbc:ingres:")) {
                                                    return "ingres";
                                                } else if (!rawUrl.startsWith("jdbc:h2:") && !rawUrl.startsWith("jdbc:log4jdbc:h2:")) {
                                                    if (rawUrl.startsWith("jdbc:mckoi:")) {
                                                        return "mckoi";
                                                    } else if (rawUrl.startsWith("jdbc:cloudscape:")) {
                                                        return "cloudscape";
                                                    } else if (!rawUrl.startsWith("jdbc:informix-sqli:") && !rawUrl.startsWith("jdbc:log4jdbc:informix-sqli:")) {
                                                        if (rawUrl.startsWith("jdbc:timesten:")) {
                                                            return "timesten";
                                                        } else if (rawUrl.startsWith("jdbc:as400:")) {
                                                            return "as400";
                                                        } else if (rawUrl.startsWith("jdbc:sapdb:")) {
                                                            return "sapdb";
                                                        } else if (rawUrl.startsWith("jdbc:JSQLConnect:")) {
                                                            return "JSQLConnect";
                                                        } else if (rawUrl.startsWith("jdbc:JTurbo:")) {
                                                            return "JTurbo";
                                                        } else if (rawUrl.startsWith("jdbc:firebirdsql:")) {
                                                            return "firebirdsql";
                                                        } else if (rawUrl.startsWith("jdbc:interbase:")) {
                                                            return "interbase";
                                                        } else if (rawUrl.startsWith("jdbc:pointbase:")) {
                                                            return "pointbase";
                                                        } else if (rawUrl.startsWith("jdbc:edbc:")) {
                                                            return "edbc";
                                                        } else if (rawUrl.startsWith("jdbc:mimer:multi1:")) {
                                                            return "mimer";
                                                        } else if (rawUrl.startsWith("jdbc:dm:")) {
                                                            return "dm";
                                                        } else if (rawUrl.startsWith("jdbc:kingbase:")) {
                                                            return "kingbase";
                                                        } else if (rawUrl.startsWith("jdbc:gbase:")) {
                                                            return "gbase";
                                                        } else if (rawUrl.startsWith("jdbc:xugu:")) {
                                                            return "xugu";
                                                        } else if (rawUrl.startsWith("jdbc:log4jdbc:")) {
                                                            return "log4jdbc";
                                                        } else if (rawUrl.startsWith("jdbc:hive:")) {
                                                            return "hive";
                                                        } else if (rawUrl.startsWith("jdbc:hive2:")) {
                                                            return "hive";
                                                        } else if (rawUrl.startsWith("jdbc:phoenix:")) {
                                                            return "phoenix";
                                                        } else if (rawUrl.startsWith("jdbc:elastic:")) {
                                                            return "elastic_search";
                                                        } else if (rawUrl.startsWith("jdbc:clickhouse:")) {
                                                            return "clickhouse";
                                                        } else {
                                                            return rawUrl.startsWith("jdbc:presto:") ? "presto" : null;
                                                        }
                                                    } else {
                                                        return "informix";
                                                    }
                                                } else {
                                                    return "h2";
                                                }
                                            } else {
                                                return "hsql";
                                            }
                                        } else {
                                            return "postgresql";
                                        }
                                    } else {
                                        return "mock";
                                    }
                                } else {
                                    return "jtds";
                                }
                            } else {
                                return "sybase";
                            }
                        } else {
                            return "sqlserver";
                        }
                    } else {
                        return "sqlserver";
                    }
                } else {
                    return "oracle";
                }
            } else {
                return "mysql";
            }
        } else {
            return "derby";
        }
    }
}
