liulin
2024-07-29 b65029f9d6e351a714682c110343a8c23eb4e793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.lunhan.xxx.common.orm2;
 
import com.lunhan.xxx.common.ConstantFactory;
import com.lunhan.xxx.common.util.CalendarUtil;
import com.lunhan.xxx.common.util.NumericUtil;
 
import java.math.BigDecimal;
import java.sql.Types;
import java.util.Date;
 
/**
 * 处理sql 相关工具类
 */
public final class SqlUtil {
    private SqlUtil() {
        throw new IllegalStateException("Utility class");
    }
 
    /**
     * 获取java类型对应的sql数据类型
     * @param value 参数值
     */
    public static Integer getParameterType(Object value) {
        Integer type = Types.VARCHAR;
 
        if (value instanceof Integer) {
            type = Types.INTEGER;
        } else if (value instanceof Long) {
            type = Types.BIGINT;
        } else if (value instanceof Float) {
            type = Types.DECIMAL;
        } else if (value instanceof Double) {
            type = Types.DECIMAL;
        } else if (value instanceof Boolean) {
            type = Types.BOOLEAN;
        } else if (value instanceof CalendarUtil) {
            type = Types.TIME;
        } else if (value instanceof Date) {
            type = Types.TIMESTAMP;
        } else if (value instanceof BigDecimal) {
            type = Types.DECIMAL;
        }
        return type;
    }
 
    /**
     * 构建in的where条件
     * @param inQuantity in条件个数
     */
    public static String getListInWhereSql(Integer inQuantity) {
        if(NumericUtil.tryParseInt(inQuantity).compareTo(0)<1) {
            return " 1!=1";
        }
        StringBuilder sbSql = new StringBuilder();
        sbSql.append("(");
        for (int i=0; i<inQuantity; i++) {
            if(i>0) {
                sbSql.append(ConstantFactory.SQL_STR_COMMA);
            }
            sbSql.append("?");
        }
        sbSql.append(")");
        return sbSql.toString();
    }
}