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

mybatis / ibatis-2 / 678

27 Dec 2025 01:13AM UTC coverage: 65.584% (+0.05%) from 65.532%
678

push

github

hazendaz
Merge remote-tracking branch 'upstream/master' into support-javax

1606 of 2810 branches covered (57.15%)

167 of 305 new or added lines in 90 files covered. (54.75%)

6 existing lines in 4 files now uncovered.

5067 of 7726 relevant lines covered (65.58%)

0.66 hits per line

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

75.0
/src/main/java/com/ibatis/sqlmap/engine/mapping/sql/dynamic/elements/ConditionalTagHandler.java
1
/*
2
 * Copyright 2004-2025 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 com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements;
17

18
import com.ibatis.common.beans.Probe;
19
import com.ibatis.common.beans.ProbeFactory;
20
import com.ibatis.sqlmap.engine.type.SimpleDateFormatter;
21

22
import java.math.BigDecimal;
23
import java.math.BigInteger;
24
import java.util.Date;
25

26
/**
27
 * The Class ConditionalTagHandler.
28
 */
29
public abstract class ConditionalTagHandler extends BaseTagHandler {
1✔
30

31
  /** The Constant PROBE. */
32
  private static final Probe PROBE = ProbeFactory.getProbe();
1✔
33

34
  /** The Constant NOT_COMPARABLE. */
35
  public static final long NOT_COMPARABLE = Long.MIN_VALUE;
36

37
  /** The Constant DATE_MASK. */
38
  private static final String DATE_MASK = "yyyy/MM/dd hh:mm:ss";
39

40
  /** The Constant START_INDEX. */
41
  private static final String START_INDEX = "[";
42

43
  /**
44
   * Checks if is condition.
45
   *
46
   * @param ctx
47
   *          the ctx
48
   * @param tag
49
   *          the tag
50
   * @param parameterObject
51
   *          the parameter object
52
   *
53
   * @return true, if is condition
54
   */
55
  public abstract boolean isCondition(SqlTagContext ctx, SqlTag tag, Object parameterObject);
56

57
  @Override
58
  public int doStartFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject) {
59

60
    ctx.pushRemoveFirstPrependMarker(tag);
1✔
61

62
    if (isCondition(ctx, tag, parameterObject)) {
1✔
63
      return SqlTagHandler.INCLUDE_BODY;
1✔
64
    }
65
    return SqlTagHandler.SKIP_BODY;
1✔
66
  }
67

68
  @Override
69
  public int doEndFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuilder bodyContent) {
70

71
    IterateContext iterate = ctx.peekIterateContext();
1✔
72

73
    if (null != iterate && iterate.isAllowNext()) {
1!
74
      iterate.next();
×
75
      iterate.setAllowNext(false);
×
76
      if (!iterate.hasNext()) {
×
77
        iterate.setFinal(true);
×
78
      }
79
    }
80

81
    // iteratePropertyReplace(bodyContent,ctx.peekIterateContext());
82

83
    return super.doEndFragment(ctx, tag, parameterObject, bodyContent);
1✔
84
  }
85

86
  /**
87
   * Compare.
88
   *
89
   * @param ctx
90
   *          the ctx
91
   * @param tag
92
   *          the tag
93
   * @param parameterObject
94
   *          the parameter object
95
   *
96
   * @return the long
97
   */
98
  protected long compare(SqlTagContext ctx, SqlTag tag, Object parameterObject) {
99
    String comparePropertyName = tag.getComparePropertyAttr();
1✔
100
    String compareValue = tag.getCompareValueAttr();
1✔
101

102
    String prop = getResolvedProperty(ctx, tag);
1✔
103
    Object value1;
104
    Class type;
105

106
    if (prop != null) {
1✔
107
      value1 = PROBE.getObject(parameterObject, prop);
1✔
108
      type = PROBE.getPropertyTypeForGetter(parameterObject, prop);
1✔
109
    } else {
110
      value1 = parameterObject;
1✔
111
      if (value1 != null) {
1!
112
        type = parameterObject.getClass();
1✔
113
      } else {
114
        type = Object.class;
×
115
      }
116
    }
117
    if (comparePropertyName != null) {
1✔
118
      Object value2 = PROBE.getObject(parameterObject, comparePropertyName);
1✔
119
      return compareValues(type, value1, value2);
1✔
120
    }
121
    if (compareValue != null) {
1!
122
      return compareValues(type, value1, compareValue);
1✔
123
    } else {
124
      throw new RuntimeException("Error comparing in conditional fragment.  Uknown 'compare to' values.");
×
125
    }
126
  }
