Search in sources :

Example 1 with PackVisitor

use of com.servoy.j2db.util.visitor.PackVisitor in project servoy-client by Servoy.

the class RelatedFoundSet method createRelatedFoundSets.

/**
 * Create multiple related foundsets in one call to the data server.
 *
 * @param factory
 * @param app
 * @param parents same length as whereArsgLists
 * @param relation
 * @param sheet
 * @param whereArsgLists
 * @param defaultSortColumns
 * @return
 * @throws ServoyException
 */
public static IFoundSetInternal[] createRelatedFoundSets(IFoundSetFactory factory, IFoundSetManagerInternal app, IRecordInternal[] parents, Relation relation, SQLSheet sheet, Object[][] whereArsgLists, List<SortColumn> defaultSortColumns) throws ServoyException {
    if (sheet == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException(app.getApplication().getI18NMessage("servoy.foundSet.error.sqlsheet"));
    }
    FoundSetManager fsm = (FoundSetManager) app;
    List<SortColumn> sortColumns;
    if (defaultSortColumns == null || defaultSortColumns.size() == 0) {
        sortColumns = sheet.getDefaultPKSort();
    } else {
        sortColumns = defaultSortColumns;
    }
    QuerySelect cleanSelect = fsm.getSQLGenerator().getPKSelectSqlSelect(fsm.getScopesScopeProvider(), sheet.getTable(), null, null, true, null, sortColumns, false);
    QuerySelect relationSelect = (QuerySelect) sheet.getRelatedSQLDescription(relation.getName()).getSQLQuery();
    // don't select all columns in pk select
    cleanSelect.setColumns(AbstractBaseQuery.relinkTable(relationSelect.getTable(), cleanSelect.getTable(), relationSelect.getColumnsClone()));
    // copy the where (is foreign where)
    cleanSelect.setCondition(SQLGenerator.CONDITION_RELATION, AbstractBaseQuery.relinkTable(relationSelect.getTable(), cleanSelect.getTable(), relationSelect.getConditionClone(SQLGenerator.CONDITION_RELATION)));
    TablePlaceholderKey placeHolderKey = SQLGenerator.createRelationKeyPlaceholderKey(cleanSelect.getTable(), relation.getName());
    // all queries
    QuerySelect[] sqlSelects = new QuerySelect[whereArsgLists.length];
    // all aggregates
    QuerySelect[] aggregateSelects = new QuerySelect[whereArsgLists.length];
    List<Integer> queryIndex = new ArrayList<Integer>(whereArsgLists.length);
    Map<Integer, Row> cachedRows = new HashMap<Integer, Row>();
    List<QueryData> queryDatas = new ArrayList<QueryData>(whereArsgLists.length);
    String transactionID = fsm.getTransactionID(sheet);
    String clientID = fsm.getApplication().getClientID();
    ArrayList<TableFilter> sqlFilters = fsm.getTableFilterParams(sheet.getServerName(), cleanSelect);
    for (int i = 0; i < whereArsgLists.length; i++) {
        Object[] whereArgs = whereArsgLists[i];
        if (whereArgs == null || whereArgs.length == 0) {
            // $NON-NLS-1$
            throw new IllegalArgumentException(app.getApplication().getI18NMessage("servoy.relatedfoundset.error.noFK") + relation.getName());
        }
        QuerySelect sqlSelect;
        if (i == whereArsgLists.length - 1) {
            // the last one, use the template, no clone needed
            sqlSelect = cleanSelect;
        } else {
            sqlSelect = AbstractBaseQuery.deepClone(cleanSelect);
        }
        if (!sqlSelect.setPlaceholderValue(placeHolderKey, whereArgs)) {
            // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
            Debug.error(new RuntimeException("Could not set placeholder " + placeHolderKey + " in query " + sqlSelect + "-- continuing"));
        }
        sqlSelects[i] = sqlSelect;
        // Check for non-empty where-arguments, joins on null-conditions are not allowed (similar to FK constraints in databases)
        if (!whereArgsIsEmpty(whereArgs)) {
            Row cachedRow = null;
            if (relation.isFKPKRef(fsm.getApplication().getFlattenedSolution())) {
                // optimize for FK->PK relation, if the data is already cached, do not query
                RowManager rowManager = fsm.getRowManager(relation.getForeignDataSource());
                if (rowManager != null) {
                    cachedRow = rowManager.getCachedRow(whereArgs).getLeft();
                }
            }
            if (cachedRow != null) {
                if (Debug.tracing()) {
                    // $NON-NLS-1$
                    Debug.trace(Thread.currentThread().getName() + ": Found cached FK record");
                }
                cachedRows.put(Integer.valueOf(i), cachedRow);
            } else if (!parents[i].existInDataSource() && !fsm.config.loadRelatedRecordsIfParentIsNew() && relation.hasPKFKCondition(fsm.getApplication().getFlattenedSolution())) {
            /*
					 * Optimize for init of related foundsets on a parent record that is new and where the relation includes equal conditions for all the parent
					 * rowIdentifier columns
					 *
					 * In this case no query has to be made to the DB to fetch existing records, as there wouldn't be any.
					 */
            } else {
                ISQLSelect selectStatement = AbstractBaseQuery.deepClone((ISQLSelect) sqlSelect);
                // Note: put a clone of sqlSelect in the queryDatas list, we will compress later over multiple queries using pack().
                // Clone is needed because packed queries may not be save to manipulate.
                SQLStatement trackingInfo = null;
                if (fsm.getEditRecordList().hasAccess(sheet.getTable(), IRepository.TRACKING_VIEWS)) {
                    trackingInfo = new SQLStatement(ISQLActionTypes.SELECT_ACTION, sheet.getServerName(), sheet.getTable().getName(), null, null);
                    trackingInfo.setTrackingData(sheet.getColumnNames(), new Object[][] {}, new Object[][] {}, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
                }
                queryDatas.add(new QueryData(selectStatement, sqlFilters, !sqlSelect.isUnique(), 0, fsm.config.initialRelatedChunkSize(), IDataServer.RELATION_QUERY, trackingInfo));
                queryIndex.add(Integer.valueOf(i));
                QuerySelect aggregateSelect = FoundSet.getAggregateSelect(sheet, sqlSelect);
                if (aggregateSelect != null) {
                    // Note: see note about clone above.
                    queryDatas.add(new QueryData(AbstractBaseQuery.deepClone((ISQLSelect) aggregateSelect), fsm.getTableFilterParams(sheet.getServerName(), aggregateSelect), false, 0, 1, IDataServer.AGGREGATE_QUERY, null));
                    // same index for aggregates
                    queryIndex.add(Integer.valueOf(i));
                    aggregateSelects[i] = aggregateSelect;
                }
            }
        }
    }
    IDataSet[] dataSets = null;
    if (queryDatas.size() > 0) {
        try {
            // pack is safe here because queryDatas contains only cloned ISQLSelect objects
            QueryData[] qDatas = queryDatas.toArray(new QueryData[queryDatas.size()]);
            AbstractBaseQuery.acceptVisitor(qDatas, new PackVisitor());
            int size = 0;
            if (// trace the message size
            Debug.tracing()) {
                try {
                    ByteArrayOutputStream bs = new ByteArrayOutputStream();
                    ObjectOutputStream os = new ObjectOutputStream(bs);
                    os.writeObject(qDatas);
                    os.close();
                    size = bs.size();
                } catch (Exception e) {
                    Debug.trace(e);
                }
            }
            long time = System.currentTimeMillis();
            dataSets = fsm.getDataServer().performQuery(clientID, sheet.getServerName(), transactionID, qDatas);
            if (Debug.tracing()) {
                Debug.trace(// $NON-NLS-1$ //$NON-NLS-2$
                Thread.currentThread().getName() + ": Relation query: " + relation.getName() + " with: " + qDatas.length + " queries,query size: " + size + ",time: " + (System.currentTimeMillis() - time) + // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
                "ms");
            }
        } catch (RepositoryException re) {
            testException(clientID, re);
            throw re;
        } catch (RemoteException e) {
            testException(clientID, e.getCause());
            throw new RepositoryException(e);
        }
    }
    IFoundSetInternal[] foundsets = new RelatedFoundSet[whereArsgLists.length];
    int d = 0;
    for (int i = 0; i < whereArsgLists.length; i++) {
        IDataSet data;
        IDataSet aggregateData = null;
        int index = (d >= queryIndex.size()) ? -1 : queryIndex.get(d).intValue();
        if (index == i) {
            // regular query
            data = dataSets[d++];
            // optionally followed by aggregate
            index = (d >= queryIndex.size()) ? -1 : queryIndex.get(d).intValue();
            if (index == i) {
                // aggregate
                aggregateData = dataSets[d++];
            }
        } else {
            data = new BufferedDataSet();
            Row row = cachedRows.get(Integer.valueOf(i));
            if (row != null) {
                // cached
                data.addRow(row.getRawColumnData());
            }
        // else whereArgsIsEmpty
        }
        foundsets[i] = factory.createRelatedFoundSet(data, sqlSelects[i], app, parents[i], relation.getName(), sheet, sortColumns, aggregateSelects[i], aggregateData);
        if (aggregateData != null && foundsets[i] instanceof FoundSet) {
            ((FoundSet) foundsets[i]).fillAggregates(aggregateSelects[i], aggregateData);
        }
    }
    if (d != queryIndex.size()) {
        // $NON-NLS-1$
        throw new RepositoryException("Related query parameters out of sync " + d + '/' + queryIndex.size());
    }
    return foundsets;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ObjectOutputStream(java.io.ObjectOutputStream) PackVisitor(com.servoy.j2db.util.visitor.PackVisitor) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QuerySelect(com.servoy.j2db.query.QuerySelect) ServoyException(com.servoy.j2db.util.ServoyException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RemoteException(java.rmi.RemoteException) ISQLSelect(com.servoy.j2db.query.ISQLSelect)

Aggregations

RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ISQLSelect (com.servoy.j2db.query.ISQLSelect)1 QuerySelect (com.servoy.j2db.query.QuerySelect)1 TablePlaceholderKey (com.servoy.j2db.query.TablePlaceholderKey)1 SafeArrayList (com.servoy.j2db.util.SafeArrayList)1 ServoyException (com.servoy.j2db.util.ServoyException)1 PackVisitor (com.servoy.j2db.util.visitor.PackVisitor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1