IT-KIMI_SHI\SINOIT.KIMI
2018-06-04 b12dfac6e495d6cf38df6b7e5222191f59762900
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package foundation.persist;
 
import java.sql.Connection;
 
import foundation.data.Entity;
import foundation.data.EntitySet;
import foundation.data.Page;
import foundation.persist.loader.EntityLoader;
import foundation.persist.loader.EntitySetLoader;
import foundation.persist.loader.ValueLoader;
import foundation.persist.sql.NamedSQL;
import foundation.persist.sql.SQLRunner;
import foundation.util.Util;
 
public class DataHandler {
 
    public static EntitySet getDataSet(String tableName) throws Exception {
        return getDataSet(tableName, null);
    }
    
    public static EntitySet getDataSet(String tableName, String filter) throws Exception {
        return getDataSet(tableName, filter, null);
    }
    
    public static EntitySet getDataSet(String tableName, String filter, String orderby) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getDataSet");
 
        namedSQL.setTableName(tableName);
        namedSQL.setFilter(filter);
        namedSQL.setOrderBy(orderby);        
        
        EntitySetLoader loader = new EntitySetLoader(tableName);
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getDataSet();
    }
    
    public static EntitySet getDataSetByPage(String tableName, String filter, Page page, String orderby) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getSetByPage");
 
        namedSQL.setTableName(tableName);
        namedSQL.setFilter(filter);
        namedSQL.setParam("beginNo", page.getBeginRecordNo_1());
        namedSQL.setParam("pageSize", page.getPageSize());        
        namedSQL.setOrderBy(orderby);        
        
        EntitySetLoader loader = new EntitySetLoader(tableName);
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getDataSet();
    }
 
    public static Entity getLine(String tableName, String id) throws Exception {
        TableMeta tableMeta = TableMetaCenter.getInstance().get(tableName);
        
        NamedSQL namedSQL = NamedSQL.getInstance("getLineById");
        namedSQL.setTableName(tableName);
        namedSQL.setParam("fieldNameId", tableMeta.getFiledName_Key());
        namedSQL.setParam("id", Util.quotedStr(id));
        
        EntityLoader loader = new EntityLoader(tableName);
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getEntity();
    }
    
    public static void deleteById(String tableName, String id) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("deleteById");
        namedSQL.setTableName(tableName);
        namedSQL.setParam("id", id);
        
        SQLRunner.execSQL(namedSQL);
    }
 
    public static int getCount(String tableName, String filter) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getCount");
        namedSQL.setTableName(tableName);
        namedSQL.setFilter(filter);
        
        ValueLoader loader = new ValueLoader();
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getInt();
    }
 
    public static EntitySet getSetByPage(String tableName, String filter, Page page) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getSetByPage");
        namedSQL.setTableName(tableName);
        namedSQL.setFilter(filter);
        namedSQL.setParam("beginNo", page.getBeginRecordNo());
        namedSQL.setParam("endNo", page.getEndRecordNo());
        
        EntitySetLoader loader = new EntitySetLoader(tableName);
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getDataSet();
    }
    
    public static EntitySet getSetByPage(TableMeta tableMeta, String filter, Page page) throws Exception {
        String tableName = tableMeta.getName();
        String fieldNames = tableMeta.getDoubleQuotedFieldNames();
        
        NamedSQL namedSQL = NamedSQL.getInstance("getSetByPage");
        namedSQL.setTableName(tableName);
        namedSQL.setFilter(filter);
        namedSQL.setParam("fieldNames", fieldNames);    
        namedSQL.setParam("beginNo", page.getBeginRecordNo());
        namedSQL.setParam("endNo", page.getEndRecordNo());
        
        EntitySetLoader loader = new EntitySetLoader(tableName);
        loader.setTableMeta(tableMeta);
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getDataSet();
    }
    
    public static EntitySet getSetByPage(TableMeta tableMeta, String fields, String filter, Page page) throws Exception {
        String tableName = tableMeta.getName();
        
        NamedSQL namedSQL = NamedSQL.getInstance("getSetByPage");
        namedSQL.setTableName(tableName);
        namedSQL.setFilter(filter);
        namedSQL.setParam("fieldNames", fields);    
        namedSQL.setParam("beginNo", page.getBeginRecordNo());
        namedSQL.setParam("endNo", page.getEndRecordNo());
        
        EntitySetLoader loader = new EntitySetLoader(tableName);
        loader.setTableMeta(tableMeta);
        SQLRunner.getData(namedSQL, loader);
        
        return loader.getDataSet();
    }
 
    public static void addLine(Entity entity) throws Exception {
        addLine(null, entity);
    }
    
    public static void addLine(Connection conn, Entity entity) throws Exception {
        TableMeta tableMeta = entity.getTableMeta();
        String tableName = tableMeta.getName();    
        
        NamedSQL namedSQL = NamedSQL.getInstance("insert");
        namedSQL.setTableName(tableName);
        namedSQL.setFieldNames(tableMeta, entity);
        namedSQL.setValues(entity);
        
        SQLRunner.execSQL(conn, namedSQL);
    }
 
    public static void saveLine(Entity entity) throws Exception {
        TableMeta tableMeta = entity.getTableMeta();
        String tableName = tableMeta.getName();    
        
        NamedSQL namedSQL = NamedSQL.getInstance("getCountOfId");
        namedSQL.setTableName(tableName);
        namedSQL.setParam("id", Util.quotedStr(entity.getString("id")));
        int cnt = SQLRunner.getInteger(namedSQL);
        
        if (cnt == 0) {
            addLine(entity); 
        }
        else {
            updateLine(entity);
        }
    }
 
    public static void updateLine(Entity entity) throws Exception {
        TableMeta tableMeta = entity.getTableMeta();
        String tableName = tableMeta.getName();    
        
        NamedSQL namedSQL = NamedSQL.getInstance("updateById");
        namedSQL.setTableName(tableName);
        namedSQL.setFieldNameValues(entity);
        namedSQL.setParam("fieldNameId", "id");
        namedSQL.setParam("id", Util.quotedStr(entity.getString("id")));
        
        SQLRunner.execSQL(namedSQL);        
    }
    
    public static void updateLine(Entity first, String key) {
        
    }
    
    public static void deleteLine(Entity entity) throws Exception {
        TableMeta tableMeta = entity.getTableMeta();
        String tableName = tableMeta.getName();    
        
        NamedSQL namedSQL = NamedSQL.getInstance("deleteById");
        namedSQL.setTableName(tableName);
        namedSQL.setParam("id", entity.getString("id"));
        
        SQLRunner.execSQL(namedSQL);
    }
 
    
 
}