001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (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 io.jboot.db.model; 017 018 019import com.jfinal.ext.kit.DateKit; 020import io.jboot.utils.CollectionUtil; 021import io.jboot.utils.StrUtil; 022 023import java.lang.reflect.Array; 024import java.util.Collection; 025import java.util.Date; 026import java.util.LinkedList; 027import java.util.List; 028import java.util.regex.Matcher; 029 030class Util { 031 032 static final Object[] NULL_PARA_ARRAY = new Object[0]; 033 034 static Object[] getValueArray(List<Column> columns) { 035 036 if (columns == null || columns.isEmpty()) { 037 return NULL_PARA_ARRAY; 038 } 039 040 List<Object> paras = new LinkedList<>(); 041 042 for (Column column : columns) { 043 if (!column.hasPara()) { 044 continue; 045 } 046 Object value = column.getValue(); 047 if (value != null) { 048 if (value.getClass().isArray()) { 049 Object[] values = (Object[]) value; 050 for (Object v : values) { 051 if (v != null && ( 052 v.getClass() == int[].class 053 || v.getClass() == long[].class 054 || v.getClass() == short[].class 055 || v.getClass() == float[].class 056 || v.getClass() == double[].class 057 )) { 058 for (int i = 0; i < Array.getLength(v); i++) { 059 paras.add(Array.get(v, i)); 060 } 061 } else { 062 paras.add(v); 063 } 064 } 065 } else { 066 paras.add(value); 067 } 068 } 069 } 070 071 return paras.isEmpty() ? NULL_PARA_ARRAY : paras.toArray(); 072 } 073 074 075 076 static String replaceSqlPara(String sql, Object value) { 077 // null 078 if (value == null) { 079 return sql.replaceFirst("\\?", "null"); 080 } 081 // number 082 else if (value instanceof Number) { 083 return sql.replaceFirst("\\?", value.toString()); 084 } 085 // numeric 086 else if (value instanceof String && StrUtil.isNumeric((String) value)) { 087 return sql.replaceFirst("\\?", (String) value); 088 } 089 // other 090 else { 091 StringBuilder sb = new StringBuilder(); 092 if (value instanceof Date) { 093 sb.append(DateKit.toStr((Date) value, DateKit.timeStampPattern)); 094 } else { 095 sb.append(value); 096 } 097 return sql.replaceFirst("\\?", Matcher.quoteReplacement(sb.toString())); 098 } 099 } 100 101 102 static String deleteWhitespace(String str) { 103 final int strLen = str.length(); 104 final char[] chs = new char[strLen]; 105 int count = 0; 106 for (int i = 0; i < strLen; i++) { 107 if (!Character.isWhitespace(str.charAt(i))) { 108 chs[count++] = str.charAt(i); 109 } 110 } 111 if (count == strLen) { 112 return str; 113 } 114 return new String(chs, 0, count); 115 } 116 117 118 static String array2String(Object[] a) { 119 if (a == null) { 120 return "null"; 121 } 122 123 int iMax = a.length - 1; 124 if (iMax == -1) { 125 return "[]"; 126 } 127 128 StringBuilder b = new StringBuilder(); 129 b.append('['); 130 for (int i = 0; ; i++) { 131 b.append(a[i]); 132 if (i == iMax) { 133 return b.append(']').toString(); 134 } 135 b.append("-"); 136 } 137 } 138 139 140 static void checkNullParas(Columns columns, String name, Object... paras) { 141 if (columns.isUseSafeMode()) { 142 for (Object obj : paras) { 143 if (obj == null) { 144 throw new NullPointerException("Column \"" + name + "\" para is null, Columns must has not null para value in safeMode."); 145 } 146 } 147 } 148 } 149 150 static void checkNullParas(Columns columns, Object... paras) { 151 if (columns.isUseSafeMode()) { 152 for (Object obj : paras) { 153 if (obj == null) { 154 throw new NullPointerException("Columns must has not null para value in safeMode."); 155 } 156 } 157 } 158 } 159 160 static void checkNullParas(Columns columns, Collection<?> collection) { 161 if (columns.isUseSafeMode()) { 162 if (CollectionUtil.isEmpty(collection)) { 163 throw new NullPointerException("Columns must has not empty collection in safeMode."); 164 } 165 } 166 } 167}