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

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

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

import au.net.causal.maven.plugins.boxdb.ScriptReaderRunner;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;

public class MariaDbDatabase extends BaseMySqlDatabase
{
    public MariaDbDatabase(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration,
                           BoxContext context, DockerRegistry dockerRegistry,
                           String dockerRepositoryName)
    {
        super(boxConfiguration, projectConfiguration, context, dockerRegistry, dockerRepositoryName);
    }

    @Override
    public JdbcConnectionInfo jdbcConnectionInfo(DatabaseTarget target) throws BoxDatabaseException
    {
        String databaseName;
        if (target == DatabaseTarget.ADMIN)
            databaseName = "mysql";
        else
            databaseName = getBoxConfiguration().getDatabaseName();

        String uri =  "jdbc:mariadb://" +
                        getContext().getDockerHostAddress() +
                        ":" + getBoxConfiguration().getDatabasePort() +
                        "/" + databaseName;

        return new JdbcConnectionInfo(uri,
                        target.user(getBoxConfiguration()),
                        target.password(getBoxConfiguration()),
                        getContext().getDockerHostAddress(),
                        getBoxConfiguration().getDatabasePort());
    }

    @Override
    public JdbcDriverInfo jdbcDriverInfo()
    throws BoxDatabaseException
    {
        //Drivers are backward/forward compatible so just use latest
        return new JdbcDriverInfo(new RunnerDependency("org.mariadb.jdbc", "mariadb-java-client", "2.4.4"), "org.mariadb.jdbc.Driver");
    }

    @Override
    public void configureNewDatabase()
    throws IOException, SQLException, BoxDatabaseException
    {
        URL initScript = MariaDbDatabase.class.getResource("mariadb-create-database.sql");

        ScriptReaderRunner scriptRunner = getContext().createScriptReaderRunner(this, getBoxConfiguration(), getProjectConfiguration());
        ScriptReaderExecution execution = new ScriptReaderExecution();
        execution.setFiltering(true);
        execution.setScripts(Arrays.asList(initScript));

        try
        {
            scriptRunner.execute(execution, DatabaseTarget.ADMIN, getProjectConfiguration().getScriptTimeout());
        }
        catch (MojoExecutionException e)
        {
            throw new BoxDatabaseException(e);
        }
    }

    protected DataSourceBuilder dataSourceBuilder(DatabaseTarget target)
    throws BoxDatabaseException
    {
        JdbcConnectionInfo jdbcInfo = jdbcConnectionInfo(target);
        return new DataSourceBuilder(getContext())
                    .dataSourceClassName("org.mariadb.jdbc.MariaDbDataSource")
                    .dependencies(jdbcDriverInfo().getDependencies())
                    .configureDataSource("setUrl", String.class, jdbcInfo.getUri())
                    .configureDataSource("setUser", String.class, jdbcInfo.getUser())
                    .configureDataSource("setPassword", String.class, jdbcInfo.getPassword());
    }

    @Override
    public Connection createJdbcConnection(DatabaseTarget targetDatabase) 
    throws SQLException, BoxDatabaseException, IOException
    {
        return dataSourceBuilder(targetDatabase).create().getConnection();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy