/*
 * This file is part of BORG.
 *
 * BORG is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * BORG is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * BORG; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA 02111-1307 USA
 *
 * Copyright 2003 by Mike Berger
 */
package net.sf.borg.model.db.jdbc;

import net.sf.borg.common.Errmsg;

import java.sql.SQLException;
import java.util.logging.Logger;

/**
 * Class JdbcDBUpgrader is used to upgrade HSQL database in-place when the schema changes for a 
 * release. This class will check an update condition, and if true, execute SQL to upgrade the DB.
 * It is meant to be called during model initialization - preferably from the constructors
 * of the JdbcDB classes
 */

public class JdbcDBUpgrader {
	static private final Logger log = Logger.getLogger("net.sf.borg");


	/** The check sql. */
	private final String checkSql;

	/** The upd sql. */
	private final String[] updSql;

	/**
	 * Instantiates a new jdbc db upgrader.
	 * 
	 * @param checkSql the sql that checks if an upgrade is needed
	 * @param usql the sql to upgrade the db if needed
	 */
	public JdbcDBUpgrader(String checkSql, String usql) {
		updSql = new String[1];
		this.updSql[0] = usql;
		this.checkSql = checkSql;
	}
	
	/**
	 * Instantiates a new jdbc db upgrader.
	 * 
	 * @param checkSql the sql that checks if an upgrade is needed
	 * @param usql an array of SQL statements to execute to perform the upgrade
	 */
	public JdbcDBUpgrader(String checkSql, String[] usql) {
		this.updSql = usql;
		this.checkSql = checkSql;
	}

	/**
	 * check if db Needs upgrade.
	 * 
	 * @return true, if upgrade needed
	 * 
	 * @throws Exception the exception
	 */
	private boolean needsUpgrade() throws Exception {
		
		if( checkSql == null )
			return true;
		
		try {
			JdbcDB.execSQL(checkSql);
		} catch (Exception e) {
			if (e instanceof SQLException)
				return true;
		}

		return false;
	}

	/**
	 * Execute the upgrade SQL.
	 *
	 * @throws Exception the exception
	 */
	private void performUpgrade() throws Exception {
		for( int i = 0; i < updSql.length; i++ )
		{
			log.info("Running Upgrade SQL:" + updSql[i]);
			JdbcDB.execSQL(updSql[i]);
		}
	}

	/**
	 * run the upgrade check and then upgrade if needed
	 */
	public void upgrade() {
		try {
			if (needsUpgrade()) {
				performUpgrade();
			}
		} catch (Exception e) {
			Errmsg.getErrorHandler().errmsg(e);
		}
	}
}
