* @throws SQLException
*/
private void generateArrayQuery(Selector sel, StatementPrototype sp, ObjectStack oStack, Boolean sorted)
throws SQLException
{
ObjectRepresentation rep = oStack.getActualRepresentation();
sp.getIdStatementGenerator().addPropertyTableToJoin(Defaults.ARRAY_TABLE_NAME, rep.getAsName());
// iterate over all non-null array entries
// satisfy all the non-null members of the array
int length = Array.getLength(sel.getSelectionObject());
// first, count the number of non-null entries
int numEntries = 0;
for (int x = 0; x < length; x++)
{
Object o = Array.get(sel.getSelectionObject(), x);
if (o != null)
{
numEntries++;
}
}
// only add relation statements if the array has entries
if (numEntries > 0)
{
// add array relation
for (int x = 0; x < length; x++)
{
Object o = Array.get(sel.getSelectionObject(), x);
if (o != null)
{
Class<?> propertyClass = sel.getSelectionObject().getClass().getComponentType();
ArrayList<Object> conditionalValues = new ArrayList<Object>();
StringBuilder sb = new StringBuilder(rep.getAsName());
sb.append(".");
sb.append(Defaults.ID_COL);
sb.append(" = ");
// add an as statement for the relation
String relationTable = rep.getTableName();
String relationAsName = this.uidGenerator.next();
sp.getIdStatementGenerator().addPropertyTableToJoin(relationTable, relationAsName);
sb.append(relationAsName);
sb.append(".");
sb.append(Defaults.ARRAY_MEMBER_ID);
sb.append(" AND ");
if (sorted)
{
// position is only valid for queries with sorted types
sb.append(relationAsName);
sb.append(".");
sb.append(Defaults.ARRAY_POSITION);
sb.append(" = ? AND ");
conditionalValues.add(Integer.valueOf(x));
}
sb.append(relationAsName);
sb.append(".");
sb.append(Defaults.VALUE_COL);
sb.append(" ");
if (ObjectTools.isPrimitive(o.getClass()) && ObjectTools.isPrimitive(propertyClass))
{
sb.append(sel.getRelationalRepresentation());
if (sel.takesPlaceholder())
{
sb.append("?");
conditionalValues.add(o);
}
// put the stuff in the query
sp.addConditionalStatement(sb.toString());
sp.addConditionalValues(conditionalValues);
}
else
{
// check if we have queried this property before
Long id = (long) System.identityHashCode(o);
if (rep.getDelayedInsertionBuffer().isKnown(id))
{
break;
}
rep.getDelayedInsertionBuffer().addId(id);
// name the property
ObjectStack propertyStack = new ObjectStack(adapter, o.getClass(), o,
rep.getDelayedInsertionBuffer());
nameStack(relationAsName + "." + Defaults.VALUE_COL, propertyStack);
ObjectRepresentation propertyRep = propertyStack.getRepresentation(propertyClass);
sb.append(" = ");
sb.append(propertyRep.getAsName());
sb.append(".");
sb.append(Defaults.ID_COL);
// save to query
sp.addConditionalStatement(sb.toString());
sp.addConditionalValues(conditionalValues);