01: package org.obe.sql;
02:
03: import java.io.IOException;
04: import java.io.Writer;
05:
06: /**
07: * @author Adrian Price
08: */
09: public class SQLLikeClause extends SQLInvertibleClause {
10: public SQLLikeClause(int id) {
11: super (id);
12: }
13:
14: public void write(Writer out) throws IOException {
15: out.write(invert ? " NOT LIKE " : " LIKE ");
16: children[0].write(out);
17: }
18:
19: public Object execute(Object context, Object lhs) {
20: Object rhs = children[0].execute(context);
21: String lvalue = lhs == null ? "" : lhs.toString();
22: String rvalue = rhs == null ? "" : rhs.toString();
23:
24: // This algorithm handles "%..." and "...%" patterns.
25: boolean result;
26: int length = rvalue.length();
27: if (rvalue.charAt(0) == '%')
28: result = lvalue.endsWith(rvalue.substring(1, length));
29: else if (rvalue.charAt(length - 1) == '%')
30: result = lvalue.startsWith(rvalue.substring(0, length - 1));
31: else
32: result = false;
33: return result ^ invert ? Boolean.TRUE : Boolean.FALSE;
34: }
35: }
|