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

com.yuehuanghun.mybatis.milu.criteria.builder.SqlTemplateBuilder Maven / Gradle / Ivy

There is a newer version: 1.18.0
Show newest version
package com.yuehuanghun.mybatis.milu.criteria.builder;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.yuehuanghun.mybatis.milu.MiluConfiguration;
import com.yuehuanghun.mybatis.milu.data.SqlBuildingHelper;
import com.yuehuanghun.mybatis.milu.data.SqlBuildingHelper.TableAliasDispacher;
import com.yuehuanghun.mybatis.milu.generic.GenericProviderContext;
import com.yuehuanghun.mybatis.milu.metamodel.Entity;
import com.yuehuanghun.mybatis.milu.metamodel.Entity.Attribute;
import com.yuehuanghun.mybatis.milu.tool.Segment;
import com.yuehuanghun.mybatis.milu.tool.StringUtils;

public abstract class SqlTemplateBuilder {

	protected final GenericProviderContext context;
	protected final Entity entity;
	protected final MiluConfiguration configuration;

	protected TableAliasDispacher tableAliasDispacher = new TableAliasDispacher();
	protected Map joinExpressMap = new HashMap<>();
	protected Map joinQueryColumnNap = new HashMap<>();
	protected String mainTableAlias;
	
	public SqlTemplateBuilder(GenericProviderContext context) {
		super();
		this.context = context;
		this.entity = context.getEntity();
		this.configuration = context.getConfiguration();
		this.mainTableAlias = tableAliasDispacher.dispach(Segment.TABLE_ + entity.getTableName());
	}
	
	protected void buildTableSegment(StringBuilder sqlBuilder) {	
		SqlBuildingHelper.appendSchema(sqlBuilder, entity.getSchema(), configuration);
		SqlBuildingHelper.appendIdentifier(sqlBuilder, entity.getTableName(), configuration); //表名
		if(!joinExpressMap.isEmpty()) { //没有关联查询时,不需要使用表别名
			sqlBuilder.append(Segment.SPACE).append(mainTableAlias);

            for(String joinExpress : joinExpressMap.values()) {
            	sqlBuilder.append(joinExpress);
            }
		}
	}
	
	protected String renderConditionSql(String sqlTemplate, Set properties) {
		String sqlTemplateTmp = sqlTemplate;
		if(StringUtils.isNotBlank(sqlTemplateTmp)) {
			for(String property : properties) {
				if(joinQueryColumnNap.containsKey(property)) {
					sqlTemplateTmp = sqlTemplateTmp.replace(SqlBuildingHelper.columnHolder(property), joinQueryColumnNap.get(property));
				} else {
					String column = Segment.EMPTY;
					if(!joinExpressMap.isEmpty()) {
						column = mainTableAlias + Segment.DOT;
					}
					Attribute attr = entity.getAttribute(property);
					column += SqlBuildingHelper.wrapIdentifier(attr.getColumnName(), configuration);
					sqlTemplateTmp = sqlTemplateTmp.replace(SqlBuildingHelper.columnHolder(property), column);
				}	
			}
		}
		return sqlTemplateTmp;
	}

	abstract Object build();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy