/*
 * Copyright (c) 2013 - 2014 xxyy (Philipp Nowak; devnull@nowak-at.net). All rights reserved.
 *
 * Any usage, including, but not limited to, compiling, running, redistributing, printing,
 *  copying and reverse-engineering is strictly prohibited without explicit written permission
 *  from the original author and may result in legal steps being taken.
 */

package io.github.xxyy.common.sql;

import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;

/**
 * Statuic utilities for the {@link io.github.xxyy.common.sql.SqlConnectable} class.
 *
 * @author <a href="http://xxyy.github.io/">xxyy</a>
 * @since 14.5.14
 */
public final class SqlConnectables {

    private SqlConnectables() {
    }

    /**
     * Returns the host string for the given Connectable. The host string consists of
     * the database URL, as returned by {@link SqlConnectable#getSqlHost()}, of a slash (/)
     * and the database name, as returned by {@link SqlConnectable#getSqlDb()}.
     * If the database name is already appended to the host, it will not be appended another time.
     *
     * @param connectable Connectable to get information from
     * @return Host string, as accepted by database drivers.
     */
    public static String getHostString(@NotNull SqlConnectable connectable) {
        String sqlHost = connectable.getSqlHost();

        Validate.notNull(sqlHost);

        return getHostString(connectable.getSqlDb(), sqlHost);
    }

    /**
     * Returns the host string for the given parameters. The host string consists of
     * the database URL, as returned by {@link SqlConnectable#getSqlHost()}, of a slash (/)
     * and the database name, as returned by {@link SqlConnectable#getSqlDb()}.
     * If the database name is already appended to the host, it will not be appended another time.
     *
     * @param database Database to use
     * @param sqlHost  Fully-qualified JDBC connection string of the host to use
     * @return Host string, as accepted by database drivers.
     */
    public static String getHostString(String database, String sqlHost) {
        if (sqlHost == null) {
            throw new NullPointerException("sqlHost");
        }
        if (database == null) {
            throw new NullPointerException("database");
        }
        return sqlHost.contains(database) ? sqlHost : (sqlHost.endsWith("/") ? (sqlHost + database) : (sqlHost + "/" + database));
    }

    /**
     * Creates a simple SqlConnectable that simply returns the connection credentials provided.
     *
     * @param host     {@link SqlConnectable#getSqlHost()}
     * @param database {@link SqlConnectable#getSqlDb()}
     * @param user     {@link SqlConnectable#getSqlUser()}
     * @param password {@link SqlConnectable#getSqlPwd()}
     * @return A simple SqlConnectable matching provided arguments.
     */
    @NotNull
    public static SqlConnectable fromCredentials(@NotNull final String host, @NotNull final String database,
                                                 @NotNull final String user, @NotNull final String password) {
        return new SqlConnectable() {
            @Override
            public String getSqlDb() {
                return database;
            }

            @Override
            public String getSqlHost() {
                return host;
            }

            @Override
            public String getSqlPwd() {
                return password;
            }

            @Override
            public String getSqlUser() {
                return user;
            }
        };
    }

    /**
     * Returns the host string for the given paramaeters. The host string consists of
     * the database URL, as returned by {@link SqlConnectable#getSqlHost()}, of a slash (/)
     * and the database name, as returned by {@link SqlConnectable#getSqlDb()}.
     * If the database name is already appended to the host, it will not be appended another time.
     *
     * @param database Database to use
     * @param hostname Fully-qualified JDBC connection string of the host to use or just a FQDN.
     * @param port     port number to use.
     * @return Host string, as accepted by database drivers.
     */
    public static String getHostString(String hostname, int port, String database) {
        String sqlHost;

        if (!hostname.startsWith("jdbc:")) {
            sqlHost = "jdbc:mysql://" + hostname + ":" + port + "/";
        } else {
            sqlHost = hostname;
        }

        return getHostString(database, sqlHost);
    }
}

@@ -2105,85 +2105,227 @@
     
-if (sqlHost == null) %7B%0A            throw new NullPointerException(%22sqlHost%22);
+String rtrn = sqlHost.contains(database) ? sqlHost : (sqlHost.endsWith(%22/%22) ? (sqlHost + database) : (sqlHost + %22/%22 + database));%0A%0A        if (!rtrn.startsWith(%22jdbc:%22)) %7B%0A            if (!rtrn.startsWith(%22mysql://%22)) %7B
 %0A   
@@ -2329,26 +2329,24 @@
         
-%7D%0A
         
 if (data
@@ -2341,96 +2341,109 @@
     
-if (database == null) %7B%0A            throw new NullPointerException(%22database%22);%0A
+rtrn = %22jdbc:mysql://%22 + rtrn;%0A            %7D else %7B%0A                rtrn = %22jdbc:%22 + rtrn;%0A  
         
 %7D%0A  
@@ -2430,32 +2430,34 @@
 rtrn;%0A          
+  
 %7D%0A        return
@@ -2454,129 +2454,119 @@
     
-return sqlHost.contains(database) ? sqlHost : (sqlHost.endsWith(%22/%22) ? (sqlHost + database) : (sqlHost + %22/%22 + database))
+%7D%0A%0A        if (!rtrn.contains(%22?%22)) %7B%0A            rtrn += %22?autoReconnect=true%22;%0A        %7D%0A%0A        return rtrn
 ;%0A  
@@ -3769,17 +3769,16 @@
 en param
-a
 eters. T

Make SQL connection automagically reconnect