127

128
  /**
129
   * Compare values.
130
   *
131
   * @param type
132
   *          the type
133
   * @param value1
134
   *          the value 1
135
   * @param value2
136
   *          the value 2
137
   *
138
   * @return the long
139
   */
140
  protected long compareValues(Class type, Object value1, Object value2) {
141
    long result;
142

143
    if (value1 == null || value2 == null) {
1!
144
      result = value1 == value2 ? 0 : NOT_COMPARABLE;
1!
145
    } else {
146
      if (value2.getClass() != type) {
1✔
147
        value2 = convertValue(type, value2.toString());
1✔
148
      }
149
      if (value2 instanceof String && type != String.class) {
1✔
150
        value1 = value1.toString();
1✔
151
      }
152
      if (!(value1 instanceof Comparable && value2 instanceof Comparable)) {
1!
153
        value1 = value1.toString();
×
154
        value2 = value2.toString();
×
155
      }
156
      result = ((Comparable) value1).compareTo(value2);
1✔
157
    }
158

159
    return result;
1✔
160
  }
161

162
  /**
163
   * Convert value.
164
   *
165
   * @param type
166
   *          the type
167
   * @param value
168
   *          the value
169
   *
170
   * @return the object
171
   */
172
  protected Object convertValue(Class type, String value) {
173
    if (type == String.class) {
1!
174
      return value;
×
175
    }
176
    if (type == Byte.class || type == byte.class) {
1!
UNCOV
177
      return Byte.valueOf(value);
×
178
    } else if (type == Short.class || type == short.class) {
1!
179
      return Short.valueOf(value);
×
180
    } else if (type == Character.class || type == char.class) {
1!
181
      return Character.valueOf(value.charAt(0));
×
182
    } else if (type == Integer.class || type == int.class) {
1✔
183
      return Integer.valueOf(value);
1✔
184
    } else if (type == Long.class || type == long.class) {
1!
185
      return Long.valueOf(value);
×
186
    } else if (type == Float.class || type == float.class) {
1!
187
      return Float.valueOf(value);
×
188
    } else if (type == Double.class || type == double.class) {
1!
189
      return Double.valueOf(value);
×
190
    } else if (type == Boolean.class || type == boolean.class) {
1!
191
      return Boolean.valueOf(value);
×
192
    } else if (type == Date.class) {
1!
193
      return SimpleDateFormatter.format(DATE_MASK, value);
×
194
    } else if (type == BigInteger.class) {
1!
195
      return new BigInteger(value);
×
196
    } else if (type == BigDecimal.class) {
1!
197
      return new BigDecimal(value);
×
198
    } else {
199
      return value;
1✔
200
    }
201

202
  }
203

204
  /**
205
   * This method will add the proper index values to an indexed property string if we are inside an iterate tag.
206
   *
207
   * @param ctx
208
   *          the ctx
209
   * @param tag
210
   *          the tag
211
   *
212
   * @return the resolved property
213
   */
214
  protected String getResolvedProperty(SqlTagContext ctx, SqlTag tag) {
215
    String prop = tag.getPropertyAttr();
1✔
216
    IterateContext itCtx = ctx.peekIterateContext();
1✔
217

218
    if (prop != null) {
1✔
219

220
      if (null != itCtx && itCtx.isAllowNext()) {
1✔
221
        itCtx.next();
1✔
222
        itCtx.setAllowNext(false);
1✔
223
        if (!itCtx.hasNext()) {
1✔
224
          itCtx.setFinal(true);
1✔
225
        }
226
      }
227

228
      if (prop.indexOf(START_INDEX) > -1 && itCtx != null) {
1!
229
        prop = itCtx.addIndexToTagProperty(prop);
1✔
230
      }
231
    }
232

233
    return prop;
1✔
234
  }
235
}
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

© 2026 Coveralls, Inc