package com.hotent.base.sqlbuilder.service.impl;

/**
 * 包名：com.hotent.platform.bpm.sqlbuilder
 * 文件名：AbstractHandlerSqlBuilder.java
 * 作者：User-mailto:liyj@jee-soft.cn
 * 日期2014-7-16-下午6:23:30
 *  2014广州宏天软件有限公司版权所有
 *
 */

import com.hotent.base.constants.SQLConst;
import com.hotent.base.sqlbuilder.*;
import com.hotent.base.sqlbuilder.service.ISqlBuilderService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;


@Service("sqlBuilderService")
public class SqlBuilderService implements ISqlBuilderService{
	protected  Map<String,ISqlBuilder> builderMap=new HashMap<String, ISqlBuilder>() ;

    @PostConstruct
    public void setBuilderMap() {
    	builderMap.put(SQLConst.DB_MYSQL, new MySqlSqlBuilder());
    	builderMap.put(SQLConst.DB_ORACLE, new OracleSqlBuilder());
    	builderMap.put(SQLConst.DB_POSTGRESQL, new PgSqlSqlBuilder());
    	builderMap.put(SQLConst.DB_KINGBASE, new KingbaseesSqlBuilder());
    }

	@Override
	public String getSql(SqlBuilderModel model) {
		ISqlBuilder builder=getSqlBuilder(model);
		if(builder!=null){
			return builder.getSql();
		}
		return "";
	}

	@Override
	public ISqlBuilder getSqlBuilder(SqlBuilderModel model){
		String dbType=model.getDbType();
		ISqlBuilder builder;
		switch(dbType) {
			case SQLConst.DB_MYSQL :
				builder = new MySqlSqlBuilder();
				break;
			case SQLConst.DB_ORACLE:
				builder = new OracleSqlBuilder();
				break;
			case SQLConst.DB_POSTGRESQL:
				builder = new PgSqlSqlBuilder();
				break;
			case SQLConst.DB_KINGBASE:
				builder = new KingbaseesSqlBuilder();
				break;
			default:
				builder=new SqlServerSqlBuilder();
		}
		builder.setModel(model);

		return builder;
	}
}
