svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / expressionbuilderformatter / Select.java @ 44750
History | View | Annotate | Download (3.31 KB)
1 | 44748 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilderformatter; |
---|---|---|---|
2 | 44198 | jjdelcerro | |
3 | import org.apache.commons.lang3.StringUtils; |
||
4 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
5 | 44750 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
6 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
7 | import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
||
8 | import org.gvsig.expressionevaluator.Formatter; |
||
9 | 44748 | jjdelcerro | import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
10 | 44198 | jjdelcerro | import org.gvsig.fmap.dal.SQLBuilder; |
11 | |||
12 | /**
|
||
13 | *
|
||
14 | * @author jjdelcerro
|
||
15 | */
|
||
16 | 44750 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
17 | 44748 | jjdelcerro | public class Select implements Formatter<Value> { |
18 | 44750 | jjdelcerro | |
19 | private final SQLBuilder sqlbuilder; |
||
20 | private final Formatter<Value> formatter; |
||
21 | |||
22 | public Select(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 | return StringUtils.equalsIgnoreCase(FUNCTION_SELECT, ((Function) value).name());
|
||
31 | 44198 | jjdelcerro | } |
32 | 44750 | jjdelcerro | return false; |
33 | } |
||
34 | 44198 | jjdelcerro | |
35 | 44750 | jjdelcerro | private static final int COLUMNS = 0; |
36 | private static final int TABLE = 1; |
||
37 | private static final int WHERE = 2; |
||
38 | private static final int ORDER = 3; |
||
39 | private static final int ORDER_MODE = 4; |
||
40 | private static final int LIMIT = 5; |
||
41 | |||
42 | @Override
|
||
43 | public String format(Value function0) { |
||
44 | Function function = (Function) function0; |
||
45 | String tableName = (String) function.parameters().get(TABLE).toString(); |
||
46 | Value where = function.parameters().get(WHERE); |
||
47 | Value order = function.parameters().get(ORDER); |
||
48 | Value limit = function.parameters().get(LIMIT); |
||
49 | Value order_mode = function.parameters().get(ORDER_MODE); |
||
50 | |||
51 | StringBuilder builder = new StringBuilder(); |
||
52 | builder.append("(SELECT ");
|
||
53 | |||
54 | try {
|
||
55 | Function columns = (Function) function.parameters().get(COLUMNS); |
||
56 | if (!StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function) columns).name(), FUNCTION_TUPLE)) {
|
||
57 | throw new IllegalArgumentException(); |
||
58 | } |
||
59 | if( columns.parameters().isEmpty() ) {
|
||
60 | builder.append("* ");
|
||
61 | } else {
|
||
62 | boolean needcomma = false; |
||
63 | for (Value parameter : columns.parameters()) {
|
||
64 | if(needcomma) {
|
||
65 | 44748 | jjdelcerro | builder.append(", ");
|
66 | } else {
|
||
67 | 44750 | jjdelcerro | needcomma = true;
|
68 | 44748 | jjdelcerro | } |
69 | 44750 | jjdelcerro | builder.append(parameter.toString(formatter)); |
70 | 44748 | jjdelcerro | } |
71 | 44750 | jjdelcerro | } |
72 | } catch (Exception ex) { |
||
73 | throw new IllegalArgumentException("Invalid columns, need a tuple function as columns."); |
||
74 | 44198 | jjdelcerro | } |
75 | 44750 | jjdelcerro | |
76 | builder.append(" FROM ");
|
||
77 | builder.append(tableName); |
||
78 | if (where != null) { |
||
79 | builder.append(" WHERE ");
|
||
80 | builder.append(where.toString(formatter)); |
||
81 | } |
||
82 | // for( int n=0 ; function.containsParameter("ORDER", n); n++) {
|
||
83 | // if( n>0 ) {
|
||
84 | // builder.append(", ");
|
||
85 | // }
|
||
86 | // builder.append(function.getParameter("ORDER",n).toString(formatter));
|
||
87 | // if( StringUtils.equalsIgnoreCase(function.getParameter("ORDER_MODE",n).toString(),"true") ) {
|
||
88 | // builder.append(" ASC ");
|
||
89 | // } else {
|
||
90 | // builder.append(" DESC ");
|
||
91 | // }
|
||
92 | // }
|
||
93 | if (limit != null) { |
||
94 | builder.append(" LIMIT ");
|
||
95 | builder.append(limit.toString(formatter)); |
||
96 | } |
||
97 | builder.append(")");
|
||
98 | return builder.toString();
|
||
99 | } |
||
100 | |||
101 | 44198 | jjdelcerro | } |