Search in sources :

Example 51 with Relation

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

the class DataAdapterList method addDataAdaptersForRelationSequence.

/**
 * Walk over the relation sequence to add data adapters.
 *
 * @param dataAdapter
 * @param relations
 * @param rel
 * @throws RepositoryException
 */
protected void addDataAdaptersForRelationSequence(IDataAdapter dataAdapter, Relation[] relations, int rel) throws RepositoryException {
    Relation relation = relations[rel];
    IDataProvider[] dps = relation.getPrimaryDataProviders(application.getFlattenedSolution());
    if (dps != null) {
        for (IDataProvider dp : dps) {
            if (dp instanceof LiteralDataprovider)
                continue;
            StringBuilder prefix = new StringBuilder();
            for (int r = 0; r < rel; r++) {
                prefix.append(relations[r].getName());
                prefix.append('.');
            }
            String dataProviderID;
            if (ScopesUtils.isVariableScope(dp.getDataProviderID())) {
                dataProviderID = dp.getDataProviderID();
            } else {
                dataProviderID = prefix.toString() + dp.getDataProviderID();
            }
            IDataAdapter da = dataAdapters.get(dataProviderID);
            if (da == null) {
                da = new DataAdapter(dataProviderID);
                dataAdapters.put(dataProviderID, da);
            }
            da.addDataListener(dataAdapter);
            if (rel > 0) {
                addDataAdaptersForRelationSequence(da, relations, rel - 1);
            }
        }
    }
}
Also used : Relation(com.servoy.j2db.persistence.Relation) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider)

Example 52 with Relation

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

the class JSDatabaseManager method convertFoundSet.

public FoundSet convertFoundSet(Object foundset, Object related) throws ServoyException {
    checkAuthorized();
    if (foundset instanceof FoundSet && ((FoundSet) foundset).getTable() != null) {
        FoundSet fs_old = (FoundSet) foundset;
        try {
            String relationName;
            if (related instanceof RelatedFoundSet) {
                relationName = ((RelatedFoundSet) related).getRelationName();
            } else if (related instanceof String) {
                relationName = (String) related;
            } else {
                // $NON-NLS-1$
                Debug.warn("convertFoundSet: invalid argument " + related);
                return null;
            }
            Relation relation = application.getFlattenedSolution().getRelation(relationName);
            if (relation == null || relation.isMultiServer() || fs_old.getTable() == null || !fs_old.getTable().equals(application.getFlattenedSolution().getTable(relation.getPrimaryDataSource()))) {
                // $NON-NLS-1$
                Debug.warn("convertFoundSet: cannot use relation " + relationName);
                return null;
            }
            ITable ft = application.getFlattenedSolution().getTable(relation.getForeignDataSource());
            FoundSet fs_new = (FoundSet) application.getFoundSetManager().getNewFoundSet(ft, null, application.getFoundSetManager().getDefaultPKSortColumns(ft.getDataSource()));
            QuerySelect sql = fs_old.getPksAndRecords().getQuerySelectForModification();
            SQLSheet sheet_new = fs_old.getSQLSheet().getRelatedSheet(relation, ((FoundSetManager) application.getFoundSetManager()).getSQLGenerator());
            if (sheet_new != null) {
                BaseQueryTable oldTable = sql.getTable();
                ISQLTableJoin join = (ISQLTableJoin) sql.getJoin(oldTable, relation.getName());
                if (join == null) {
                    join = SQLGenerator.createJoin(application.getFlattenedSolution(), relation, oldTable, new QueryTable(ft.getSQLName(), ft.getDataSource(), ft.getCatalog(), ft.getSchema()), true, fs_old);
                    sql.addJoin(join);
                }
                BaseQueryTable mainTable = join.getForeignTable();
                // invert the join
                sql.setTable(mainTable);
                // $NON-NLS-1$
                join.invert("INVERTED." + join.getName());
                // set the columns to be the PKs from the related table
                ArrayList<IQuerySelectValue> pkColumns = new ArrayList<IQuerySelectValue>();
                Iterator<Column> pks = sheet_new.getTable().getRowIdentColumns().iterator();
                while (pks.hasNext()) {
                    Column column = pks.next();
                    pkColumns.add(column.queryColumn(mainTable));
                }
                sql.setColumns(pkColumns);
                // sorting will be on the original columns, when distinct is set, this will conflict with the related pk columns
                sql.setDistinct(false);
                fs_new.setSQLSelect(sql);
                return fs_new;
            }
        } catch (Exception e) {
            Debug.error(e);
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) QueryString(com.servoy.j2db.persistence.QueryString) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) SQLException(java.sql.SQLException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QBColumn(com.servoy.j2db.querybuilder.impl.QBColumn) ITable(com.servoy.j2db.persistence.ITable) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 53 with Relation

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

the class FoundSetManager method getGlobalRelatedFoundSet.

public IFoundSetInternal getGlobalRelatedFoundSet(String name) throws RepositoryException, ServoyException {
    Relation relation = application.getFlattenedSolution().getRelation(name);
    if (relation != null && relation.isGlobal()) {
        SQLSheet childSheet = getSQLGenerator().getCachedTableSQLSheet(relation.getForeignDataSource());
        // this returns quickly if it already has a sheet for that relation, but optimize further?
        getSQLGenerator().makeRelatedSQL(childSheet, relation);
        return getRelatedFoundSet(new PrototypeState(getSharedFoundSet(relation.getForeignDataSource())), childSheet, name, getDefaultPKSortColumns(relation.getForeignDataSource()));
    }
    return null;
}
Also used : Relation(com.servoy.j2db.persistence.Relation)

Aggregations

Relation (com.servoy.j2db.persistence.Relation)53 ServoyException (com.servoy.j2db.util.ServoyException)16 ArrayList (java.util.ArrayList)16 FlattenedSolution (com.servoy.j2db.FlattenedSolution)15 RepositoryException (com.servoy.j2db.persistence.RepositoryException)15 Column (com.servoy.j2db.persistence.Column)11 ITable (com.servoy.j2db.persistence.ITable)11 RemoteException (java.rmi.RemoteException)10 BaseQueryTable (com.servoy.base.query.BaseQueryTable)9 IDataProvider (com.servoy.j2db.persistence.IDataProvider)8 QuerySelect (com.servoy.j2db.query.QuerySelect)8 QueryTable (com.servoy.j2db.query.QueryTable)8 IColumn (com.servoy.j2db.persistence.IColumn)7 Placeholder (com.servoy.j2db.query.Placeholder)7 ApplicationException (com.servoy.j2db.ApplicationException)6 Table (com.servoy.j2db.persistence.Table)6 QueryColumn (com.servoy.j2db.query.QueryColumn)6 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)5 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)5 SafeArrayList (com.servoy.j2db.util.SafeArrayList)5