Statistics
| Revision:

root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / postgresql / dal / expressionbuilderformatter / JsonValue.java @ 999

History | View | Annotate | Download (2.04 KB)

1 570 jjdelcerro
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_IFNULL;
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 999 omartinez
import org.gvsig.tools.dataTypes.DataTypes;
13 570 jjdelcerro
14
/**
15
 *
16
 * @author jjdelcerro
17
 */
18 980 omartinez
class JsonValue implements Formatter<Value> {
19 570 jjdelcerro
20
    private final Formatter<Value> formatter;
21
    private final SQLBuilder builder;
22 999 omartinez
23 980 omartinez
    public JsonValue(SQLBuilder builder, Formatter<Value> formatter) {
24 570 jjdelcerro
        this.builder = builder;
25
        this.formatter = formatter;
26
    }
27 999 omartinez
28 570 jjdelcerro
    @Override
29
    public boolean canApply(Value value) {
30
        if (value instanceof Function) {
31
            if (value instanceof ExpressionBuilder.Function) {
32 980 omartinez
                boolean canApply = StringUtils.equalsIgnoreCase(ExpressionBuilder.FUNCTION_JSON_VALUE, ((Function) value).name());
33
                return canApply;
34 570 jjdelcerro
            }
35
        }
36
        return false;
37
    }
38
39
    @Override
40
    public String format(Value function) {
41 999 omartinez
        List<Value> parameters = ((Function) function).parameters();
42
        String p1 = parameters.get(0).toString(formatter);
43
        String p2 = parameters.get(1).toString(formatter);
44
        String p3 = null;
45
        if(parameters.size()==3) {
46
          p3 = parameters.get(2).toString(formatter).replace("'", "");
47
        }
48
49
        String r;
50
        if (p3 != null) {
51
            r = MessageFormat.format(
52
                    "jsonb_path_query_first({0}::jsonb, {1})::{2}",
53
                    p1,
54
                    p2,
55
                    p3
56
            );
57
        } else {
58
            r = MessageFormat.format(
59 980 omartinez
                    "jsonb_path_query_first({0}::jsonb, {1})",
60 570 jjdelcerro
                    p1,
61
                    p2
62
            );
63 999 omartinez
        }
64
        return r;
65
66 570 jjdelcerro
    }
67 999 omartinez
68 570 jjdelcerro
}