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