Search in sources :

Example 16 with Placeholder

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

the class RelatedFoundSet method notifyChange_checkForUpdate.

private boolean notifyChange_checkForUpdate(Row r, boolean updateTest) {
    // if already in state for new query then don't test anything.
    if (mustQueryForUpdates) {
        return false;
    }
    boolean retval = true;
    String pkHash = r.getPKHashKey();
    Relation relation = fsm.getApplication().getFlattenedSolution().getRelation(relationName);
    if (relation == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("Relation not found for related foundset: " + relationName);
    }
    Placeholder whereArgsPlaceholder = creationSqlSelect.getPlaceholder(SQLGenerator.createRelationKeyPlaceholderKey(creationSqlSelect.getTable(), relation.getName()));
    if (whereArgsPlaceholder == null || !whereArgsPlaceholder.isSet()) {
        // $NON-NLS-1$
        Debug.error("creationSqlSelect = " + creationSqlSelect);
        // $NON-NLS-1$
        Debug.error("PlaceholderKey = " + SQLGenerator.createRelationKeyPlaceholderKey(creationSqlSelect.getTable(), relation.getName()));
        // $NON-NLS-1$
        Debug.error("RelatedFoundSet = " + this);
        // $NON-NLS-1$
        Debug.error("Row = " + r);
        // $NON-NLS-1$
        Debug.error("whereArgsPlaceholder = " + whereArgsPlaceholder);
        // log on server as well
        try {
            fsm.getApplication().getDataServer().logMessage(// $NON-NLS-1$ //$NON-NLS-2$
            "creationSqlSelect = " + creationSqlSelect + '\n' + "PlaceholderKey = " + SQLGenerator.createRelationKeyPlaceholderKey(creationSqlSelect.getTable(), relation.getName()) + '\n' + "RelatedFoundSet = " + this + // $NON-NLS-1$
            '\n' + "Row = " + r + '\n' + "whereArgsPlaceholder = " + // $NON-NLS-1$//$NON-NLS-2$
            whereArgsPlaceholder);
        } catch (Exception e) {
            // $NON-NLS-1$
            Debug.error("Failed to log on server", e);
        }
        // $NON-NLS-1$
        Debug.error("Relation values not set for related foundset " + relation.getName());
        return false;
    }
    // createWhereArgs 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[][] createWhereArgs = (Object[][]) whereArgsPlaceholder.getValue();
    SQLSheet.SQLDescription sqlDesc = sheet.getRelatedSQLDescription(relationName);
    // foreign key names
    List<String> req = sqlDesc.getRequiredDataProviderIDs();
    if (createWhereArgs.length != req.size()) {
        // $NON-NLS-1$
        Debug.error("RelatedFoundset check for updated row, creation args and relation args are not the same!!");
        // how can this happen??
        return false;
    }
    IDataSet pks = getPksAndRecords().getPks();
    if (pks != null) {
        int[] operators = relation.getOperators();
        for (int i = 0; i < req.size(); i++) {
            String foreignKeyName = req.get(i);
            // createWhereArgs is a matrix as wide as the relation keys and 1 deep, compare unconverted values
            boolean remove = !checkForeignKeyValue(r.getRawValue(foreignKeyName), createWhereArgs[i][0], operators[i]);
            if (remove) {
                retval = false;
                // row is not longer part of this related foundset, so remove in myself
                for (int ii = pks.getRowCount() - 1; ii >= 0; ii--) {
                    Object[] pk = pks.getRow(ii);
                    if (RowManager.createPKHashKey(pk).equals(pkHash)) {
                        // does fireIntervalRemoved(this,ii,ii);
                        removeRecordInternal(ii);
                        break;
                    }
                }
                break;
            }
        }
        if (retval && updateTest) {
            for (int ii = pks.getRowCount() - 1; ii >= 0; ii--) {
                Object[] pk = pks.getRow(ii);
                if (RowManager.createPKHashKey(pk).equals(pkHash)) {
                    fireAggregateChangeWithEvents(getRecord(ii));
                    retval = false;
                    break;
                }
            }
        }
    }
    return retval;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) ServoyException(com.servoy.j2db.util.ServoyException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation)

Example 17 with Placeholder

use of com.servoy.j2db.query.Placeholder 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)

Example 18 with Placeholder

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

the class EditRecordList method mergeInsertStatements.

/**
 * Merge the src statement into the target statement.
 */
