/**
 *
 * Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
 *
 * 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.
 */
package com.speedment.runtime.connector.mysql.internal;

import com.speedment.common.injector.annotation.Inject;
import com.speedment.runtime.config.Column;
import com.speedment.runtime.config.Dbms;
import com.speedment.runtime.core.db.*;


import java.util.Collections;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.speedment.runtime.core.internal.db.AbstractDatabaseNamingConvention;
import com.speedment.runtime.core.internal.db.AbstractDbmsType;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toSet;

/**
 *
 * @author Per Minborg
 * @author Emil Forslund
 */
public final class MySqlDbmsType extends AbstractDbmsType {

    private final MySqlNamingConvention namingConvention;
    private final MySqlConnectionUrlGenerator connectionUrlGenerator;

    @Inject private MySqlDbmsMetadataHandler metadataHandler;
    @Inject private MySqlDbmsOperationHandler operationHandler;
    @Inject private MySqlSpeedmentPredicateView fieldPredicateView;

    private MySqlDbmsType() {
        namingConvention = new MySqlNamingConvention();
        connectionUrlGenerator = new MySqlConnectionUrlGenerator();
    }

    @Override
    public String getName() {
        return "MySQL";
    }

    @Override
    public String getDriverManagerName() {
        return "MySQL-AB JDBC Driver";
    }

    @Override
    public int getDefaultPort() {
        return 3306;
    }

    @Override
    public String getDbmsNameMeaning() {
        return "The name of the MySQL Database.";
    }

    @Override
    public boolean hasSchemaNames() {
        return false;
    }

    @Override
    public String getDriverName() {
        return "com.mysql.jdbc.Driver";
    }

    @Override
    public boolean isSupported() {
        // make sure we touch new new driver first.
        return isSupported("com.mysql.cj.jdbc.Driver") || isSupported(getDriverName());
    }

    @Override
    public DatabaseNamingConvention getDatabaseNamingConvention() {
        return namingConvention;
    }

    @Override
    public DbmsMetadataHandler getMetadataHandler() {
        return metadataHandler;
    }

    @Override
    public DbmsOperationHandler getOperationHandler() {
        return operationHandler;
    }

    @Override
    public ConnectionUrlGenerator getConnectionUrlGenerator() {
        return connectionUrlGenerator;
    }

    @Override
    public FieldPredicateView getFieldPredicateView() {
        return fieldPredicateView;
    }

    @Override
    public String getInitialQuery() {
        return "select version() as `MySQL version`";
    }

    @Override
    public DbmsColumnHandler getColumnHandler() {
        return new DbmsColumnHandler() {
            @Override
            public Predicate<Column> excludedInInsertStatement() {
                return c -> false; // For MySQL, even autoincrement fields are added to insert statements 
            }

            @Override
            public Predicate<Column> excludedInUpdateStatement() {
                return c -> false;
            }
            
        };
    }

    private final static class MySqlNamingConvention extends AbstractDatabaseNamingConvention {

        private final static String ENCLOSER = "`",
            QUOTE = "'";

        private final static Set<String> EXCLUDE_SET = Stream.of(
            "information_schema"
        ).collect(collectingAndThen(toSet(), Collections::unmodifiableSet));

        @Override
        public Set<String> getSchemaExcludeSet() {
            return EXCLUDE_SET;
        }

        @Override
        protected String getFieldQuoteStart() {
            return QUOTE;
        }

        @Override
        protected String getFieldQuoteEnd() {
            return QUOTE;
        }

        @Override
        protected String getFieldEncloserStart() {
            return ENCLOSER;
        }

        @Override
        protected String getFieldEncloserEnd() {
            return ENCLOSER;
        }
    }

    private final static class MySqlConnectionUrlGenerator implements ConnectionUrlGenerator {

        @Override
        public String from(Dbms dbms) {
            final StringBuilder result = new StringBuilder()
                .append("jdbc:mysql://")
                .append(dbms.getIpAddress().orElse(""));

            dbms.getPort().ifPresent(p -> result.append(":").append(p));

            result/*.append("/").append(dbms.getName())*/ // MySQL treats this as default schema name
                .append("?useUnicode=true&characterEncoding=UTF-8")
                .append("&useServerPrepStmts=true&useSSL=false")
                .append("&zeroDateTimeBehavior=convertToNull")
                .append("&nullNamePatternMatchesAll=true") // Fix #190
                .append("&useLegacyDatetimeCode=true");    // Fix #190

            return result.toString();
        }
    }
}

@@ -865,16 +865,40 @@
 db.*;%0A%0A%0A
+import java.sql.Driver;%0A
 import j
@@ -1011,16 +1011,80 @@
 tream;%0A%0A
+import com.speedment.runtime.core.exception.SpeedmentException;%0A
 import c
@@ -5349,21 +5349,8 @@
 true
-&useSSL=false
 %22)%0A 
@@ -5395,16 +5395,87 @@
 ehavior=
+%22)%0A                .append(driverVersion() %3E= 8 ? %22CONVERT_TO_NULL%22 : %22
 convertT
@@ -5641,31 +5641,720 @@
     
-return result.toString(
+if (driverVersion() %3C= 5) %7B%0A                result.append(%22&useSSL=false%22);%0A            %7D else %7B%0A                result.append(%22&serverTimezone=UTC%22);%0A            %7D%0A%0A            return result.toString();%0A        %7D%0A    %7D%0A%0A    private static int driverVersion() %7B%0A        try %7B%0A            final Class%3C?%3E mySqlDriver = Class.forName(%22com.mysql.cj.jdbc.Driver%22);%0A            final Driver driver = (Driver) mySqlDriver.newInstance();%0A            return driver.getMajorVersion();%0A        %7D catch (final InstantiationException%0A                     %7C IllegalAccessException%0A                     %7C ClassNotFoundException ex) %7B%0A            throw new SpeedmentException(%22Error using reflection to read driver version.%22, ex
 );%0A 

Fix #621, #629 Speedment now checks which MySQL connector version is used