Skip to content

Fixed Limit and Offset Support bugs when using Subqueries in where cl… #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2016-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select.render;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.PagingModel;
Expand All @@ -26,13 +27,15 @@ public class FetchFirstPagingModelRenderer {
private static final String OFFSET_PARAMETER = "_offset"; //$NON-NLS-1$
private RenderingStrategy renderingStrategy;
private PagingModel pagingModel;
private AtomicInteger sequence;

public FetchFirstPagingModelRenderer(RenderingStrategy renderingStrategy,
PagingModel pagingModel) {
PagingModel pagingModel, AtomicInteger sequence) {
this.renderingStrategy = renderingStrategy;
this.pagingModel = pagingModel;
this.sequence = sequence;
}

public Optional<FragmentAndParameters> render() {
return pagingModel.offset()
.map(this::renderWithOffset)
Expand All @@ -44,37 +47,45 @@ private Optional<FragmentAndParameters> renderWithOffset(Long offset) {
.map(ffr -> renderOffsetAndFetchFirstRows(offset, ffr))
.orElseGet(() -> renderOffsetOnly(offset));
}

private Optional<FragmentAndParameters> renderFetchFirstRowsOnly() {
return pagingModel.fetchFirstRows().flatMap(this::renderFetchFirstRowsOnly);
}

private Optional<FragmentAndParameters> renderFetchFirstRowsOnly(Long fetchFirstRows) {
String mapKey = formatParameterMapKey(FETCH_FIRST_ROWS_PARAMETER);
return FragmentAndParameters
.withFragment("fetch first " + renderPlaceholder(FETCH_FIRST_ROWS_PARAMETER) //$NON-NLS-1$
+ " rows only") //$NON-NLS-1$
.withParameter(FETCH_FIRST_ROWS_PARAMETER, fetchFirstRows)
.withFragment("fetch first " + renderPlaceholder(mapKey) //$NON-NLS-1$
+ " rows only") //$NON-NLS-1$
.withParameter(mapKey, fetchFirstRows)
.buildOptional();
}

private Optional<FragmentAndParameters> renderOffsetOnly(Long offset) {
return FragmentAndParameters.withFragment("offset " + renderPlaceholder(OFFSET_PARAMETER) //$NON-NLS-1$
+ " rows") //$NON-NLS-1$
.withParameter(OFFSET_PARAMETER, offset)
String mapKey = formatParameterMapKey(OFFSET_PARAMETER);
return FragmentAndParameters.withFragment("offset " + renderPlaceholder(mapKey) //$NON-NLS-1$
+ " rows") //$NON-NLS-1$
.withParameter(mapKey, offset)
.buildOptional();
}

private Optional<FragmentAndParameters> renderOffsetAndFetchFirstRows(Long offset, Long fetchFirstRows) {
return FragmentAndParameters.withFragment("offset " + renderPlaceholder(OFFSET_PARAMETER) //$NON-NLS-1$
+ " rows fetch first " + renderPlaceholder(FETCH_FIRST_ROWS_PARAMETER) //$NON-NLS-1$
+ " rows only") //$NON-NLS-1$
.withParameter(OFFSET_PARAMETER, offset)
.withParameter(FETCH_FIRST_ROWS_PARAMETER, fetchFirstRows)
String mapKey1 = formatParameterMapKey(OFFSET_PARAMETER);
String mapKey2 = formatParameterMapKey(FETCH_FIRST_ROWS_PARAMETER);
return FragmentAndParameters.withFragment("offset " + renderPlaceholder(mapKey1) //$NON-NLS-1$
+ " rows fetch first " + renderPlaceholder(mapKey2) //$NON-NLS-1$
+ " rows only") //$NON-NLS-1$
.withParameter(mapKey1, offset)
.withParameter(mapKey2, fetchFirstRows)
.buildOptional();
}


private String formatParameterMapKey(String parameterMapKey) {
return parameterMapKey + sequence.getAndIncrement(); //$NON-NLS-1$
}

private String renderPlaceholder(String parameterName) {
return renderingStrategy.getFormattedJdbcPlaceholder(RenderingStrategy.DEFAULT_PARAMETER_PREFIX,
parameterName);
parameterName);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2016-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select.render;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.PagingModel;
Expand All @@ -27,35 +28,44 @@ public class LimitAndOffsetPagingModelRenderer {
private RenderingStrategy renderingStrategy;
private Long limit;
private PagingModel pagingModel;
private AtomicInteger sequence;

public LimitAndOffsetPagingModelRenderer(RenderingStrategy renderingStrategy,
Long limit, PagingModel pagingModel) {
Long limit, PagingModel pagingModel, AtomicInteger sequence) {
this.renderingStrategy = renderingStrategy;
this.limit = limit;
this.pagingModel = pagingModel;
this.sequence = sequence;
}

public Optional<FragmentAndParameters> render() {
return pagingModel.offset().map(this::renderLimitAndOffset)
.orElseGet(this::renderLimitOnly);
}

private Optional<FragmentAndParameters> renderLimitOnly() {
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(LIMIT_PARAMETER)) //$NON-NLS-1$
.withParameter(LIMIT_PARAMETER, limit)
String mapKey = formatParameterMapKey(LIMIT_PARAMETER);
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(mapKey)) //$NON-NLS-1$
.withParameter(mapKey, limit)
.buildOptional();
}

private Optional<FragmentAndParameters> renderLimitAndOffset(Long offset) {
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(LIMIT_PARAMETER) //$NON-NLS-1$
+ " offset " + renderPlaceholder(OFFSET_PARAMETER)) //$NON-NLS-1$
.withParameter(LIMIT_PARAMETER, limit)
.withParameter(OFFSET_PARAMETER, offset)
String mapKey1 = formatParameterMapKey(LIMIT_PARAMETER);
String mapKey2 = formatParameterMapKey(OFFSET_PARAMETER);
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(mapKey1) //$NON-NLS-1$
+ " offset " + renderPlaceholder(mapKey2)) //$NON-NLS-1$
.withParameter(mapKey1, limit)
.withParameter(mapKey2, offset)
.buildOptional();
}


