mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-09 07:37:05 +08:00
feat: redesign databae type extension (#124)
* feat: update frontend resources * feat: redesign database type extension * fix: ut failed * fix: ut failed
This commit is contained in:
@@ -14,9 +14,9 @@ public interface DatabaseTypePojoConverter {
|
||||
@Mapping(target = "id", ignore = true)
|
||||
@Mapping(target = "updateAt", ignore = true)
|
||||
@Mapping(target = "createAt", ignore = true)
|
||||
DatabaseTypePojo of(DatabaseTypeCreateRequest request);
|
||||
DatabaseTypePojo of(DatabaseTypeCreateRequest request, String jdbcDriverFilePath);
|
||||
|
||||
DatabaseTypePojo of(DatabaseTypeUpdateRequest request);
|
||||
DatabaseTypePojo of(DatabaseTypeUpdateRequest request, String jdbcDriverFilePath);
|
||||
|
||||
DatabaseTypeDetailResponse toDetailResponse(DatabaseTypePojo data);
|
||||
|
||||
|
@@ -17,6 +17,8 @@ public class DatabaseTypeDetailResponse {
|
||||
|
||||
private String jdbcDriverFileUrl;
|
||||
|
||||
private String jdbcDriverFilePath;
|
||||
|
||||
private String jdbcDriverClassName;
|
||||
|
||||
private String jdbcProtocol;
|
||||
|
@@ -17,6 +17,8 @@ public class DatabaseTypePageResponse {
|
||||
|
||||
private String jdbcDriverFileUrl;
|
||||
|
||||
private String jdbcDriverFilePath;
|
||||
|
||||
private String jdbcDriverClassName;
|
||||
|
||||
private String jdbcProtocol;
|
||||
|
@@ -12,4 +12,6 @@ public class DatabaseTypeSimpleResponse {
|
||||
private String urlPattern;
|
||||
|
||||
private String jdbcProtocol;
|
||||
|
||||
private String icon;
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import com.databasir.core.domain.database.converter.DatabaseTypePojoConverter;
|
||||
import com.databasir.core.domain.database.data.*;
|
||||
import com.databasir.core.infrastructure.connection.DatabaseTypes;
|
||||
import com.databasir.core.infrastructure.driver.DriverResources;
|
||||
import com.databasir.core.infrastructure.driver.DriverResult;
|
||||
import com.databasir.dao.impl.DatabaseTypeDao;
|
||||
import com.databasir.dao.impl.ProjectDao;
|
||||
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
|
||||
@@ -36,8 +37,13 @@ public class DatabaseTypeService {
|
||||
private final DatabaseTypePojoConverter databaseTypePojoConverter;
|
||||
|
||||
public Integer create(DatabaseTypeCreateRequest request) {
|
||||
driverResources.validateJar(request.getJdbcDriverFileUrl(), request.getJdbcDriverClassName());
|
||||
DatabaseTypePojo pojo = databaseTypePojoConverter.of(request);
|
||||
if (databaseTypeDao.existsByDatabaseType(request.getDatabaseType())) {
|
||||
throw DomainErrors.DATABASE_TYPE_NAME_DUPLICATE.exception();
|
||||
}
|
||||
DriverResult result =
|
||||
driverResources.load(null, request.getJdbcDriverFileUrl(), request.getDatabaseType());
|
||||
driverResources.validateDriverJar(result.getDriverFile(), request.getJdbcDriverClassName());
|
||||
DatabaseTypePojo pojo = databaseTypePojoConverter.of(request, result.getDriverFilePath());
|
||||
try {
|
||||
return databaseTypeDao.insertAndReturnId(pojo);
|
||||
} catch (DuplicateKeyException e) {
|
||||
@@ -48,24 +54,27 @@ public class DatabaseTypeService {
|
||||
@Transactional
|
||||
public void update(DatabaseTypeUpdateRequest request) {
|
||||
databaseTypeDao.selectOptionalById(request.getId()).ifPresent(data -> {
|
||||
if (DatabaseTypes.has(data.getDatabaseType())) {
|
||||
throw DomainErrors.MUST_NOT_MODIFY_SYSTEM_DEFAULT_DATABASE_TYPE.exception();
|
||||
DatabaseTypePojo pojo;
|
||||
if (!Objects.equals(request.getDatabaseType(), data.getDatabaseType())
|
||||
|| !Objects.equals(request.getJdbcDriverFileUrl(), data.getJdbcDriverFileUrl())) {
|
||||
// 名称修改,下载地址修改需要删除原有的 driver 并重新下载验证
|
||||
driverResources.deleteByDatabaseType(data.getDatabaseType());
|
||||
// download
|
||||
DriverResult result =
|
||||
driverResources.load(null, request.getJdbcDriverFileUrl(), request.getDatabaseType());
|
||||
driverResources.validateDriverJar(result.getDriverFile(), request.getJdbcDriverClassName());
|
||||
// validate
|
||||
pojo = databaseTypePojoConverter.of(request, result.getDriverFilePath());
|
||||
} else {
|
||||
pojo = databaseTypePojoConverter.of(request, data.getJdbcDriverFilePath());
|
||||
}
|
||||
driverResources.validateJar(request.getJdbcDriverFileUrl(), request.getJdbcDriverClassName());
|
||||
DatabaseTypePojo pojo = databaseTypePojoConverter.of(request);
|
||||
|
||||
try {
|
||||
databaseTypeDao.updateById(pojo);
|
||||
} catch (DuplicateKeyException e) {
|
||||
throw DomainErrors.DATABASE_TYPE_NAME_DUPLICATE.exception();
|
||||
}
|
||||
|
||||
// 名称修改,下载地址修改需要删除原有的 driver
|
||||
if (!Objects.equals(request.getDatabaseType(), data.getDatabaseType())
|
||||
|| !Objects.equals(request.getJdbcDriverFileUrl(), data.getJdbcDriverFileUrl())) {
|
||||
driverResources.deleteByDatabaseType(data.getDatabaseType());
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void deleteById(Integer id) {
|
||||
@@ -99,6 +108,7 @@ public class DatabaseTypeService {
|
||||
response.setUrlPattern(type.getUrlPattern());
|
||||
response.setDescription(type.getDescription());
|
||||
response.setJdbcProtocol(type.getJdbcProtocol());
|
||||
response.setIcon(type.getIcon());
|
||||
return response;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
@@ -31,6 +31,7 @@ import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -138,9 +139,20 @@ public class DocumentService {
|
||||
DatabasirConfig databasirConfig = new DatabasirConfig();
|
||||
databasirConfig.setIgnoreTableNameRegex(jsonConverter.fromJson(rule.getIgnoreTableNameRegexArray()));
|
||||
databasirConfig.setIgnoreTableColumnNameRegex(jsonConverter.fromJson(rule.getIgnoreColumnNameRegexArray()));
|
||||
return Databasir.of(databasirConfig)
|
||||
.get(jdbcConnection, dataSource.getDatabaseName(), dataSource.getSchemaName())
|
||||
.orElseThrow(DomainErrors.DATABASE_META_NOT_FOUND::exception);
|
||||
try {
|
||||
DatabaseMeta databaseMeta = Databasir.of(databasirConfig)
|
||||
.get(jdbcConnection, dataSource.getDatabaseName(), dataSource.getSchemaName())
|
||||
.orElseThrow(DomainErrors.DATABASE_META_NOT_FOUND::exception);
|
||||
return databaseMeta;
|
||||
} finally {
|
||||
try {
|
||||
if (!jdbcConnection.isClosed()) {
|
||||
jdbcConnection.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.error("close jdbc connection error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveNewDocument(DatabaseMeta meta,
|
||||
|
@@ -2,6 +2,7 @@ package com.databasir.core.infrastructure.connection;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.infrastructure.driver.DriverResources;
|
||||
import com.databasir.core.infrastructure.driver.DriverResult;
|
||||
import com.databasir.dao.impl.DatabaseTypeDao;
|
||||
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -17,6 +18,7 @@ import java.net.URLClassLoader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
@@ -38,8 +40,8 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
||||
public Connection getConnection(Context context) throws SQLException {
|
||||
String databaseType = context.getDatabaseType();
|
||||
DatabaseTypePojo type = databaseTypeDao.selectByDatabaseType(databaseType);
|
||||
File driverFile = driverResources.loadOrDownloadByDatabaseType(databaseType, type.getJdbcDriverFileUrl());
|
||||
|
||||
File driverFile = loadDriver(type);
|
||||
URLClassLoader loader = null;
|
||||
try {
|
||||
loader = new URLClassLoader(
|
||||
@@ -53,7 +55,6 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e.getMessage());
|
||||
}
|
||||
// retrieve the driver class
|
||||
|
||||
Class<?> clazz = null;
|
||||
Driver driver = null;
|
||||
try {
|
||||
@@ -81,4 +82,15 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
||||
return driver.connect(jdbcUrl, info);
|
||||
}
|
||||
|
||||
private File loadDriver(DatabaseTypePojo type) {
|
||||
String databaseType = type.getDatabaseType();
|
||||
String file = type.getJdbcDriverFilePath();
|
||||
String url = type.getJdbcDriverFileUrl();
|
||||
DriverResult result = driverResources.load(file, url, databaseType);
|
||||
File driverFile = result.getDriverFile();
|
||||
if (!Objects.equals(result.getDriverFilePath(), type.getJdbcDriverFilePath())) {
|
||||
databaseTypeDao.updateDriverFile(type.getId(), result.getDriverFilePath());
|
||||
}
|
||||
return driverFile;
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,6 @@ import java.net.URLClassLoader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
@@ -33,29 +32,79 @@ public class DriverResources {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public void deleteByDatabaseType(String databaseType) {
|
||||
Path path = Paths.get(driverFilePath(driverBaseDirectory, databaseType));
|
||||
public DriverResult load(String driverFilePath, String driverFileUrl, String databaseType) {
|
||||
if (driverFilePath == null) {
|
||||
String targetFile = targetDriverFile(databaseType);
|
||||
File file = download(driverFileUrl, targetFile);
|
||||
return new DriverResult(targetFile, file);
|
||||
}
|
||||
File driverFile = Paths.get(driverFilePath).toFile();
|
||||
if (driverFile.exists()) {
|
||||
return new DriverResult(driverFilePath, driverFile);
|
||||
} else {
|
||||
String targetFile = targetDriverFile(databaseType);
|
||||
File file = download(driverFileUrl, targetFile);
|
||||
return new DriverResult(targetFile, file);
|
||||
}
|
||||
}
|
||||
|
||||
private File download(String driverFileUrl, String targetFile) {
|
||||
Path path = Path.of(targetFile);
|
||||
|
||||
// create parent directory
|
||||
if (Files.notExists(path)) {
|
||||
path.getParent().toFile().mkdirs();
|
||||
try {
|
||||
Files.createFile(path);
|
||||
} catch (IOException e) {
|
||||
log.error("create file error " + targetFile, e);
|
||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// download
|
||||
try {
|
||||
return restTemplate.execute(driverFileUrl, HttpMethod.GET, null, response -> {
|
||||
if (response.getStatusCode().is2xxSuccessful()) {
|
||||
File file = path.toFile();
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
StreamUtils.copy(response.getBody(), out);
|
||||
IOUtils.closeQuietly(out, ex -> log.error("close file error", ex));
|
||||
log.info("{} download success ", targetFile);
|
||||
return file;
|
||||
} else {
|
||||
log.error("{} download error from {}: {} ", targetFile, driverFileUrl, response);
|
||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception("驱动下载失败:"
|
||||
+ response.getStatusCode()
|
||||
+ ", "
|
||||
+ response.getStatusText());
|
||||
}
|
||||
});
|
||||
} catch (RestClientException e) {
|
||||
log.error(targetFile + " download driver error", e);
|
||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteByDatabaseType(String databaseType) {
|
||||
String baseDir = driverBaseDirectory + "/" + databaseType;
|
||||
Path path = Paths.get(baseDir);
|
||||
try {
|
||||
if (Files.exists(path)) {
|
||||
Files.list(path).forEach(file -> {
|
||||
try {
|
||||
Files.deleteIfExists(file);
|
||||
} catch (IOException e) {
|
||||
log.error("delete file error " + file, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
Files.deleteIfExists(path);
|
||||
} catch (IOException e) {
|
||||
log.error("delete driver error " + databaseType, e);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<File> loadByDatabaseType(String databaseType) {
|
||||
Path path = Paths.get(driverFilePath(driverBaseDirectory, databaseType));
|
||||
if (Files.exists(path)) {
|
||||
return Optional.of(path.toFile());
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public File loadOrDownloadByDatabaseType(String databaseType, String driverFileUrl) {
|
||||
return loadByDatabaseType(databaseType)
|
||||
.orElseGet(() -> download(driverFileUrl, driverFilePath(driverBaseDirectory, databaseType)));
|
||||
}
|
||||
|
||||
public String resolveDriverClassName(String driverFileUrl) {
|
||||
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
||||
File driverFile = download(driverFileUrl, tempFilePath);
|
||||
@@ -100,47 +149,7 @@ public class DriverResources {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
private File download(String driverFileUrl, String targetFile) {
|
||||
Path path = Path.of(targetFile);
|
||||
|
||||
// create parent directory
|
||||
if (Files.notExists(path)) {
|
||||
path.getParent().toFile().mkdirs();
|
||||
try {
|
||||
Files.createFile(path);
|
||||
} catch (IOException e) {
|
||||
log.error("create file error " + targetFile, e);
|
||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// download
|
||||
try {
|
||||
return restTemplate.execute(driverFileUrl, HttpMethod.GET, null, response -> {
|
||||
if (response.getStatusCode().is2xxSuccessful()) {
|
||||
File file = path.toFile();
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
StreamUtils.copy(response.getBody(), out);
|
||||
IOUtils.closeQuietly(out, ex -> log.error("close file error", ex));
|
||||
log.info("{} download success ", targetFile);
|
||||
return file;
|
||||
} else {
|
||||
log.error("{} download error from {}: {} ", targetFile, driverFileUrl, response);
|
||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception("驱动下载失败:"
|
||||
+ response.getStatusCode()
|
||||
+ ", "
|
||||
+ response.getStatusText());
|
||||
}
|
||||
});
|
||||
} catch (RestClientException e) {
|
||||
log.error(targetFile + " download driver error", e);
|
||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void validateJar(String driverFileUrl, String className) {
|
||||
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
||||
File driverFile = download(driverFileUrl, tempFilePath);
|
||||
public void validateDriverJar(File driverFile, String className) {
|
||||
URLClassLoader loader = null;
|
||||
try {
|
||||
loader = new URLClassLoader(
|
||||
@@ -165,22 +174,12 @@ public class DriverResources {
|
||||
throw DomainErrors.DRIVER_CLASS_NOT_FOUND.exception("驱动初始化异常, 请检查驱动类名:" + e.getMessage());
|
||||
} finally {
|
||||
IOUtils.closeQuietly(loader);
|
||||
try {
|
||||
Files.deleteIfExists(driverFile.toPath());
|
||||
} catch (IOException e) {
|
||||
log.error("delete driver error " + tempFilePath, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String driverFilePath(String baseDir, String databaseType) {
|
||||
String fileName = databaseType + ".jar";
|
||||
String filePath;
|
||||
if (baseDir.endsWith(File.separator)) {
|
||||
filePath = baseDir + fileName;
|
||||
} else {
|
||||
filePath = baseDir + File.separator + fileName;
|
||||
}
|
||||
return filePath;
|
||||
private String targetDriverFile(String databaseType) {
|
||||
return driverBaseDirectory
|
||||
+ "/" + databaseType
|
||||
+ "/" + System.currentTimeMillis() + ".jar";
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package com.databasir.core.infrastructure.driver;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class DriverResult {
|
||||
|
||||
private String driverFilePath;
|
||||
|
||||
private File driverFile;
|
||||
|
||||
}
|
@@ -6,6 +6,7 @@ import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.database.data.DatabaseTypeCreateRequest;
|
||||
import com.databasir.core.domain.database.data.DatabaseTypeUpdateRequest;
|
||||
import com.databasir.core.infrastructure.driver.DriverResources;
|
||||
import com.databasir.core.infrastructure.driver.DriverResult;
|
||||
import com.databasir.dao.impl.DatabaseTypeDao;
|
||||
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@@ -17,6 +18,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
||||
@Transactional
|
||||
@@ -33,7 +35,9 @@ class DatabaseTypeServiceTest extends BaseTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
Mockito.doNothing().when(driverResources).validateJar(anyString(), anyString());
|
||||
Mockito.doNothing().when(driverResources).validateDriverJar(any(), anyString());
|
||||
Mockito.when(driverResources.load(any(), anyString(), anyString()))
|
||||
.thenReturn(new DriverResult("", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user