View Javadoc
1   package com.randomnoun.common.jexl.sql.function;
2   
3   /* (c) 2013-2018 randomnoun. All Rights Reserved. This work is licensed under a
4    * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
5    */
6   
7   import java.util.List;
8   
9   import com.randomnoun.common.jexl.eval.EvalContext;
10  import com.randomnoun.common.jexl.eval.EvalException;
11  import com.randomnoun.common.jexl.sql.SqlColumn;
12  import com.randomnoun.common.jexl.sql.SqlFunction;
13  import com.randomnoun.common.jexl.sql.SqlGenerator;
14  import com.randomnoun.common.jexl.sql.SqlText;
15  
16  /** SQL 'LIKE' operator. Returns true if the data in the specified SqlColumn (in the first
17   *  argument) matches the supplied LIKE pattern (in the second argument)
18   */
19  public class LikeFunction extends com.randomnoun.common.jexl.eval.function.LikeFunction implements SqlFunction 
20  {
21  	/** @inheritdoc */
22  	public String toSql(String functionName, EvalContext evalContext, List<Object> arguments) {
23  		if (arguments.size() != 2) { throw new EvalException(functionName + "() must contain two parameters"); }
24  		if (!(arguments.get(0) instanceof SqlColumn)) { throw new EvalException(functionName + "() parameter 1 must be an SQL column"); }
25  		if (!(arguments.get(1) instanceof String || arguments.get(1) instanceof SqlText)) { throw new EvalException(functionName + "() parameter 2 must be a string type"); }
26  
27  		SqlColumn arg0 = (SqlColumn) arguments.get(0);
28  		Object arg1 = arguments.get(1);
29  		if (arg0 == null) { throw new EvalException(functionName + "() first parameter cannot be null"); }
30  		if (arg1 == null) { throw new EvalException(functionName + "() second parameter cannot be null"); }
31  
32  		return "(" + arg0.getFullName() + " LIKE " + SqlGenerator.toSql(evalContext, arg1) + ")";
33  	}
34  }