david-PC\david
2018-06-12 cc7f57619fd09f68582b748a3580402717b84c50
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
package frame.persist.loader;
 
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
import frame.config.Configer;
import frame.persist.ILoadable;
 
 
public class PrimaryKeyLoader implements ILoadable {
 
    private Map<String, String> items;
    private String schema;
    private String keyField;
    private String table;
    
    
    public PrimaryKeyLoader() {
        items = new HashMap<String, String>();
    }
    
    public void load(ResultSet rslt, Object ...arg) throws Exception {
        String table_schem;
        String fieldName;
        
        while (rslt.next()) {
            table_schem = rslt.getString(1);
            fieldName = rslt.getString(3);
            keyField = fieldName;
            
            append(table_schem, fieldName);
        }
    }
 
    private void append(String tableSchem, String fieldName) {
        items.put(tableSchem.toLowerCase(), fieldName);
    }
 
    public String getPrimaryKeyField() throws Exception {
        int size = items.size();
        
        if (size == 0) {
            return null;
        }
        
        if (size == 1) {
            return keyField;
        }
        
        Set<String> keys = items.keySet();
        
        if (schema == null) {
            schema = Configer.getDataBase_Schema();
        }
        
        for (String key: keys) {
            if (key.equalsIgnoreCase(schema)) {
                keyField = items.get(key);
                return keyField;
            }
        }
        
        throw new Exception("can not get primary key field: more than one schema has the table [" + table + "], you should privade schema");
    }
 
    public void setSchema(String schema) {
        this.schema = schema;
    }
 
}