1
1
/**
2
- * Copyright 2016-2019 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
- * http://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.
2
+ * Copyright 2016-2019 the original author or authors.
3
+ * <p>
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
+ * <p>
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ * <p>
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
15
*/
16
16
package org .mybatis .dynamic .sql .select .render ;
17
17
18
18
import java .util .Optional ;
19
+ import java .util .concurrent .atomic .AtomicInteger ;
19
20
20
21
import org .mybatis .dynamic .sql .render .RenderingStrategy ;
21
22
import org .mybatis .dynamic .sql .select .PagingModel ;
@@ -26,13 +27,15 @@ public class FetchFirstPagingModelRenderer {
26
27
private static final String OFFSET_PARAMETER = "_offset" ; //$NON-NLS-1$
27
28
private RenderingStrategy renderingStrategy ;
28
29
private PagingModel pagingModel ;
30
+ private AtomicInteger sequence ;
29
31
30
32
public FetchFirstPagingModelRenderer (RenderingStrategy renderingStrategy ,
31
- PagingModel pagingModel ) {
33
+ PagingModel pagingModel , AtomicInteger sequence ) {
32
34
this .renderingStrategy = renderingStrategy ;
33
35
this .pagingModel = pagingModel ;
36
+ this .sequence = sequence ;
34
37
}
35
-
38
+
36
39
public Optional <FragmentAndParameters > render () {
37
40
return pagingModel .offset ()
38
41
.map (this ::renderWithOffset )
@@ -44,37 +47,45 @@ private Optional<FragmentAndParameters> renderWithOffset(Long offset) {
44
47
.map (ffr -> renderOffsetAndFetchFirstRows (offset , ffr ))
45
48
.orElseGet (() -> renderOffsetOnly (offset ));
46
49
}
47
-
50
+
48
51
private Optional <FragmentAndParameters > renderFetchFirstRowsOnly () {
49
52
return pagingModel .fetchFirstRows ().flatMap (this ::renderFetchFirstRowsOnly );
50
53
}
51
-
54
+
52
55
private Optional <FragmentAndParameters > renderFetchFirstRowsOnly (Long fetchFirstRows ) {
56
+ String mapKey = formatParameterMapKey (FETCH_FIRST_ROWS_PARAMETER );
53
57
return FragmentAndParameters
54
- .withFragment ("fetch first " + renderPlaceholder (FETCH_FIRST_ROWS_PARAMETER ) //$NON-NLS-1$
55
- + " rows only" ) //$NON-NLS-1$
56
- .withParameter (FETCH_FIRST_ROWS_PARAMETER , fetchFirstRows )
58
+ .withFragment ("fetch first " + renderPlaceholder (mapKey ) //$NON-NLS-1$
59
+ + " rows only" ) //$NON-NLS-1$
60
+ .withParameter (mapKey , fetchFirstRows )
57
61
.buildOptional ();
58
62
}
59
-
63
+
60
64
private Optional <FragmentAndParameters > renderOffsetOnly (Long offset ) {
61
- return FragmentAndParameters .withFragment ("offset " + renderPlaceholder (OFFSET_PARAMETER ) //$NON-NLS-1$
62
- + " rows" ) //$NON-NLS-1$
63
- .withParameter (OFFSET_PARAMETER , offset )
65
+ String mapKey = formatParameterMapKey (OFFSET_PARAMETER );
66
+ return FragmentAndParameters .withFragment ("offset " + renderPlaceholder (mapKey ) //$NON-NLS-1$
67
+ + " rows" ) //$NON-NLS-1$
68
+ .withParameter (mapKey , offset )
64
69
.buildOptional ();
65
70
}
66
-
71
+
67
72
private Optional <FragmentAndParameters > renderOffsetAndFetchFirstRows (Long offset , Long fetchFirstRows ) {
68
- return FragmentAndParameters .withFragment ("offset " + renderPlaceholder (OFFSET_PARAMETER ) //$NON-NLS-1$
69
- + " rows fetch first " + renderPlaceholder (FETCH_FIRST_ROWS_PARAMETER ) //$NON-NLS-1$
70
- + " rows only" ) //$NON-NLS-1$
71
- .withParameter (OFFSET_PARAMETER , offset )
72
- .withParameter (FETCH_FIRST_ROWS_PARAMETER , fetchFirstRows )
73
+ String mapKey1 = formatParameterMapKey (OFFSET_PARAMETER );
74
+ String mapKey2 = formatParameterMapKey (FETCH_FIRST_ROWS_PARAMETER );
75
+ return FragmentAndParameters .withFragment ("offset " + renderPlaceholder (mapKey1 ) //$NON-NLS-1$
76
+ + " rows fetch first " + renderPlaceholder (mapKey2 ) //$NON-NLS-1$
77
+ + " rows only" ) //$NON-NLS-1$
78
+ .withParameter (mapKey1 , offset )
79
+ .withParameter (mapKey2 , fetchFirstRows )
73
80
.buildOptional ();
74
81
}
75
-
82
+
83
+ private String formatParameterMapKey (String parameterMapKey ) {
84
+ return parameterMapKey + sequence .getAndIncrement (); //$NON-NLS-1$
85
+ }
86
+
76
87
private String renderPlaceholder (String parameterName ) {
77
88
return renderingStrategy .getFormattedJdbcPlaceholder (RenderingStrategy .DEFAULT_PARAMETER_PREFIX ,
78
- parameterName );
89
+ parameterName );
79
90
}
80
91
}
0 commit comments