15
15
*/
16
16
package org .mybatis .dynamic .sql .common ;
17
17
18
+ import static org .mybatis .dynamic .sql .util .StringUtilities .spaceAfter ;
19
+
18
20
import java .util .Objects ;
19
21
import java .util .Optional ;
20
22
import java .util .concurrent .atomic .AtomicInteger ;
30
32
31
33
public abstract class AbstractBooleanExpressionRenderer <M extends AbstractBooleanExpressionModel > {
32
34
protected final M model ;
33
- private final CriterionRenderer criterionRenderer ;
34
35
private final String prefix ;
36
+ private final CriterionRenderer criterionRenderer ;
35
37
36
38
protected AbstractBooleanExpressionRenderer (String prefix , AbstractBuilder <M , ?> builder ) {
37
39
model = Objects .requireNonNull (builder .model );
40
+ this .prefix = Objects .requireNonNull (prefix );
38
41
39
42
criterionRenderer = new CriterionRenderer .Builder ()
40
43
.withSequence (builder .sequence )
41
44
.withRenderingStrategy (builder .renderingStrategy )
42
45
.withTableAliasCalculator (builder .tableAliasCalculator )
43
46
.withParameterName (builder .parameterName )
44
47
.build ();
45
-
46
- this .prefix = Objects .requireNonNull (prefix );
47
48
}
48
49
49
50
public Optional <FragmentAndParameters > render () {
@@ -67,24 +68,29 @@ private Optional<RenderedCriterion> renderWithoutInitialCriterion() {
67
68
private String calculateClause (FragmentCollector collector ) {
68
69
if (collector .hasMultipleFragments ()) {
69
70
return collector .fragments ()
70
- .collect (Collectors .joining (" " , prefix + " " , "" )); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3 $
71
+ .collect (Collectors .joining (" " , spaceAfter ( prefix ) , "" )); //$NON-NLS-1$ //$NON-NLS-2$
71
72
} else {
72
73
return collector .firstFragment ()
73
74
.map (this ::stripEnclosingParenthesesIfPresent )
74
- .map (s -> prefix + " " + s ) //$NON-NLS-1$
75
+ .map (this :: addPrefix )
75
76
.orElse ("" ); //$NON-NLS-1$
76
77
}
77
78
}
78
79
79
80
private String stripEnclosingParenthesesIfPresent (String fragment ) {
80
81
// The fragment will have surrounding open/close parentheses if there is more than one rendered condition.
81
- // Since there is only a single fragment, we don't need these in the where clause
82
+ // Since there is only a single fragment, we don't need these in the final rendered clause
82
83
if (fragment .startsWith ("(" ) && fragment .endsWith (")" )) { //$NON-NLS-1$ //$NON-NLS-2$
83
84
return fragment .substring (1 , fragment .length () - 1 );
84
85
} else {
85
86
return fragment ;
86
87
}
87
88
}
89
+
90
+ private String addPrefix (String fragment ) {
91
+ return spaceAfter (prefix ) + fragment ;
92
+ }
93
+
88
94
public abstract static class AbstractBuilder <M , B extends AbstractBuilder <M , B >> {
89
95
private final M model ;
90
96
private RenderingStrategy renderingStrategy ;
0 commit comments