예제 #1
0
  public void appendTo(Select sel, ExpContext ctx, ExpState state, SQLBuffer sql, int index) {
    if (!(_map instanceof PCPath)) throw new UnsupportedOperationException();
    if (!(_key instanceof Const)) throw new UnsupportedOperationException();

    GetMapValueExpState gstate = (GetMapValueExpState) state;
    PCPath map = (PCPath) _map;
    Object key = ((Const) _key).getValue(ctx, gstate.keyState);
    FieldMapping field = map.getFieldMapping(gstate.mapState);
    if (!(field.getStrategy() instanceof LRSMapFieldStrategy))
      throw new UnsupportedOperationException();

    LRSMapFieldStrategy strat = (LRSMapFieldStrategy) field.getStrategy();
    ClassMapping[] clss = strat.getIndependentValueMappings(true);
    if (clss != null && clss.length > 1) throw RelationStrategies.unjoinable(field);

    ClassMapping cls = (clss == null || clss.length == 0) ? null : clss[0];
    ForeignKey fk = strat.getJoinForeignKey(cls);

    // manually create a subselect for the Map's value
    sql.append("(SELECT ");
    Column[] values = field.getElementMapping().getColumns();
    for (int i = 0; i < values.length; i++) {
      if (i > 0) sql.append(", ");
      sql.append(values[i].getTable()).append(".").append(values[i]);
    }
    sql.append(" FROM ").append(values[0].getTable());
    sql.append(" WHERE ");

    // add in the joins
    ContainerFieldStrategy.appendUnaliasedJoin(
        sql, sel, null, ctx.store.getDBDictionary(), field, fk);
    sql.append(" AND ");

    key = strat.toKeyDataStoreValue(key, ctx.store);
    Column[] cols = strat.getKeyColumns(cls);
    Object[] vals = (cols.length == 1) ? null : (Object[]) key;

    for (int i = 0; i < cols.length; i++) {
      sql.append(cols[i].getTable()).append(".").append(cols[i]);
      if (vals == null) sql.append((key == null) ? " IS " : " = ").appendValue(key, cols[i]);
      else sql.append((vals[i] == null) ? " IS " : " = ").appendValue(vals[i], cols[i]);
    }
    sql.append(")");
  }