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;
 
 

Author(s):
modified by jflute (originated in S2Dao)
 
 public class EmbeddedVariableNode extends VariableNode {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     public static final String PREFIX_NORMAL = "$";
     public static final String PREFIX_REPLACE_ONLY = "$$";
     public static final String PREFIX_TERMINAL_DOT = "$.";
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final boolean _replaceOnly;
     protected final boolean _terminalDot;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     public EmbeddedVariableNode(String expressionString testValueString specifiedSqlboolean blockNullParameter,
             NodeAdviceFactory nodeAdviceFactoryboolean replaceOnlyboolean terminalDot) {
         super(expressiontestValuespecifiedSqlblockNullParameternodeAdviceFactory);
          = replaceOnly;
          = terminalDot;
     }
 
     // ===================================================================================
     //                                                                              Accept
     //                                                                              ======
     @Override
     protected void doProcess(CommandContext ctxBoundValue boundValueLoopInfo loopInfo) {
         final Object finalValue = boundValue.getTargetValue();
         final Class<?> finalType = boundValue.getTargetType();
         if (isInScope()) {
             if (finalValue == null) { // in-scope does not allow null value
                 throwBindOrEmbeddedCommentParameterNullValueException(boundValue);
             }
             if (Collection.class.isAssignableFrom(finalType)) {
                 embedArray(ctx, ((Collection<?>) finalValue).toArray());
             } else if (finalType.isArray()) {
                 embedArray(ctxfinalValue);
             } else {
                 throwBindOrEmbeddedCommentInScopeNotListException(boundValue);
             }
         } else {
             if (finalValue == null) {
                 ctx.addSql("null");
             } else if (!(finalValue instanceof String)) {
                 final String embeddedValue = finalValue.toString();
                 if (isQuotedScalar()) { // basically for condition value
                     ctx.addSql(quote(embeddedValue));
                 } else { // basically for cannot-bound condition (for example, paging)
                     ctx.addSql(embeddedValue);
                 }
             } else {
                 // string type here
                 final String embeddedStr = (StringfinalValue;
                 assertNotContainBindSymbol(embeddedStr);
                 if (isQuotedScalar()) { // basically for condition value
                     ctx.addSql(quote(embeddedStr));
                     if (isAcceptableLikeSearch(loopInfo)) {
                         setupRearOption(ctxboundValue);
                     }
                 } else {
                     final Object firstValue = boundValue.getFirstValue();
                     final Class<?> firstType = boundValue.getFirstType();
                     final boolean bound = processDynamicBinding(ctxfirstValuefirstTypeembeddedStr);
                     if (!bound) {
                        ctx.addSql(embeddedStr);
                    }
                }
            }
        }
        if ( != null) {
            if () { // e.g. select ... from /*$$pmb.schema*/MEMBER
                // actually the test value is not test value
                // but a part of SQL statement here
                ctx.addSql();
            } else if () { // e.g. select ... from /*$$pmb.schema*/dev.MEMBER
                // the real test value is until a dot character
                ctx.addSql("." + Srl.substringFirstRear("."));
            }
        }
    }
    protected void embedArray(CommandContext ctxObject array) {
        if (array == null) {
            return;
        }
        final int length = Array.getLength(array);
        if (length == 0) {
        }
        final boolean quotedInScope = isQuotedInScope();
        ctx.addSql("(");
        int validCount = 0;
        for (int i = 0; i < length; ++i) {
            final Object currentElement = Array.get(arrayi);
            if (currentElement != null) {
                if (validCount > 0) {
                    ctx.addSql(", ");
                }
                final String currentStr = currentElement.toString();
                assertNotContainBindSymbol(currentStr);
                if (quotedInScope) {
                    ctx.addSql(quote(currentStr));
                } else {
                    ctx.addSql(currentStr);
                }
                ++validCount;
            }
        }
        if (validCount == 0) {
        }
        ctx.addSql(")");
    }
    protected void assertNotContainBindSymbol(String value) {
        if (containsBindSymbol(value)) {
            final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
            br.addNotice("The value of embedded comment contained bind symbols.");
            br.addItem("Advice");
            br.addElement("The value of embedded comment should not contain bind symbols.");
            br.addElement("For example, a question mark '?'.");
            br.addItem("Comment Expression");
            br.addElement();
            br.addItem("Embedded Value");
            br.addElement(value);
            final String msg = br.buildExceptionMessage();
            throw new EmbeddedVariableCommentContainsBindSymbolException(msg);
        }
    }
    protected boolean containsBindSymbol(String value) {
        return value.indexOf("?") > -1;
    }
    protected String quote(String value) {
        return "'" + value + "'";
    }
    protected boolean isQuotedScalar() {
        if ( == null) {
            return false;
        }
        return Srl.count("'") > 1 && .startsWith("'") && .endsWith("'");
    }
    protected boolean isQuotedInScope() {
        if (!isInScope()) {
            return false;
        }
        return Srl.count("'") > 1;
    }
    protected boolean processDynamicBinding(CommandContext ctxObject firstValueClass<?> firstTypeString embeddedString) {
        final ScopeInfo first = Srl.extractScopeFirst(embeddedString"/*""*/");
        if (first == null) {
            return false;
        }
        final SqlAnalyzer analyzer = new SqlAnalyzer(embeddedString);
        final Node rootNode = analyzer.analyze();
        final CommandContextCreator creator = new CommandContextCreator(new String[] { "pmb" }, new Class<?>[] { firstType });
        final CommandContext rootCtx = creator.createCommandContext(new Object[] { firstValue });
        rootNode.accept(rootCtx);
        final String sql = rootCtx.getSql();
        ctx.addSql(sqlrootCtx.getBindVariables(), rootCtx.getBindVariableTypes());
        return true;
    }
    // ===================================================================================
    //                                                                      Implementation
    //                                                                      ==============
    @Override
    protected ParameterCommentType getCommentType() {
        return .;
    }
New to GrepCode? Check out our FAQ X