/*
 * 作成日: 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.dto.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 AbstractDtoMappingFactory implements IDtoMappingFactory {
	
	public static IDtoMappingFactory getFactory(IDBConfig config) {
		return getFactory(config.getDriverName());
	}
	
	public static IDtoMappingFactory getFactory(DatabaseMetaData objMet, boolean isConvertUnicode) {
		try {
			return getFactory(objMet.getDriverName());
			
		} catch (SQLException e) {
			throw new IllegalStateException("DriverNameの取得に失敗しました"); //$NON-NLS-1$
		}
		
	}
	
	private static Map map = new HashMap();
	
	public static IDtoMappingFactory getFactory(String driverName) {
		IDtoMappingFactory factory = null;
		String key = driverName;
		if (map.containsKey(key)) {
			factory = (IDtoMappingFactory) map.get(key);
		} else {
			switch (DBType.getType(driverName)) {
				case DBType.DB_TYPE_ORACLE:
					factory = new OracleDtoMappingFactory();
					break;
				case DBType.DB_TYPE_H2:
					factory = new H2DtoMappingFactory();
					break;
				default:
					factory = new DefaultDtoMappingFactory();
					break;
			}
			
			map.put(key, factory);
		}
		return factory;
	}
}