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

com.github.antelopeframework.mybatis.shard.converter.SelectSqlConverter Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package com.github.antelopeframework.mybatis.shard.converter;

import java.util.Map;

import com.github.antelopeframework.mybatis.shard.ShardContext.ShardOnPair;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.SubJoin;
import net.sf.jsqlparser.statement.select.SubSelect;
import net.sf.jsqlparser.statement.select.TableFunction;
import net.sf.jsqlparser.statement.select.ValuesList;
import net.sf.jsqlparser.statement.select.WithItem;

/**
 * SELECT 语句表名替换.
 * 
 * @author yangzhi.yzh
 *
 */
class SelectSqlConverter extends AbstractSqlConverter {
	
	@Override
	protected Statement doConvert(String sqlMapperId, Statement statement, Map shardOns) {
		if (!(statement instanceof Select)) {
			throw new IllegalArgumentException("The argument statement must is instance of Select.");
		}
		
		TableNameModifier modifier = new TableNameModifier(sqlMapperId, shardOns);
		((Select) statement).getSelectBody().accept(modifier);
		
		return statement;
	}

	private class TableNameModifier implements SelectVisitor, FromItemVisitor {
		private Map shardOns;
		private String sqlMapperId;

		TableNameModifier(String sqlMapperId, Map shardOns) {
			this.shardOns = shardOns;
			this.sqlMapperId = sqlMapperId;
		}

		//~ SelectVisitor
		
		@Override
		public void visit(PlainSelect plainSelect) {
			plainSelect.getFromItem().accept(this);
		}

		@Override
		public void visit(SetOperationList setOpList) {
			
		}

		@Override
		public void visit(WithItem withItem) {
			
		}

		//~ FromItemVisitor
		
		@Override
		public void visit(Table table) {
			String tableName = table.getName();
			tableName = SelectSqlConverter.this.convertTableName(this.sqlMapperId, tableName, this.shardOns);
			
			table.setName(tableName);
		}

		@Override
		public void visit(SubSelect subSelect) {
			subSelect.getSelectBody().accept(this);
		}

		@Override
		public void visit(SubJoin subjoin) {
			subjoin.getLeft().accept(this);
			subjoin.getJoin().getRightItem().accept(this);
		}

		@Override
		public void visit(LateralSubSelect lateralSubSelect) {
			
		}

		@Override
		public void visit(ValuesList valuesList) {
			
		}

		@Override
		public void visit(TableFunction tableFunction) {
			
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy