package ws.prova;


/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 * Copyright (C) 1999-2005 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * Database wrapper adapted from original Mandarax
 * normally used in standard distribution file utils.prova.
 * <p>Title: Prova</p>
 * <p>Description: Database wrapper adapted from original Mandarax
 * normally used in standard distribution file utils.prova.
 * included from rule bases</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: City University</p>
 *
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 2.0 <25 March 2007>
 */
public class Database {
	// XXX TODO load driver classes from property resource
	static {

	}

	/**
	 * Initialize driver, currently working for MySQL, Oracle, Postgres, and MSSQL drivers.
	 * More correct classes should be known for other
	 * popoular databases.
	 *
	 * @param uriprefix String JDBC connection string,
	 *                  not including username and password
	 * @param uriprefix
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	protected void initDriver(String uriprefix)
			throws ClassNotFoundException, IllegalAccessException, InstantiationException {
		if (uriprefix.indexOf("mysql") > 0) {
			ws.prova.Calc.forName("com.mysql.jdbc.Driver").newInstance();
		} else if (uriprefix.indexOf("microsoft") > 0) {
			ws.prova.Calc.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").
					newInstance();
		} else if (uriprefix.indexOf("oracle") > 0) {
			ws.prova.Calc.forName("oracle.jdbc.driver.OracleDriver").newInstance();
		} else if (uriprefix.indexOf("postgresql") > 0) {
			ws.prova.Calc.forName("org.postgresql.Driver").newInstance();
		}
	}

	/**
	 * Factory method for DataSource normally called from utils.prova
	 * followed by DataSource.getConnection()
	 *
	 * @param uri String
	 * @return DataSource
	 */
	public DataSource getDataSource(String uri)
			throws IllegalAccessException, InstantiationException, ClassNotFoundException {
		initDriver(uri);
		DataSource ds = new DataSource(uri);
		return ds;
	}

}
