Package com.celum.dbtool

Source Code of com.celum.dbtool.Db

/*****************************************************************************
* 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;

import com.celum.dbtool.configuration.DbConfiguration;
import com.celum.dbtool.installer.DbInstaller;
import com.celum.dbtool.resource.*;
import com.celum.dbtool.step.Version;

/**
* This is the main facade for DB tool. You will use
* mostly this class.
*
* This class needs JDBC DataSource or Connection. If you
* support the Db with Connection, the whole connection management
* like closing, transactions are up to you.
*
* @author Zdenko Vrabel (zdenko.vrabel@celum.com)
*/
public class Db {

    /** holds configuration information */
    private final DbConfiguration configuration;

    /**
     * @param configuration
     */
    public Db(DbConfiguration configuration)
    {
        this.configuration = configuration;
    }


    /**
     * method executes the scripts without any 'version' updating
     * checking etc. It's usable for DB initialization or dropping.
     */
    public void run()
    {
        DbInstaller installer = new DbInstaller(configuration.getDataSource(), configuration.getVariables());
        installer.setEventListener(configuration.getEventListener());
        installer.install(configuration.getStepsSource());
    }


    /**
     * instead of patch method the install method executes all scripts without
     * any version check but the version table is updated anyway (if it's
     * defined via 'withVersionUpdate()'
     */
    public void install()
    {
        DbInstaller installer = new DbInstaller(configuration.getDataSource(), configuration.getVariables());
        installer.setEventListener(configuration.getEventListener());
        installer.setVersionUpdateSql(configuration.getVersionUpdateSqlScript());
        installer.install(configuration.getStepsSource());
    }


    /**
     * Method update the database to latest version. There are 2
     * strategies how patches could be applied.
     *
     * Simple strategy, when is 'versionSql' defined and
     * apply all patches greater than current DB. This strategy
     * is used when you've got VERSION table only with one value.
     *
     * Conditional strategy, when is precondition SQL defined. This
     * strategy apply all patches they're missing. This strategy
     * is used when you've got in VERSION table list of all applied
     * patches.
     *
     */
    public void patch()
    {
        DbStepResource filteredScripts = applyFilter();
        DbInstaller installer = new DbInstaller(configuration.getDataSource(), configuration.getVariables());
        installer.setEventListener(configuration.getEventListener());
        installer.setVersionUpdateSql(configuration.getVersionUpdateSqlScript());
        installer.install(filteredScripts);
    }


    /**
     * Method update the database to version you wish.
     */
    public void patchTo(Version v)
    {
        DbStepResource filteredScripts = applyFilter();
        filteredScripts = VersionFilter.filter(filteredScripts).smallerOrEqualsTo(v);

        DbInstaller installer = new DbInstaller(configuration.getDataSource(), configuration.getVariables());
        installer.setEventListener(configuration.getEventListener());
        installer.setVersionUpdateSql(configuration.getVersionUpdateSqlScript());
        installer.install(filteredScripts);
    }


    /**
     * Method apply filter on resource by patching strategy
     */
    private DbStepResource applyFilter()
    {
        DbStepResource filteredScripts =
                        new AppliedPatchesFiter(
                                configuration.getStepsSource(),
                                configuration.getDataSource(),
                                configuration.getVersionSql(),
                                configuration.getVariables());
        return filteredScripts;
    }
}
TOP

Related Classes of com.celum.dbtool.Db

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.