|
@@ -6,7 +6,8 @@ import com.flow.common.core.exception.BaseException;
|
|
|
import com.flow.common.core.model.PageResult;
|
|
|
import com.flow.common.mybatis.service.impl.BaseServiceImpl;
|
|
|
import com.flow.dao.ReportDatasourceDao;
|
|
|
-import com.flow.entity.ReportDatasource;
|
|
|
+import com.flow.entity.ReportDataSource;
|
|
|
+import com.flow.manger.DataSourceManager;
|
|
|
import com.flow.mapstruct.ReportDatasourceMapper;
|
|
|
import com.flow.model.ExecSql;
|
|
|
import com.flow.model.ReportDatasourceQuery;
|
|
@@ -16,22 +17,21 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
-import java.sql.Connection;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
-public class ReportDatasourceServiceImpl extends BaseServiceImpl<ReportDatasourceDao, ReportDatasource> implements ReportDatasourceService {
|
|
|
+public class ReportDatasourceServiceImpl extends BaseServiceImpl<ReportDatasourceDao, ReportDataSource> implements ReportDatasourceService {
|
|
|
@Autowired
|
|
|
private ReportDatasourceDao reportDatasourceDao;
|
|
|
@Autowired
|
|
|
private ReportDatasourceMapper reportDatasourceMapper;
|
|
|
|
|
|
@Override
|
|
|
- public PageResult<ReportDatasource> getList(ReportDatasourceQuery reportDatasourceQuery) {
|
|
|
- ReportDatasource reportDatasource = reportDatasourceMapper.toEntity(reportDatasourceQuery);
|
|
|
- Page<ReportDatasource> page = reportDatasourceDao.lambdaQueryChain()
|
|
|
- .setEntity(reportDatasource)
|
|
|
+ public PageResult<ReportDataSource> getList(ReportDatasourceQuery reportDatasourceQuery) {
|
|
|
+ ReportDataSource reportDataSource = reportDatasourceMapper.toEntity(reportDatasourceQuery);
|
|
|
+ Page<ReportDataSource> page = reportDatasourceDao.lambdaQueryChain()
|
|
|
+ .setEntity(reportDataSource)
|
|
|
.page(
|
|
|
new Page<>(reportDatasourceQuery.getPage(), reportDatasourceQuery.getLimit())
|
|
|
);
|
|
@@ -39,15 +39,15 @@ public class ReportDatasourceServiceImpl extends BaseServiceImpl<ReportDatasourc
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ReportDatasource create(ReportDatasource reportDatasource) {
|
|
|
- reportDatasourceDao.insert(reportDatasource);
|
|
|
- return reportDatasource;
|
|
|
+ public ReportDataSource create(ReportDataSource reportDataSource) {
|
|
|
+ reportDatasourceDao.insert(reportDataSource);
|
|
|
+ return reportDataSource;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ReportDatasource update(ReportDatasource reportDatasource) {
|
|
|
- reportDatasourceDao.updateById(reportDatasource);
|
|
|
- return reportDatasource;
|
|
|
+ public ReportDataSource update(ReportDataSource reportDataSource) {
|
|
|
+ reportDatasourceDao.updateById(reportDataSource);
|
|
|
+ return reportDataSource;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -64,27 +64,27 @@ public class ReportDatasourceServiceImpl extends BaseServiceImpl<ReportDatasourc
|
|
|
if(!sql.matches("^\\s*select\\s+.*\\s+from\\s+.*$")){
|
|
|
throw new BaseException("sql格式错误");
|
|
|
}
|
|
|
- Optional<ReportDatasource> optional = this.lambdaQuery()
|
|
|
- .eq(ReportDatasource::getId, execSql.getDatasourceId())
|
|
|
+ Optional<ReportDataSource> optional = this.lambdaQuery()
|
|
|
+ .eq(ReportDataSource::getId, execSql.getDatasourceId())
|
|
|
.oneOpt();
|
|
|
if(!optional.isPresent()){
|
|
|
throw new BaseException("数据源不存在");
|
|
|
}
|
|
|
- ReportDatasource reportDatasource = optional.get();
|
|
|
- DataSource dataSource = reportDatasource.buildDataSource();
|
|
|
+ ReportDataSource reportDataSource = optional.get();
|
|
|
+ DataSource dataSource = reportDataSource.buildDataSource();
|
|
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
|
|
return jdbcTemplate.queryForList(sql,execSql.getParams());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean testConnection(ReportDatasource reportDatasource) {
|
|
|
+ public boolean testConnection(ReportDataSource reportDataSource) {
|
|
|
try {
|
|
|
- DataSource dataSource = reportDatasource.buildDataSource();
|
|
|
- Connection connection = dataSource.getConnection();
|
|
|
- connection.close();
|
|
|
+ DataSourceManager.getConnection(reportDataSource);
|
|
|
return true;
|
|
|
} catch (SQLException e) {
|
|
|
throw new BaseException(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ DataSourceManager.release();
|
|
|
}
|
|
|
}
|
|
|
}
|