feat: support database, schema config
This commit is contained in:
parent
ffaa3c5e21
commit
e84f3aae92
|
@ -1,23 +1,24 @@
|
|||
package com.databasir.api.validator;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.databasir.core.domain.DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN;
|
||||
|
||||
@Component
|
||||
public class DatabaseTypeValidator {
|
||||
|
||||
public void isValidUrlPattern(String urlPattern) {
|
||||
if (urlPattern == null) {
|
||||
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("url pattern 不能为空");
|
||||
throw INVALID_DATABASE_TYPE_URL_PATTERN.exception("url pattern 不能为空");
|
||||
}
|
||||
if (!urlPattern.contains("{{jdbc.protocol}}")) {
|
||||
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{jdbc.protocol}}");
|
||||
throw INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{jdbc.protocol}}");
|
||||
}
|
||||
if (!urlPattern.contains("{{db.url}}")) {
|
||||
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{db.url}}不能为空");
|
||||
throw INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{db.url}}不能为空");
|
||||
}
|
||||
if (!urlPattern.contains("{{db.name}}")) {
|
||||
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{db.name}}不能为空");
|
||||
if (!urlPattern.contains("{{db.schema}}") && !urlPattern.contains("{{db.name}}")) {
|
||||
throw INVALID_DATABASE_TYPE_URL_PATTERN.exception("{{db.schema}} 和 {{db.name}} 至少设置一个");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import java.util.stream.Collectors;
|
|||
public interface DocumentPojoConverter extends BaseConverter {
|
||||
|
||||
@Mapping(target = "databaseName", source = "meta.databaseName")
|
||||
@Mapping(target = "schemaName", source = "meta.schemaName")
|
||||
@Mapping(target = "isArchive", constant = "false")
|
||||
DatabaseDocumentPojo toDatabasePojo(Integer projectId,
|
||||
com.databasir.core.meta.data.DatabaseMeta meta,
|
||||
|
|
|
@ -19,6 +19,8 @@ public class DatabaseDocumentResponse {
|
|||
|
||||
private String databaseName;
|
||||
|
||||
private String schemaName;
|
||||
|
||||
private String productName;
|
||||
|
||||
private String productVersion;
|
||||
|
|
|
@ -13,6 +13,8 @@ public class DatabaseDocumentSimpleResponse {
|
|||
|
||||
private String databaseName;
|
||||
|
||||
private String schemaName;
|
||||
|
||||
private String productName;
|
||||
|
||||
private String productVersion;
|
||||
|
|
|
@ -89,7 +89,7 @@ public class DocumentService {
|
|||
databasirConfig.setIgnoreTableNameRegex(jsonConverter.fromJson(rule.getIgnoreTableNameRegexArray()));
|
||||
databasirConfig.setIgnoreTableColumnNameRegex(jsonConverter.fromJson(rule.getIgnoreColumnNameRegexArray()));
|
||||
return Databasir.of(databasirConfig)
|
||||
.get(jdbcConnection, dataSource.getDatabaseName())
|
||||
.get(jdbcConnection, dataSource.getDatabaseName(), dataSource.getSchemaName())
|
||||
.orElseThrow(DomainErrors.DATABASE_META_NOT_FOUND::exception);
|
||||
}
|
||||
|
||||
|
|
|
@ -40,6 +40,9 @@ public class ProjectCreateRequest {
|
|||
@NotBlank
|
||||
private String databaseName;
|
||||
|
||||
@NotBlank
|
||||
private String schemaName;
|
||||
|
||||
@NotBlank
|
||||
private String databaseType;
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ public class ProjectDetailResponse {
|
|||
|
||||
private String databaseName;
|
||||
|
||||
private String schemaName;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private List<DataSourcePropertyValue> properties = new ArrayList<>();
|
||||
|
|
|
@ -15,6 +15,8 @@ public class ProjectListCondition {
|
|||
|
||||
private String databaseNameContains;
|
||||
|
||||
private String schemaNameContains;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private Integer groupId;
|
||||
|
@ -29,6 +31,10 @@ public class ProjectListCondition {
|
|||
Condition condition = Tables.DATA_SOURCE.DATABASE_NAME.contains(databaseNameContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (schemaNameContains != null) {
|
||||
Condition condition = Tables.DATA_SOURCE.SCHEMA_NAME.contains(schemaNameContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (databaseType != null) {
|
||||
Condition condition = Tables.DATA_SOURCE.DATABASE_TYPE.eq(databaseType);
|
||||
conditions.add(condition);
|
||||
|
|
|
@ -15,6 +15,8 @@ public class ProjectSimpleResponse {
|
|||
|
||||
private String databaseName;
|
||||
|
||||
private String schemaName;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private Boolean isAutoSync;
|
||||
|
|
|
@ -22,6 +22,9 @@ public class ProjectTestConnectionRequest {
|
|||
@NotBlank
|
||||
private String databaseName;
|
||||
|
||||
@NotBlank
|
||||
private String schemaName;
|
||||
|
||||
@NotBlank
|
||||
private String databaseType;
|
||||
|
||||
|
|
|
@ -39,6 +39,9 @@ public class ProjectUpdateRequest {
|
|||
@NotBlank
|
||||
private String databaseName;
|
||||
|
||||
@NotBlank
|
||||
private String schemaName;
|
||||
|
||||
@NotBlank
|
||||
private String databaseType;
|
||||
|
||||
|
|
|
@ -169,6 +169,7 @@ public class ProjectService {
|
|||
password,
|
||||
request.getUrl(),
|
||||
request.getDatabaseName(),
|
||||
request.getSchemaName(),
|
||||
request.getDatabaseType(),
|
||||
properties);
|
||||
}
|
||||
|
|
|
@ -78,7 +78,8 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
|||
String urlPattern = type.getUrlPattern();
|
||||
String jdbcUrl = urlPattern.replace("{{jdbc.protocol}}", type.getJdbcProtocol())
|
||||
.replace("{{db.url}}", context.getUrl())
|
||||
.replace("{{db.name}}", context.getSchema());
|
||||
.replace("{{db.name}}", context.getDatabaseName())
|
||||
.replace("{{db.schema}}", context.getSchemaName());
|
||||
Properties info = new Properties();
|
||||
info.put("user", context.getUsername());
|
||||
info.put("password", context.getPassword());
|
||||
|
|
|
@ -25,7 +25,9 @@ public interface DatabaseConnectionFactory {
|
|||
|
||||
private String url;
|
||||
|
||||
private String schema;
|
||||
private String databaseName;
|
||||
|
||||
private String schemaName;
|
||||
|
||||
private Properties properties;
|
||||
}
|
||||
|
|
|
@ -36,7 +36,8 @@ public class DatabaseConnectionService {
|
|||
.username(username)
|
||||
.password(password)
|
||||
.url(url)
|
||||
.schema(dataSource.getDatabaseName())
|
||||
.databaseName(dataSource.getDatabaseName())
|
||||
.schemaName(dataSource.getSchemaName())
|
||||
.properties(info)
|
||||
.databaseType(dataSource.getDatabaseType())
|
||||
.build();
|
||||
|
@ -54,6 +55,7 @@ public class DatabaseConnectionService {
|
|||
String password,
|
||||
String url,
|
||||
String databaseName,
|
||||
String schemaName,
|
||||
String databaseType,
|
||||
Properties properties) {
|
||||
try {
|
||||
|
@ -61,7 +63,8 @@ public class DatabaseConnectionService {
|
|||
.username(username)
|
||||
.password(password)
|
||||
.url(url)
|
||||
.schema(databaseName)
|
||||
.databaseName(databaseName)
|
||||
.schemaName(schemaName)
|
||||
.properties(properties)
|
||||
.databaseType(databaseType)
|
||||
.build();
|
||||
|
|
|
@ -29,7 +29,7 @@ public class MysqlDatabaseConnectionFactory implements DatabaseConnectionFactory
|
|||
info.put("user", context.getUsername());
|
||||
info.put("password", context.getPassword());
|
||||
info.putAll(context.getProperties());
|
||||
String jdbcUrl = "jdbc:mysql://" + context.getUrl() + "/" + context.getSchema();
|
||||
String jdbcUrl = "jdbc:mysql://" + context.getUrl() + "/" + context.getDatabaseName();
|
||||
return DriverManager.getConnection(jdbcUrl, info);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ public class PostgresqlDatabaseConnectionFactory implements DatabaseConnectionFa
|
|||
info.put("user", context.getUsername());
|
||||
info.put("password", context.getPassword());
|
||||
info.putAll(context.getProperties());
|
||||
String jdbcUrl = "jdbc:postgresql://" + context.getUrl() + "/" + context.getSchema();
|
||||
String jdbcUrl = "jdbc:postgresql://" + context.getUrl() + "/" + context.getDatabaseName();
|
||||
return DriverManager.getConnection(jdbcUrl, info);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,10 @@ public class Databasir {
|
|||
|
||||
private final DatabasirConfig config;
|
||||
|
||||
public Optional<DatabaseMeta> get(Connection connection, String databaseName) {
|
||||
public Optional<DatabaseMeta> get(Connection connection, String databaseName, String schemaName) {
|
||||
Condition condition = Condition.builder()
|
||||
.databaseName(databaseName)
|
||||
.schemaName(schemaName)
|
||||
.ignoreTableNameRegex(config.getIgnoreTableNameRegex())
|
||||
.ignoreTableColumnNameRegex(config.getIgnoreTableColumnNameRegex())
|
||||
.build();
|
||||
|
|
|
@ -35,6 +35,11 @@ public class DatabaseMeta {
|
|||
*/
|
||||
private String databaseName;
|
||||
|
||||
/**
|
||||
* schema_name
|
||||
*/
|
||||
private String schemaName;
|
||||
|
||||
@Builder.Default
|
||||
private List<TableMeta> tables = Collections.emptyList();
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@ public class Condition {
|
|||
@NonNull
|
||||
private String databaseName;
|
||||
|
||||
private String schemaName;
|
||||
|
||||
@Builder.Default
|
||||
private Collection<String> ignoreTableNameRegex = Collections.emptyList();
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ public class JdbcDatabaseMetaRepository implements DatabaseMetaRepository {
|
|||
.productName(metaData.getDatabaseProductName())
|
||||
.productVersion(metaData.getDatabaseProductVersion())
|
||||
.databaseName(catalogName)
|
||||
.schemaName(condition.getSchemaName())
|
||||
.tables(tableDocs)
|
||||
.build();
|
||||
return Optional.of(meta);
|
||||
|
|
|
@ -40,7 +40,7 @@ public class JdbcTableMetaRepository implements TableMetaRepository {
|
|||
List<TableMeta> tableMetas = new ArrayList<>();
|
||||
String databaseName = condition.getDatabaseName();
|
||||
ResultSet tablesResult = connection.getMetaData()
|
||||
.getTables(databaseName, null, null, new String[]{"TABLE"});
|
||||
.getTables(databaseName, condition.getSchemaName(), null, new String[]{"TABLE"});
|
||||
while (tablesResult.next()) {
|
||||
String tableName = tablesResult.getString("TABLE_NAME");
|
||||
if (condition.tableIsIgnored(tableName)) {
|
||||
|
|
Loading…
Reference in New Issue