Revision 46517 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/JDBCHelperBase.java
JDBCHelperBase.java | ||
---|---|---|
27 | 27 |
import java.sql.Blob; |
28 | 28 |
import java.sql.Clob; |
29 | 29 |
import java.sql.ResultSet; |
30 |
import java.sql.ResultSetMetaData; |
|
31 | 30 |
import java.util.ArrayList; |
32 |
import java.util.Collections; |
|
33 |
import java.util.HashMap; |
|
34 | 31 |
import java.util.HashSet; |
35 | 32 |
import java.util.List; |
36 |
import java.util.Map;
|
|
33 |
import java.util.ListIterator;
|
|
37 | 34 |
import java.util.function.Predicate; |
35 |
import org.apache.commons.collections.CollectionUtils; |
|
38 | 36 |
import org.apache.commons.io.IOUtils; |
39 | 37 |
import org.apache.commons.lang3.ArrayUtils; |
40 | 38 |
import org.apache.commons.lang3.StringUtils; |
41 | 39 |
import org.apache.commons.lang3.mutable.MutableBoolean; |
42 | 40 |
import org.apache.commons.lang3.mutable.MutableObject; |
41 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
|
43 | 42 |
import org.apache.commons.lang3.tuple.Pair; |
44 | 43 |
import org.gvsig.expressionevaluator.Code; |
45 | 44 |
import org.gvsig.expressionevaluator.Expression; |
46 | 45 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
46 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_$CONSTANT; |
|
47 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_$IDENTIFIER; |
|
47 | 48 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
48 | 49 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
49 | 50 |
import org.gvsig.expressionevaluator.Function; |
50 | 51 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
51 | 52 |
import org.gvsig.expressionevaluator.SymbolTable; |
53 |
import org.gvsig.fmap.dal.DALLocator; |
|
54 |
import org.gvsig.fmap.dal.DataManager; |
|
52 | 55 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_EXISTS; |
53 | 56 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREING_VALUE; |
54 | 57 |
import org.gvsig.fmap.dal.DataTypes; |
55 | 58 |
import org.gvsig.fmap.dal.SQLBuilder; |
59 |
import org.gvsig.fmap.dal.SQLBuilder.Column; |
|
60 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
|
56 | 61 |
import org.gvsig.fmap.dal.exception.DataException; |
57 | 62 |
import org.gvsig.fmap.dal.exception.InitializeException; |
58 | 63 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
... | ... | |
78 | 83 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
79 | 84 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
80 | 85 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCCantFetchValueException; |
86 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
|
81 | 87 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
82 | 88 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
83 | 89 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
... | ... | |
86 | 92 |
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler; |
87 | 93 |
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler.ResultSetEntry; |
88 | 94 |
import org.gvsig.fmap.dal.store.jdbc2.impl.ResulSetControlerBase; |
95 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ComputedAttribute; |
|
89 | 96 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.OperationsFactoryBase; |
90 | 97 |
import org.gvsig.fmap.geom.Geometry; |
91 | 98 |
import org.gvsig.fmap.geom.GeometryLocator; |
... | ... | |
94 | 101 |
import org.gvsig.tools.evaluator.Evaluator; |
95 | 102 |
import org.gvsig.tools.exception.BaseException; |
96 | 103 |
import org.gvsig.tools.exception.NotYetImplemented; |
104 |
import org.gvsig.tools.locator.LocatorException; |
|
105 |
import org.gvsig.tools.util.ContainerUtils; |
|
97 | 106 |
import org.gvsig.tools.visitor.FilteredVisitable; |
98 | 107 |
import org.gvsig.tools.visitor.VisitCanceledException; |
99 | 108 |
import org.gvsig.tools.visitor.Visitor; |
100 | 109 |
import org.slf4j.Logger; |
101 | 110 |
import org.slf4j.LoggerFactory; |
102 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_$CONSTANT; |
|
103 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_$IDENTIFIER; |
|
104 |
import org.gvsig.fmap.dal.DataManager; |
|
105 |
import org.gvsig.fmap.dal.SQLBuilder.Column; |
|
106 |
import org.gvsig.fmap.dal.SQLBuilder.OrderByBuilder; |
|
107 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
|
108 |
import org.gvsig.fmap.dal.SQLBuilder.SelectColumnBuilder; |
|
109 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
|
110 |
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ComputedAttribute; |
|
111 |
import org.gvsig.tools.util.ContainerUtils; |
|
112 | 111 |
|
113 | 112 |
@SuppressWarnings("UseSpecificCatch") |
114 | 113 |
public class JDBCHelperBase extends AbstractDisposable implements ResourceConsumer, JDBCHelper { |
... | ... | |
977 | 976 |
usedLeftJoins.add(foreingTable.getName()); |
978 | 977 |
} |
979 | 978 |
//No est? claro si debe a?adirse esta columna o no, OJO quitarlo o ponerlo altera los test de H2 |
980 |
// if ( !(sqlbuilder.select().has_group_by() || sqlbuilder.select().has_aggregate_functions()) ) { |
|
981 |
// sqlbuilder.select().column().name(foreingTable, columnNameForeing); |
|
982 |
// } |
|
979 |
//Si se comentarizan ojo con extra_column_names ya que se habr?n a?adido columnas que no estar?n en la select |
|
980 |
if ( !(sqlbuilder.select().has_group_by() || sqlbuilder.select().has_aggregate_functions()) ) { |
|
981 |
sqlbuilder.select().column().name(foreingTable, columnNameForeing); |
|
982 |
} |
|
983 | 983 |
} |
984 | 984 |
} |
985 | 985 |
|
... | ... | |
992 | 992 |
} |
993 | 993 |
|
994 | 994 |
protected void addTableToColumnReferences(SQLBuilder sqlbuilder, FeatureType type) { |
995 |
// List<SelectBuilder> selectBuilders = new ArrayList<>(); |
|
996 |
// selectBuilders.add(sqlbuilder.select()); |
|
997 |
// sqlbuilder.select().accept((ExpressionBuilder.Visitable value) -> { |
|
998 |
// selectBuilders.add((SelectBuilder) value); |
|
999 |
// |
|
1000 |
// }, (ExpressionBuilder.Visitable visitable) -> (visitable instanceof SelectBuilder)); |
|
1001 |
// for (SelectBuilder selectBuilder : selectBuilders) { |
|
1002 |
// addTableToColumnReferencesInSingleSelect(sqlbuilder, selectBuilder); |
|
1003 |
// } |
|
995 |
SelectBuilder select = sqlbuilder.select(); |
|
996 |
List<Pair<SQLBuilder.TableNameBuilder, FeatureType>> tables = new ArrayList<>(); |
|
997 |
collectTablesFromSelect(select, tables, type); |
|
1004 | 998 |
|
999 |
addTableToColumnReferencesInSingleSelect( |
|
1000 |
sqlbuilder, |
|
1001 |
select, |
|
1002 |
tables |
|
1003 |
); |
|
1004 |
|
|
1005 |
} |
|
1005 | 1006 |
|
1006 |
addTableToColumnReferencesInSingleSelect(sqlbuilder, sqlbuilder.select(), new ArrayList<>()); |
|
1007 |
private void collectTablesFromSelect(SelectBuilder select, List<Pair<SQLBuilder.TableNameBuilder, FeatureType>> tables, FeatureType type) throws LocatorException { |
|
1008 |
DataManager dataManager = DALLocator.getDataManager(); |
|
1007 | 1009 |
|
1010 |
List<SQLBuilder.JoinBuilder> joins = select.from().getJoins(); |
|
1011 |
if(!CollectionUtils.isEmpty(joins)){ |
|
1012 |
for (SQLBuilder.JoinBuilder join : joins) { |
|
1013 |
SQLBuilder.TableNameBuilder joinTable = join.getTable(); |
|
1014 |
FeatureType featureType = dataManager.getStoresRepository().getFeatureType(joinTable.getName()); |
|
1015 |
tables.add(new ImmutablePair<>(joinTable,featureType)); |
|
1016 |
} |
|
1017 |
} |
|
1018 |
SQLBuilder.TableNameBuilder table = select.from().table(); |
|
1019 |
if(type == null){ |
|
1020 |
type = dataManager.getStoresRepository().getFeatureType(table.getName()); |
|
1021 |
} |
|
1022 |
tables.add(new ImmutablePair<>(table,type)); |
|
1008 | 1023 |
} |
1009 | 1024 |
|
1010 |
protected void addTableToColumnReferencesInSingleSelect(SQLBuilder sqlbuilder, SelectBuilder select, List<Column> parentColumns) {
|
|
1025 |
protected void addTableToColumnReferencesInSingleSelect(SQLBuilder sqlbuilder, SelectBuilder select, List<Pair<SQLBuilder.TableNameBuilder,FeatureType>> outerTables) {
|
|
1011 | 1026 |
|
1012 | 1027 |
final SQLBuilder.TableNameBuilder table = select.from().table(); |
1013 | 1028 |
|
... | ... | |
1016 | 1031 |
List<ExpressionBuilder.Value> variablesToExclude = new ArrayList<>(); |
1017 | 1032 |
|
1018 | 1033 |
select.accept((ExpressionBuilder.Visitable value) -> { |
1019 |
if(value instanceof ExpressionBuilder.Function) { |
|
1020 |
ExpressionBuilder.Function fn = (ExpressionBuilder.Function) value; |
|
1021 |
if(StringUtils.equals(fn.name(), ExpressionBuilder.FUNCTION_GETATTR)){ |
|
1022 |
ExpressionBuilder.Value p1 = fn.parameters().get(0); |
|
1023 |
if(p1 instanceof ExpressionBuilder.Variable){ |
|
1024 |
ExpressionBuilder.Variable v = (ExpressionBuilder.Variable) p1; |
|
1025 |
variablesToExclude.add(v); |
|
1026 |
} |
|
1027 |
ExpressionBuilder.Value p2 = fn.parameters().get(1); |
|
1028 |
if(p2 instanceof ExpressionBuilder.Constant){ |
|
1029 |
ExpressionBuilder.Constant c = (ExpressionBuilder.Constant) p2; |
|
1030 |
variablesToExclude.add(c); |
|
1031 |
} |
|
1032 |
} |
|
1033 |
} else if(value instanceof Column) { |
|
1034 |
if(value instanceof Column) { |
|
1034 | 1035 |
Column c = (Column)value; |
1035 | 1036 |
if(c.table() == null || !c.table().has_name()){ |
1036 | 1037 |
variables.add(c); |
... | ... | |
1038 | 1039 |
|
1039 | 1040 |
} else if(value instanceof ExpressionBuilder.Variable) { |
1040 | 1041 |
variables.add((ExpressionBuilder.Variable) value); |
1041 |
// } else if(value instanceof SelectColumnBuilder) { |
|
1042 |
// SelectColumnBuilder selectColumn = (SelectColumnBuilder)value; |
|
1043 |
// if(StringUtils.isNotBlank(selectColumn.getName()) && selectColumn.getTable().has_name()){ |
|
1044 |
// variablesToExclude.add(sel) |
|
1045 |
// } |
|
1046 | 1042 |
} |
1047 | 1043 |
|
1048 | 1044 |
}, new ExpressionBuilder.VisitorFilter() { |
... | ... | |
1056 | 1052 |
if(select == visitable){ |
1057 | 1053 |
return true; |
1058 | 1054 |
} |
1059 |
// if(visitable instanceof OrderByBuilder) { |
|
1060 |
// return false; |
|
1061 |
// } |
|
1062 | 1055 |
if(visitable instanceof SelectBuilder) { |
1063 |
ArrayList<Column> columns = new ArrayList<>(parentColumns); |
|
1064 |
for (SelectColumnBuilder column : select.getColumns()) { |
|
1065 |
columns.add(sqlbuilder.column( |
|
1066 |
table, |
|
1067 |
column.getName())); |
|
1068 |
} |
|
1069 |
addTableToColumnReferencesInSingleSelect(sqlbuilder, (SelectBuilder) visitable, columns); |
|
1056 |
ArrayList<Pair<SQLBuilder.TableNameBuilder, FeatureType>> tables = new ArrayList<>(outerTables); |
|
1057 |
collectTablesFromSelect(select, tables, null); |
|
1058 |
|
|
1059 |
addTableToColumnReferencesInSingleSelect(sqlbuilder, (SelectBuilder) visitable, tables); |
|
1070 | 1060 |
return false; |
1071 | 1061 |
} |
1072 | 1062 |
return true; |
1073 | 1063 |
} |
1074 | 1064 |
}); |
1075 | 1065 |
|
1076 |
List<String> variableNamesToExclude = new ArrayList<>(); |
|
1077 |
for(SQLBuilder.SelectColumnBuilder column : select.getColumns()){ |
|
1078 |
if(StringUtils.isNotBlank(column.getAlias())){ |
|
1079 |
variableNamesToExclude.add(column.getAlias()); |
|
1080 |
} else { |
|
1081 |
parentColumns.add(sqlbuilder.column( |
|
1082 |
table, |
|
1083 |
column.getName())); |
|
1084 |
} |
|
1085 |
} |
|
1086 |
|
|
1087 | 1066 |
for (ExpressionBuilder.Variable variable : variables) { |
1088 | 1067 |
boolean found = false; |
1089 | 1068 |
for (ExpressionBuilder.Value v : variablesToExclude) { |
... | ... | |
1095 | 1074 |
if(found){ |
1096 | 1075 |
continue; |
1097 | 1076 |
} |
1098 |
// if(variableNamesToExclude.contains(variable.name())){ |
|
1099 |
// continue; |
|
1100 |
// } |
|
1101 |
Column column = getColumn(parentColumns,variable.name()); |
|
1102 |
if(column != null){ |
|
1077 |
|
|
1078 |
SQLBuilder.TableNameBuilder tableName = getTable(outerTables,variable.name()); |
|
1079 |
if(tableName != null){ |
|
1103 | 1080 |
ExpressionBuilder.Variable variable_replacement = sqlbuilder.column( |
1104 |
column.table(),
|
|
1081 |
tableName,
|
|
1105 | 1082 |
variable.name() |
1106 | 1083 |
); |
1107 | 1084 |
value_replacements.add( |
... | ... | |
1113 | 1090 |
} else { |
1114 | 1091 |
ExpressionBuilder.Variable variable_replacement = null; |
1115 | 1092 |
if(variable instanceof SQLBuilder.Column){ |
1116 |
column = (SQLBuilder.Column) variable; |
|
1093 |
Column column = (SQLBuilder.Column) variable;
|
|
1117 | 1094 |
if (column.table() == null || !column.table().has_name()) { |
1118 | 1095 |
variable_replacement = sqlbuilder.column( |
1119 | 1096 |
table, |
... | ... | |
1158 | 1135 |
|
1159 | 1136 |
} |
1160 | 1137 |
|
1161 |
protected Column getColumn(List<Column> columns, String name){ |
|
1162 |
for (int i = columns.size()-1; i >= 0; i--) { |
|
1163 |
if(StringUtils.equalsIgnoreCase(columns.get(i).name(), name)){ |
|
1164 |
return columns.get(i); |
|
1138 |
protected SQLBuilder.TableNameBuilder getTable(List<Pair<SQLBuilder.TableNameBuilder, FeatureType>> outerTables, String columnName) { |
|
1139 |
ListIterator<Pair<SQLBuilder.TableNameBuilder, FeatureType>> it = outerTables.listIterator(outerTables.size()); |
|
1140 |
while (it.hasPrevious()) { |
|
1141 |
Pair<SQLBuilder.TableNameBuilder, FeatureType> pair = it.previous(); |
|
1142 |
FeatureType featureType = pair.getRight(); |
|
1143 |
if(featureType.get(columnName) != null){ |
|
1144 |
return pair.getLeft(); |
|
1165 | 1145 |
} |
1166 | 1146 |
} |
1167 | 1147 |
return null; |
1168 | 1148 |
} |
1169 |
|
|
1170 | 1149 |
|
1171 | 1150 |
@Override |
1172 | 1151 |
public void setTransaction(DataTransactionServices transaction) { |
Also available in: Unified diff