All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.net.causal.maven.plugins.boxdb.db.SqlServerLinuxFactory Maven / Gradle / Ivy

There is a newer version: 3.3
Show newest version
package au.net.causal.maven.plugins.boxdb.db;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@BoxDbBundled
public class SqlServerLinuxFactory extends DockerDatabaseFactory
{
    static final String SQLSERVER_DOCKER_REPOSITORY = "mssql/server";

    public SqlServerLinuxFactory()
    {
        super("sqlserver-linux");
    }

    @Override
    protected void initializeDefaults(BoxConfiguration boxConfiguration)
    {
        if (boxConfiguration.getAdminUser() == null)
            boxConfiguration.setAdminUser("sa");
        if (boxConfiguration.getAdminPassword() == null)
            boxConfiguration.setAdminPassword("SqlServerBox1");
        if (boxConfiguration.getDatabaseName() == null)
            boxConfiguration.setDatabaseName("app");
        if (boxConfiguration.getDatabaseVersion() == null)
            boxConfiguration.setDatabaseVersion("2017-cu16");
        if (boxConfiguration.getDatabasePort() <= 0)
            boxConfiguration.setDatabasePort(1433);
        if (boxConfiguration.getDatabaseUser() == null)
            boxConfiguration.setDatabaseUser(boxConfiguration.getDatabaseName());
        if (boxConfiguration.getDatabasePassword() == null)
            boxConfiguration.setDatabasePassword(boxConfiguration.getDatabaseUser());

        if (!boxConfiguration.getConfiguration().containsKey(SqlServerJdbcDriverType.CONFIGURATION_PROPERTY))
            boxConfiguration.getConfiguration().put(SqlServerJdbcDriverType.CONFIGURATION_PROPERTY, SqlServerJdbcDriverType.MICROSOFT.name().toLowerCase(Locale.ENGLISH));

        CollationTranslator collationTranslator = new CollationTranslator("Latin1_General_BIN", this::mapCommonCollation);
        collationTranslator.processCollation(boxConfiguration);

        super.initializeDefaults(boxConfiguration);
    }

    private String mapCommonCollation(CommonCollation collation)
    {
        switch (collation)
        {
            case BINARY:
                return "Latin1_General_BIN";
            case CASE_INSENSITIVE:
                return "Latin1_General_CI_AI";
            default:
                throw new Error("Unknown common collation: " + collation);
        }
    }

    @Override
    protected SqlServerLinuxDatabase createDockerDatabase(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration, BoxContext context)
    throws BoxDatabaseException
    {
        try
        {
            return new SqlServerLinuxDatabase(boxConfiguration, projectConfiguration, context, dockerRegistry());
        }
        catch (IOException e)
        {
            throw new BoxDatabaseException("I/O error creating database: " + e, e);
        }
    }

    @Override
    protected DockerRegistry dockerRegistry()
    {
        return new DockerRegistry(URI.create("https://mcr.microsoft.com/v2/"));
    }

    @Override
    public List availableVersions(ProjectConfiguration projectConfiguration, BoxContext context)
    throws BoxDatabaseException
    {
        try
        {
            //The '-ubuntu' suffix is assumed in SqlServerLinuxDatabase#dockerImageName() so when listing versions trim it off as well
            //(and only include those versions that have this suffix)
            return dockerRegistry().readTags(SQLSERVER_DOCKER_REPOSITORY).stream()
                                   .filter(tag -> tag.endsWith("-ubuntu"))
                                   .map(tag -> tag.substring(0, tag.length() - "-ubuntu".length()))
                                   .collect(Collectors.toList());
        }
        catch (IOException e)
        {
            throw new BoxDatabaseException("Error reading tags: " + e.getMessage(), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy