1 package com.randomnoun.common.jexl.sql.function;
2
3
4
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
17
18
19 public class EndsWithFunction extends com.randomnoun.common.jexl.eval.function.EndsWithFunction implements SqlFunction {
20
21 public String toSql(String functionName, EvalContext evalContext, List<Object> arguments) {
22 if (arguments.size() != 2) { throw new EvalException(functionName + "() must contain two parameters"); }
23 if (!(arguments.get(0) instanceof SqlColumn)) { throw new EvalException(functionName + "() parameter 1 must be an SQL column"); }
24 if (!(arguments.get(1) instanceof String || arguments.get(1) instanceof SqlText)) { throw new EvalException(functionName + "() parameter 2 must be a string type"); }
25
26 SqlColumn arg0 = (SqlColumn) arguments.get(0);
27 String likePattern = "";
28
29 if (arguments.get(1) instanceof String) {
30 likePattern = "%" + SqlGenerator.escapeLikeLiteral(evalContext, (String) arguments.get(1));
31 } else {
32
33 throw new EvalException("Cannot translate " + functionName + "(a,b) where 'b' is a dynamically-generated value");
34 }
35
36 Object arg1 = arguments.get(1);
37
38 if (arg0 == null) { throw new EvalException(functionName + "() first parameter cannot be null"); }
39 if (arg1 == null) { throw new EvalException(functionName + "() second parameter cannot be null"); }
40 return "(" + arg0.getFullName() + " LIKE " + SqlGenerator.toSql(evalContext, likePattern) + ")";
41 }
42 }