/*
 * 作成日: 2008/08/05
 * 著作権: Copyright (c) 2005 ZIGEN
 * ライセンス：Eclipse Public License - v 1.0
 * 原文：http://www.eclipse.org/legal/epl-v10.html
 */
package zigen.plugin.db.ext.s2jdbc.entity.rule;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import zigen.plugin.db.core.DBType;
import zigen.plugin.db.core.IDBConfig;


public abstract class AbstractSqlFactory implements ISqlFactory {
	
	public static ISqlFactory getFactory(IDBConfig config) {
		return getFactory(config.getDriverName());
	}
	
	public static ISqlFactory getFactory(DatabaseMetaData objMet) {
		try {
			return getFactory(objMet.getDriverName());
			
		} catch (SQLException e) {
			throw new IllegalStateException("DriverNameの取得に失敗しました"); //$NON-NLS-1$
		}
		
	}
	
	private static Map map = new HashMap();
	
	public static ISqlFactory getFactory(String driverName) {
		ISqlFactory factory = null;
		String key = driverName;
		if (map.containsKey(key)) {
			factory = (ISqlFactory) map.get(key);
		} else {
			switch (DBType.getType(driverName)) {
				case DBType.DB_TYPE_ORACLE:
					factory = new OracleSqlFactory();
					break;
				case DBType.DB_TYPE_MYSQL:
					factory = new MySQLSqlFactory();
					break;
				default:
					factory = new DefaultSqlFactory();
					break;
			}
			
			map.put(key, factory);
		}
		return factory;
	}
}