001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.query;
017
018import com.mybatisflex.core.dialect.IDialect;
019import com.mybatisflex.core.util.CollectionUtil;
020import com.mybatisflex.core.util.StringUtil;
021
022import java.util.List;
023import java.util.Map;
024
025/**
026 * Cross Package Invoke
027 * 夸包调用工具类,这么设计的原因,是需要保证 QueryWrapper 方法对于用户的纯净性
028 * 而 framework 又可以通过 CPI 来调用 QueryWrapper 的其他方法
029 */
030
031public class CPI {
032
033    private CPI() {}
034
035    public static Object[] getValueArray(QueryWrapper queryWrapper) {
036        return queryWrapper.getValueArray();
037    }
038
039    public static List<QueryWrapper> getChildSelect(QueryWrapper queryWrapper) {
040        return queryWrapper.getChildSelect();
041    }
042
043    public static List<QueryTable> getQueryTables(QueryWrapper queryWrapper) {
044        return queryWrapper.getQueryTables();
045    }
046
047    public static void setQueryTable(QueryWrapper queryWrapper, List<QueryTable> queryTables) {
048        queryWrapper.setQueryTables(queryTables);
049    }
050
051    public static String getDataSource(QueryWrapper queryWrapper) {
052        return queryWrapper.getDataSource();
053    }
054
055    public static void setDataSource(QueryWrapper queryWrapper, String datasource) {
056        queryWrapper.setDataSource(datasource);
057    }
058
059    public static String getHint(QueryWrapper queryWrapper) {
060        return queryWrapper.getHint();
061    }
062
063    public static void setHint(QueryWrapper queryWrapper, String hint) {
064        queryWrapper.setHint(hint);
065    }
066
067    public static List<QueryColumn> getSelectColumns(QueryWrapper queryWrapper) {
068        return queryWrapper.getSelectColumns();
069    }
070
071    public static void setSelectColumns(QueryWrapper queryWrapper, List<QueryColumn> selectColumns) {
072        queryWrapper.setSelectColumns(selectColumns);
073    }
074
075    public static void setSelectColumnsIfNecessary(QueryWrapper queryWrapper, List<QueryColumn> selectColumns) {
076        if (CollectionUtil.isEmpty(queryWrapper.getSelectColumns())
077                && CollectionUtil.isNotEmpty(selectColumns)
078                && CollectionUtil.isEmpty(CPI.getJoinTables(queryWrapper))
079        ) {
080            queryWrapper.setSelectColumns(selectColumns);
081        }
082    }
083
084    public static List<Join> getJoins(QueryWrapper queryWrapper) {
085        return queryWrapper.getJoins();
086    }
087
088    public static void setJoins(QueryWrapper queryWrapper, List<Join> joins) {
089        queryWrapper.setJoins(joins);
090    }
091
092    public static String getJoinType(Join join) {
093        return join.type;
094    }
095
096    public static QueryTable getJoinQueryTable(Join join) {
097        return join.getQueryTable();
098    }
099
100    public static List<QueryTable> getJoinTables(QueryWrapper queryWrapper) {
101        return queryWrapper.getJoinTables();
102    }
103
104    public static void setJoinTables(QueryWrapper queryWrapper, List<QueryTable> joinTables) {
105        queryWrapper.setJoinTables(joinTables);
106    }
107
108
109    public static QueryCondition getWhereQueryCondition(QueryWrapper queryWrapper) {
110        return queryWrapper.getWhereQueryCondition();
111    }
112
113    public static List<QueryColumn> getGroupByColumns(QueryWrapper queryWrapper) {
114        return queryWrapper.getGroupByColumns();
115    }
116
117    public static void setGroupByColumns(QueryWrapper queryWrapper, List<QueryColumn> groupByColumns) {
118        queryWrapper.setGroupByColumns(groupByColumns);
119    }
120
121    public static QueryCondition getHavingQueryCondition(QueryWrapper queryWrapper) {
122        return queryWrapper.getHavingQueryCondition();
123    }
124
125    public static void setHavingQueryCondition(QueryWrapper queryWrapper, QueryCondition havingQueryCondition) {
126        queryWrapper.setHavingQueryCondition(havingQueryCondition);
127    }
128
129    public static List<QueryOrderBy> getOrderBys(QueryWrapper queryWrapper) {
130        return queryWrapper.getOrderBys();
131    }
132
133    public static void setOrderBys(QueryWrapper queryWrapper, List<QueryOrderBy> orderBys) {
134        queryWrapper.setOrderBys(orderBys);
135    }
136
137    public static List<UnionWrapper> getUnions(QueryWrapper queryWrapper) {
138        return queryWrapper.getUnions();
139    }
140
141    public static void setUnions(QueryWrapper queryWrapper, List<UnionWrapper> unions) {
142        queryWrapper.setUnions(unions);
143    }
144
145
146    public static Integer getLimitOffset(QueryWrapper queryWrapper) {
147        return queryWrapper.getLimitOffset();
148    }
149
150    public static void setLimitOffset(QueryWrapper queryWrapper, Integer limitOffset) {
151        queryWrapper.setLimitOffset(limitOffset);
152    }
153
154    public static Integer getLimitRows(QueryWrapper queryWrapper) {
155        return queryWrapper.getLimitRows();
156    }
157
158    public static void setLimitRows(QueryWrapper queryWrapper, Integer limitRows) {
159        queryWrapper.setLimitRows(limitRows);
160    }
161
162    public static List<String> getEndFragments(QueryWrapper queryWrapper) {
163        return queryWrapper.getEndFragments();
164    }
165
166    public static void setEndFragments(QueryWrapper queryWrapper, List<String> endFragments) {
167        queryWrapper.setEndFragments(endFragments);
168    }
169
170
171    public static Map<String, Object> getContext(QueryWrapper queryWrapper) {
172        return queryWrapper.getContext();
173    }
174
175    public static void setContext(QueryWrapper queryWrapper, Map<String, Object> context) {
176        queryWrapper.setContext(context);
177    }
178
179    public static void putContext(QueryWrapper queryWrapper, String key, Object value) {
180        queryWrapper.putContext(key, value);
181    }
182
183
184    public static <R> R getContext(QueryWrapper queryWrapper, String key) {
185        return queryWrapper.getContext(key);
186    }
187
188
189    public static String toConditionSql(QueryColumn queryColumn, List<QueryTable> queryTables, IDialect dialect) {
190        return queryColumn.toConditionSql(queryTables, dialect);
191    }
192
193    public static String toSelectSql(QueryColumn queryColumn, List<QueryTable> queryTables, IDialect dialect) {
194        return queryColumn.toSelectSql(queryTables, dialect);
195    }
196
197    public static void setFromIfNecessary(QueryWrapper queryWrapper, String tableName) {
198        if (StringUtil.isNotBlank(tableName)
199                && CollectionUtil.isEmpty(queryWrapper.getQueryTables())) {
200            queryWrapper.from(tableName);
201        }
202    }
203
204    public static void setFromIfNecessary(QueryWrapper queryWrapper, String schema, String tableName) {
205        if (StringUtil.isNotBlank(tableName)
206                && CollectionUtil.isEmpty(queryWrapper.getQueryTables())) {
207            queryWrapper.from(new QueryTable(schema, tableName));
208        }
209    }
210
211    public static boolean containsTable(QueryCondition condition, String... tables) {
212        return condition != null && condition.containsTable(tables);
213    }
214
215    public static QueryWrapper getQueryWrapper(SelectQueryColumn selectQueryColumn) {
216        return selectQueryColumn.getQueryWrapper();
217    }
218}