package org.opencloudb.parser.druid;

import java.io.UnsupportedEncodingException;

import org.opencloudb.config.model.SystemConfig;
import org.opencloudb.sequence.handler.IncrSequenceTimeHandler;
import org.opencloudb.sequence.handler.IncrSequenceMySQLHandler;
import org.opencloudb.sequence.handler.IncrSequencePropHandler;
import org.opencloudb.sequence.handler.SequenceHandler;
import org.opencloudb.util.StringUtil;

/**
 * 使用Druid解析器实现对Sequence处理
 * @author 兵临城下
 * @date 2015/03/13
 */
public class DruidSequenceHandler {
	private final SequenceHandler sequenceHandler;

	public DruidSequenceHandler(int seqHandlerType) {
		switch(seqHandlerType){
		case SystemConfig.SEQUENCEHANDLER_MYSQLDB:
			sequenceHandler = IncrSequenceMySQLHandler.getInstance();
			break;
		case SystemConfig.SEQUENCEHANDLER_LOCALFILE:
			sequenceHandler = IncrSequencePropHandler.getInstance();
			break;
		case SystemConfig.SEQUENCEHANDLER_LOCAL_TIME:
			sequenceHandler = IncrSequenceTimeHandler.getInstance();
			break;
		default:
			throw new java.lang.IllegalArgumentException("Invalid sequnce handler type "+seqHandlerType);
		}
	}

	/**
	 * 根据原sql获取可执行的sql
	 * @param sql
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String getExecuteSql(String sql,String charset) throws UnsupportedEncodingException{
		String executeSql = null;
		if (null!=sql && !"".equals(sql)) {
			sql = new String(sql.getBytes(), charset);
			String tableName = StringUtil.getTableName(sql).toUpperCase();
			long value = sequenceHandler.nextId(tableName.toUpperCase());
			String replaceStr = "next value for MYCATSEQ_"+tableName;
			executeSql = sql.replace(replaceStr, value+"");
		}
		return executeSql;
	}

}

@@ -1603,84 +1603,751 @@
 %0A%09%09%09
-String replaceStr = %22next value for MYCATSEQ_%22+tableName;%0D%0A%09%09%09executeSql = s
+executeSql = this.replaceSql(sql, tableName,value);%0D%0A%09%09%7D%0D%0A%09%09return executeSql;%0D%0A%09%7D%0D%0A%0D%0A%09private String replaceSql(String orgSql,String tableName,long sequnce)%7B%0D%0A%09%09if(orgSql.indexOf(%22next value for MYCATSEQ_%22)==-1)%7B%0D%0A%09%09%09throw new java.lang.IllegalArgumentException(%22Invalid sequnce Sql , must need next value for MYCATSEQ_%22);%0D%0A%09%09%7D%0D%0A%09%09String squenceStr = %22next value for MYCATSEQ_%22;%0D%0A%09%09String repanceStr = %22next value for MYCATSEQ_%22 + tableName;%0D%0A%0D%0A%09%09int startIndex = orgSql.indexOf(squenceStr) + squenceStr.length();%0D%0A%09%09int endIndex = startIndex + tableName.length();%0D%0A%09%09orgSql = orgSql.substring(0, startIndex)%0D%0A%09%09%09   + orgSql.substring(startIndex, endIndex).toUpperCase()%0D%0A%09%09%09   + orgSql.substring(endIndex, orgSql.length()) ;%0D%0A%09%09orgSql = orgS
 ql.r
@@ -2360,29 +2360,31 @@
 (rep
-l
 a
+n
 ceStr, 
-valu
+sequnc
 e+%22%22);%0D%0A
 %09%09%7D%0D
@@ -2375,27 +2375,24 @@
 qunce+%22%22);%0D%0A
-%09%09%7D
 %0D%0A%09%09return e
@@ -2382,38 +2382,34 @@
 %22);%0D%0A%0D%0A%09%09return 
-execute
+org
 Sql;%0D%0A%09%7D%0D%0A%0D%0A%7D%0D%0A

修复 自增主键，表写错大小写导致mycat无限循环获取