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

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
import java.util.List;

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

   public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
      List<SQLExpr> arguments = x.getArguments();
      if (arguments.size() == 0) {
         return SQLEvalVisitor.EVAL_ERROR;
      } else {
         SQLExpr condition = (SQLExpr)arguments.get(0);
         condition.accept(visitor);
         Object itemValue = condition.getAttributes().get("eval.value");
         if (itemValue == SQLEvalVisitor.EVAL_VALUE_NULL) {
            return Boolean.TRUE;
         } else {
            return itemValue == null ? null : Boolean.FALSE;
         }
      }
   }
}
