Search in sources :

Example 1 with TypePredicate

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);
}
Also used : Arrays(java.util.Arrays) ReplacedObject(com.servoy.j2db.util.serialize.ReplacedObject) ObjectCountVisitor(com.servoy.j2db.util.visitor.ObjectCountVisitor) Iterator(java.util.Iterator) IVisitor(com.servoy.j2db.util.visitor.IVisitor) HashMap(java.util.HashMap) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ArrayList(java.util.ArrayList) QueryFunctionType(com.servoy.j2db.query.QueryFunction.QueryFunctionType) Objects(java.util.Objects) List(java.util.List) Map(java.util.Map) Entry(java.util.Map.Entry) Optional(java.util.Optional) TypePredicate(com.servoy.j2db.util.TypePredicate) BaseQueryTable(com.servoy.base.query.BaseQueryTable) Objects(java.util.Objects)

Example 2 with TypePredicate

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;
}
Also used : ReplacedObject(com.servoy.j2db.util.serialize.ReplacedObject) IWriteReplace(com.servoy.j2db.util.serialize.IWriteReplace) Iterator(java.util.Iterator) Pair(com.servoy.j2db.util.Pair) Predicate(java.util.function.Predicate) DeepCloneVisitor(com.servoy.j2db.util.visitor.DeepCloneVisitor) IVisitor(com.servoy.j2db.util.visitor.IVisitor) HashMap(java.util.HashMap) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ReplaceVisitor(com.servoy.j2db.util.visitor.ReplaceVisitor) ArrayList(java.util.ArrayList) VisitorResult(com.servoy.j2db.util.visitor.IVisitor.VisitorResult) List(java.util.List) IVisitable(com.servoy.j2db.util.visitor.IVisitable) SearchVisitor(com.servoy.j2db.util.visitor.SearchVisitor) Map(java.util.Map) Optional(java.util.Optional) IBaseSQLCondition(com.servoy.base.query.IBaseSQLCondition) TypePredicate(com.servoy.j2db.util.TypePredicate) ArrayList(java.util.ArrayList) ReplacedObject(com.servoy.j2db.util.serialize.ReplacedObject)

Aggregations

BaseQueryTable (com.servoy.base.query.BaseQueryTable)2 TypePredicate (com.servoy.j2db.util.TypePredicate)2 ReplacedObject (com.servoy.j2db.util.serialize.ReplacedObject)2 IVisitor (com.servoy.j2db.util.visitor.IVisitor)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 IBaseSQLCondition (com.servoy.base.query.IBaseSQLCondition)1 QueryFunctionType (com.servoy.j2db.query.QueryFunction.QueryFunctionType)1 Pair (com.servoy.j2db.util.Pair)1 IWriteReplace (com.servoy.j2db.util.serialize.IWriteReplace)1 DeepCloneVisitor (com.servoy.j2db.util.visitor.DeepCloneVisitor)1 IVisitable (com.servoy.j2db.util.visitor.IVisitable)1 VisitorResult (com.servoy.j2db.util.visitor.IVisitor.VisitorResult)1 ObjectCountVisitor (com.servoy.j2db.util.visitor.ObjectCountVisitor)1 ReplaceVisitor (com.servoy.j2db.util.visitor.ReplaceVisitor)1 SearchVisitor (com.servoy.j2db.util.visitor.SearchVisitor)1