package com.chenyang.druid.sql.visitor.functions;

import com.chenyang.druid.sql.ast.SQLExpr;
import com.chenyang.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.chenyang.druid.sql.ast.expr.SQLValuableExpr;
import com.chenyang.druid.sql.visitor.SQLEvalVisitor;
import java.util.Iterator;

public class Concat implements Function {
   public static final Concat instance = new Concat();

   public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
      StringBuilder buf = new StringBuilder();

      for(SQLExpr item : x.getArguments()) {
         item.accept(visitor);
         Object itemValue = item.getAttribute("eval.value");
         if (itemValue == null) {
            return null;
         }

         buf.append(itemValue.toString());
      }

      return buf.toString();
   }

   public Object eval(SQLMethodInvokeExpr x) {
      StringBuilder buf = new StringBuilder();
      Iterator var3 = x.getArguments().iterator();

      while(true) {
         if (!var3.hasNext()) {
            return buf.toString();
         }

         SQLExpr param = (SQLExpr)var3.next();
         if (!(param instanceof SQLValuableExpr)) {
            break;
         }

         Object val = ((SQLValuableExpr)param).getValue();
         if (val instanceof String) {
            buf.append(val);
         } else {
            if (!(val instanceof Integer)) {
               break;
            }

            buf.append(val);
         }
      }

      return SQLEvalVisitor.EVAL_ERROR;
   }
}
