Search in sources :

Example 1 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class PartNode method getAggregatesFromRenderer.

private void getAggregatesFromRenderer(final List<AggregateVariable> aggregates, final Form f, DataRenderer a_renderer) throws RepositoryException {
    Map<IPersist, IDisplay> allFields = a_renderer.getFieldComponents();
    Iterator<IDisplay> it = allFields.values().iterator();
    while (it.hasNext()) {
        IDisplay display = it.next();
        if (display instanceof IDisplayData) {
            String dataProviderID = ((IDisplayData) display).getDataProviderID();
            if (dataProviderID != null) {
                IDataProvider dp = application.getFlattenedSolution().getDataproviderLookup(application.getFoundSetManager(), f).getDataProvider(dataProviderID);
                if (dp instanceof AggregateVariable) {
                    if (!aggregates.contains(dp))
                        aggregates.add((AggregateVariable) dp);
                }
            } else if (display instanceof IDisplayTagText) {
                String tagText = ((IDisplayTagText) display).getTagText();
                Text.processTags(tagText, new ITagResolver() {

                    public String getStringValue(String name) {
                        try {
                            IDataProvider dp = application.getFlattenedSolution().getDataproviderLookup(application.getFoundSetManager(), f).getDataProvider(name);
                            if (dp instanceof AggregateVariable) {
                                if (!aggregates.contains(dp))
                                    aggregates.add((AggregateVariable) dp);
                            }
                        } catch (Exception e) {
                            Debug.error(e);
                        }
                        return null;
                    }
                });
            }
        }
    }
}
Also used : IPersist(com.servoy.j2db.persistence.IPersist) ITagResolver(com.servoy.base.util.ITagResolver) IDisplay(com.servoy.j2db.dataprocessing.IDisplay) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) IDataProvider(com.servoy.j2db.persistence.IDataProvider) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) IDisplayTagText(com.servoy.j2db.ui.IDisplayTagText) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 2 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class FoundSet method alldataproviders.

/**
 * Get all dataproviders of the foundset.
 *
 * @sample
 * var dataprovidersNames = %%prefix%%alldataproviders;
 * application.output("This foundset has " + dataprovidersNames.length + " data providers.")
 * for (var i=0; i<dataprovidersNames.length; i++)
 * 	application.output(dataprovidersNames[i]);
 *
 * @special
 */
