use of com.servoy.j2db.persistence.IRelation in project servoy-client by Servoy.
the class FindState method createFindStateJoins.
/**
* Find all processable related find states and create joins. A find state is processable when it has changed or when a related find state has changed.
* @param sqlSelect
* @param relations path to this state
* @param selectTable
* @param provider
* @return
* @throws RepositoryException
*/
public List<RelatedFindState> createFindStateJoins(QuerySelect sqlSelect, List<IRelation> relations, BaseQueryTable selectTable, IGlobalValueEntry provider) throws RepositoryException {
List<RelatedFindState> relatedFindStates = null;
List<Relation> searchRelations = getValidSearchRelations();
// find processable find states of related find states
for (Relation relation : searchRelations) {
if (relation != null) {
IFoundSetInternal set = relatedStates.get(relation.getName());
if (set != null && set.getSize() > 0) {
ISQLTableJoin existingJoin = (ISQLTableJoin) sqlSelect.getJoin(selectTable, relation.getName());
BaseQueryTable foreignQTable;
if (existingJoin == null) {
ITable foreignTable = parent.getFoundSetManager().getApplication().getFlattenedSolution().getTable(relation.getForeignDataSource());
foreignQTable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
} else {
foreignQTable = existingJoin.getForeignTable();
}
FindState fs = (FindState) set.getRecord(0);
List<IRelation> nextRelations = new ArrayList<IRelation>(relations);
nextRelations.add(relation);
List<RelatedFindState> rfs = fs.createFindStateJoins(sqlSelect, nextRelations, foreignQTable, provider);
if (rfs != null && rfs.size() > 0) {
// changed related findstate, add self with join
if (relatedFindStates == null) {
relatedFindStates = rfs;
} else {
relatedFindStates.addAll(rfs);
}
if (existingJoin == null) {
sqlSelect.addJoin(SQLGenerator.createJoin(parent.getFoundSetManager().getApplication().getFlattenedSolution(), relation, selectTable, foreignQTable, false, provider));
}
}
}
}
}
// add yourself if you have changed or one or more related states has changed
if (isChanged() || (relatedFindStates != null && relatedFindStates.size() > 0)) {
if (relatedFindStates == null) {
relatedFindStates = new ArrayList<RelatedFindState>();
}
relatedFindStates.add(new RelatedFindState(this, relations, selectTable));
}
return relatedFindStates;
}
use of com.servoy.j2db.persistence.IRelation in project servoy-client by Servoy.
the class SQLGenerator method createExistsCondition.
public static ISQLCondition createExistsCondition(IDataProviderHandler flattenedSolution, QuerySelect sqlSelect, ISQLCondition condition, List<IRelation> relations, BaseQueryTable columnTable, IGlobalValueEntry provider, BaseQueryColumn[] pkQueryColumns) throws RepositoryException {
// exists (select 1 from innermain join related1 ... join relatedn where innermain.pk = main.pk having aggregate(relatedn))
if (relations.size() == 0) {
// searching for aggregate in main table, does no make sense.. ignore in search
return null;
}
QuerySelect existsSelect = new QuerySelect(new QueryTable(sqlSelect.getTable().getName(), sqlSelect.getTable().getDataSource(), sqlSelect.getTable().getCatalogName(), sqlSelect.getTable().getSchemaName()));
existsSelect.addColumn(new QueryColumnValue(Integer.valueOf(1), null, true));
// innermain.pk = main.pk
QueryColumn[] innerPkColumns = new QueryColumn[pkQueryColumns.length];
for (int p = 0; p < pkQueryColumns.length; p++) {
BaseQueryColumn pk = pkQueryColumns[p];
innerPkColumns[p] = new QueryColumn(existsSelect.getTable(), pk.getId(), pk.getName(), pk.getColumnType().getSqlType(), pk.getColumnType().getLength(), pk.getColumnType().getScale(), pk.getNativeTypename(), pk.getFlags(), pk.isIdentity());
// group by on the inner pk, some dbs (hxtt dbf) require that
existsSelect.addGroupBy(innerPkColumns[p]);
}
existsSelect.addCondition("AGGREGATE-SEARCH", new // $NON-NLS-1$
SetCondition(// $NON-NLS-1$
new int[] { IBaseSQLCondition.EQUALS_OPERATOR }, // $NON-NLS-1$
innerPkColumns, pkQueryColumns, true));
// add the joins
BaseQueryTable prevTable = existsSelect.getTable();
for (IRelation relation : relations) {
ITable foreignTable = flattenedSolution.getTable(relation.getForeignDataSource());
QueryTable foreignQtable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
existsSelect.addJoin(createJoin(flattenedSolution, relation, prevTable, foreignQtable, true, provider));
prevTable = foreignQtable;
}
existsSelect.addHaving(AbstractBaseQuery.relinkTable(columnTable, prevTable, condition));
return new ExistsCondition(existsSelect, true);
}
Aggregations