root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / postgresql / dal / expressionbuilderformatter / Decode.java @ 650
History | View | Annotate | Download (2.33 KB)
1 | 516 | jjdelcerro | package org.gvsig.postgresql.dal.expressionbuilderformatter; |
---|---|---|---|
2 | |||
3 | import java.util.List; |
||
4 | import java.util.Objects; |
||
5 | import org.apache.commons.lang3.StringUtils; |
||
6 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
7 | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECODE; |
||
8 | import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
||
9 | import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
||
10 | import org.gvsig.expressionevaluator.Formatter; |
||
11 | import org.gvsig.fmap.dal.SQLBuilder; |
||
12 | |||
13 | /**
|
||
14 | *
|
||
15 | * @author jjdelcerro
|
||
16 | */
|
||
17 | public class Decode implements Formatter<Value> { |
||
18 | |||
19 | private final SQLBuilder sqlbuilder; |
||
20 | private final Formatter<Value> formatter; |
||
21 | |||
22 | public Decode(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
||
23 | this.sqlbuilder = sqlbuilder;
|
||
24 | this.formatter = formatter;
|
||
25 | } |
||
26 | |||
27 | @Override
|
||
28 | public boolean canApply(ExpressionBuilder.Value value) { |
||
29 | if (value instanceof ExpressionBuilder.Function) { |
||
30 | if( StringUtils.equalsIgnoreCase(FUNCTION_DECODE, ((Function) value).name()) ) {
|
||
31 | List<Value> parameters = ((Function) value).parameters();
|
||
32 | if( parameters.size()==2) { |
||
33 | Value p1 = parameters.get(0);
|
||
34 | Value p2 = parameters.get(1);
|
||
35 | if( p1 instanceof ExpressionBuilder.Constant && |
||
36 | p2 instanceof ExpressionBuilder.Constant &&
|
||
37 | ((ExpressionBuilder.Constant)p1).value() instanceof String && |
||
38 | ((ExpressionBuilder.Constant)p2).value() instanceof String |
||
39 | ) { |
||
40 | String p2value = ((ExpressionBuilder.Constant)p2).value().toString();
|
||
41 | if( StringUtils.equalsIgnoreCase(p2value, "hex") ) { |
||
42 | return true; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | } |
||
47 | } |
||
48 | } |
||
49 | return false; |
||
50 | } |
||
51 | |||
52 | @Override
|
||
53 | public String format(Value function) { |
||
54 | ExpressionBuilder builder = this.sqlbuilder.expression();
|
||
55 | List<Value> parameters = ((Function) function).parameters();
|
||
56 | Value p1 = parameters.get(0);
|
||
57 | String p1value = Objects.toString(((ExpressionBuilder.Constant)p1).value(), ""); |
||
58 | String r = builder.string("\\x"+p1value)+"::bytea"; |
||
59 | return r;
|
||
60 | } |
||
61 | |||
62 | } |