11/**
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.
1515 */
1616package org .mybatis .dynamic .sql .select .render ;
1717
1818import java .util .Optional ;
19+ import java .util .concurrent .atomic .AtomicInteger ;
1920
2021import org .mybatis .dynamic .sql .render .RenderingStrategy ;
2122import org .mybatis .dynamic .sql .select .PagingModel ;
@@ -26,13 +27,15 @@ public class FetchFirstPagingModelRenderer {
2627 private static final String OFFSET_PARAMETER = "_offset" ; //$NON-NLS-1$
2728 private RenderingStrategy renderingStrategy ;
2829 private PagingModel pagingModel ;
30+ private AtomicInteger sequence ;
2931
3032 public FetchFirstPagingModelRenderer (RenderingStrategy renderingStrategy ,
31- PagingModel pagingModel ) {
33+ PagingModel pagingModel , AtomicInteger sequence ) {
3234 this .renderingStrategy = renderingStrategy ;
3335 this .pagingModel = pagingModel ;
36+ this .sequence = sequence ;
3437 }
35-
38+
3639 public Optional <FragmentAndParameters > render () {
3740 return pagingModel .offset ()
3841 .map (this ::renderWithOffset )
@@ -44,37 +47,45 @@ private Optional<FragmentAndParameters> renderWithOffset(Long offset) {
4447 .map (ffr -> renderOffsetAndFetchFirstRows (offset , ffr ))
4548 .orElseGet (() -> renderOffsetOnly (offset ));
4649 }
47-
50+
4851 private Optional <FragmentAndParameters > renderFetchFirstRowsOnly () {
4952 return pagingModel .fetchFirstRows ().flatMap (this ::renderFetchFirstRowsOnly );
5053 }
51-
54+
5255 private Optional <FragmentAndParameters > renderFetchFirstRowsOnly (Long fetchFirstRows ) {
56+ String mapKey = formatParameterMapKey (FETCH_FIRST_ROWS_PARAMETER );
5357 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 )
5761 .buildOptional ();
5862 }
59-
63+
6064 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 )
6469 .buildOptional ();
6570 }
66-
71+
6772 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 )
7380 .buildOptional ();
7481 }
75-
82+
83+ private String formatParameterMapKey (String parameterMapKey ) {
84+ return parameterMapKey + sequence .getAndIncrement (); //$NON-NLS-1$
85+ }
86+
7687 private String renderPlaceholder (String parameterName ) {
7788 return renderingStrategy .getFormattedJdbcPlaceholder (RenderingStrategy .DEFAULT_PARAMETER_PREFIX ,
78- parameterName );
89+ parameterName );
7990 }
8091}
0 commit comments