/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.shardingproxy.backend.jdbc.datasource;

import io.shardingsphere.core.metadata.datasource.dialect.MySQLDataSourceMetaData;
import io.shardingsphere.core.metadata.datasource.dialect.PostgreSQLDataSourceMetaData;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class JDBCClassDetermine {
    
    /**
     * Get driver class name.
     *
     * @param url url
     * @return driver class name
     */
    public String getDriverClassName(final String url) {
        // TODO getXADataSourceClassName
        String result = "";
        Pattern pattern = Pattern.compile("jdbc:(mysql|postgresql):.*", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            String databaseType = matcher.group(1).toLowerCase();
            switch (databaseType) {
                case "mysql":
                    new MySQLDataSourceMetaData(url);
                    if (result.isEmpty()) {
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                            result = "com.mysql.jdbc.Driver";
                        } catch (ClassNotFoundException e) {
                            try {
                                Class.forName("com.mysql.cj.jdbc.Driver");
                                result = "com.mysql.cj.jdbc.Driver";
                            } catch (ClassNotFoundException ex) {
                                throw new UnsupportedOperationException(String.format("Cannot support url `%s`,no vailed mysql driver found", url));
                            }
                        }
                    }
                    return result;
                case "postgresql":
                    new PostgreSQLDataSourceMetaData(url);
                    return "org.postgresql.Driver";
                default:
                    throw new UnsupportedOperationException(String.format("Cannot support url `%s`", url));
            }
        }
        throw new UnsupportedOperationException(String.format("Cannot support url `%s`", url));
    }
}

@@ -3,65 +3,258 @@
 %0A * 
-Copyright 2016-2018 shardingsphere.io.%0A * %3Cp%3E%0A * Licensed
+Licensed to the Apache Software Foundation (ASF) under one or more%0A *  contributor license agreements.  See the NOTICE file distributed with%0A *  this work for additional information regarding copyright ownership.%0A *  The ASF licenses this file to You
  und
@@ -287,16 +287,20 @@
 sion 2.0
+%0A * 
  (the %22L
@@ -308,19 +308,16 @@
 cense%22);
-%0A *
  you may
@@ -360,16 +360,20 @@
 nce with
+%0A * 
  the Lic
@@ -377,19 +377,17 @@
 License.
-%0A *
+ 
  You may
@@ -421,19 +421,22 @@
 se at%0A *
+  
 %0A *
+ 
      htt
@@ -477,19 +477,22 @@
 E-2.0%0A *
+  
 %0A *
+ 
  Unless 
@@ -554,16 +554,17 @@
 tware%0A *
+ 
  distrib
@@ -624,16 +624,17 @@
 ASIS,%0A *
+ 
  WITHOUT
@@ -701,16 +701,17 @@
 lied.%0A *
+ 
  See the
@@ -773,16 +773,17 @@
 s and%0A *
+ 
  limitat
@@ -812,13 +812,8 @@
 .%0A *
- %3C/p%3E
 %0A */
@@ -1111,16 +1111,17 @@
 ructor;%0A
+%0A
 import j
@@ -2090,32 +2090,38 @@
        %7D catch (
+final 
 ClassNotFoundExc
@@ -2347,16 +2347,22 @@
  catch (
+final 
 ClassNot
@@ -2502,18 +2502,17 @@
 o va
-iled mysql
+lid MySQL
  dri
@@ -2520,16 +2520,17 @@
 er found
+.
 %22, url))

fix typo for JDBCClassDetermine