田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.vci.server.query.refquery.utils;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.hibernate.SQLQuery;
import org.hibernate.Session;
 
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
 
 
public class RefQueryUtils {
 
    @SuppressWarnings("unchecked")
    public static Map<String, Object> executeQuery(String sql, String[] cnames)
            throws SQLException {
        Map<String, Object> datas = new HashMap<String, Object>();
        Session session = HibernateSessionFactory.getSession();
 
        SQLQuery query = session.createSQLQuery(sql);
 
        List<Object[]> rs = (List<Object[]>) query.list();
        if(!rs.isEmpty()){
            if(rs.iterator().hasNext()){
                Object element = rs.iterator().next();
                if (element instanceof String) {
                    datas.put(cnames[0], element);
                    return datas;
                }
            }
            String attrVal = "";
            for (Object[] o : rs) {
                for (int i = 0; i < cnames.length; i++) {
                    Object resltSet = datas.get(cnames[i]);
                    List<Object> temp = null;
                    if (resltSet instanceof List) {
                        temp = (List<Object>) resltSet;
                    }
                    if(o[i] instanceof Clob){
                        attrVal = String.valueOf(o[i] == null ? "" : clobToString((Clob)o[i]));
                    }else{
                        attrVal = String.valueOf(o[i] == null ? "" : o[i]);
                    }
                    if (temp == null) {
                        List<Object> values = new ArrayList<Object>();
                        values.add(attrVal);
                        datas.put(cnames[i], values);
                    } else {
                        temp.add(attrVal);
                    }
 
                }
            }
        }
        
        return datas;
    }
    
    /**
     * clob转String
     * @param value
     * @return
     */
    public static String clobToString(Clob value) {
        // TODO Auto-generated method stub
        String clobValue = "";
        try {
            Reader is = value.getCharacterStream();
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while(s!=null){
                sb.append(s);
                s=br.readLine();
            }
            clobValue = sb.toString();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return clobValue;
    }
}