mirror of
				https://github.com/vran-dev/databasir.git
				synced 2025-10-31 12:39:20 +08:00 
			
		
		
		
	feat:support custom url pattern
This commit is contained in:
		| @@ -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(); | ||||
|     } | ||||
|   | ||||
| @@ -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}}不能为空"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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; | ||||
|   | ||||
| @@ -23,4 +23,8 @@ public class DatabaseTypeCreateRequest { | ||||
|  | ||||
|     @NotBlank | ||||
|     private String jdbcProtocol; | ||||
|  | ||||
|     @NotBlank | ||||
|     private String urlPattern; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -21,6 +21,8 @@ public class DatabaseTypeDetailResponse { | ||||
|  | ||||
|     private String jdbcProtocol; | ||||
|  | ||||
|     private String urlPattern; | ||||
|  | ||||
|     private Boolean deleted; | ||||
|  | ||||
|     private Integer deletedToken; | ||||
|   | ||||
| @@ -27,6 +27,8 @@ public class DatabaseTypePageResponse { | ||||
|  | ||||
|     private Integer projectCount; | ||||
|  | ||||
|     private String urlPattern; | ||||
|  | ||||
|     private LocalDateTime updateAt; | ||||
|  | ||||
|     private LocalDateTime createAt; | ||||
|   | ||||
| @@ -27,4 +27,7 @@ public class DatabaseTypeUpdateRequest { | ||||
|  | ||||
|     @NotBlank | ||||
|     private String jdbcProtocol; | ||||
|  | ||||
|     @NotBlank | ||||
|     private String urlPattern; | ||||
| } | ||||
|   | ||||
| @@ -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()); | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user