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

it.openutils.migration.generic.JdbcIfColumnExistsConditionalTask Maven / Gradle / Ivy

Go to download

DB Migration is a framework based on Spring for handling automatic db schema updates. With DBMigration you can bundle upgrade scripts in you application. Its purpose is to manage db updates consistently both for development than for production releases.

There is a newer version: 2.2.0
Show newest version
package it.openutils.migration.generic;

import it.openutils.migration.task.setup.BaseConditionalTask;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;


/**
 * @author fgiust
 * @version $Id: JdbcIfColumnExistsConditionalTask.java 611 2008-02-11 20:47:48Z fgiust $
 */
public class JdbcIfColumnExistsConditionalTask extends BaseConditionalTask
{

    private String column;

    protected String catalog;

    protected String schema;

    /**
     * Sets the catalog.
     * @param catalog the catalog to set
     */
    public void setCatalog(String catalog)
    {
        this.catalog = catalog;
    }

    /**
     * Sets the schema.
     * @param schema the schema to set
     */
    public void setSchema(String schema)
    {
        this.schema = schema;
    }

    /**
     * Sets the column.
     * @param column the column to set
     */
    public void setColumn(String column)
    {
        this.column = column;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean check(SimpleJdbcTemplate jdbcTemplate)
    {

        String columnTrim = StringUtils.trim(column);

        final String tableName = StringUtils.substringBefore(columnTrim, ".");
        final String columnName = StringUtils.substringAfter(columnTrim, ".");
        return (Boolean) jdbcTemplate.getJdbcOperations().execute(new ConnectionCallback()
        {

            public Object doInConnection(Connection con) throws SQLException, DataAccessException
            {

                DatabaseMetaData dbMetadata = con.getMetaData();
                ResultSet rs = dbMetadata.getColumns(catalog, schema, tableName, columnName);
                boolean tableExists = rs.next();
                rs.close();

                return !tableExists;
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy