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
018
019import com.mybatisflex.core.util.CollectionUtil;
020import com.mybatisflex.core.util.StringUtil;
021
022import java.lang.reflect.Array;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.List;
027
028class WrapperUtil {
029
030
031    static String buildAsAlias(String alias) {
032        return StringUtil.isBlank(alias) ? "" : " AS " + alias;
033    }
034
035    static final Object[] NULL_PARA_ARRAY = new Object[0];
036
037    static List<QueryWrapper> getChildSelect(QueryCondition condition) {
038        List<QueryWrapper> list = null;
039        while (condition != null) {
040            if (condition.checkEffective()) {
041                Object value = condition.getValue();
042                if (value instanceof QueryWrapper) {
043                    if (list == null) {
044                        list = new ArrayList<>();
045                    }
046                    list.add((QueryWrapper) value);
047                    list.addAll(((QueryWrapper) value).getChildSelect());
048                } else if (value != null && value.getClass().isArray()) {
049                    for (int i = 0; i < Array.getLength(value); i++) {
050                        Object arrayValue = Array.get(value, i);
051                        if (arrayValue instanceof QueryWrapper) {
052                            if (list == null) {
053                                list = new ArrayList<>();
054                            }
055                            list.add((QueryWrapper) arrayValue);
056                            list.addAll(((QueryWrapper) arrayValue).getChildSelect());
057                        }
058                    }
059                }
060            }
061            condition = condition.next;
062        }
063        return list == null ? Collections.emptyList() : list;
064    }
065
066
067    static Object[] getValues(QueryCondition condition) {
068        if (condition == null) {
069            return NULL_PARA_ARRAY;
070        }
071
072        List<Object> paras = new ArrayList<>();
073        getValues(condition, paras);
074
075        return paras.isEmpty() ? NULL_PARA_ARRAY : paras.toArray();
076    }
077
078
079    private static void getValues(QueryCondition condition, List<Object> paras) {
080        if (condition == null) {
081            return;
082        }
083
084        Object value = condition.getValue();
085        if (value == null
086                || value instanceof QueryColumn
087                || value instanceof RawValue) {
088            getValues(condition.next, paras);
089            return;
090        }
091
092        if (value.getClass().isArray()) {
093            Object[] values = (Object[]) value;
094            for (Object object : values) {
095                if (object != null && (object.getClass().isArray()
096                        || object.getClass() == int[].class
097                        || object.getClass() == long[].class
098                        || object.getClass() == short[].class
099                        || object.getClass() == float[].class
100                        || object.getClass() == double[].class)) {
101                    for (int i = 0; i < Array.getLength(object); i++) {
102                        paras.add(Array.get(object, i));
103                    }
104                } else {
105                    paras.add(object);
106                }
107            }
108        } else if (value instanceof QueryWrapper) {
109            Object[] valueArray = ((QueryWrapper) value).getValueArray();
110            paras.addAll(Arrays.asList(valueArray));
111        } else {
112            paras.add(value);
113        }
114
115        getValues(condition.next, paras);
116    }
117
118
119    public static String getColumnTableName(List<QueryTable> queryTables, QueryTable queryTable) {
120        if (queryTables == null) {
121            return "";
122        }
123
124        if (queryTables.size() == 1 && queryTables.get(0).isSameTable(queryTable)) {
125            return "";
126        }
127
128        QueryTable realTable = getRealTable(queryTables, queryTable);
129        if (realTable == null) {
130            return "";
131        }
132
133        return StringUtil.isNotBlank(realTable.alias) ? realTable.alias : realTable.name;
134    }
135
136    public static QueryTable getRealTable(List<QueryTable> queryTables, QueryTable queryTable) {
137        if (CollectionUtil.isEmpty(queryTables)) {
138            return queryTable;
139        }
140
141        if (queryTable == null && queryTables.size() == 1) {
142            return queryTables.get(0);
143        }
144
145        for (QueryTable table : queryTables) {
146            if (table.isSameTable(queryTable)) {
147                return table;
148            }
149        }
150        return queryTable;
151    }
152
153
154}