private String formatParameterMapKey(String parameterMapKey) {
return parameterMapKey + sequence.getAndIncrement(); //$NON-NLS-1$
}

private String renderPlaceholder(String parameterName) {
return renderingStrategy.getFormattedJdbcPlaceholder(RenderingStrategy.DEFAULT_PARAMETER_PREFIX,
parameterName);
parameterName);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2016-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select.render;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.PagingModel;
Expand All @@ -25,30 +26,33 @@
public class PagingModelRenderer {
private RenderingStrategy renderingStrategy;
private PagingModel pagingModel;
private AtomicInteger sequence;

private PagingModelRenderer(Builder builder) {
renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
pagingModel = Objects.requireNonNull(builder.pagingModel);
sequence = Objects.requireNonNull(builder.sequence);
}

public Optional<FragmentAndParameters> render() {
return pagingModel.limit().map(this::limitAndOffsetRender)
.orElseGet(this::fetchFirstRender);
}

private Optional<FragmentAndParameters> limitAndOffsetRender(Long limit) {
return new LimitAndOffsetPagingModelRenderer(renderingStrategy, limit,
pagingModel).render();
pagingModel, sequence).render();
}

private Optional<FragmentAndParameters> fetchFirstRender() {
return new FetchFirstPagingModelRenderer(renderingStrategy, pagingModel).render();
return new FetchFirstPagingModelRenderer(renderingStrategy, pagingModel, sequence).render();
}

public static class Builder {
private RenderingStrategy renderingStrategy;
private PagingModel pagingModel;

private AtomicInteger sequence;

public Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
this.renderingStrategy = renderingStrategy;
return this;
Expand All @@ -58,7 +62,12 @@ public Builder withPagingModel(PagingModel pagingModel) {
this.pagingModel = pagingModel;
return this;
}


public Builder withSequence(AtomicInteger sequence) {
this.sequence = sequence;
return this;
}

public PagingModelRenderer build() {
return new PagingModelRenderer(this);
}
Expand Down
Loading