/*****************************************************************************
* Copyright 2012 celum Slovakia s r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************/
package com.celum.dbtool.installer;
import com.celum.dbtool.DbException;
import com.celum.dbtool.resource.DbStepResource;
import com.celum.dbtool.step.DbStep;
import com.celum.dbtool.step.StepType;
import com.celum.dbtool.step.Version;
import com.celum.dbtool.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Logger;
/**
* Doing installation of the DB from scripts/steps that comes from resource.
* Installer do loading of all steps. Each step is checked and executed
* depends on his type.
*
* The most important method is 'install' that take all DB steps from
* {@link com.celum.dbtool.resource.DbStepResource} and executes them.
*
* @author Zdenko Vrabel (zdenko.vrabel@celum.com)
*/
public class DbInstaller
{
/** logger */
private static Logger LOG = Logger.getLogger(DbInstaller.class.getName());
/** event listener handles DbInstaller's events */
private DbEventListener eventListener;
/** database connection */
private final Connection dbConnection;
/** this SQL is executed after each step and updates the DB version */
private String versionUpdateSqlScript;
/** holds all registered executors related to they're types */
private final Executor[] executors;
/** holds variables */
private final Map<String, Object> variables;
/**
* Constructor
*/
public DbInstaller(Connection dbConnection, Map<String, Object> variables)
{
this.eventListener = new DummyEventListener();
this.dbConnection = dbConnection;
this.variables = variables;
this.executors = new Executor[] {
new SqlExecutor(dbConnection, variables),
new GroovyExecutor(dbConnection, variables),
new JavaExecutor(dbConnection, variables)
};
}
/**
* Easy constructor when you need use it fast with default
* behaviour without Velocity support
*/
public DbInstaller(Connection con)
{
this(con, new HashMap<String, Object>());
}
/**
* set the event listener, when your try to set 'null' listener
* then the original one stay as it is.
*/
public void setEventListener(DbEventListener eventListener)
{
if (eventListener != null) {
this.eventListener = eventListener;
}
}
/**
* set the version UPDATE SQL which is executed after each
* step where $version is replaced by step version.
*
* example of sql:
* <code>
* TRUNCATE TABLE VERSIONTABLE;
* INSERT INTO VERSIONTABLE(VERSIONCOL) VALUES ($version);
* </code>
*
* @param updateSql version update sql step. If its null or empty, the version update is skipped.
*/
public void setVersionUpdateSql(String updateSql)
{
this.versionUpdateSqlScript = updateSql;
}
/**
* Executes all SQL scripts from given resource
*/
public void install(DbStepResource resource)
{
List<DbStep> steps = resource.getSteps();
Collections.sort(steps);
for (DbStep step : steps) {
executeStep(step);
}
}
/**
* method is invoked for each step that comes from resrouce and
* decide which execution method will be used (SQL or GROOVY or another one)
*/
private void executeStep(DbStep step)
{
try {
step = eventListener.onProcessScript(step);
if (step != null) {
executeViaExecutor(step);
updateDbVersion(step);
}
} catch (Exception e) {
eventListener.onError(step, e);
throw new DbException("error in step " + step.getName() + ". See the problem below in caused exception. ", e);
}
}
/**
* execute step via concrete executor
*/
private void executeViaExecutor(DbStep step)
{
StepType type = step.getType();
for (Executor executor : executors) {
if (executor.getType() == type) {
executor.execute(step);
return;
}
}
throw new DbException("Unsupported type:" + type.toString() + ". No Executor for this type.");
}
/**
* if versionUpdateSql is defined, it's executed after each step
* when $version is replaced by real step's version.
*/
private void updateDbVersion(DbStep step)
{
if (versionUpdateSqlScript == null || versionUpdateSqlScript.isEmpty()) {
return;
}
Version v = step.getVersion();
Map<String, Object> args = new HashMap<String, Object>();
args.put("version", v);
args.put("name", step.getName());
args.putAll(variables);
Sql.asString(versionUpdateSqlScript)
.interceptingWith(new VelocityInterceptor(args), new LogInterceptor())
.execute(dbConnection);
}
}