package com.chenyang.druid.sql.visitor;

import com.chenyang.druid.DbType;
import com.chenyang.druid.sql.SQLUtils;
import com.chenyang.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.chenyang.druid.util.FnvHash;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SQLSubQueryGroupVisitor extends SQLASTVisitorAdapter {
   private final DbType dbType;
   protected Map<Long, List<SQLSubqueryTableSource>> tableSourceMap = new LinkedHashMap();

   public SQLSubQueryGroupVisitor(DbType dbType) {
      this.dbType = dbType;
   }

   public boolean visit(SQLSubqueryTableSource x) {
      String sql = SQLUtils.toSQLString(x.getSelect(), (DbType)this.dbType);
      long hashCode64 = FnvHash.fnv1a_64(sql);
      List<SQLSubqueryTableSource> list = (List)this.tableSourceMap.get(hashCode64);
      if (list == null) {
         list = new ArrayList();
         this.tableSourceMap.put(hashCode64, list);
      }

      list.add(x);
      return true;
   }

   public Collection<List<SQLSubqueryTableSource>> getGroupedSubqueryTableSources() {
      return this.tableSourceMap.values();
   }
}