private void mergeInsertStatements(ISQLStatement targetStatement, ISQLStatement srcStatement) {
    QueryInsert sqlUpdateTarget = (QueryInsert) targetStatement.getUpdate();
    Placeholder placeholderTarget = (Placeholder) sqlUpdateTarget.getValues();
    // Placeholder placeholderTarget = sqlUpdateTarget.getPlaceholder(new TablePlaceholderKey(sqlUpdateTarget.getTable(), SQLGenerator.PLACEHOLDER_INSERT_KEY));
    Object[][] valTarget = (Object[][]) placeholderTarget.getValue();
    QueryInsert sqlUpdateSrc = (QueryInsert) srcStatement.getUpdate();
    Placeholder placeholderSrc = (Placeholder) sqlUpdateSrc.getValues();
    // Placeholder placeholderSrc = sqlUpdateSrc.getPlaceholder(new TablePlaceholderKey(sqlUpdateSrc.getTable(), SQLGenerator.PLACEHOLDER_INSERT_KEY));
    Object[][] valSrc = (Object[][]) placeholderSrc.getValue();
    // Copy insert values into the target insert placeholder
    for (int i = 0; i < valTarget.length; i++) {
        valTarget[i] = Utils.arrayJoin(valSrc[i], valTarget[i]);
    }
    // Copy the pks into the target pks
    IDataSet targetpKs = targetStatement.getPKs();
    IDataSet srcpKs = srcStatement.getPKs();
    for (int row = 0; row < srcpKs.getRowCount(); row++) {
        targetpKs.addRow(srcpKs.getRow(row));
    }
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) NativeObject(org.mozilla.javascript.NativeObject) QueryInsert(com.servoy.j2db.query.QueryInsert)

Example 19 with Placeholder

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

the class JSDatabaseManager method validateQueryArguments.

private boolean validateQueryArguments(ISQLQuery select) {
    if (select != null) {
        if (select instanceof QuerySelect && ((QuerySelect) select).getColumns() == null) {
            application.reportJSError("Custom query: " + select + " not executed because no columns are specified to be selected", null);
            return false;
        }
        final List<Placeholder> placeHolders = new ArrayList<Placeholder>();
        AbstractBaseQuery.acceptVisitor(select, new IVisitor() {

            public Object visit(Object o) {
                if (o instanceof Placeholder) {
                    placeHolders.add((Placeholder) o);
                }
                return o;
            }
        });
        for (Placeholder placeholder : placeHolders) {
            if (!placeholder.isSet()) {
                application.reportJSError(// $NON-NLS-1$
                "Custom query: " + select + " not executed because not all arguments have been set: " + placeholder.getKey(), // $NON-NLS-1$
                null);
                return false;
            }
            Object value = placeholder.getValue();
            if (value instanceof DbIdentValue && ((DbIdentValue) value).getPkValue() == null) {
                application.reportJSError(// $NON-NLS-1$
                "Custom query: " + select + " not executed because the arguments have a database ident value that is null, from a not yet saved record", // $NON-NLS-1$
                null);
                return false;
            }
            if (value instanceof java.util.Date && !(value instanceof Timestamp) && !(value instanceof Time)) {
                placeholder.setValue(new Timestamp(((java.util.Date) value).getTime()));
            }
        }
    }
    return true;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) IVisitor(com.servoy.j2db.util.visitor.IVisitor) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) ArrayList(java.util.ArrayList) NativeObject(org.mozilla.javascript.NativeObject) Time(java.sql.Time) QuerySelect(com.servoy.j2db.query.QuerySelect) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

Placeholder (com.servoy.j2db.query.Placeholder)19 Column (com.servoy.j2db.persistence.Column)11 TablePlaceholderKey (com.servoy.j2db.query.TablePlaceholderKey)9 RepositoryException (com.servoy.j2db.persistence.RepositoryException)8 QueryColumn (com.servoy.j2db.query.QueryColumn)8 IColumn (com.servoy.j2db.persistence.IColumn)7 Relation (com.servoy.j2db.persistence.Relation)7 QuerySelect (com.servoy.j2db.query.QuerySelect)7 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)6 ArrayList (java.util.ArrayList)6 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)5 SetCondition (com.servoy.j2db.query.SetCondition)5 ServoyException (com.servoy.j2db.util.ServoyException)5 BaseQueryTable (com.servoy.base.query.BaseQueryTable)3 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)3 ITable (com.servoy.j2db.persistence.ITable)3 Table (com.servoy.j2db.persistence.Table)3 CompareCondition (com.servoy.j2db.query.CompareCondition)3 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)3 IBaseSQLCondition (com.servoy.base.query.IBaseSQLCondition)2