use of com.servoy.j2db.util.TypePredicate in project servoy-client by Servoy.
the class QuerySelect method getRealColumn.
/**
* Get the real QueryColumn for a column in the query.
* <p>
* For a column from a table it is the column itself
* <br>
* For a column that refers to a derived table this function returns the actual column from the derived table.
* For example, <code>table_1.fld</code> is returned as real column for <code>col</code> in
* <pre>
* SELECT col
* FROM
* (SELECT fld
* FROM table_1) derived_table_name;
* </pre>
*
* @param column
*/
public Optional<QueryColumn> getRealColumn(IQuerySelectValue column) {
if (column == null) {
return Optional.empty();
}
QueryColumn qColumn = column.getColumn();
if (qColumn == null) {
return Optional.empty();
}
BaseQueryTable qTable = qColumn.getTable();
if (qTable.getDataSource() != null) {
// column on regular table
return Optional.of(qColumn);
}
// find real column in derived table
return AbstractBaseQuery.<DerivedTable>searchOne(this, new TypePredicate<>(DerivedTable.class, derivedTable -> derivedTable.getTable() == qTable)).map(DerivedTable::getQuery).map(QuerySelect::getColumns).flatMap(derivedColumns -> derivedColumns.stream().filter(Objects::nonNull).filter(dtcol -> qColumn.getName().equals(dtcol.getAlias()) || (dtcol.getColumn() != null && qColumn.getName().equals(dtcol.getColumn().getName()))).map(IQuerySelectValue::getColumn).filter(Objects::nonNull).findAny()).flatMap(// recursive for nested derived tables
this::getRealColumn);
}
use of com.servoy.j2db.util.TypePredicate in project servoy-client by Servoy.
the class AbstractBaseQuery method getInvalidRangeConditions.
public static List<String> getInvalidRangeConditions(ISQLCondition condition) {
List<String> invalidRangeConditions = new ArrayList<>();
if (condition != null) {
List<CompareCondition> betweenConditions = search(condition, new TypePredicate<>(CompareCondition.class, o -> o.getOperator() == IBaseSQLCondition.BETWEEN_OPERATOR));
IQuerySelectValue[] ccKey;
Object ccOperand2;
for (CompareCondition cc : betweenConditions) {
ccKey = cc.getKeys();
ccOperand2 = cc.getOperand2();
if (// be safe
ccKey != null && ccKey.length > 0 && ccOperand2 != null && ccOperand2 instanceof Object[]) {
String columnName = ccKey[0].getColumn().getName();
Object[] values = (Object[]) ccOperand2;
if (values.length > 1 && values[0] instanceof Comparable && values[1] instanceof Comparable && ((Comparable<Object>) values[0]).compareTo(values[1]) > 0) {
invalidRangeConditions.add(columnName);
}
}
}
}
return invalidRangeConditions;
}
Aggregations