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