All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.sylinx.hbatis.ext.parse.SqlParser Maven / Gradle / Ivy

There is a newer version: 2.0.0.RELEASE
Show newest version
package cn.sylinx.hbatis.ext.parse;

import java.util.Map;

import cn.sylinx.hbatis.exception.HbatisException;
import cn.sylinx.hbatis.ext.res.ClasspathSqlResource;
import cn.sylinx.hbatis.ext.res.ClasspathSqlResourceManager;
import cn.sylinx.hbatis.ext.res.StatementHandler;
import cn.sylinx.hbatis.kit.StrKit;
import cn.sylinx.hbatis.kit.Tuple;
import cn.sylinx.hbatis.log.GLog;

public abstract class SqlParser {

	public static Tuple parseSql(ClasspathSqlResource sqlResource, Map params) {
		String statement = ClasspathSqlResourceManager.getStatement(sqlResource.getSqlpath());
		if (StrKit.isBlank(statement)) {
			throw new HbatisException("SQL语句为空, 资源:" + sqlResource.getSqlpath());
		}
		return parseSql(statement, params, sqlResource.isFormat(), sqlResource.getStatementHandler());
	}

	public static Tuple parseSql(String statement, Map params, StatementHandler sqlHandler) {
		return parseSql(statement, params, true, sqlHandler);
	}
	
	public static Tuple parseSql(String statement, Map params) {
		return parseSql(statement, params, null);
	}

	public static Tuple parseSql(String statement, Map params, boolean format,
			StatementHandler sqlHandler) {

		SqlTokenHandler handler = new SqlTokenHandler(params);
		GenericTokenParser gt = new GenericTokenParser(handler);
		Tuple tp = gt.parse(statement, format);

		if (sqlHandler != null) {
			String statementReplace = sqlHandler.handle(tp.getObject(0, String.class));
			GLog.debug("changed sql: " + statementReplace);
			return Tuple.apply(statementReplace, tp.get(1));
		}

		return tp;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy