• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / velocity-scripting / #375

24 Sep 2023 01:30AM CUT coverage: 82.545%. Remained the same
#375

Pull #187

github

web-flow
Merge 833f74f79 into df890ab13
Pull Request #187: Update dependency org.mybatis:mybatis-parent to v39

506 of 613 relevant lines covered (82.54%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.79
/src/main/java/org/mybatis/scripting/velocity/InDirective.java
1
/*
2
 *    Copyright 2012-2022 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.scripting.velocity;
17

18
import java.io.IOException;
19
import java.io.StringWriter;
20
import java.io.Writer;
21
import java.util.Iterator;
22

23
import org.apache.velocity.context.InternalContextAdapter;
24
import org.apache.velocity.exception.TemplateInitException;
25
import org.apache.velocity.exception.VelocityException;
26
import org.apache.velocity.runtime.RuntimeServices;
27
import org.apache.velocity.runtime.directive.StopCommand;
28
import org.apache.velocity.runtime.parser.node.ASTReference;
29
import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
30
import org.apache.velocity.runtime.parser.node.Node;
31
import org.apache.velocity.runtime.parser.node.ParserTreeConstants;
32
import org.apache.velocity.util.introspection.Info;
33

34
/**
35
 * #in($collection $item COLUMN).
36
 */
37
public class InDirective extends RepeatDirective {
1✔
38

39
  /**
40
   * Immutable fields
41
   */
42
  private String var;
43

44
  private String open = "(";
1✔
45

46
  private String close = ")";
1✔
47

48
  private String separator = ", ";
1✔
49

50
  private String column = "";
1✔
51

52
  @Override
53
  public String getName() {
54
    return "in";
1✔
55
  }
56

57
  @Override
58
  public void init(RuntimeServices rs, InternalContextAdapter context, Node node) {
59
    super.init(rs, context, node);
1✔
60
    final int n = node.jjtGetNumChildren() - 1;
1✔
61
    for (int i = 1; i < n; i++) {
1✔
62
      Node child = node.jjtGetChild(i);
1✔
63
      if (i == 1) {
1✔
64
        if (child.getType() == ParserTreeConstants.JJTREFERENCE) {
1✔
65
          this.var = ((ASTReference) child).getRootString();
1✔
66
        } else {
67
          throw new TemplateInitException("Syntax error", getTemplateName(), getLine(), getColumn());
×
68
        }
69
      } else if (child.getType() == ParserTreeConstants.JJTSTRINGLITERAL) {
1✔
70
        String value = (String) ((ASTStringLiteral) child).value(context);
1✔
71
        if (i == 2) {
1✔
72
          this.column = value;
1✔
73
        }
74
      } else {
1✔
75
        throw new TemplateInitException("Syntax error", getTemplateName(), getLine(), getColumn());
×
76
      }
77
    }
78
    this.uberInfo = new Info(this.getTemplateName(), getLine(), getColumn());
1✔
79
  }
1✔
80

81
  @Override
82
  public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
83
    Object listObject = node.jjtGetChild(0).value(context);
1✔
84
    if (listObject == null) {
1✔
85
      return false;
×
86
    }
87

88
    Iterator<?> iterator = null;
1✔
89

90
    try {
91
      iterator = this.rsvc.getUberspect().getIterator(listObject, this.uberInfo);
1✔
92
    } catch (RuntimeException e) {
×
93
      throw e;
×
94
    } catch (Exception ee) {
×
95
      String msg = "Error getting iterator for #in at " + this.uberInfo;
×
96
      this.rsvc.getLog().error(msg, ee);
×
97
      throw new VelocityException(msg, ee);
×
98
    }
1✔
99

100
    if (iterator == null) {
1✔
101
      throw new VelocityException("Invalid collection");
×
102
    }
103

104
    int counter = 0;
1✔
105
    Object o = context.get(this.var);
1✔
106

107
    ParameterMappingCollector collector = (ParameterMappingCollector) context
1✔
108
        .get(SQLScriptSource.MAPPING_COLLECTOR_KEY);
1✔
109
    String savedItemKey = collector.getItemKey();
1✔
110
    collector.setItemKey(this.var);
1✔
111
    RepeatScope foreach = new RepeatScope(this, context.get(getName()), this.var);
1✔
112
    context.put(getName(), foreach);
1✔
113

114
    NullHolderContext nullHolderContext = null;
1✔
115
    Object value = null;
1✔
116
    StringWriter buffer = new StringWriter();
1✔
117

118
    while (iterator.hasNext()) {
1✔
119

120
      if (counter % MAX_IN_CLAUSE_SIZE == 0) {
1✔
121
        buffer.append(this.open); // Group begins
1✔
122
        buffer.append(this.column);
1✔
123
        buffer.append(" IN ");
1✔
124
        buffer.append(this.open); // In starts
1✔
125
      }
126

127
      value = iterator.next();
1✔
128
      put(context, this.var, value);
1✔
129
      foreach.index++;
1✔
130
      foreach.hasNext = iterator.hasNext();
1✔
131

132
      try {
133
        if (value == null) {
1✔
134
          if (nullHolderContext == null) {
×
135
            nullHolderContext = new NullHolderContext(this.var, context);
×
136
          }
137
          node.jjtGetChild(node.jjtGetNumChildren() - 1).render(nullHolderContext, buffer);
×
138
        } else {
139
          node.jjtGetChild(node.jjtGetNumChildren() - 1).render(context, buffer);
1✔
140
        }
141
      } catch (StopCommand stop) {
×
142
        if (stop.isFor(this)) {
×
143
          break;
×
144
        }
145
        clean(context, o, collector, savedItemKey);
×
146
        // close does not perform any action and this is here
147
        // to avoid eclipse reporting possible leak.
148
        buffer.close();
×
149
        throw stop;
×
150
      }
1✔
151
      counter++;
1✔
152

153
      if ((counter > 0 && counter % MAX_IN_CLAUSE_SIZE == 0) || !iterator.hasNext()) {
1✔
154
        buffer.append(this.close); // In ends
1✔
155
        buffer.append(this.close); // Group ends
1✔
156
        if (iterator.hasNext()) {
1✔
157
          buffer.append(" OR ");
1✔
158
        }
159
      } else if (iterator.hasNext()) {
1✔
160
        buffer.append(this.separator);
1✔
161
      }
162

163
    }
164
    String content = buffer.toString().trim();
1✔
165
    if (!"".equals(content)) {
1✔
166
      writer.append(this.open);
1✔
167
      writer.append(content);
1✔
168
      writer.append(this.close);
1✔
169
    } else {
170
      writer.append(this.open);
1✔
171
      writer.append(this.open);
1✔
172
      writer.append(this.column);
1✔
173
      writer.append(" NOT IN ");
1✔
174
      writer.append(this.open);
1✔
175
      writer.append(" NULL ");
1✔
176
      writer.append(this.close);
1✔
177
      writer.append(this.close);
1✔
178
      writer.append(this.close);
1✔
179
    }
180
    clean(context, o, collector, savedItemKey);
1✔
181
    return true;
1✔
182
  }
183

184
  @Override
185
  public int getType() {
186
    return BLOCK;
1✔
187
  }
188

189
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc