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

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

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
    }
124
    throw new RuntimeException("Error comparing in conditional fragment.  Uknown 'compare to' values.");
×
125
  }
126

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

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

158
    return result;
1✔
159
  }
160

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

210
  }
211

212
  /**
213
   * This method will add the proper index values to an indexed property string if we are inside an iterate tag.
214
   *
215
   * @param ctx
216
   *          the ctx
217
   * @param tag
218
   *          the tag
219
   *
220
   * @return the resolved property
221
   */
222
  protected String getResolvedProperty(SqlTagContext ctx, SqlTag tag) {
223
    String prop = tag.getPropertyAttr();
1✔
224
    IterateContext itCtx = ctx.peekIterateContext();
1✔
225

226
    if (prop != null) {
1✔
227

228
      if (null != itCtx && itCtx.isAllowNext()) {
1✔
229
        itCtx.next();
1✔
230
        itCtx.setAllowNext(false);
1✔
231
        if (!itCtx.hasNext()) {
1✔
232
          itCtx.setFinal(true);
1✔
233
        }
234
      }
235

236
      if (prop.indexOf(START_INDEX) > -1 && itCtx != null) {
1!
237
        prop = itCtx.addIndexToTagProperty(prop);
1✔
238
      }
239
    }
240

241
    return prop;
1✔
242
  }
243
}
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