Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.dbflute.twowaysql.node;
 
 import java.util.List;
 import java.util.Map;
 
The node for FOR (loop).
FOR comment is evaluated before analyzing nodes, so it is not related to container node.

Author(s):
jflute
 
 public class ForNode extends ScopeNode implements SqlConnectorAdjustableLoopAcceptable {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     public static final String PREFIX = "FOR ";
     public static final String CURRENT_VARIABLE = "#current";
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final String _expression;
     protected final List<String_nameList;
     protected final String _specifiedSql;
     protected final NodeAdviceFactory _nodeAdviceFactory;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     public ForNode(String expressionString specifiedSqlNodeAdviceFactory nodeAdviceFactory) {
          = expression;
          = Srl.splitList(expression".");
          = specifiedSql;
          = nodeAdviceFactory;
     }
 
     // ===================================================================================
     //                                                                              Accept
     //                                                                              ======
     public void accept(CommandContext ctx) {
         doAccept(ctxnull);
     }
 
     public void accept(CommandContext ctxLoopInfo loopInfo) {
         final String firstName = .get(0);
         if (firstName.equals(.)) { // use loop element
             final Object parameter = loopInfo.getCurrentParameter();
             final Class<?> parameterType = loopInfo.getCurrentParameterType();
             doAccept(ctxparameterparameterTypeloopInfotrue);
         } else { // normal
             doAccept(ctxloopInfo);
         }
     }
 
     public void doAccept(CommandContext ctxLoopInfo parentLoop) {
         final String firstName = .get(0);
         assertFirstNameAsNormal(ctxfirstName);
         final Object value = ctx.getArg(firstName);
         final Class<?> clazz = ctx.getArgType(firstName);
         doAccept(ctxvalueclazzparentLoopfalse);
     }
 
     public void doAccept(CommandContext ctxObject firstValueClass<?> firstTypeLoopInfo parentLoopboolean inheritLoop) {
         if (firstValue == null) {
             return// if base object is null, do nothing at FOR comment
         }
         final BoundValue boundValue = new BoundValue();
         boundValue.setFirstValue(firstValue);
         boundValue.setFirstType(firstType);
         setupBoundValue(boundValue);
         if (inheritLoop) {
             boundValue.inheritLikeSearchOptionIfNeeds(parentLoop);
         }
         final Object targetValue = boundValue.getTargetValue();
        if (targetValue == null) {
            return// if target value is null, do nothing at FOR comment
        }
        assertParameterList(targetValue);
        final List<?> parameterList = (List<?>) targetValue;
        final int loopSize = parameterList.size();
        final LoopInfo loopInfo = new LoopInfo();
        loopInfo.setParentLoop(parentLoop);
        loopInfo.setExpression();
        loopInfo.setSpecifiedSql();
        loopInfo.setParameterList(parameterList);
        loopInfo.setLoopSize(loopSize);
        loopInfo.setFilteringBindOption(boundValue.getFilteringBindOption());
        for (int loopIndex = 0; loopIndex < loopSizeloopIndex++) {
            loopInfo.setLoopIndex(loopIndex);
            processAcceptingChildren(ctxloopInfo);
        }
        if (loopSize > 0) {
            ctx.setEnabled(true);
        }
    }
    protected void assertFirstNameAsNormal(CommandContext ctxString firstName) {
        if (NodeChecker.isCurrentVariableOutOfScope(firstNamefalse)) {
        }
        if (NodeChecker.isWrongParameterBeanName(firstNamectx)) {
        }
    }
    }
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("The FOR comment had the illegal parameter-bean specification!");
        br.addItem("Advice");
        br.addElement("Please confirm your FOR comment.");
        br.addElement("For example:");
        br.addElement("  (x):");
        br.addElement("    /*FOR pmb,memberId*/");
        br.addElement("    /*FOR p mb,memberId*/");
        br.addElement("    /*FOR pmb:memberId*/");
        br.addElement("    /*FOR pmb,memberId*/");
        br.addElement("  (o):");
        br.addElement("    /*FOR pmb.memberId*/");
        br.addItem("FOR Comment Expression");
        br.addElement();
        // *debug to this exception does not need contents of the parameter-bean
        //  (and for security to application data)
        //br.addItem("ParameterBean");
        //br.addElement(pmb);
        br.addItem("Specified SQL");
        br.addElement();
        final String msg = br.buildExceptionMessage();
    }
    protected void setupBoundValue(BoundValue boundValue) {
        final ParameterCommentType type = .;
        final BoundValueTracer tracer = createBoundValueTracer(type);
        tracer.trace(boundValue);
    }
    }
    protected void assertParameterList(Object targetValue) {
        if (!List.class.isInstance(targetValue)) {
            final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
            br.addNotice("The parameter for FOR coment was not list.");
            br.addItem("FOR Comment Expression");
            br.addElement();
            br.addItem("Parameter");
            br.addElement(targetValue.getClass());
            br.addElement(targetValue);
            br.addItem("Specified SQL");
            br.addElement();
            String msg = br.buildExceptionMessage();
            throw new ForCommentParameterNotListException(msg);
        }
    }
    // ===================================================================================
    //                                                                       Loop Variable
    //                                                                       =============
    public enum LoopVariableType {
        FIRST("first"new LoopVariableNodeFactory() {
            public LoopAbstractNode create(String expressionString specifiedSql) {
                return new LoopFirstNode(expressionspecifiedSql);
            }
        }), NEXT("next"new LoopVariableNodeFactory() {
            public LoopAbstractNode create(String expressionString specifiedSql) {
                return new LoopNextNode(expressionspecifiedSql);
            }
        }), LAST("last"new LoopVariableNodeFactory() {
            public LoopAbstractNode create(String expressionString specifiedSql) {
                return new LoopLastNode(expressionspecifiedSql);
            }
        });
        private static final Map<StringLoopVariableType_codeValueMap = new HashMap<StringLoopVariableType>();
        static {
            for (LoopVariableType value : values()) {
                .put(value.code().toLowerCase(), value);
            }
        }
        private String _code;
        private LoopVariableNodeFactory _factory;
        private LoopVariableType(String codeLoopVariableNodeFactory factory) {
             = code;
             = factory;
        }
        public String code() {
            return ;
        }
        public static LoopVariableType codeOf(Object code) {
            if (code == null) {
                return null;
            }
            if (code instanceof LoopVariableType) {
                return (LoopVariableTypecode;
            }
            return .get(code.toString().toLowerCase());
        }
        public LoopAbstractNode createNode(String expressionString specifiedSql) {
            return .create(expressionspecifiedSql);
        }
    }
    public interface LoopVariableNodeFactory {
        LoopAbstractNode create(String expressionString specifiedSql);
    }
    // ===================================================================================
    //                                                                      Basic Override
    //                                                                      ==============
    @Override
    public String toString() {
        return DfTypeUtil.toClassTitle(this) + ":{" +  + "}";
    }
    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    public String getExpression() {
        return ;
    }
New to GrepCode? Check out our FAQ X