package io.github.xxyy.common.sql;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.apache.commons.lang.Validate;

/**
 * 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
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class 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(@NonNull SqlConnectable connectable){
        String sqlHost = connectable.getSqlHost();

        Validate.notNull(sqlHost);

        return sqlHost.contains(connectable.getSqlDb()) ? sqlHost : ((sqlHost.endsWith("/")) ? sqlHost + connectable.
                getSqlDb() : sqlHost + "/" + connectable.getSqlDb());
    }

    /**
     * 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.
     */
    @NonNull
    public static SqlConnectable fromCredentials(@NonNull final String host, @NonNull final String database,
                                                 @NonNull final String user, @NonNull 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;
            }
        };
    }
}

@@ -1095,24 +1095,21 @@
 urn 
-sql
+get
 Host
-.contains
+String
 (con
@@ -1127,27 +1127,25 @@
 tSqlDb()
-) ?
+,
  sqlHost
  : ((sql
@@ -1140,128 +1140,769 @@
 Host
- : ((sqlHost.endsWith(%22/%22)) ? sqlHost + connectable.%0A                getSqlDb() : sqlHost + %22/%22 + connectable.getSqlDb()
+);%0A    %7D%0A%0A    /**%0A     * Returns the host string for the given paramaeters. The host string consists of%0A     * the database URL, as returned by %7B@link SqlConnectable#getSqlHost()%7D, of a slash (/)%0A     * and the database name, as returned by %7B@link SqlConnectable#getSqlDb()%7D.%0A     * If the database name is already appended to the host, it will not be appended another time.%0A     * @param database Database to use%0A     * @param sqlHost Fully-qualified JDBC connection string of the host to use%0A     * @return Host string, as accepted by database drivers.%0A     */%0A    public static String getHostString(String database, String sqlHost) %7B%0A        return sqlHost.contains(database) ? sqlHost : (sqlHost.endsWith(%22/%22) ? sqlHost + database : sqlHost + %22/%22 + database
 );%0A 
@@ -3015,16 +3015,16 @@
       %7D%0A
-
         
@@ -3031,11 +3031,959 @@
 %7D;%0A    %7D
+%0A%0A    /**%0A     * Returns the host string for the given paramaeters. The host string consists of%0A     * the database URL, as returned by %7B@link SqlConnectable#getSqlHost()%7D, of a slash (/)%0A     * and the database name, as returned by %7B@link SqlConnectable#getSqlDb()%7D.%0A     * If the database name is already appended to the host, it will not be appended another time.%0A     * @param database Database to use%0A     * @param hostname Fully-qualified JDBC connection string of the host to use or just a FQDN.%0A     * @param port port number to use.%0A     * @return Host string, as accepted by database drivers.%0A     */%0A    public static String getHostString(String hostname, int port, String database) %7B%0A        String sqlHost;%0A%0A        if(!hostname.startsWith(%22jdbc:%22)) %7B%0A            sqlHost = %22jdbc:mysql://%22 + hostname + %22:%22 + port + %22/%22;%0A        %7D else %7B%0A            sqlHost = hostname;%0A        %7D%0A%0A        return getHostString(sqlHost, database);%0A    %7D
 %0A%7D%0A

Add more utility methods for host strings to SqlConnectables