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 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); } } }