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

org.onetwo.common.db.filequery.DefaultFileNamedSqlGenerator Maven / Gradle / Ivy

The newest version!
package org.onetwo.common.db.filequery;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.onetwo.common.db.ParsedSqlContext;
import org.onetwo.common.db.filequery.func.SqlFunctionDialet;
import org.onetwo.common.db.spi.FileNamedSqlGenerator;
import org.onetwo.common.db.spi.FileSqlParserType;
import org.onetwo.common.db.spi.NamedQueryInfo;
import org.onetwo.common.db.spi.QueryContextVariable;
import org.onetwo.common.db.spi.SqlTemplateParser;
import org.onetwo.common.db.sqlext.ExtQueryUtils;
import org.onetwo.common.utils.LangUtils;
import org.onetwo.dbm.exception.DbmException;
import org.springframework.util.Assert;

/****
 * sql语句解释
 * @author way
 *
 */
public class DefaultFileNamedSqlGenerator implements FileNamedSqlGenerator {
	
//	private static final Logger logger = JFishLoggerFactory.getLogger(DefaultFileNamedSqlGenerator.class);
	protected NamedQueryInfo info;
	protected boolean countQuery;
	private SqlTemplateParser parser;
	/***
	 */
	private ParserContext parserContext;
//	private Class resultClass;
	
//	private String[] ascFields;
//	private String[] desFields;

	private Map params;
	private final Optional sqlFunctionDialet;
	
	
	
//	public DefaultFileNamedSqlGenerator(boolean countQuery,
//										SqlTemplateParser parser, 
//										ParserContext parserContext, 
//										Map params, 
//										Optional sqlFunctionDialet) {
//		super();
//		this.info = parserContext.getQueryInfo();
//		this.countQuery = countQuery;
//		this.parser = parser;
//		this.params = LangUtils.emptyIfNull(params);
//		/*if(params!=null){
//			this.parserContext = (ParserContext)this.params.get(JNamedQueryKey.ParserContext);
//		}*/
//		this.parserContext = parserContext;
//		this.sqlFunctionDialet = sqlFunctionDialet;
//	}

	public DefaultFileNamedSqlGenerator(boolean countQuery,
			SqlTemplateParser parser, ParserContext parserContext,
//			Class resultClass, String[] ascFields, String[] desFields,
			Map params, Optional sqlFunctionDialet) {
		super();
		this.info = parserContext.getQueryInfo();
		this.countQuery = countQuery;
		this.parser = parser;
		this.parserContext = parserContext;
//		this.resultClass = resultClass;
//		this.ascFields = ascFields;
//		this.desFields = desFields;
		this.params = new HashMap<>();
		this.sqlFunctionDialet = sqlFunctionDialet;
		
		this.params.putAll(LangUtils.emptyIfNull(params));
		QueryContextVariable[] vars = parserContext.getQueryInfo().getQueryConfig().getVariables();
		if (vars!=null) {
			for(QueryContextVariable v : vars) {
				this.params.put(v.varName(), v);
			}
		}
	}

	@Override
	public ParsedSqlContext generatSql(){
		String parsedSql = null;
		ParsedSqlContext sv = null;
		// 4.8版本后移除非TEMPLATE类型的判断
//		if(info.getParserType()==FileSqlParserType.IGNORENULL){
//			String sql = countQuery?info.getCountSql():info.getSql();
//			DynamicQuery query = DynamicQueryFactory.createJFishDynamicQuery(sql, resultClass);
//			for(Entry entry : this.params.entrySet()){
//				query.setParameter(entry.getKey().toString(), entry.getValue());
//			}
//			query.asc(ascFields);
//			query.desc(desFields);
//			query.compile();
//			parsedSql = query.getTransitionSql();
//			sv = new SqlAndValues(false, parsedSql, query.getValues());
//			
//		}else 
		if(info.getParserType()==FileSqlParserType.TEMPLATE){
			Assert.notNull(parserContext, "parserContext can not be null");
			// _func -> SqlFunctionDialet
			this.sqlFunctionDialet.ifPresent(func-> {
				parserContext.put(ParserContext.CONTEXT_KEY, func);
			});
			this.parserContext.putAll(params);
			FragmentTemplateParser attrParser = new FragmentTemplateParser(parser, parserContext, info);
			// 把FragmentTemplateParser放到sql的解释上下问里,key为"fragment",可通过fragment['sql query name']引用到parserContext里的sql语句
			this.parserContext.put(attrParser.getVarName(), attrParser);
			
			if(countQuery){
//				parsedSql = this.parser.parse(info.isAutoGeneratedCountSql()?info.getFullName():info.getCountName(), parserContext);
				parsedSql = parseSql(info.isAutoGeneratedCountSql()?info.getFullName():info.getCountName(), parserContext);
				if(info.isAutoGeneratedCountSql()){
					parsedSql = ExtQueryUtils.buildCountSql(parsedSql, "");
				}
			}else{
				parsedSql = parseSql(info.getFullName(), parserContext);
			}
			sv = new SqlAndValues(true, parsedSql, params);
			
		}else{
			throw new DbmException("unsupported parser type: " + info.getParserType());
			// 4.8版本后移除非TEMPLATE类型的判断
//			parsedSql = countQuery?info.getCountSql():info.getSql();
//			sv = new SqlAndValues(true, parsedSql, params);
		}

		return sv;
	}
	
	protected String parseSql(String fullName, ParserContext parserContext) {
		String parsedSql = parser.parse(fullName, parserContext);
		return parsedSql;
	}
	
	protected Map getParams() {
		return params;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy