许鹏程
2024-11-20 5060d5d4d298d7e8149dbace1bf74152b6ef5e45
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.product.datasource.dao;
 
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldMetaEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.datasource.config.DataBaseType;
import com.product.datasource.entity.BatchResultEntity;
import com.product.datasource.entity.UpdateFilterEntity;
 
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @Author cheng
 * @Date 2022/7/6 16:42
 * @Desc
 */
public interface Dao {
 
    /**
     * 获取数据库类型
     *
     * @return
     * @throws BaseException
     */
    DataBaseType getDataBaseType() throws BaseException;
 
    /**
     * 获取当前连接
     *
     * @return
     * @throws BaseException
     */
    Connection getConnection() throws BaseException;
 
    /**
     * 查询列表数据
     *
     * @param sql
     * @return
     */
    DataTableEntity getList(String sql) throws BaseException;
 
    /**
     * 查询列表数据
     *
     * @param sql    sql
     * @param params 参数
     * @return
     */
    DataTableEntity getList(String sql, Object[] params) throws BaseException;
 
    /**
     * 查询列表数据
     *
     * @param tableName
     * @param filter
     * @return
     */
    DataTableEntity getList(String tableName, String filter) throws BaseException;
 
    /**
     * 查询列表数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @return
     */
    DataTableEntity getList(String tableName, String filter, Object[] params) throws BaseException;
 
    /**
     * 查询列表数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @return
     */
    DataTableEntity getList(String tableName, String filter, String[] fields, Object[] params) throws BaseException;
 
    /**
     * 查询列表数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @param pageIndex 页数
     * @param pageSize  条数
     * @return
     */
    DataTableEntity getList(String tableName, String filter, Object[] params, int pageIndex, int pageSize) throws BaseException;
 
 
    DataTableEntity getList(String tableName, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws BaseException;
 
    DataTableEntity getList(String tableName, String[] fields, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws BaseException;
 
    DataTableEntity getList(String sql, Object[] params, int pageIndex, int pageSize) throws BaseException;
 
 
    /**
     * 查询一条数据
     *
     * @param sql
     * @return
     */
    FieldSetEntity getOne(String sql) throws BaseException;
 
    /**
     * 查询一条数据
     *
     * @param sql    sql
     * @param params 参数
     * @return
     */
    FieldSetEntity getOne(String sql, Object[] params) throws BaseException;
 
    /**
     * 查询一条数据
     *
     * @param tableName
     * @param filter
     * @return
     */
    FieldSetEntity getOne(String tableName, String filter) throws BaseException;
 
    /**
     * 查询一条数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @return
     */
    FieldSetEntity getOne(String tableName, String filter, Object[] params) throws BaseException;
 
    /**
     * 查询一条数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @return
     */
    FieldSetEntity getOne(String tableName, String filter, String[] fields, Object[] params) throws BaseException;
 
    /**
     * 新增单条记录
     *
     * @param fse
     * @throws BaseException
     */
    void add(FieldSetEntity fse) throws BaseException;
 
    /**
     * 新增单条记录
     *
     * @param fse
     * @throws BaseException
     */
    default void add(FieldSetEntity fse, String automaticallyPrimaryField) throws BaseException {
        return;
    }
 
    /**
     * 批处理添加数据
     *
     * @param data 数据集
     * @return
     */
    BatchResultEntity addBatch(DataTableEntity data) throws BaseException;
 
    /**
     * 批处理添加数据
     *
     * @param data 数据集
     * @return
     */
    BatchResultEntity addBatch(DataTableEntity data, String AutomaticallyPrimaryField) throws BaseException;
 
    int update(FieldSetEntity fse, UpdateFilterEntity updateFilter) throws BaseException;
 
    /**
     * 批处理更新数据
     *
     * @param data
     * @param updateFilter 过滤条件
     * @return
     */
    BatchResultEntity updateBatch(DataTableEntity data, UpdateFilterEntity updateFilter, boolean isCommit) throws BaseException;
 
    /**
     * 删除数据
     *
     * @param sql    sql语句
     * @param params 参数
     * @return
     */
    Boolean delete(String sql, Object[] params) throws BaseException;
 
    /**
     * 删除数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @return
     */
    Boolean delete(String tableName, String filter) throws BaseException;
 
    /**
     * 删除数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @return
     */
    Boolean delete(String tableName, String filter, Object[] params) throws BaseException;
 
    /**
     * 删除数据
     *
     * @param tableName 表名
     * @param filter    条件
     * @param params    参数
     * @return
     */
    int deleteRInt(String tableName, String filter, Object[] params) throws BaseException;
 
    /**
     * 执行sql
     *
     * @param sql
     * @return
     */
    boolean executeSql(String sql) throws BaseException;
 
    boolean executeSql(String sql, Object[] params) throws BaseException;
 
    void closeConnection();
 
    default FieldMetaEntity getFields(ResultSetMetaData metaData) throws BaseException {
        if (metaData != null) {
            try {
                int columnCount = metaData.getColumnCount();
                FieldMetaEntity fields = new FieldMetaEntity();
                List<String> fieldNames = new ArrayList<>();
                for (int i = 1; i < columnCount + 1; i++) {
                    if (fields.getTableName() == null || fields.getTableName().length == 0) {
                        fields.setTableName(new Object[]{metaData.getTableName(i)});
                    }
                    String columnName = metaData.getColumnName(i).toLowerCase();
                    fieldNames.add(columnName);
                }
                fields.setFields(fieldNames.toArray());
                return fields;
            } catch (SQLException e) {
                throw new BaseException(e);
            }
 
        }
        return new FieldMetaEntity();
 
    }
 
    default DataTableEntity loaddingDataList(ResultSet rs) throws BaseException, SQLException {
        DataTableEntity list = new DataTableEntity();
        try {
            while (rs.next()) {
                FieldMetaEntity f = getFields(rs.getMetaData());
                FieldSetEntity loaddingData = new FieldSetEntity();
                loaddingData.setTableName(f.getTableName()[0]);
                for (int i = 0; i < f.getFields().length; i++) {
                    Object field = f.getFields()[i];
                    Object value = rs.getObject(field.toString());
                    //把 oracle text类型字段转换为文本字符串
                    if (value instanceof Clob) {
                        value = ClobToString((Clob) value);
                    } else if (value instanceof Blob) {
                        value = BlobToBytes((Blob) value);
                    }
                    loaddingData.setValue((String) field, value);
                }
                if (loaddingData == null) {
                    continue;
                }
                list.addFieldSetEntity(loaddingData);
            }
        } catch (Exception e) {
            throw new BaseException(e);
        } finally {
            rs.close();
        }
        return list;
    }
 
    /**
     * Bolg to byte[]
     */
 
    default byte[] BlobToBytes(Blob blob) {
        BufferedInputStream bufferedInputStream = null;
        try {
            //利用Blob自带的一个函数去将blob转换成InputStream
            bufferedInputStream = new BufferedInputStream(blob.getBinaryStream());
            //申请一个字节流,长度和blob相同
            byte[] bytes = new byte[(int) blob.length()];
            int len = bytes.length;
            int offset = 0;
            int read = 0;
            while (offset < len//确保不会读过头
                    && (read = bufferedInputStream.read(bytes, offset, len - offset)) >= 0) {
                //BufferedInputStream内部有一个缓冲区,默认大小为8M,每次调用read方法的时候,它首先尝试从缓冲区里读取数据,
                //若读取失败(缓冲区无可读数据),则选择从物理数据源(譬如文件)读取新数据(这里会尝试尽可能读取多的字节)放入到缓冲区中,
                //最后再将缓冲区中的内容部分或全部返回给用户
                //也就是说read函数一次性可能读不完,所以可能会分多次读,于是就有了上面的逻辑
                offset += read;
            }
            return bytes;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                return null;
            }
        }
    }
 
    default String ClobToString(Clob clob) throws BaseException {
        try (Reader is = clob.getCharacterStream();
             BufferedReader br = new BufferedReader(is)) {
            String reString = "";
 
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {
                sb.append(s);
                s = br.readLine();
            }
            reString = sb.toString();
            return reString;
        } catch (Exception e) {
            throw new BaseException(e);
        }
    }
}