package com.celum.dbtool.configuration;
import com.celum.dbtool.Db;
import com.celum.dbtool.installer.DbEventListener;
import com.celum.dbtool.resource.DbStepResource;
import com.celum.dbtool.resource.PackageResource;
import com.celum.nyx.DataSourceWrapper;
import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* The configuration information from which the {@link com.celum.dbtool.Db engine} can be build.
* You might use some predefined configurations available as static methods.
*/
public abstract class DbConfiguration
{
/** default name of the version table */
public final static String PATCH_TABLE = "PATCH";
/** database source */
protected DataSource dataSource;
/** resource from where scripts comes */
protected DbStepResource stepsSource;
/** variables which will be replaced as placeholders in SQLs */
protected Map<String, Object> variables;
/** listener reacting on events like start executing SQL etc. */
protected DbEventListener eventListener;
/** select that returns version for DB */
protected String versionSql;
/** Sql step which updates the version in DB */
protected String versionUpdateSqlScript;
/**
* Returns the default configuration where version table is called 'VERSION'
*/
public static DbConfiguration createDefaultConfiguration()
{
return new DefaultConfiguration(PATCH_TABLE);
}
/**
* Returns the default configuration where you determine the version table
*/
public static DbConfiguration createDefaultConfigurationForTable(String versionTable)
{
return new DefaultConfiguration(versionTable);
}
/**
* create the configuration for {@link Db engine} with self-init capability
* of version table called 'PATCH'.
*/
public static SelfInitConfiguration createSelfInitConfiguration()
{
return createSelfInitConfigurationForTable(PATCH_TABLE);
}
/**
* create the configuration for {@link Db engine} with self-init capability
* for given version table
*/
public static SelfInitConfiguration createSelfInitConfigurationForTable(String versionTable)
{
return new SelfInitConfiguration(versionTable);
}
/**
* Constructor
*/
protected DbConfiguration()
{
this.variables = new HashMap<String, Object>();
}
/**
* Method build a DB instance with current configuration
*/
public abstract Db build();
public DbConfiguration setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
return this;
}
public DbConfiguration setConnection(Connection connection)
{
this.dataSource = new DataSourceWrapper(connection);
return this;
}
public DbConfiguration setStepsSource(DbStepResource stepsSource)
{
this.stepsSource = stepsSource;
return this;
}
public DbConfiguration setStepsSourceForPackage(String packageName)
{
this.stepsSource = new PackageResource(packageName, this.getClass().getClassLoader());
return this;
}
public DbConfiguration setStepsSourceForPackage(Package pckg)
{
this.stepsSource = new PackageResource(pckg);
return this;
}
public DbConfiguration setStepsSourceForDirectory(File directory)
{
return this;
}
public DbConfiguration setVariables(Map<?,?> variables)
{
if (variables != null) {
for (Map.Entry<?,?> entry : variables.entrySet()) {
setVariable(entry.getKey().toString(), entry.getValue());
}
}
return this;
}
public DbConfiguration setVariable(String name, Object value)
{
this.variables.put(name, value);
return this;
}
public DbConfiguration setVariablesFromProperties(Properties properties)
{
for (Map.Entry<Object,Object> e : properties.entrySet()) {
setVariable(e.getKey().toString(), e.getValue().toString());
}
return this;
}
public DbConfiguration setEventListener(DbEventListener eventListener)
{
this.eventListener = eventListener;
return this;
}
public DbConfiguration setVersionSql(String versionSql)
{
this.versionSql = versionSql;
return this;
}
public DbConfiguration setVersionUpdateSqlScript(String versionUpdateSqlScript)
{
this.versionUpdateSqlScript = versionUpdateSqlScript;
return this;
}
public DataSource getDataSource()
{
return dataSource;
}
public DbStepResource getStepsSource()
{
return stepsSource;
}
public Map<String, Object> getVariables()
{
return variables;
}
public DbEventListener getEventListener()
{
return eventListener;
}
public String getVersionSql()
{
return versionSql;
}
public String getVersionUpdateSqlScript()
{
return versionUpdateSqlScript;
}
}