mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-08 18:10:26 +08:00
feat: add test connection api (#4)
This commit is contained in:
@@ -35,4 +35,8 @@ public enum DomainErrors implements DatabasirErrors {
|
||||
public DatabasirException exception(Throwable origin) {
|
||||
return new DatabasirException(this, origin);
|
||||
}
|
||||
|
||||
public DatabasirException exception(String message, Throwable origin) {
|
||||
return new DatabasirException(this, message, origin);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,29 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ProjectTestConnectionRequest {
|
||||
|
||||
private Integer projectId;
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
@NotBlank
|
||||
private String url;
|
||||
|
||||
@NotBlank
|
||||
private String databaseName;
|
||||
|
||||
@NotBlank
|
||||
private String databaseType;
|
||||
|
||||
private List<DataSourcePropertyValue> properties = new ArrayList<>();
|
||||
}
|
@@ -6,6 +6,7 @@ import com.databasir.core.domain.project.converter.DataSourcePojoConverter;
|
||||
import com.databasir.core.domain.project.converter.ProjectPojoConverter;
|
||||
import com.databasir.core.domain.project.converter.ProjectResponseConverter;
|
||||
import com.databasir.core.domain.project.data.*;
|
||||
import com.databasir.core.infrastructure.connection.DatabaseConnectionService;
|
||||
import com.databasir.dao.impl.*;
|
||||
import com.databasir.dao.tables.pojos.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -19,6 +20,7 @@ import org.springframework.util.StringUtils;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -42,6 +44,8 @@ public class ProjectService {
|
||||
|
||||
private final ProjectResponseConverter projectResponseConverter;
|
||||
|
||||
private final DatabaseConnectionService databaseConnectionService;
|
||||
|
||||
public ProjectDetailResponse getOne(Integer id) {
|
||||
return projectDao.selectOptionalById(id)
|
||||
.map(schemaSource -> {
|
||||
@@ -142,4 +146,25 @@ public class ProjectService {
|
||||
});
|
||||
}
|
||||
|
||||
public void testConnection(ProjectTestConnectionRequest request) {
|
||||
String password;
|
||||
if (request.getProjectId() != null && !StringUtils.hasText(request.getPassword())) {
|
||||
DataSourcePojo dataSource = dataSourceDao.selectByProjectId(request.getProjectId());
|
||||
SysKeyPojo sysKey = sysKeyDao.selectTopOne();
|
||||
password = Aes.decryptFromBase64Data(dataSource.getPassword(), sysKey.getAesKey());
|
||||
} else if (StringUtils.hasText(request.getPassword())) {
|
||||
password = request.getPassword();
|
||||
} else {
|
||||
throw DomainErrors.PASSWORD_MUST_NOT_BE_BLANK.exception();
|
||||
}
|
||||
Properties properties = new Properties();
|
||||
request.getProperties().forEach(prop -> properties.put(prop.getKey(), prop.getValue()));
|
||||
databaseConnectionService.testConnection(request.getUsername(),
|
||||
password,
|
||||
request.getUrl(),
|
||||
request.getDatabaseName(),
|
||||
request.getDatabaseType(),
|
||||
properties);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public interface DatabaseConnectionFactory {
|
||||
@@ -11,5 +12,5 @@ public interface DatabaseConnectionFactory {
|
||||
String password,
|
||||
String url,
|
||||
String schema,
|
||||
Properties properties);
|
||||
Properties properties) throws SQLException;
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -30,10 +31,31 @@ public class DatabaseConnectionService {
|
||||
|
||||
Properties info = new Properties();
|
||||
dataSourceProperties.forEach(prop -> info.put(prop.getKey(), prop.getValue()));
|
||||
return factories.stream()
|
||||
.filter(factory -> factory.support(dataSource.getDatabaseType()))
|
||||
.findFirst()
|
||||
.orElseThrow(DomainErrors.NOT_SUPPORT_DATABASE_TYPE::exception)
|
||||
.getConnection(username, password, url, dataSource.getDatabaseName(), info);
|
||||
try {
|
||||
return factories.stream()
|
||||
.filter(factory -> factory.support(dataSource.getDatabaseType()))
|
||||
.findFirst()
|
||||
.orElseThrow(DomainErrors.NOT_SUPPORT_DATABASE_TYPE::exception)
|
||||
.getConnection(username, password, url, dataSource.getDatabaseName(), info);
|
||||
} catch (SQLException e) {
|
||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void testConnection(String username,
|
||||
String password,
|
||||
String url,
|
||||
String databaseName,
|
||||
String databaseType,
|
||||
Properties properties) {
|
||||
try {
|
||||
factories.stream()
|
||||
.filter(factory -> factory.support(databaseType))
|
||||
.findFirst()
|
||||
.orElseThrow(DomainErrors.NOT_SUPPORT_DATABASE_TYPE::exception)
|
||||
.getConnection(username, password, url, databaseName, properties);
|
||||
} catch (SQLException e) {
|
||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
@@ -17,7 +16,11 @@ public class MysqlDatabaseConnectionFactory implements DatabaseConnectionFactory
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password, String url, String schema, Properties properties) {
|
||||
public Connection getConnection(String username,
|
||||
String password,
|
||||
String url,
|
||||
String schema,
|
||||
Properties properties) throws SQLException {
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
@@ -29,11 +32,7 @@ public class MysqlDatabaseConnectionFactory implements DatabaseConnectionFactory
|
||||
info.put("password", password);
|
||||
info.putAll(properties);
|
||||
String jdbcUrl = "jdbc:mysql://" + url + "/" + schema;
|
||||
try {
|
||||
return DriverManager.getConnection(jdbcUrl, info);
|
||||
} catch (SQLException e) {
|
||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e);
|
||||
}
|
||||
return DriverManager.getConnection(jdbcUrl, info);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
@@ -18,7 +17,11 @@ public class PostgresqlDatabaseConnectionFactory implements DatabaseConnectionFa
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password, String url, String schema, Properties properties) {
|
||||
public Connection getConnection(String username,
|
||||
String password,
|
||||
String url,
|
||||
String schema,
|
||||
Properties properties) throws SQLException {
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
@@ -30,10 +33,6 @@ public class PostgresqlDatabaseConnectionFactory implements DatabaseConnectionFa
|
||||
info.put("password", password);
|
||||
info.putAll(properties);
|
||||
String jdbcUrl = "jdbc:postgresql://" + url + "/" + schema;
|
||||
try {
|
||||
return DriverManager.getConnection(jdbcUrl, info);
|
||||
} catch (SQLException e) {
|
||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e);
|
||||
}
|
||||
return DriverManager.getConnection(jdbcUrl, info);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user