@JSReadonlyProperty
public String[] alldataproviders() {
    List<String> al = new ArrayList<String>();
    Table table = (Table) getTable();
    if (table != null) {
        try {
            Iterator<Column> columnsIt = table.getColumnsSortedByName();
            while (columnsIt.hasNext()) {
                IColumn c = columnsIt.next();
                al.add(c.getDataProviderID());
            }
            Iterator<AggregateVariable> aggIt = fsm.getApplication().getFlattenedSolution().getAggregateVariables(table, true);
            while (aggIt.hasNext()) {
                AggregateVariable av = aggIt.next();
                al.add(av.getDataProviderID());
            }
            Iterator<ScriptCalculation> scriptIt = fsm.getApplication().getFlattenedSolution().getScriptCalculations(table, true);
            while (scriptIt.hasNext()) {
                ScriptCalculation sc = scriptIt.next();
                if (al.contains(sc.getDataProviderID()))
                    al.remove(sc.getDataProviderID());
                al.add(sc.getDataProviderID());
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
    return al.toArray(new String[al.size()]);
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty)

Example 3 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class SQLGenerator method addSorts.

public void addSorts(QuerySelect sqlSelect, BaseQueryTable selectTable, IGlobalValueEntry provider, ITable table, List<SortColumn> orderByFields, boolean includeRelated, boolean permanentJoins) throws RepositoryException {
    List<Column> unusedRowidentColumns = new ArrayList<Column>(table.getRowIdentColumns());
    for (int i = 0; orderByFields != null && i < orderByFields.size(); i++) {
        SortColumn sc = orderByFields.get(i);
        // can be column or aggregate
        IColumn column = sc.getColumn();
        if (column.getDataProviderType() == MEDIA && (column.getFlags() & (IDENT_COLUMNS | UUID_COLUMN)) == 0) {
            // skip cannot sort blob columns
            continue;
        }
        SortOptions sortOptions = application.getFoundSetManager().getSortOptions(sc.getColumn());
        Relation[] relations = sc.getRelations();
        // compare on server objects, relation.foreignServerName may be different in case of duplicates
        boolean doRelatedJoin = (includeRelated && relations != null);
        if (doRelatedJoin) {
            FlattenedSolution fs = application.getFlattenedSolution();
            for (Relation relation : relations) {
                if (relation.isMultiServer() && !fs.getTable(relation.getForeignDataSource()).getServerName().equals(table.getServerName())) {
                    doRelatedJoin = false;
                    break;
                }
            }
        }
        if (doRelatedJoin) // related sort, cannot join across multiple servers
        {
            BaseQueryTable primaryQtable = selectTable;
            BaseQueryTable foreignQtable = null;
            for (Relation relation : relations) {
                // join must be re-created as it is possible to have globals involved;
                // first remove, then create it
                ISQLTableJoin join = (ISQLTableJoin) sqlSelect.getJoin(primaryQtable, relation.getName());
                if (join != null)
                    sqlSelect.getJoins().remove(join);
                if (join == null) {
                    ITable foreignTable = application.getFlattenedSolution().getTable(relation.getForeignDataSource());
                    foreignQtable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
                } else {
                    foreignQtable = join.getForeignTable();
                }
                sqlSelect.addJoin(createJoin(application.getFlattenedSolution(), relation, primaryQtable, foreignQtable, permanentJoins, provider));
                primaryQtable = foreignQtable;
            }
            IQuerySelectValue queryColumn;
            if (column instanceof Column) {
                queryColumn = ((Column) column).queryColumn(foreignQtable);
                unusedRowidentColumns.remove(column);
            } else if (column instanceof AggregateVariable) {
                AggregateVariable aggregate = (AggregateVariable) column;
                queryColumn = new QueryAggregate(aggregate.getType(), new QueryColumn(foreignQtable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName());
                // there has to be a group-by clause for all selected fields
                List<IQuerySelectValue> columns = sqlSelect.getColumns();
                for (IQuerySelectValue selectVal : columns) {
                    List<IQuerySelectValue> groupBy = sqlSelect.getGroupBy();
                    if (selectVal instanceof QueryColumn && (groupBy == null || !groupBy.contains(selectVal))) {
                        sqlSelect.addGroupBy(selectVal);
                    }
                }
                // if the aggregate has not been selected yet, add it and skip it in the result
                QueryAggregate skippedAggregate = new QueryAggregate(aggregate.getType(), QueryAggregate.ALL, new QueryColumn(foreignQtable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName(), null, true);
                if (!columns.contains(skippedAggregate)) {
                    sqlSelect.addColumn(skippedAggregate);
                }
            } else {
                // $NON-NLS-1$
                Debug.log("Skipping sort on unexpected related column type " + column.getClass());
                continue;
            }
            sqlSelect.addSort(new QuerySort(queryColumn, sc.getSortOrder() == ASCENDING, sortOptions));
        } else {
            // make sure an invalid sort is not possible
            if (column instanceof Column && column.getTable().getName().equals(table.getName())) {
                sqlSelect.addSort(new QuerySort(((Column) column).queryColumn(selectTable), sc.getSortOrder() == ASCENDING, sortOptions));
                unusedRowidentColumns.remove(column);
            } else {
                // $NON-NLS-1$ //$NON-NLS-2$
                Debug.log("Skipping sort on unrelated column " + column.getName() + '.' + column.getTable().getName() + " for table " + table.getName());
            }
        }
    }
    // Make sure pk is part of the sort, in case of non-unique sort columns, the sorted result may not be the same in each fetch
    if (enforcePkInSort) {
        for (Column column : unusedRowidentColumns) {
            SortOptions sortOptions = application.getFoundSetManager().getSortOptions(column);
            sqlSelect.addSort(new QuerySort(column.queryColumn(selectTable), true, sortOptions));
        }
    }
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) Relation(com.servoy.j2db.persistence.Relation) IRelation(com.servoy.j2db.persistence.IRelation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) QuerySort(com.servoy.j2db.query.QuerySort) IQuerySort(com.servoy.j2db.query.IQuerySort) ITable(com.servoy.j2db.persistence.ITable) List(java.util.List) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) Collectors.toList(java.util.stream.Collectors.toList) SortOptions(com.servoy.j2db.query.SortOptions) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 4 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class SQLGenerator method createAggregates.

private void createAggregates(SQLSheet sheet, QueryTable queryTable) throws RepositoryException {
    Table table = sheet.getTable();
    Iterator<AggregateVariable> it = application.getFlattenedSolution().getAggregateVariables(table, false);
    while (it.hasNext()) {
        AggregateVariable aggregate = it.next();
        QuerySelect sql = new QuerySelect(queryTable);
        sql.addColumn(new QueryAggregate(aggregate.getType(), new QueryColumn(queryTable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName()));
        sheet.addAggregate(aggregate.getDataProviderID(), aggregate.getDataProviderIDToAggregate(), sql);
    }
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 5 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class SQLGenerator method createConditionFromFindState.

private ISQLCondition createConditionFromFindState(FindState s, QuerySelect sqlSelect, IGlobalValueEntry provider, List<IQuerySelectValue> pkQueryColumns) throws RepositoryException {
    ISQLCondition and = null;
    List<RelatedFindState> relatedFindStates = s.createFindStateJoins(sqlSelect, Collections.<IRelation>emptyList(), sqlSelect.getTable(), provider);
    for (int i = 0; relatedFindStates != null && i < relatedFindStates.size(); i++) {
        RelatedFindState rfs = relatedFindStates.get(i);
        FindState state = rfs.getFindState();
        BaseQueryTable columnTable = rfs.getPrimaryTable();
        SQLSheet sheet = state.getParentFoundSet().getSQLSheet();
        Table table = sheet.getTable();
        Iterator<Map.Entry<String, Object>> it = state.getColumnData().entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> elem = it.next();
            final String dataProviderID = elem.getKey();
            Object raw = elem.getValue();
            if (raw == null)
                continue;
            int dataProviderType = -1;
            ConverterInfo columnConverterInfo = null;
            IColumnConverter columnConverter = null;
            IQuerySelectValue qCol = null;
            IColumn c = table.getColumn(dataProviderID);
            if (c != null) {
                dataProviderType = c.getDataProviderType();
                columnConverterInfo = sheet.getColumnConverterInfo(dataProviderID);
                if (columnConverterInfo != null) {
                    columnConverter = application.getFoundSetManager().getColumnConverterManager().getConverter(columnConverterInfo.converterName);
                    if (columnConverter instanceof ITypedColumnConverter) {
                        int convType = ((ITypedColumnConverter) columnConverter).getToObjectType(columnConverterInfo.props);
                        if (convType != Integer.MAX_VALUE) {
                            dataProviderType = mapToDefaultType(convType);
                        }
                    }
                }
                // a column
                qCol = ((Column) c).queryColumn(columnTable);
            } else {
                // not a column, check for aggregates
                Iterator<AggregateVariable> aggregateVariables = application.getFlattenedSolution().getAggregateVariables(sheet.getTable(), false);
                while (c == null && aggregateVariables.hasNext()) {
                    AggregateVariable agg = aggregateVariables.next();
                    if (dataProviderID.equals(agg.getDataProviderID())) {
                        // found aggregate
                        c = agg;
                    }
                }
                if (c != null) {
                    dataProviderType = c.getDataProviderType();
                    Map<String, QuerySelect> aggregates = sheet.getAggregates();
                    if (aggregates != null) {
                        QuerySelect aggregateSelect = aggregates.get(dataProviderID);
                        if (aggregateSelect != null) {
                            qCol = ((List<IQuerySelectValue>) AbstractBaseQuery.relinkTable(aggregateSelect.getTable(), columnTable, aggregateSelect.getColumnsClone())).get(0);
                        }
                    }
                }
            }
            if (qCol == null) {
                // not a column and not an aggregate
                // $NON-NLS-1$ //$NON-NLS-2$
                Debug.log("Ignoring search on unknown/unsupported data provider '" + dataProviderID + "'");
                continue;
            }
            ParsedFormat format = state.getFormat(dataProviderID);
            String formatString = null;
            if (format != null) {
                formatString = format.getEditFormat();
                if (formatString == null) {
                    formatString = format.getDisplayFormat();
                }
            }
            if (Utils.stringIsEmpty(formatString)) {
                formatString = TagResolver.getDefaultFormatForType(application, dataProviderType);
            }
            ISQLCondition or = null;
            if (raw.getClass().isArray()) {
                int length = Array.getLength(raw);
                Object[] elements = new Object[length];
                for (int e = 0; e < length; e++) {
                    Object obj = Array.get(raw, e);
                    if (obj instanceof Wrapper) {
                        obj = ((Wrapper) obj).unwrap();
                    }
                    // Have to use getAsRightType twice here, once to parse using format (getAsType(dataProviderType, formatString))
                    // and once to convert for query (getAsType(c.getDataProviderType(), null))
                    Object converted = convertFromObject(application, columnConverter, columnConverterInfo, dataProviderID, c.getDataProviderType(), Column.getAsRightType(dataProviderType, c.getFlags(), obj, formatString, c.getLength(), null, false, false), false);
                    elements[e] = Column.getAsRightType(c.getDataProviderType(), c.getFlags(), converted, null, c.getLength(), null, false, false);
                }
                // where qCol in (e1, e2, ..., en)
                or = new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, new IQuerySelectValue[] { qCol }, new Object[][] { elements }, true);
            } else {
                final IColumnConverter fColumnConverter = columnConverter;
                final ConverterInfo fColumnConverterInfo = columnConverterInfo;
                final int fDataProviderType = c.getDataProviderType();
                or = (ISQLCondition) BaseSQLGenerator.parseFindExpression(QueryFactory.INSTANCE, raw, qCol, columnTable, dataProviderType, formatString, c, rfs.getRelations().size() > 0 && relatedNullSearchAddPkCondition(), new IValueConverter() {

                    @Override
                    public Object convertFromObject(Object value) {
                        return SQLGenerator.convertFromObject(application, fColumnConverter, fColumnConverterInfo, dataProviderID, fDataProviderType, value, false);
                    }
                }, new ITypeConverter() {

                    @Override
                    public Object getAsRightType(int type, int flags, Object obj, int l, boolean throwOnFail) {
                        return Column.getAsRightType(type, flags, obj, l, throwOnFail, false);
                    }

                    @Override
                    public Object getAsRightType(int type, int flags, Object obj, String format, int l, boolean throwOnFail) {
                        return Column.getAsRightType(type, flags, obj, format, l, null, throwOnFail, false);
                    }
                }, table.getRowIdentColumns().get(0), Debug.LOGGER);
            }
            if (or != null) {
                ISQLCondition condition;
                if (c instanceof AggregateVariable) {
                    condition = createExistsCondition(application.getFlattenedSolution(), sqlSelect, or, rfs.getRelations(), columnTable, provider, pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]));
                } else {
                    condition = or;
                }
                and = AndCondition.and(and, condition);
            }
        }
    }
    return and;
}
Also used : ConverterInfo(com.servoy.j2db.dataprocessing.SQLSheet.ConverterInfo) SetCondition(com.servoy.j2db.query.SetCondition) RelatedFindState(com.servoy.j2db.dataprocessing.FindState.RelatedFindState) RelatedFindState(com.servoy.j2db.dataprocessing.FindState.RelatedFindState) IValueConverter(com.servoy.base.dataprocessing.IValueConverter) ParsedFormat(com.servoy.j2db.util.FormatParser.ParsedFormat) Wrapper(org.mozilla.javascript.Wrapper) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) ITypeConverter(com.servoy.base.dataprocessing.ITypeConverter) ISQLCondition(com.servoy.j2db.query.ISQLCondition) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) IColumn(com.servoy.j2db.persistence.IColumn) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Aggregations

AggregateVariable (com.servoy.j2db.persistence.AggregateVariable)8 BaseQueryTable (com.servoy.base.query.BaseQueryTable)5 ITable (com.servoy.j2db.persistence.ITable)5 Column (com.servoy.j2db.persistence.Column)4 IColumn (com.servoy.j2db.persistence.IColumn)4 QueryColumn (com.servoy.j2db.query.QueryColumn)4 QueryTable (com.servoy.j2db.query.QueryTable)4 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)3 Table (com.servoy.j2db.persistence.Table)3 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)3 QueryAggregate (com.servoy.j2db.query.QueryAggregate)3 QuerySelect (com.servoy.j2db.query.QuerySelect)3 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)2 IPersist (com.servoy.j2db.persistence.IPersist)2 Relation (com.servoy.j2db.persistence.Relation)2 IQuerySort (com.servoy.j2db.query.IQuerySort)2 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)2 QuerySort (com.servoy.j2db.query.QuerySort)2 SafeArrayList (com.servoy.j2db.util.SafeArrayList)2