Search in sources :

Example 1 with AndOrCondition

use of com.servoy.j2db.query.AndOrCondition in project servoy-client by Servoy.

the class QBSelect method having.

/**
 * Get the having-part of the query, used to add conditions.
 * The conditions added here are AND-ed.
 * @sample
 * var query = datasources.db.example_data.orders.createSelect();
 * query.groupBy.addPk() // have to group by on pk when using having-conditions in (foundset) pk queries
 * .root.having.add(query.joins.orders_to_order_details.columns.quantity.count.eq(0))
 * foundset.loadRecords(query)
 */
@JSReadonlyProperty
public QBLogicalCondition having() throws RepositoryException {
    if (having == null) {
        ISQLCondition c = getQuery().getHaving();
        if (!(c instanceof AndOrCondition)) {
            getQuery().setHaving(c = AndCondition.and(c, new AndCondition()));
        }
        having = new QBLogicalCondition(this, this, (AndOrCondition) c);
    }
    return having;
}
Also used : AndOrCondition(com.servoy.j2db.query.AndOrCondition) ISQLCondition(com.servoy.j2db.query.ISQLCondition) AndCondition(com.servoy.j2db.query.AndCondition) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty)

Example 2 with AndOrCondition

use of com.servoy.j2db.query.AndOrCondition in project servoy-client by Servoy.

the class RelatedFoundSet method notifyChange_checkForNewRow.

private void notifyChange_checkForNewRow(Row row) {
    // if already in state for new query then don't test anything.
    if (mustQueryForUpdates) {
        return;
    }
    // check if sql where is still the same, if there is search in it do nothing
    AndOrCondition createCondition = creationSqlSelect.getCondition(SQLGenerator.CONDITION_RELATION);
    AndOrCondition condition = getPksAndRecords().getQuerySelectForReading().getCondition(SQLGenerator.CONDITION_RELATION);
    if (// does not include placeholder values in comparison
    (createCondition == null && condition == null) || createCondition != null && createCondition.equals(condition)) {
        try {
            boolean doCheck = true;
            Relation relation = fsm.getApplication().getFlattenedSolution().getRelation(relationName);
            if (relation == null) {
                // this may happen when the relation was removed using solution model
                return;
            }
            // check the foreign key if they match, if so it will fall in this foundset
            Placeholder ph = creationSqlSelect.getPlaceholder(SQLGenerator.createRelationKeyPlaceholderKey(creationSqlSelect.getTable(), relation.getName()));
            if (ph == null || !ph.isSet()) {
                Column[] cols = relation.getForeignColumns(fsm.getApplication().getFlattenedSolution());
                StringBuilder columns = new StringBuilder();
                columns.append("(");
                if (cols != null && cols.length > 0) {
                    for (Column col : cols) {
                        columns.append(col.getName());
                        columns.append(",");
                    }
                    columns.setLength(columns.length() - 1);
                }
                columns.append(")");
                // $NON-NLS-1$
                Debug.error("RelatedFoundset check for relation:" + relationName + " for a new row, creation args " + columns + "not found!!");
                // how can this happen??
                return;
            }
            // foreignData is a matrix as wide as the relation keys and 1 deep
            // Really get only the where params not all of them (like table filter)
            Object[][] foreignData = (Object[][]) ph.getValue();
            Column[] cols = relation.getForeignColumns(fsm.getApplication().getFlattenedSolution());
            if (foreignData.length != cols.length) {
                StringBuilder columns = new StringBuilder();
                columns.append("(");
                if (cols.length > 0) {
                    for (Column col : cols) {
                        columns.append(col.getName());
                        columns.append(",");
                    }
                    columns.setLength(columns.length() - 1);
                }
                columns.append(")");
                StringBuilder data = new StringBuilder();
                data.append("(");
                if (foreignData.length > 0) {
                    for (Object[] d : foreignData) {
                        data.append("[");
                        if (d.length > 0) {
                            for (Object object : d) {
                                data.append(object);
                                data.append(",");
                            }
                            data.setLength(data.length() - 1);
                        }
                        data.append("]");
                    }
                    data.setLength(data.length() - 1);
                }
                data.append(")");
                Debug.error(// $NON-NLS-1$
                "RelatedFoundset check for relation:" + relationName + " for new row, creation args " + columns + " and relation args " + data + "  are not the same!!");
                // how can this happen??
                return;
            }
            int[] operators = relation.getOperators();
            for (int i = 0; i < cols.length; i++) {
                // compare unconverted values
                Object obj = row.getRawValue(cols[i].getDataProviderID());
                if (!checkForeignKeyValue(obj, foreignData[i][0], operators[i])) {
                    doCheck = false;
                    break;
                }
            }
            if (doCheck) {
                invalidateFoundset();
                getFoundSetManager().getEditRecordList().fireEvents();
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) AndOrCondition(com.servoy.j2db.query.AndOrCondition) ServoyException(com.servoy.j2db.util.ServoyException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation) Column(com.servoy.j2db.persistence.Column)

Aggregations

AndOrCondition (com.servoy.j2db.query.AndOrCondition)2 Column (com.servoy.j2db.persistence.Column)1 Relation (com.servoy.j2db.persistence.Relation)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 AndCondition (com.servoy.j2db.query.AndCondition)1 ISQLCondition (com.servoy.j2db.query.ISQLCondition)1 Placeholder (com.servoy.j2db.query.Placeholder)1 JSReadonlyProperty (com.servoy.j2db.scripting.annotations.JSReadonlyProperty)1 ServoyException (com.servoy.j2db.util.ServoyException)1 RemoteException (java.rmi.RemoteException)1