use of com.servoy.base.query.BaseQueryColumn 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