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 if (condition instanceof Brackets) { 042 List<QueryWrapper> childQueryWrapper = getChildSelect(((Brackets) condition).getChildCondition()); 043 if (!childQueryWrapper.isEmpty()) { 044 if (list == null) { 045 list = new ArrayList<>(); 046 } 047 list.addAll(childQueryWrapper); 048 } 049 } 050 // not Brackets 051 else { 052 Object value = condition.getValue(); 053 if (value instanceof QueryWrapper) { 054 if (list == null) { 055 list = new ArrayList<>(); 056 } 057 list.add((QueryWrapper) value); 058 list.addAll(((QueryWrapper) value).getChildSelect()); 059 } else if (value != null && value.getClass().isArray()) { 060 for (int i = 0; i < Array.getLength(value); i++) { 061 Object arrayValue = Array.get(value, i); 062 if (arrayValue instanceof QueryWrapper) { 063 if (list == null) { 064 list = new ArrayList<>(); 065 } 066 list.add((QueryWrapper) arrayValue); 067 list.addAll(((QueryWrapper) arrayValue).getChildSelect()); 068 } 069 } 070 } 071 } 072 } 073 condition = condition.next; 074 } 075 return list == null ? Collections.emptyList() : list; 076 } 077 078 079 static Object[] getValues(QueryCondition condition) { 080 if (condition == null) { 081 return NULL_PARA_ARRAY; 082 } 083 084 List<Object> paras = new ArrayList<>(); 085 getValues(condition, paras); 086 087 return paras.isEmpty() ? NULL_PARA_ARRAY : paras.toArray(); 088 } 089 090 091 private static void getValues(QueryCondition condition, List<Object> paras) { 092 if (condition == null) { 093 return; 094 } 095 096 Object value = condition.getValue(); 097 if (value == null 098 || value instanceof QueryColumn 099 || value instanceof RawValue) { 100 getValues(condition.next, paras); 101 return; 102 } 103 104 if (value.getClass().isArray()) { 105 Object[] values = (Object[]) value; 106 for (Object object : values) { 107 if (object != null && (object.getClass().isArray() 108 || object.getClass() == int[].class 109 || object.getClass() == long[].class 110 || object.getClass() == short[].class 111 || object.getClass() == float[].class 112 || object.getClass() == double[].class)) { 113 for (int i = 0; i < Array.getLength(object); i++) { 114 paras.add(Array.get(object, i)); 115 } 116 } else { 117 paras.add(object); 118 } 119 } 120 } else if (value instanceof QueryWrapper) { 121 Object[] valueArray = ((QueryWrapper) value).getValueArray(); 122 paras.addAll(Arrays.asList(valueArray)); 123 } else { 124 paras.add(value); 125 } 126 127 getValues(condition.next, paras); 128 } 129 130 131 public static String getColumnTableName(List<QueryTable> queryTables, QueryTable queryTable) { 132 if (queryTables == null) { 133 return ""; 134 } 135 136 if (queryTables.size() == 1 && queryTables.get(0).isSameTable(queryTable)) { 137 return ""; 138 } 139 140 QueryTable realTable = getRealTable(queryTables, queryTable); 141 if (realTable == null) { 142 return ""; 143 } 144 145 return StringUtil.isNotBlank(realTable.alias) ? realTable.alias : realTable.name; 146 } 147 148 public static QueryTable getRealTable(List<QueryTable> queryTables, QueryTable queryTable) { 149 if (CollectionUtil.isEmpty(queryTables)) { 150 return queryTable; 151 } 152 153 if (queryTable == null && queryTables.size() == 1) { 154 return queryTables.get(0); 155 } 156 157 for (QueryTable table : queryTables) { 158 if (table.isSameTable(queryTable)) { 159 return table; 160 } 161 } 162 return queryTable; 163 } 164 165 166}