| | |
| | | import com.product.datasource.config.DataBaseType; |
| | | import com.product.datasource.config.ErrorCode; |
| | | import com.product.datasource.connection.ConnectionManager; |
| | | import com.product.datasource.dao.ConnectionInterface; |
| | | import com.product.datasource.dao.Dao; |
| | | import com.product.datasource.entity.BatchResultEntity; |
| | | import com.product.datasource.entity.DataBaseEntity; |
| | |
| | | import com.product.datasource.utils.BatchUtil; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | import javax.swing.*; |
| | | import java.sql.Connection; |
| | | import java.sql.PreparedStatement; |
| | | import java.sql.ResultSet; |
| | |
| | | */ |
| | | public class MysqlDaoImpl implements Dao { |
| | | |
| | | private DataBaseEntity dataBaseEntity; |
| | | |
| | | private Connection connection; |
| | | private Connection connection; |
| | | |
| | | private boolean outLog; |
| | | private boolean outLog; |
| | | |
| | | public MysqlDaoImpl(DataBaseEntity dataBaseEntity) { |
| | | if (!DataBaseType.MYSQL.equals(dataBaseEntity.getDataBaseType())) { |
| | | throw new BaseException(ErrorCode.INIT_DAO_IMPL_TYPE_ERROR); |
| | | } |
| | | this.dataBaseEntity = dataBaseEntity; |
| | | this.outLog = "dev".equals(Global.getSystemConfig("spring.profiles.active", "prod")); |
| | | } |
| | | private ConnectionInterface connectionInterface; |
| | | |
| | | private void info(String message) { |
| | | if (this.outLog) { |
| | | SpringMVCContextHolder.getSystemLogger().info(message); |
| | | } |
| | | } |
| | | public MysqlDaoImpl(DataBaseEntity dataBaseEntity) { |
| | | if (!DataBaseType.MYSQL.equals(dataBaseEntity.getDataBaseType())) { |
| | | throw new BaseException(ErrorCode.INIT_DAO_IMPL_TYPE_ERROR); |
| | | } |
| | | this.outLog = "dev".equals(Global.getSystemConfig("spring.profiles.active", "prod")); |
| | | connectionInterface = () -> ConnectionManager.getConnection(dataBaseEntity); |
| | | } |
| | | |
| | | public MysqlDaoImpl(ConnectionInterface connectionInterface) { |
| | | this.connectionInterface = connectionInterface; |
| | | } |
| | | |
| | | private void info(String message) { |
| | | if (this.outLog) { |
| | | SpringMVCContextHolder.getSystemLogger().info(message); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è·åæ°æ®åºç±»å |
| | | * |
| | | * @return |
| | | * @throws BaseException |
| | | */ |
| | | @Override |
| | | public DataBaseType getDataBaseType() throws com.product.core.exception.BaseException { |
| | | return DataBaseType.MYSQL; |
| | | } |
| | | /** |
| | | * è·åæ°æ®åºç±»å |
| | | * |
| | | * @return |
| | | * @throws BaseException |
| | | */ |
| | | @Override |
| | | public DataBaseType getDataBaseType() throws com.product.core.exception.BaseException { |
| | | return DataBaseType.MYSQL; |
| | | } |
| | | |
| | | public Connection getConnection() throws com.product.core.exception.BaseException { |
| | | try { |
| | | if (this.connection == null || this.connection.isClosed()) { |
| | | //åå§åè¿æ¥ |
| | | this.connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | } |
| | | return this.connection; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BaseException(ErrorCode.GET_CONNECTION_FAIL, e); |
| | | } |
| | | } |
| | | public Connection getConnection() throws com.product.core.exception.BaseException { |
| | | try { |
| | | if (this.connection == null || this.connection.isClosed()) { |
| | | //åå§åè¿æ¥ |
| | | this.connection = this.connectionInterface.getConnection(); |
| | | } |
| | | return this.connection; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BaseException(ErrorCode.GET_CONNECTION_FAIL, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢åæ¡è®°å½ |
| | | * |
| | | * @param fse |
| | | * @throws OracleDaoImpl.BaseException |
| | | */ |
| | | @Override |
| | | public void add(FieldSetEntity fse) throws com.product.core.exception.BaseException { |
| | | StringBuilder insertSql = BatchUtil.getInsertSql(fse.getMeta()); |
| | | try (PreparedStatement pst = getConnection().prepareStatement(insertSql.toString())) { |
| | | Object[] fields = fse.getFields(); |
| | | for (int i = 0; i < fields.length; i++) { |
| | | pst.setObject(i + 1, fse.getObject((String) fields[i])); |
| | | } |
| | | pst.execute(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BaseException(ErrorCode.ADD_RECORD_FAIL, e); |
| | | } |
| | | } |
| | | /** |
| | | * æ°å¢åæ¡è®°å½ |
| | | * |
| | | * @param fse |
| | | * @throws OracleDaoImpl.BaseException |
| | | */ |
| | | @Override |
| | | public void add(FieldSetEntity fse) throws com.product.core.exception.BaseException { |
| | | StringBuilder insertSql = BatchUtil.getInsertSql(fse.getMeta()); |
| | | try (PreparedStatement pst = getConnection().prepareStatement(insertSql.toString())) { |
| | | Object[] fields = fse.getFields(); |
| | | for (int i = 0; i < fields.length; i++) { |
| | | pst.setObject(i + 1, fse.getObject((String) fields[i])); |
| | | } |
| | | pst.execute(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BaseException(ErrorCode.ADD_RECORD_FAIL, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param sql |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String sql) throws com.product.core.exception.BaseException { |
| | | return this.getList(sql, new Object[]{}); |
| | | } |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param sql |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String sql) throws com.product.core.exception.BaseException { |
| | | return this.getList(sql, new Object[]{}); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param sql sql |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | try (PreparedStatement pst = getConnection().prepareStatement(sql)) { |
| | | info("æ§è¡SQLï¼\n" + sql); |
| | | if (params != null && params.length > 0) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | info("param" + (i + 1) + ": " + params[i]); |
| | | pst.setObject(i + 1, params[i]); |
| | | } |
| | | } |
| | | ResultSet resultSet = pst.executeQuery(); |
| | | return loaddingDataList(resultSet); |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error("EXECUTE_ERROR_SQL:\n" + sql); |
| | | throw new BaseException(ErrorCode.GET_LIST_FAIL, e); |
| | | } |
| | | } |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param sql sql |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | try (PreparedStatement pst = getConnection().prepareStatement(sql)) { |
| | | info("æ§è¡SQLï¼\n" + sql); |
| | | if (params != null && params.length > 0) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | info("param" + (i + 1) + ": " + params[i]); |
| | | pst.setObject(i + 1, params[i]); |
| | | } |
| | | } |
| | | ResultSet resultSet = pst.executeQuery(); |
| | | return loaddingDataList(resultSet); |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error("EXECUTE_ERROR_SQL:\n" + sql); |
| | | throw new BaseException(ErrorCode.GET_LIST_FAIL, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName |
| | | * @param filter |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter) throws com.product.core.exception.BaseException { |
| | | return getList(tableName, filter, new Object[]{}); |
| | | } |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName |
| | | * @param filter |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter) throws com.product.core.exception.BaseException { |
| | | return getList(tableName, filter, new Object[]{}); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | return this.getList(tableName, filter, null, params); |
| | | } |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | return this.getList(tableName, filter, null, params); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param fields |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder("SELECT "); |
| | | if (ArrayUtil.isEmpty(fields)) { |
| | | sql.append(" * "); |
| | | } else { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | sql.append(fields[i]); |
| | | if (i + 1 < fields.length) { |
| | | sql.append(","); |
| | | } |
| | | } |
| | | } |
| | | sql.append(" FROM "); |
| | | sql.append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" ) "); |
| | | } |
| | | return this.getList(sql.toString(), params); |
| | | } |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param fields |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder("SELECT "); |
| | | if (ArrayUtil.isEmpty(fields)) { |
| | | sql.append(" * "); |
| | | } else { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | sql.append(fields[i]); |
| | | if (i + 1 < fields.length) { |
| | | sql.append(","); |
| | | } |
| | | } |
| | | } |
| | | sql.append(" FROM "); |
| | | sql.append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" ) "); |
| | | } |
| | | return this.getList(sql.toString(), params); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @param pageIndex é¡µæ° |
| | | * @param pageSize æ¡æ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" SELECT * FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" )"); |
| | | } |
| | | return getList(sql.toString(), params, pageIndex, pageSize); |
| | | } |
| | | /** |
| | | * æ¥è¯¢åè¡¨æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @param pageIndex é¡µæ° |
| | | * @param pageSize æ¡æ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" SELECT * FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" )"); |
| | | } |
| | | return getList(sql.toString(), params, pageIndex, pageSize); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" SELECT * FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" )"); |
| | | } |
| | | if (!StringUtils.isEmpty(orderBy)) { |
| | | sql.append(" ORDER BY ").append(orderBy); |
| | | } |
| | | return getList(sql.toString(), params, pageIndex, pageSize); |
| | | } |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" SELECT * FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" )"); |
| | | } |
| | | if (!StringUtils.isEmpty(orderBy)) { |
| | | sql.append(" ORDER BY ").append(orderBy); |
| | | } |
| | | return getList(sql.toString(), params, pageIndex, pageSize); |
| | | } |
| | | |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String[] fields, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" SELECT "); |
| | | if (ArrayUtil.isEmpty(fields)) { |
| | | sql.append("*"); |
| | | } else { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | sql.append(fields[i]); |
| | | if (i + 1 < fields.length) { |
| | | sql.append(","); |
| | | } |
| | | } |
| | | } |
| | | sql.append(" FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" )"); |
| | | } |
| | | if (!StringUtils.isEmpty(orderBy)) { |
| | | sql.append(" ORDER BY ").append(orderBy); |
| | | } |
| | | return null; |
| | | } |
| | | @Override |
| | | public DataTableEntity getList(String tableName, String[] fields, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" SELECT "); |
| | | if (ArrayUtil.isEmpty(fields)) { |
| | | sql.append("*"); |
| | | } else { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | sql.append(fields[i]); |
| | | if (i + 1 < fields.length) { |
| | | sql.append(","); |
| | | } |
| | | } |
| | | } |
| | | sql.append(" FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" )"); |
| | | } |
| | | if (!StringUtils.isEmpty(orderBy)) { |
| | | sql.append(" ORDER BY ").append(orderBy); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public DataTableEntity getList(String sql, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | int number = getPageParam(pageIndex, pageSize); |
| | | StringBuilder sb = new StringBuilder(sql); |
| | | sb.append(" LIMIT ?,? "); |
| | | if (params == null) { |
| | | params = new Object[]{number, pageSize}; |
| | | } else { |
| | | params = ArrayUtil.append(params, number, pageSize); |
| | | } |
| | | return this.getList(sb.toString(), params); |
| | | } |
| | | public DataTableEntity getList(String sql, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { |
| | | int number = getPageParam(pageIndex, pageSize); |
| | | StringBuilder sb = new StringBuilder(sql); |
| | | sb.append(" LIMIT ?,? "); |
| | | if (params == null) { |
| | | params = new Object[]{number, pageSize}; |
| | | } else { |
| | | params = ArrayUtil.append(params, number, pageSize); |
| | | } |
| | | return this.getList(sb.toString(), params); |
| | | } |
| | | |
| | | private int getPageParam(int pageIndex, int pageSize) { |
| | | return pageIndex <= 1 ? 0 : (pageIndex - 1) * pageSize; |
| | | } |
| | | private int getPageParam(int pageIndex, int pageSize) { |
| | | return pageIndex <= 1 ? 0 : (pageIndex - 1) * pageSize; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param sql |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String sql) throws com.product.core.exception.BaseException { |
| | | return this.getOne(sql, new Object[]{}); |
| | | } |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param sql |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String sql) throws com.product.core.exception.BaseException { |
| | | return this.getOne(sql, new Object[]{}); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param sql sql |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | DataTableEntity list = this.getList(sql, params); |
| | | if (DataTableEntity.isEmpty(list)) { |
| | | return null; |
| | | } |
| | | return list.getFieldSetEntity(0); |
| | | } |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param sql sql |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | DataTableEntity list = this.getList(sql, params); |
| | | if (DataTableEntity.isEmpty(list)) { |
| | | return null; |
| | | } |
| | | return list.getFieldSetEntity(0); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param tableName |
| | | * @param filter |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String tableName, String filter) throws com.product.core.exception.BaseException { |
| | | return this.getOne(tableName, filter, null); |
| | | } |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param tableName |
| | | * @param filter |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String tableName, String filter) throws com.product.core.exception.BaseException { |
| | | return this.getOne(tableName, filter, null); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | return this.getOne(tableName, filter, null, params); |
| | | } |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | return this.getOne(tableName, filter, null, params); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param fields |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append(" SELECT "); |
| | | if (ArrayUtil.isEmpty(fields)) { |
| | | sb.append(" * "); |
| | | } else { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | sb.append(fields[i]); |
| | | if (i + 1 < fields.length) { |
| | | sb.append(","); |
| | | } |
| | | } |
| | | } |
| | | sb.append(" FROM ").append(tableName); |
| | | sb.append(" WHERE "); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sb.append(" (").append(filter).append(") "); |
| | | } |
| | | sb.append(" limit 1"); |
| | | DataTableEntity list = this.getList(sb.toString(), params); |
| | | return DataTableEntity.isEmpty(list) ? null : list.getFieldSetEntity(0); |
| | | } |
| | | /** |
| | | * æ¥è¯¢ä¸æ¡æ°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param fields |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public FieldSetEntity getOne(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append(" SELECT "); |
| | | if (ArrayUtil.isEmpty(fields)) { |
| | | sb.append(" * "); |
| | | } else { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | sb.append(fields[i]); |
| | | if (i + 1 < fields.length) { |
| | | sb.append(","); |
| | | } |
| | | } |
| | | } |
| | | sb.append(" FROM ").append(tableName); |
| | | sb.append(" WHERE "); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sb.append(" (").append(filter).append(") "); |
| | | } |
| | | sb.append(" limit 1"); |
| | | DataTableEntity list = this.getList(sb.toString(), params); |
| | | return DataTableEntity.isEmpty(list) ? null : list.getFieldSetEntity(0); |
| | | } |
| | | |
| | | /** |
| | | * æ¹å¤çæ·»å æ°æ® |
| | | * |
| | | * @param data æ°æ®é |
| | | * @return |
| | | */ |
| | | @Override |
| | | public BatchResultEntity addBatch(DataTableEntity data) throws com.product.core.exception.BaseException { |
| | | info("å¼å§æ¹éæ°å¢æ°æ®"); |
| | | if (DataTableEntity.isEmpty(data)) { |
| | | info("æ¹éæ°å¢æ°æ®æ¡æ°ä¸º0"); |
| | | return new BatchResultEntity(0); |
| | | } |
| | | TimeInterval timer = DateUtil.timer(); |
| | | info("æ¹éæ°å¢æ°æ®æ¡æ°ä¸ºï¼" + data.getRows()); |
| | | BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); |
| | | // Connection connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | try { |
| | | StringBuilder insertSql = BatchUtil.getInsertSql(data.getMeta()); |
| | | info("SQL:" + insertSql); |
| | | Connection connection = getConnection(); |
| | | connection.setAutoCommit(false); |
| | | /** |
| | | * æ¹å¤çæ·»å æ°æ® |
| | | * |
| | | * @param data æ°æ®é |
| | | * @return |
| | | */ |
| | | @Override |
| | | public BatchResultEntity addBatch(DataTableEntity data) throws com.product.core.exception.BaseException { |
| | | info("å¼å§æ¹éæ°å¢æ°æ®"); |
| | | if (DataTableEntity.isEmpty(data)) { |
| | | info("æ¹éæ°å¢æ°æ®æ¡æ°ä¸º0"); |
| | | return new BatchResultEntity(0); |
| | | } |
| | | TimeInterval timer = DateUtil.timer(); |
| | | info("æ¹éæ°å¢æ°æ®æ¡æ°ä¸ºï¼" + data.getRows()); |
| | | BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); |
| | | // ConnectionInterface connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | try { |
| | | StringBuilder insertSql = BatchUtil.getInsertSql(data.getMeta()); |
| | | info("SQL:" + insertSql); |
| | | Connection connection = getConnection(); |
| | | connection.setAutoCommit(false); |
| | | |
| | | try (PreparedStatement pst = connection.prepareStatement(insertSql.toString())) { |
| | | Object[] fields = data.getMeta().getFields(); |
| | | for (int i = 0; i < data.getRows(); i++) { |
| | | FieldSetEntity fse = data.getFieldSetEntity(i); |
| | | for (int j = 1; j <= fields.length; j++) { |
| | | if (fse.getObject(fields[j - 1].toString()) instanceof oracle.sql.TIMESTAMP) { |
| | | pst.setObject(j, fse.getObject(fields[j - 1].toString()).toString()); |
| | | } else { |
| | | pst.setObject(j, fse.getObject(fields[j - 1].toString())); |
| | | } |
| | | } |
| | | pst.addBatch(); |
| | | } |
| | | info("å¼å§æ§è¡æ¹éæäº¤æ°æ®"); |
| | | int[] ints = pst.executeBatch(); |
| | | connection.commit(); |
| | | info("æ§è¡æ¹éæäº¤æ°æ®å®æï¼å
±æäº¤ " + data.getRows() + " æ¡,èæ¶ï¼" + timer.intervalSecond() + " ç§ ï¼"); |
| | | pst.clearBatch(); |
| | | } catch (Exception e) { |
| | | throw e; |
| | | } finally { |
| | | connection.setAutoCommit(true); |
| | | } |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw new BaseException(ErrorCode.ADD_BATCH_ERROR, e); |
| | | } |
| | | return batchResultEntity; |
| | | } |
| | | try (PreparedStatement pst = connection.prepareStatement(insertSql.toString())) { |
| | | Object[] fields = data.getMeta().getFields(); |
| | | for (int i = 0; i < data.getRows(); i++) { |
| | | FieldSetEntity fse = data.getFieldSetEntity(i); |
| | | for (int j = 1; j <= fields.length; j++) { |
| | | if (fse.getObject(fields[j - 1].toString()) instanceof oracle.sql.TIMESTAMP) { |
| | | pst.setObject(j, fse.getObject(fields[j - 1].toString()).toString()); |
| | | } else { |
| | | pst.setObject(j, fse.getObject(fields[j - 1].toString())); |
| | | } |
| | | } |
| | | pst.addBatch(); |
| | | } |
| | | info("å¼å§æ§è¡æ¹éæäº¤æ°æ®"); |
| | | int[] ints = pst.executeBatch(); |
| | | connection.commit(); |
| | | info("æ§è¡æ¹éæäº¤æ°æ®å®æï¼å
±æäº¤ " + data.getRows() + " æ¡,èæ¶ï¼" + timer.intervalSecond() + " ç§ ï¼"); |
| | | pst.clearBatch(); |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw e; |
| | | } finally { |
| | | connection.setAutoCommit(true); |
| | | } |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw new BaseException(ErrorCode.ADD_BATCH_ERROR, e); |
| | | } |
| | | return batchResultEntity; |
| | | } |
| | | |
| | | /** |
| | | * æ¹å¤çæ·»å æ°æ® |
| | | * |
| | | * @param data æ°æ®é |
| | | * @param AutomaticallyPrimaryField |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Deprecated |
| | | public BatchResultEntity addBatch(DataTableEntity data, String AutomaticallyPrimaryField) throws com.product.core.exception.BaseException { |
| | | return addBatch(data); |
| | | } |
| | | /** |
| | | * æ¹å¤çæ·»å æ°æ® |
| | | * |
| | | * @param data æ°æ®é |
| | | * @param AutomaticallyPrimaryField |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Deprecated |
| | | public BatchResultEntity addBatch(DataTableEntity data, String AutomaticallyPrimaryField) throws com.product.core.exception.BaseException { |
| | | return addBatch(data); |
| | | } |
| | | |
| | | @Override |
| | | public int update(FieldSetEntity fse, UpdateFilterEntity updateFilter) throws com.product.core.exception.BaseException { |
| | | //todo å¾
å®ç° |
| | | return -1; |
| | | } |
| | | @Override |
| | | public int update(FieldSetEntity fse, UpdateFilterEntity updateFilter) throws com.product.core.exception.BaseException { |
| | | //todo å¾
å®ç° |
| | | return -1; |
| | | } |
| | | |
| | | /** |
| | | * æ¹å¤çæ´æ°æ°æ® |
| | | * |
| | | * @param data |
| | | * @param updateFilter è¿æ»¤æ¡ä»¶ |
| | | * @param isCommit èªå¨æäº¤ |
| | | * @return |
| | | */ |
| | | @Override |
| | | public BatchResultEntity updateBatch(DataTableEntity data, UpdateFilterEntity updateFilter, boolean isCommit) throws com.product.core.exception.BaseException { |
| | | info("å¼å§æ¹éæ´æ°æ°æ®"); |
| | | if (DataTableEntity.isEmpty(data)) { |
| | | info("æ¹éæ´æ°æ°æ®æ¡æ°ä¸º0"); |
| | | return new BatchResultEntity(0); |
| | | } |
| | | TimeInterval timer = DateUtil.timer(); |
| | | info("æ¹éæ´æ°æ°æ®æ¡æ°ä¸ºï¼" + data.getRows()); |
| | | BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); |
| | | // Connection connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | try { |
| | | StringBuilder updateSql = BatchUtil.getUpdateSql(data.getMeta(), updateFilter.getFilter()); |
| | | info("SQL: " + updateSql); |
| | | Connection connection = getConnection(); |
| | | connection.setAutoCommit(false); |
| | | try (PreparedStatement pst = connection.prepareStatement(updateSql.toString())) { |
| | | Object[] fields = data.getMeta().getFields(); |
| | | for (int i = 0; i < data.getRows(); i++) { |
| | | FieldSetEntity fse = data.getFieldSetEntity(i); |
| | | int j = 1; |
| | | for (; j <= fields.length; j++) { |
| | | Object value = fse.getObject(fields[j - 1].toString()); |
| | | pst.setObject(j,value ); |
| | | info("åæ°ï¼ " + (fields[j - 1]) + " = " + value); |
| | | } |
| | | j--; |
| | | Object[] valueFields = updateFilter.getValueFields(); |
| | | for (int i1 = 1; i1 <= valueFields.length; i1++) { |
| | | Object value = fse.getObject(valueFields[i1 - 1].toString()); |
| | | pst.setObject(j + i1, value); |
| | | info("åæ°ï¼ " + (valueFields[i1 - 1]) + " = " + value); |
| | | } |
| | | pst.addBatch(); |
| | | } |
| | | info("å¼å§æ§è¡æ¹éæäº¤æ°æ®"); |
| | | pst.executeBatch(); |
| | | info("æ§è¡æ¹éæäº¤æ°æ®å®æï¼å
±æäº¤ " + data.getRows() + " æ¡,èæ¶ï¼" + timer.intervalSecond() + " ç§ ï¼"); |
| | | pst.clearBatch(); |
| | | if (isCommit) { |
| | | connection.commit(); |
| | | } |
| | | batchResultEntity.setConnection(connection); |
| | | return batchResultEntity; |
| | | } catch (Exception e) { |
| | | throw e; |
| | | } finally { |
| | | if (isCommit) { |
| | | connection.setAutoCommit(true); |
| | | } |
| | | /** |
| | | * æ¹å¤çæ´æ°æ°æ® |
| | | * |
| | | * @param data |
| | | * @param updateFilter è¿æ»¤æ¡ä»¶ |
| | | * @param isCommit èªå¨æäº¤ |
| | | * @return |
| | | */ |
| | | @Override |
| | | public BatchResultEntity updateBatch(DataTableEntity data, UpdateFilterEntity updateFilter, boolean isCommit) throws com.product.core.exception.BaseException { |
| | | info("å¼å§æ¹éæ´æ°æ°æ®"); |
| | | if (DataTableEntity.isEmpty(data)) { |
| | | info("æ¹éæ´æ°æ°æ®æ¡æ°ä¸º0"); |
| | | return new BatchResultEntity(0); |
| | | } |
| | | TimeInterval timer = DateUtil.timer(); |
| | | info("æ¹éæ´æ°æ°æ®æ¡æ°ä¸ºï¼" + data.getRows()); |
| | | BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); |
| | | // ConnectionInterface connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | try { |
| | | StringBuilder updateSql = BatchUtil.getUpdateSql(data.getMeta(), updateFilter.getFilter()); |
| | | info("SQL: " + updateSql); |
| | | Connection connection = getConnection(); |
| | | connection.setAutoCommit(false); |
| | | try (PreparedStatement pst = connection.prepareStatement(updateSql.toString())) { |
| | | Object[] fields = data.getMeta().getFields(); |
| | | for (int i = 0; i < data.getRows(); i++) { |
| | | FieldSetEntity fse = data.getFieldSetEntity(i); |
| | | int j = 1; |
| | | for (; j <= fields.length; j++) { |
| | | Object value = fse.getObject(fields[j - 1].toString()); |
| | | pst.setObject(j, value); |
| | | info("åæ°ï¼ " + (fields[j - 1]) + " = " + value); |
| | | } |
| | | j--; |
| | | Object[] valueFields = updateFilter.getValueFields(); |
| | | for (int i1 = 1; i1 <= valueFields.length; i1++) { |
| | | Object value = fse.getObject(valueFields[i1 - 1].toString()); |
| | | pst.setObject(j + i1, value); |
| | | info("åæ°ï¼ " + (valueFields[i1 - 1]) + " = " + value); |
| | | } |
| | | pst.addBatch(); |
| | | } |
| | | info("å¼å§æ§è¡æ¹éæäº¤æ°æ®"); |
| | | pst.executeBatch(); |
| | | info("æ§è¡æ¹éæäº¤æ°æ®å®æï¼å
±æäº¤ " + data.getRows() + " æ¡,èæ¶ï¼" + timer.intervalSecond() + " ç§ ï¼"); |
| | | pst.clearBatch(); |
| | | if (isCommit) { |
| | | connection.commit(); |
| | | } |
| | | batchResultEntity.setConnection(connection); |
| | | return batchResultEntity; |
| | | } catch (Exception e) { |
| | | throw e; |
| | | } finally { |
| | | if (isCommit) { |
| | | connection.setAutoCommit(true); |
| | | } |
| | | // this.closeConnection(); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw new BaseException(ErrorCode.UPDATE_BATCH_ERROR, e); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw new BaseException(ErrorCode.UPDATE_BATCH_ERROR, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å 餿°æ® |
| | | * |
| | | * @param sql sqlè¯å¥ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean delete(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | return executeSql(sql, params); |
| | | } |
| | | /** |
| | | * å 餿°æ® |
| | | * |
| | | * @param sql sqlè¯å¥ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean delete(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | return executeSql(sql, params); |
| | | } |
| | | |
| | | /** |
| | | * å 餿°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean delete(String tableName, String filter) throws com.product.core.exception.BaseException { |
| | | return delete(tableName, filter, null); |
| | | } |
| | | /** |
| | | * å 餿°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean delete(String tableName, String filter) throws com.product.core.exception.BaseException { |
| | | return delete(tableName, filter, null); |
| | | } |
| | | |
| | | /** |
| | | * å 餿°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean delete(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" DELETE FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" ) "); |
| | | } |
| | | return executeSql(sql.toString(), params); |
| | | } |
| | | /** |
| | | * å 餿°æ® |
| | | * |
| | | * @param tableName 表å |
| | | * @param filter æ¡ä»¶ |
| | | * @param params åæ° |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean delete(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" DELETE FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" ) "); |
| | | } |
| | | return executeSql(sql.toString(), params); |
| | | } |
| | | |
| | | public int deleteRInt(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" DELETE FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" ) "); |
| | | } |
| | | return executeSqlResult(sql.toString(), params); |
| | | } |
| | | public int deleteRInt(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append(" DELETE FROM ").append(tableName); |
| | | if (!StringUtils.isEmpty(filter)) { |
| | | sql.append(" WHERE (").append(filter).append(" ) "); |
| | | } |
| | | return executeSqlResult(sql.toString(), params); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ§è¡sql |
| | | * |
| | | * @param sql |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean executeSql(String sql) throws com.product.core.exception.BaseException { |
| | | return executeSql(sql, null); |
| | | } |
| | | /** |
| | | * æ§è¡sql |
| | | * |
| | | * @param sql |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean executeSql(String sql) throws com.product.core.exception.BaseException { |
| | | return executeSql(sql, null); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ§è¡sql |
| | | * |
| | | * @param sql |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean executeSql(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | info("æ§è¡SQLï¼\n" + sql); |
| | | try (PreparedStatement pst = getConnection().prepareStatement(sql)) { |
| | | if (params != null && params.length > 0) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | info("param" + (i + 1) + ": " + params[i]); |
| | | pst.setObject(i + 1, params[i].toString()); |
| | | } |
| | | } |
| | | return pst.execute(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | info("ERROR_SQLï¼\n" + sql); |
| | | throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); |
| | | } |
| | | } |
| | | /** |
| | | * æ§è¡sql |
| | | * |
| | | * @param sql |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean executeSql(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | info("æ§è¡SQLï¼\n" + sql); |
| | | try (PreparedStatement pst = getConnection().prepareStatement(sql)) { |
| | | if (params != null && params.length > 0) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | info("param" + (i + 1) + ": " + params[i]); |
| | | pst.setObject(i + 1, params[i].toString()); |
| | | } |
| | | } |
| | | return pst.execute(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | info("ERROR_SQLï¼\n" + sql); |
| | | throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ§è¡sql |
| | | * |
| | | * @param sql |
| | | * @param params |
| | | * @return |
| | | */ |
| | | public int executeSqlResult(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | try (PreparedStatement pst = getConnection().prepareStatement(sql)) { |
| | | info("æ§è¡SQLï¼\n" + sql); |
| | | if (params != null && params.length > 0) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | info("param" + (i + 1) + ": " + params[i]); |
| | | pst.setObject(i + 1, params[i].toString()); |
| | | } |
| | | } |
| | | return pst.executeUpdate(); |
| | | } catch (Exception e) { |
| | | throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); |
| | | } |
| | | } |
| | | /** |
| | | * æ§è¡sql |
| | | * |
| | | * @param sql |
| | | * @param params |
| | | * @return |
| | | */ |
| | | public int executeSqlResult(String sql, Object[] params) throws com.product.core.exception.BaseException { |
| | | try (PreparedStatement pst = getConnection().prepareStatement(sql)) { |
| | | info("æ§è¡SQLï¼\n" + sql); |
| | | if (params != null && params.length > 0) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | info("param" + (i + 1) + ": " + params[i]); |
| | | pst.setObject(i + 1, params[i].toString()); |
| | | } |
| | | } |
| | | return pst.executeUpdate(); |
| | | } catch (Exception e) { |
| | | throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void closeConnection() { |
| | | try { |
| | | if (this.connection != null && !this.connection.isClosed()) { |
| | | this.connection.close(); |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | this.connection = null; |
| | | } |
| | | } |
| | | @Override |
| | | public void closeConnection() { |
| | | try { |
| | | if (this.connection != null && !this.connection.isClosed()) { |
| | | this.connection.close(); |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | this.connection = null; |
| | | } |
| | | } |
| | | |
| | | class BaseException extends com.product.core.exception.BaseException { |
| | | public BaseException(IEnum a) { |
| | | super(a); |
| | | } |
| | | class BaseException extends com.product.core.exception.BaseException { |
| | | public BaseException(IEnum a) { |
| | | super(a); |
| | | } |
| | | |
| | | |
| | | public BaseException(IEnum iEnum, Throwable throwable) { |
| | | super(iEnum.getValue(), iEnum.getText() + (throwable.getMessage() != null ? "," + throwable.getMessage() : "")); |
| | | } |
| | | } |
| | | public BaseException(IEnum iEnum, Throwable throwable) { |
| | | super(iEnum.getValue(), iEnum.getText() + (throwable.getMessage() != null ? "," + throwable.getMessage() : "")); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void finalize() throws Throwable { |
| | | this.closeConnection(); |
| | | super.finalize(); |
| | | } |
| | | @Override |
| | | protected void finalize() throws Throwable { |
| | | this.closeConnection(); |
| | | super.finalize(); |
| | | } |
| | | } |