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);
}
}
}
}
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;
}
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;
}
Aggregations