pers.clare.hisql.util.SQLQueryUtil Maven / Gradle / Ivy
The newest version!
package pers.clare.hisql.util;
import pers.clare.hisql.exception.HiSqlException;
import pers.clare.hisql.function.ArgumentHandler;
import pers.clare.hisql.query.SQLQuery;
import pers.clare.hisql.query.SQLQueryBuilder;
import pers.clare.hisql.query.SQLQueryReplace;
import pers.clare.hisql.query.SQLQueryReplaceBuilder;
import pers.clare.hisql.support.SqlReplace;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class SQLQueryUtil {
private SQLQueryUtil() {
}
public static void appendValue(
StringBuilder sb
, Object value
) {
if (value instanceof String) {
sb.append('\'');
char[] cs = ((String) value).toCharArray();
for (char c : cs) {
switch (c) {
case '\'':
case '\\':
sb.append(c);
break;
}
sb.append(c);
}
sb.append('\'');
} else {
sb.append(value);
}
}
// gen (?,?,?,?) or ((?,?),(?,?))
public static void appendInValue(
StringBuilder sb
, Object value
) {
Class> valueClass = value.getClass();
if (valueClass.isArray()) {
sb.append('(');
if (value instanceof Object[]) {
Object[] vs = (Object[]) value;
if (vs.length == 0) throw new IllegalArgumentException("SQL WHERE IN doesn't empty value");
for (Object v : vs) appendInValue(sb, v);
} else if (value instanceof int[]) {
int[] vs = (int[]) value;
if (vs.length == 0) throw new IllegalArgumentException("SQL WHERE IN doesn't empty value");
for (int v : vs) appendInValue(sb, v);
} else if (value instanceof long[]) {
long[] vs = (long[]) value;
if (vs.length == 0) throw new IllegalArgumentException("SQL WHERE IN doesn't empty value");
for (long v : vs) appendInValue(sb, v);
} else if (value instanceof char[]) {
char[] vs = (char[]) value;
if (vs.length == 0) throw new IllegalArgumentException("SQL WHERE IN doesn't empty value");
for (char v : vs) appendInValue(sb, v);
}
sb.deleteCharAt(sb.length() - 1).append(')');
} else if (Collection.class.isAssignableFrom(valueClass)) {
@SuppressWarnings("unchecked")
Collection