use of com.servoy.j2db.query.ObjectPlaceholderKey in project servoy-client by Servoy.
the class SQLGenerator method createJoin.
/**
* Join clause for this relation.
*/
public static ISQLTableJoin createJoin(IDataProviderHandler flattenedSolution, IRelation relation, BaseQueryTable primaryTable, BaseQueryTable foreignTable, boolean permanentJoin, final IGlobalValueEntry provider) throws RepositoryException {
if (relation instanceof AbstractBase) {
ISQLTableJoin queryJoin = ((AbstractBase) relation).getRuntimeProperty(Relation.RELATION_JOIN);
if (queryJoin != null) {
// a query join was defined for this relation, just relink the tables for the first and last in the joins
queryJoin = deepClone(queryJoin);
queryJoin = AbstractBaseQuery.relinkTable(queryJoin.getPrimaryTable(), primaryTable, queryJoin);
queryJoin = AbstractBaseQuery.relinkTable(queryJoin.getForeignTable(), foreignTable, queryJoin);
// update the placeholders for globals
queryJoin.acceptVisitor(new IVisitor() {
public Object visit(Object o) {
if (o instanceof Placeholder && ((Placeholder) o).getKey() instanceof ObjectPlaceholderKey) {
Object value = provider.getDataProviderValue(((ObjectPlaceholderKey<int[]>) ((Placeholder) o).getKey()).getName());
int[] args = ((ObjectPlaceholderKey<int[]>) ((Placeholder) o).getKey()).getObject();
int dataProviderType = args[0];
int flags = args[1];
if (value == null) {
return ValueFactory.createNullValue(dataProviderType);
}
return Column.getAsRightType(dataProviderType, flags, value, Integer.MAX_VALUE, false, false);
}
return o;
}
});
return queryJoin;
}
}
// build a join from the relation items
IDataProvider[] primary = relation.getPrimaryDataProviders(flattenedSolution);
Column[] foreign = relation.getForeignColumns(flattenedSolution);
int[] operators = relation.getOperators();
AndCondition joinCondition = new AndCondition();
for (int x = 0; x < primary.length; x++) {
Column primaryColumn = null;
// check if stored script calc or table column
if (primary[x] instanceof ScriptCalculation) {
ScriptCalculation sc = ((ScriptCalculation) primary[x]);
// null when not stored
primaryColumn = sc.getTable().getColumn(sc.getName());
} else if (primary[x] instanceof Column) {
primaryColumn = (Column) primary[x];
}
QueryColumn foreignColumn = foreign[x].queryColumn(foreignTable);
Object value;
if (primaryColumn == null) {
if (primary[x] instanceof LiteralDataprovider) {
value = ((LiteralDataprovider) primary[x]).getValue();
value = foreign[x].getAsRightType(value);
} else {
value = provider.getDataProviderValue(primary[x].getDataProviderID());
if (value == null) {
value = ValueFactory.createNullValue(primary[x].getDataProviderType());
} else if (value instanceof Placeholder) {
if (((Placeholder) value).getKey() instanceof ObjectPlaceholderKey<?>) {
((ObjectPlaceholderKey) ((Placeholder) value).getKey()).setObject(new int[] { primary[x].getDataProviderType(), primary[x].getFlags() });
}
} else {
value = Column.getAsRightType(primary[x].getDataProviderType(), primary[x].getFlags(), value, Integer.MAX_VALUE, false, false);
}
}
} else // table type, can be stored calc
{
value = primaryColumn.queryColumn(primaryTable);
}
// all operators are swappable because only relation operators in RelationItem.RELATION_OPERATORS can be defined.
// NOTE: elements in joinCondition MUST be CompareConditions (expected in QueryGenerator and SQLGenerator.createConditionFromFindState)
joinCondition.addCondition(new CompareCondition(RelationItem.swapOperator(operators[x]), foreignColumn, value));
}
if (joinCondition.getConditions().size() == 0) {
// $NON-NLS-1$
throw new RepositoryException("Missing join condition in relation " + relation.getName());
}
return new QueryJoin(relation.getName(), primaryTable, foreignTable, joinCondition, relation.getJoinType(), permanentJoin);
}
Aggregations