|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 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 org.mybatis.dynamic.sql.select; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.mybatis.dynamic.sql.SortSpecification; |
| 25 | +import org.mybatis.dynamic.sql.common.OrderByModel; |
| 26 | +import org.mybatis.dynamic.sql.util.Buildable; |
| 27 | +public class MultiSelectDSL implements Buildable<MultiSelectModel> { |
| 28 | + private final List<MultiSelectModel.UnionQuery> unionQueries = new ArrayList<>(); |
| 29 | + private final SelectModel initialSelect; |
| 30 | + private OrderByModel orderByModel; |
| 31 | + private Long limit; |
| 32 | + private Long offset; |
| 33 | + private Long fetchFirstRows; |
| 34 | + |
| 35 | + public MultiSelectDSL(Buildable<SelectModel> builder) { |
| 36 | + initialSelect = builder.build(); |
| 37 | + } |
| 38 | + |
| 39 | + public MultiSelectDSL union(Buildable<SelectModel> builder) { |
| 40 | + unionQueries.add(new MultiSelectModel.UnionQuery("union", builder.build())); //$NON-NLS-1$ |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public MultiSelectDSL unionAll(Buildable<SelectModel> builder) { |
| 45 | + unionQueries.add(new MultiSelectModel.UnionQuery("union all", builder.build())); //$NON-NLS-1$ |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public MultiSelectDSL orderBy(SortSpecification... columns) { |
| 50 | + return orderBy(Arrays.asList(columns)); |
| 51 | + } |
| 52 | + |
| 53 | + public MultiSelectDSL orderBy(Collection<SortSpecification> columns) { |
| 54 | + orderByModel = OrderByModel.of(columns); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public LimitFinisher limit(long limit) { |
| 59 | + this.limit = limit; |
| 60 | + return new LimitFinisher(); |
| 61 | + } |
| 62 | + |
| 63 | + public OffsetFirstFinisher offset(long offset) { |
| 64 | + this.offset = offset; |
| 65 | + return new OffsetFirstFinisher(); |
| 66 | + } |
| 67 | + |
| 68 | + public FetchFirstFinisher fetchFirst(long fetchFirstRows) { |
| 69 | + this.fetchFirstRows = fetchFirstRows; |
| 70 | + return new FetchFirstFinisher(); |
| 71 | + } |
| 72 | + |
| 73 | + @NotNull |
| 74 | + @Override |
| 75 | + public MultiSelectModel build() { |
| 76 | + return new MultiSelectModel.Builder() |
| 77 | + .withInitialSelect(initialSelect) |
| 78 | + .withUnionQueries(unionQueries) |
| 79 | + .withOrderByModel(orderByModel) |
| 80 | + .withPagingModel(buildPagingModel()) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + private PagingModel buildPagingModel() { |
| 85 | + if (limit == null && offset == null && fetchFirstRows == null) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + return new PagingModel.Builder() |
| 90 | + .withLimit(limit) |
| 91 | + .withOffset(offset) |
| 92 | + .withFetchFirstRows(fetchFirstRows) |
| 93 | + .build(); |
| 94 | + } |
| 95 | + |
| 96 | + public class LimitFinisher implements Buildable<MultiSelectModel> { |
| 97 | + public OffsetFinisher offset(long offset) { |
| 98 | + MultiSelectDSL.this.offset(offset); |
| 99 | + return new OffsetFinisher(); |
| 100 | + } |
| 101 | + |
| 102 | + @NotNull |
| 103 | + @Override |
| 104 | + public MultiSelectModel build() { |
| 105 | + return MultiSelectDSL.this.build(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public class OffsetFinisher implements Buildable<MultiSelectModel> { |
| 110 | + @NotNull |
| 111 | + @Override |
| 112 | + public MultiSelectModel build() { |
| 113 | + return MultiSelectDSL.this.build(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public class OffsetFirstFinisher implements Buildable<MultiSelectModel> { |
| 118 | + public FetchFirstFinisher fetchFirst(long fetchFirstRows) { |
| 119 | + MultiSelectDSL.this.fetchFirst(fetchFirstRows); |
| 120 | + return new FetchFirstFinisher(); |
| 121 | + } |
| 122 | + |
| 123 | + @NotNull |
| 124 | + @Override |
| 125 | + public MultiSelectModel build() { |
| 126 | + return MultiSelectDSL.this.build(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public class FetchFirstFinisher { |
| 131 | + public RowsOnlyFinisher rowsOnly() { |
| 132 | + return new RowsOnlyFinisher(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public class RowsOnlyFinisher implements Buildable<MultiSelectModel> { |
| 137 | + @NotNull |
| 138 | + @Override |
| 139 | + public MultiSelectModel build() { |
| 140 | + return MultiSelectDSL.this.build(); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments