Browse Source

feat:support custom url pattern

master
vran 3 years ago
parent
commit
327d637557
  1. 5
      api/src/main/java/com/databasir/api/DatabaseTypeController.java
  2. 23
      api/src/main/java/com/databasir/api/validator/DatabaseTypeValidator.java
  3. 1
      core/src/main/java/com/databasir/core/domain/DomainErrors.java
  4. 4
      core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypeCreateRequest.java
  5. 2
      core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypeDetailResponse.java
  6. 2
      core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypePageResponse.java
  7. 3
      core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypeUpdateRequest.java
  8. 13
      core/src/main/java/com/databasir/core/domain/document/service/DocumentService.java
  9. 7
      core/src/main/java/com/databasir/core/infrastructure/connection/CustomDatabaseConnectionFactory.java
  10. 2
      core/src/main/java/com/databasir/core/infrastructure/connection/MysqlDatabaseConnectionFactory.java
  11. 2
      core/src/main/java/com/databasir/core/infrastructure/connection/PostgresqlDatabaseConnectionFactory.java

5
api/src/main/java/com/databasir/api/DatabaseTypeController.java

@ -1,5 +1,6 @@
package com.databasir.api;
import com.databasir.api.validator.DatabaseTypeValidator;
import com.databasir.common.JsonData;
import com.databasir.core.domain.database.data.*;
import com.databasir.core.domain.database.service.DatabaseTypeService;
@ -24,6 +25,8 @@ public class DatabaseTypeController {
private final DatabaseTypeService databaseTypeService;
private final DatabaseTypeValidator databaseTypeValidator;
@GetMapping(Routes.DatabaseType.LIST_SIMPLE)
public JsonData<List<String>> listSimpleDatabaseTypes() {
List<String> types = databaseTypeService.listSimpleDatabaseTypes();
@ -41,6 +44,7 @@ public class DatabaseTypeController {
@PostMapping(Routes.DatabaseType.CREATE)
@Operation(module = Operation.Modules.DATABASE_TYPE, name = "创建数据库类型")
public JsonData<Integer> create(@RequestBody @Valid DatabaseTypeCreateRequest request) {
databaseTypeValidator.isValidUrlPattern(request.getUrlPattern());
Integer id = databaseTypeService.create(request);
return JsonData.ok(id);
}
@ -48,6 +52,7 @@ public class DatabaseTypeController {
@PatchMapping(Routes.DatabaseType.UPDATE)
@Operation(module = Operation.Modules.DATABASE_TYPE, name = "更新数据库类型")
public JsonData<Void> update(@RequestBody @Valid DatabaseTypeUpdateRequest request) {
databaseTypeValidator.isValidUrlPattern(request.getUrlPattern());
databaseTypeService.update(request);
return JsonData.ok();
}

23
api/src/main/java/com/databasir/api/validator/DatabaseTypeValidator.java

@ -0,0 +1,23 @@
package com.databasir.api.validator;
import com.databasir.core.domain.DomainErrors;
import org.springframework.stereotype.Component;
@Component
public class DatabaseTypeValidator {
public void isValidUrlPattern(String urlPattern) {
if (urlPattern == null) {
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("url pattern 不能为空");
}
if (!urlPattern.contains("{{jdbc.protocol}}")) {
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{jdbc.protocol}}");
}
if (!urlPattern.contains("{{db.url}}")) {
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{db.url}}不能为空");
}
if (!urlPattern.contains("{{db.name}}")) {
throw DomainErrors.INVALID_DATABASE_TYPE_URL_PATTERN.exception("必须包含变量{{db.name}}不能为空");
}
}
}

1
core/src/main/java/com/databasir/core/domain/DomainErrors.java

@ -31,6 +31,7 @@ public enum DomainErrors implements DatabasirErrors {
DATABASE_TYPE_NAME_DUPLICATE("A_10016", "数据库类型名已存在"),
MUST_NOT_MODIFY_SYSTEM_DEFAULT_DATABASE_TYPE("A_10017", "禁止修改系统默认数据库类型"),
DOWNLOAD_DRIVER_ERROR("A_10018", "驱动下载失败"),
INVALID_DATABASE_TYPE_URL_PATTERN("A_10019", "不合法的 url pattern"),
;
private final String errCode;

4
core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypeCreateRequest.java

@ -23,4 +23,8 @@ public class DatabaseTypeCreateRequest {
@NotBlank
private String jdbcProtocol;
@NotBlank
private String urlPattern;
}

2
core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypeDetailResponse.java

@ -21,6 +21,8 @@ public class DatabaseTypeDetailResponse {
private String jdbcProtocol;
private String urlPattern;
private Boolean deleted;
private Integer deletedToken;

2
core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypePageResponse.java

@ -27,6 +27,8 @@ public class DatabaseTypePageResponse {
private Integer projectCount;
private String urlPattern;
private LocalDateTime updateAt;
private LocalDateTime createAt;

3
core/src/main/java/com/databasir/core/domain/database/data/DatabaseTypeUpdateRequest.java

@ -27,4 +27,7 @@ public class DatabaseTypeUpdateRequest {
@NotBlank
private String jdbcProtocol;
@NotBlank
private String urlPattern;
}

13
core/src/main/java/com/databasir/core/domain/document/service/DocumentService.java

@ -107,7 +107,18 @@ public class DocumentService {
documentPojoConverter.toColumnPojo(docId, tableMetaId, table.getColumns());
tableColumnDocumentDao.batchInsert(tableColumnMetas);
List<TableIndexDocumentPojo> tableIndexMetas =
documentPojoConverter.toIndexPojo(docId, tableMetaId, table.getIndexes());
documentPojoConverter.toIndexPojo(docId, tableMetaId, table.getIndexes())
.stream()
.filter(index -> {
if (index.getName() != null) {
return true;
} else {
log.warn("ignore table {} index {}, cause name is null", table.getName(), index);
return false;
}
})
.collect(Collectors.toList());
tableIndexDocumentDao.batchInsert(tableIndexMetas);
List<TableTriggerDocumentPojo> tableTriggerMetas =
documentPojoConverter.toTriggerPojo(docId, tableMetaId, table.getTriggers());

7
core/src/main/java/com/databasir/core/infrastructure/connection/CustomDatabaseConnectionFactory.java

@ -6,6 +6,7 @@ import com.databasir.dao.impl.DatabaseTypeDao;
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.io.File;
@ -22,6 +23,7 @@ import java.util.Properties;
@Component
@RequiredArgsConstructor
@Slf4j
@Order
public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactory {
private final DatabaseTypeDao databaseTypeDao;
@ -73,10 +75,13 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
throw DomainErrors.CONNECT_DATABASE_FAILED.exception("驱动初始化异常:" + e.getMessage());
}
String urlPattern = type.getUrlPattern();
String jdbcUrl = urlPattern.replace("{{jdbc.protocol}}", type.getJdbcProtocol())
.replace("{{db.url}}", context.getUrl())
.replace("{{db.name}}", context.getSchema());
Properties info = new Properties();
info.put("user", context.getUsername());
info.put("password", context.getPassword());
String jdbcUrl = type.getJdbcProtocol() + "://" + context.getUrl() + "/" + context.getSchema();
return driver.connect(jdbcUrl, info);
}

2
core/src/main/java/com/databasir/core/infrastructure/connection/MysqlDatabaseConnectionFactory.java

@ -1,5 +1,6 @@
package com.databasir.core.infrastructure.connection;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.sql.Connection;
@ -8,6 +9,7 @@ import java.sql.SQLException;
import java.util.Properties;
@Component
@Order(1)
public class MysqlDatabaseConnectionFactory implements DatabaseConnectionFactory {
@Override

2
core/src/main/java/com/databasir/core/infrastructure/connection/PostgresqlDatabaseConnectionFactory.java

@ -1,5 +1,6 @@
package com.databasir.core.infrastructure.connection;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.sql.Connection;
@ -8,6 +9,7 @@ import java.sql.SQLException;
import java.util.Properties;
@Component
@Order(2)
public class PostgresqlDatabaseConnectionFactory implements DatabaseConnectionFactory {
@Override

Loading…
Cancel
Save