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

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

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

@BoxDbBundled
public class PostgresFactory extends DockerDatabaseFactory
{
    public PostgresFactory()
    {
        super("postgres");
    }

    @Override
    protected void initializeDefaults(BoxConfiguration boxConfiguration)
    {
        if (boxConfiguration.getAdminUser() == null)
            boxConfiguration.setAdminUser("postgres");
        if (boxConfiguration.getAdminPassword() == null)
            boxConfiguration.setAdminPassword("postgresbox");
        if (boxConfiguration.getDatabaseName() == null)
            boxConfiguration.setDatabaseName("app");
        if (boxConfiguration.getDatabaseVersion() == null)
            boxConfiguration.setDatabaseVersion("9.6");
        if (boxConfiguration.getDatabasePort() <= 0)
            boxConfiguration.setDatabasePort(5433);
        if (boxConfiguration.getDatabaseUser() == null)
            boxConfiguration.setDatabaseUser(boxConfiguration.getDatabaseName());
        if (boxConfiguration.getDatabasePassword() == null)
            boxConfiguration.setDatabasePassword(boxConfiguration.getDatabaseUser());

        EncodingTranslator encodingTranslator = new EncodingTranslator("UTF8", this::mapCommonEncoding);
        encodingTranslator.processEncoding(boxConfiguration);
        CollationTranslator collationTranslator = new CollationTranslator("C", col -> mapCommonCollation(col, boxConfiguration.getDatabaseEncoding()));
        collationTranslator.processCollation(boxConfiguration);

        super.initializeDefaults(boxConfiguration);
    }
    
    private String mapCommonEncoding(CommonEncoding encoding)
    {
        switch (encoding)
        {
            case UNICODE:
                return "UTF8";
            case ASCII:
                return "LATIN1";
            default:
                throw new Error("Unknown common encoding: " + encoding);
        }
    }
    
    private String mapCommonCollation(CommonCollation collation, String normalizedEncoding)
    {
        switch (collation)
        {
            case BINARY:
                return "C";
            case CASE_INSENSITIVE:
                return "en_US." + normalizedEncoding;
            default:
                throw new Error("Unknown common collation: " + collation);
        }
    }

    @Override
    protected PostgresDatabase createDockerDatabase(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration, BoxContext context)
    throws BoxDatabaseException
    {
        return new PostgresDatabase(boxConfiguration, projectConfiguration, context);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy