test_class.file: dal-jdbc/src/test/java/org/unidal/dal/jdbc/query/token/resolver/FieldsTokenResolverTest.java
test_class.identifier: FieldsTokenResolverTest
test_class.superclass: extends AbstractTokenResolverTest
test_case.body: @Test
     public void testSelect2() throws Exception {
        QueryDef query = new QueryDef("test", UserEntity.class, QueryType.SELECT, "<fields/>");
        Readset<?> readset = UserEntity.READSET_FULL_U;
        User user = new User();
        QueryContext ctx = getSelectContext(query, user, readset);
        
        m_queryResolver.resolve(ctx);
        Assert.assertEquals("u.user_id,u.full_name,u.creation_date,u.last_modified_date,upper(full_name)", ctx.getSqlStatement());
        Assert.assertEquals(null, ctx.getOutSubObjectNames().get(0));
        Assert.assertEquals(null, ctx.getOutSubObjectNames().get(4));
        Assert.assertEquals("[user-id, user-name, creation-date, last-modified-date, upper-user-name]", ctx.getOutFields().toString());
     }
test_case.class_method_signature: FieldsTokenResolverTest.testSelect2()
test_case.full_signature: @Test public void testSelect2()
test_case.identifier: testSelect2
test_case.invocations: getSelectContext resolve assertEquals getSqlStatement assertEquals get getOutSubObjectNames assertEquals get getOutSubObjectNames assertEquals toString getOutFields
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testSelect2()
focal_class.declarator: m_manager
focal_class.modifier: @Inject
     private
focal_class.original_string: @Inject
     private EntityInfoManager m_manager;
focal_class.type: EntityInfoManager
focal_class.var_name: m_manager
focal_class.declarator: m_expressionResolver
focal_class.modifier: @Inject
     private
focal_class.original_string: @Inject
     private ExpressionResolver m_expressionResolver;
focal_class.type: ExpressionResolver
focal_class.var_name: m_expressionResolver
focal_class.declarator: m_naming
focal_class.modifier: @Inject
     private
focal_class.original_string: @Inject
     private QueryNaming m_naming;
focal_class.type: QueryNaming
focal_class.var_name: m_naming
focal_class.file: dal-jdbc/src/main/java/org/unidal/dal/jdbc/query/token/resolver/FieldsTokenResolver.java
focal_class.identifier: FieldsTokenResolver
focal_class.interfaces: implements TokenResolver
focal_class.class_method_signature: FieldsTokenResolver.resolve(Token token, QueryContext ctx)
focal_class.full_signature: @SuppressWarnings("unchecked") public String resolve(Token token, QueryContext ctx)
focal_class.identifier: resolve
focal_class.modifiers: @SuppressWarnings("unchecked") public
focal_class.parameters: (Token token, QueryContext ctx)
focal_class.return: String
focal_class.signature: String resolve(Token token, QueryContext ctx)
focal_class.class_method_signature: FieldsTokenResolver.split(String data, String delimiter)
focal_class.full_signature: private List<String> split(String data, String delimiter)
focal_class.identifier: split
focal_class.modifiers: private
focal_class.parameters: (String data, String delimiter)
focal_class.return: List<String>
focal_class.signature: List<String> split(String data, String delimiter)
focal_method.body: @SuppressWarnings("unchecked")
     public String resolve(Token token, QueryContext ctx) {
        if (token.getType() != TokenType.FIELDS) {
           throw new DalRuntimeException("Internal error: only FIELDS token is supported by " + getClass());
        }
  
        EntityInfo entityInfo = ctx.getEntityInfo();
        SimpleTagToken fields = (SimpleTagToken) token;
        String output = fields.getAttribute("output", "true");
        List<String> excludes = split(fields.getAttribute("excludes", ""), ",");
        StringBuilder sb = new StringBuilder(1024);
        DataObject proto = ctx.getProto();
  
        switch (ctx.getQuery().getType()) {
        case SELECT:
           SubObjects subobjects = entityInfo.getSubobjects(ctx.getReadset());
           String[] names;
           List<Readset<Object>> readsets;
  
           if (subobjects != null) {
              names = subobjects.value();
              readsets = ((Readset<Object>) ctx.getReadset()).getChildren();
           } else {
              names = null;
              readsets = new ArrayList<Readset<Object>>(1);
              readsets.add((Readset<Object>) ctx.getReadset());
           }
  
           int size = readsets.size();
  
           for (int i = 0; i < size; i++) {
              Readset<Object> readset = readsets.get(i);
              Relation relation = (names == null ? null : entityInfo.getRelation(names[i]));
              String alias = (relation == null ? entityInfo.getAlias() : relation.alias());
              String subObjectName = (relation == null ? null : names[i]);
  
              for (DataField field : readset.getFields()) {
                 EntityInfo ei = m_manager.getEntityInfo(field.getEntityClass());
                 Attribute attribute = ei.getAttribute(field);
  
                 if (attribute != null) {
                    if (excludes.contains(field.getName())) {
                       continue;
                    }
  
                    if (sb.length() > 0) {
                       sb.append(',');
                    }
  
                    if (attribute.selectExpr().length() > 0) {
                       sb.append(m_expressionResolver.resolve(ctx, attribute.selectExpr()));
                    } else {
                       sb.append(alias).append('.').append(m_naming.getField(attribute.field()));
                    }
  
                    if ("true".equals(output)) {
                       ctx.addOutField(field);
                       ctx.addOutSubObjectName(subObjectName);
                    }
                 } else {
                    throw new DalRuntimeException("Internal error: No Attribute annotation defined for field: " + field);
                 }
              }
           }
  
           break;
        case INSERT:
           for (DataField field : entityInfo.getAttributeFields()) {
              Attribute attribute = entityInfo.getAttribute(field);
  
              if (attribute != null) {
                 if (attribute.field().length() > 0 && !(attribute.autoIncrement() && !proto.isFieldUsed(field))) {
                    if (sb.length() > 0) {
                       sb.append(',');
                    }
  
                    sb.append(m_naming.getField(attribute.field()));
                 }
              } else {
                 throw new DalRuntimeException("Internal error: No Attribute annotation defined for field: " + field);
              }
           }
  
           break;
        case UPDATE:
           for (DataField field : ctx.getUpdateset().getFields()) {
              Attribute attribute = entityInfo.getAttribute(field);
  
              if (attribute != null) {
                 if (proto.isFieldUsed(field) || attribute.updateExpr().length() > 0) {
                    if (sb.length() > 0) {
                       sb.append(',');
                    }
  
                    if (!proto.isFieldUsed(field) && attribute.updateExpr().length() > 0) {
                       sb.append(m_naming.getField(attribute.field())).append('=').append(m_expressionResolver.resolve(ctx, attribute.updateExpr()));
                    } else {
                       sb.append(m_naming.getField(attribute.field())).append("=?");
                       ctx.addParameter(new Parameter(field));
                    }
                 }
              } else {
                 throw new DalRuntimeException("Internal error: No Attribute annotation defined for field: " + field);
              }
           }
  
           break;
        case DELETE:
           throw new DalRuntimeException("FIELDS token does not support query type: " + ctx.getQuery().getType());
        default:
           throw new DalRuntimeException("FIELDS token does not support query type: " + ctx.getQuery().getType());
        }
  
        return sb.toString();
     }
focal_method.class_method_signature: FieldsTokenResolver.resolve(Token token, QueryContext ctx)
focal_method.full_signature: @SuppressWarnings("unchecked") public String resolve(Token token, QueryContext ctx)
focal_method.identifier: resolve
focal_method.invocations: getType getClass getEntityInfo getAttribute split getAttribute getProto getType getQuery getSubobjects getReadset value getChildren getReadset add getReadset size get getRelation getAlias alias getFields getEntityInfo getEntityClass getAttribute contains getName length append length selectExpr append resolve selectExpr append append append getField field equals addOutField addOutSubObjectName getAttributeFields getAttribute length field autoIncrement isFieldUsed length append append getField field getFields getUpdateset getAttribute isFieldUsed length updateExpr length append isFieldUsed length updateExpr append append append getField field resolve updateExpr append append getField field addParameter getType getQuery getType getQuery toString
focal_method.modifiers: @SuppressWarnings("unchecked") public
focal_method.parameters: (Token token, QueryContext ctx)
focal_method.return: String
focal_method.signature: String resolve(Token token, QueryContext ctx)