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