mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-09 13:56:50 +08:00
feat: init api (#2)
This commit is contained in:
@@ -1,5 +1,37 @@
|
||||
plugins {
|
||||
id 'io.spring.dependency-management'
|
||||
id 'org.springframework.boot' apply false
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation 'mysql:mysql-connector-java:8.0.27'
|
||||
// internal module
|
||||
implementation project(":common")
|
||||
implementation project(":dao")
|
||||
implementation project(":plugin")
|
||||
|
||||
// spring boot
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-jooq'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-mail'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||
|
||||
// datasource
|
||||
implementation "com.zaxxer:HikariCP:${hikariVersion}"
|
||||
// jdbc driver
|
||||
implementation "mysql:mysql-connector-java:${mysqlConnectorVersion}"
|
||||
implementation "org.postgresql:postgresql:${postgresqlConnectorVersion}"
|
||||
|
||||
// jackson
|
||||
implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
|
||||
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
|
||||
|
||||
// others
|
||||
implementation 'com.auth0:java-jwt:3.18.3'
|
||||
implementation 'org.commonmark:commonmark:0.18.1'
|
||||
implementation 'org.freemarker:freemarker:2.3.31'
|
||||
|
||||
// test
|
||||
testImplementation "mysql:mysql-connector-java:${mysqlConnectorVersion}"
|
||||
}
|
||||
|
||||
|
@@ -1,74 +0,0 @@
|
||||
package com.databasir.core;
|
||||
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import com.databasir.core.meta.repository.*;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
import com.databasir.core.meta.repository.impl.jdbc.*;
|
||||
import com.databasir.core.render.Render;
|
||||
import com.databasir.core.render.RenderConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.sql.Connection;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public class Databasir {
|
||||
|
||||
private final DatabasirConfig config;
|
||||
|
||||
public Optional<DatabaseMeta> get(Connection connection, String databaseName) {
|
||||
Condition condition = Condition.builder()
|
||||
.databaseName(databaseName)
|
||||
.ignoreTableNameRegex(config.getIgnoreTableNameRegex())
|
||||
.ignoreTableColumnNameRegex(config.getIgnoreTableColumnNameRegex())
|
||||
.build();
|
||||
return config.getDatabaseMetaRepository().select(connection, condition);
|
||||
}
|
||||
|
||||
public void renderAsMarkdown(DatabaseMeta meta, OutputStream out) throws IOException {
|
||||
renderAsMarkdown(new RenderConfig(), meta, out);
|
||||
}
|
||||
|
||||
public void renderAsMarkdown(RenderConfig config, DatabaseMeta meta, OutputStream stream) throws IOException {
|
||||
Render.markdownRender(config).rendering(meta, stream);
|
||||
}
|
||||
|
||||
public static Databasir of() {
|
||||
return of(new DatabasirConfig());
|
||||
}
|
||||
|
||||
public static Databasir of(DatabasirConfig config) {
|
||||
TriggerMetaRepository triggerMetaRepository = config.getTriggerMetaRepository();
|
||||
if (triggerMetaRepository == null) {
|
||||
triggerMetaRepository = new JdbcTriggerMetaRepository();
|
||||
}
|
||||
IndexMetaRepository indexMetaRepository = config.getIndexMetaRepository();
|
||||
if (indexMetaRepository == null) {
|
||||
indexMetaRepository = new JdbcIndexMetaRepository();
|
||||
}
|
||||
ColumnMetaRepository columnMetaRepository = config.getColumnMetaRepository();
|
||||
if (columnMetaRepository == null) {
|
||||
columnMetaRepository = new JdbcColumnMetaRepository();
|
||||
}
|
||||
TableMetaRepository tableMetaRepository = config.getTableMetaRepository();
|
||||
if (tableMetaRepository == null) {
|
||||
tableMetaRepository =
|
||||
new JdbcTableMetaRepository(columnMetaRepository, indexMetaRepository, triggerMetaRepository);
|
||||
}
|
||||
DatabaseMetaRepository databaseMetaRepository = config.getDatabaseMetaRepository();
|
||||
if (databaseMetaRepository == null) {
|
||||
databaseMetaRepository = new JdbcDatabaseMetaRepository(tableMetaRepository);
|
||||
}
|
||||
config.setTriggerMetaRepository(triggerMetaRepository);
|
||||
config.setIndexMetaRepository(indexMetaRepository);
|
||||
config.setColumnMetaRepository(columnMetaRepository);
|
||||
config.setTableMetaRepository(tableMetaRepository);
|
||||
config.setDatabaseMetaRepository(databaseMetaRepository);
|
||||
return new Databasir(config);
|
||||
}
|
||||
|
||||
}
|
@@ -1,37 +0,0 @@
|
||||
package com.databasir.core;
|
||||
|
||||
import com.databasir.core.meta.repository.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class DatabasirConfig {
|
||||
|
||||
private IndexMetaRepository indexMetaRepository;
|
||||
|
||||
private TriggerMetaRepository triggerMetaRepository;
|
||||
|
||||
private ColumnMetaRepository columnMetaRepository;
|
||||
|
||||
private TableMetaRepository tableMetaRepository;
|
||||
|
||||
private DatabaseMetaRepository databaseMetaRepository;
|
||||
|
||||
private Collection<String> ignoreTableNameRegex = new HashSet<>();
|
||||
|
||||
private Collection<String> ignoreTableColumnNameRegex = new HashSet<>();
|
||||
|
||||
public DatabasirConfig ignoreTable(String tableNameRegex) {
|
||||
ignoreTableNameRegex.add(tableNameRegex);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DatabasirConfig ignoreColumn(String columnNameRegex) {
|
||||
ignoreTableColumnNameRegex.add(columnNameRegex);
|
||||
return this;
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package com.databasir.core.domain;
|
||||
|
||||
import com.databasir.common.DatabasirErrors;
|
||||
import com.databasir.common.DatabasirException;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public enum DomainErrors implements DatabasirErrors {
|
||||
REFRESH_TOKEN_EXPIRED("X_0001", "refresh token expired"),
|
||||
ACCESS_TOKEN_REFRESH_INVALID("X_0002", "invalid refresh token operation"),
|
||||
|
||||
NOT_SUPPORT_DATABASE_TYPE("A_10000", "不支持的数据库类型, 请检查项目配置"),
|
||||
PROJECT_NOT_FOUND("A_10001", "项目不存在"),
|
||||
DATABASE_META_NOT_FOUND("A_10002", "获取数据库信息失败"),
|
||||
CONNECT_DATABASE_FAILED("A_10003", "连接数据库失败,请检查连接配置"),
|
||||
GROUP_OWNER_MUST_NOT_BE_EMPTY("A_10004", "请至少指定一个分组组长"),
|
||||
PASSWORD_MUST_NOT_BE_BLANK("A_10005", "密码不能为空"),
|
||||
USERNAME_OR_EMAIL_DUPLICATE("A_10006", "用户名或邮箱已存在"),
|
||||
USER_ROLE_DUPLICATE("A_10007", "用户角色已存在"),
|
||||
PROJECT_NAME_DUPLICATE("A_10008", "项目名称已被占用"),
|
||||
CANNOT_UPDATE_SELF_ROLE("A_10009", "无法对自己执行角色变更的操作"),
|
||||
UPDATE_PASSWORD_CONFIRM_FAILED("A_10010", "两次密码输入不一致"),
|
||||
ORIGIN_PASSWORD_NOT_CORRECT("A_10011", "原密码不正确");
|
||||
|
||||
private final String errCode;
|
||||
|
||||
private final String errMessage;
|
||||
|
||||
public DatabasirException exception() {
|
||||
return new DatabasirException(this);
|
||||
}
|
||||
|
||||
public DatabasirException exception(Throwable origin) {
|
||||
return new DatabasirException(this, origin);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
public interface BaseConverter {
|
||||
|
||||
@NullToEmpty
|
||||
default String nullToEmpty(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.dao.tables.pojos.DatabaseDocumentHistoryPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = JsonConverter.class, unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface DocumentHistoryPojoConverter {
|
||||
|
||||
@Mapping(target = "databaseDocumentObject", source = "databaseMetaObject")
|
||||
@Mapping(target = "id", ignore = true)
|
||||
@Mapping(target = "createAt", ignore = true)
|
||||
DatabaseDocumentHistoryPojo of(DatabaseDocumentResponse databaseMetaObject,
|
||||
Integer projectId,
|
||||
Integer databaseDocumentId,
|
||||
Long version);
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.core.meta.data.ColumnMeta;
|
||||
import com.databasir.core.meta.data.IndexMeta;
|
||||
import com.databasir.core.meta.data.TriggerMeta;
|
||||
import com.databasir.dao.tables.pojos.*;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = JsonConverter.class, unmappedTargetPolicy = ReportingPolicy.WARN)
|
||||
public interface DocumentPojoConverter extends BaseConverter {
|
||||
|
||||
@Mapping(target = "databaseName", source = "meta.databaseName")
|
||||
DatabaseDocumentPojo toDatabasePojo(Integer projectId,
|
||||
com.databasir.core.meta.data.DatabaseMeta meta,
|
||||
Long version);
|
||||
|
||||
@Mapping(target = "databaseName", source = "meta.databaseName")
|
||||
DatabaseDocumentPojo toDatabasePojo(Integer projectId,
|
||||
com.databasir.core.meta.data.DatabaseMeta meta,
|
||||
Integer id,
|
||||
Long version);
|
||||
|
||||
@Mapping(target = "comment", qualifiedBy = NullToEmpty.class)
|
||||
TableDocumentPojo toTablePojo(Integer databaseDocumentId,
|
||||
com.databasir.core.meta.data.TableMeta meta);
|
||||
|
||||
default List<TableColumnDocumentPojo> toColumnPojo(Integer databaseDocumentId,
|
||||
Integer tableDocumentId,
|
||||
List<ColumnMeta> metaList) {
|
||||
return metaList.stream()
|
||||
.map(meta -> toColumnPojo(databaseDocumentId, tableDocumentId, meta))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Mapping(target = "comment", qualifiedBy = NullToEmpty.class)
|
||||
TableColumnDocumentPojo toColumnPojo(Integer databaseDocumentId,
|
||||
Integer tableDocumentId,
|
||||
ColumnMeta meta);
|
||||
|
||||
default List<TableIndexDocumentPojo> toIndexPojo(Integer databaseDocumentId,
|
||||
Integer tableDocumentId,
|
||||
List<IndexMeta> metaList) {
|
||||
return metaList.stream()
|
||||
.map(meta -> toIndexPojo(databaseDocumentId, tableDocumentId, meta))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Mapping(target = "isPrimary", source = "meta.isPrimaryKey")
|
||||
@Mapping(target = "isUnique", source = "meta.isUniqueKey")
|
||||
@Mapping(target = "columnNameArray", source = "meta.columnNames")
|
||||
TableIndexDocumentPojo toIndexPojo(Integer databaseDocumentId,
|
||||
Integer tableDocumentId,
|
||||
IndexMeta meta);
|
||||
|
||||
default List<TableTriggerDocumentPojo> toTriggerPojo(Integer databaseDocumentId,
|
||||
Integer tableDocumentId,
|
||||
List<TriggerMeta> metaList) {
|
||||
return metaList.stream()
|
||||
.map(meta -> toTriggerPojo(databaseDocumentId, tableDocumentId, meta))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Mapping(target = "triggerCreateAt", source = "meta.createAt")
|
||||
TableTriggerDocumentPojo toTriggerPojo(Integer databaseDocumentId,
|
||||
Integer tableDocumentId,
|
||||
TriggerMeta meta);
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.dao.tables.pojos.*;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = JsonConverter.class, unmappedTargetPolicy = ReportingPolicy.WARN)
|
||||
public interface DocumentResponseConverter {
|
||||
|
||||
@Mapping(target = "columns", source = "columns")
|
||||
@Mapping(target = "indexes", source = "indexes")
|
||||
@Mapping(target = "triggers", source = "triggers")
|
||||
DatabaseDocumentResponse.TableDocumentResponse of(TableDocumentPojo tableDocument,
|
||||
List<TableColumnDocumentPojo> columns,
|
||||
List<TableIndexDocumentPojo> indexes,
|
||||
List<TableTriggerDocumentPojo> triggers);
|
||||
|
||||
@Mapping(target = "columnNames", source = "columnNameArray")
|
||||
DatabaseDocumentResponse.TableDocumentResponse.IndexDocumentResponse of(TableIndexDocumentPojo indexDocument);
|
||||
|
||||
@Mapping(target = "id", source = "databaseDocument.id")
|
||||
@Mapping(target = "createAt", source = "databaseDocument.createAt")
|
||||
@Mapping(target = "documentVersion", source = "databaseDocument.version")
|
||||
DatabaseDocumentResponse of(DatabaseDocumentPojo databaseDocument,
|
||||
List<DatabaseDocumentResponse.TableDocumentResponse> tables);
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
import org.mapstruct.Qualifier;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Qualifier
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
public @interface NullToEmpty {
|
||||
}
|
@@ -0,0 +1,125 @@
|
||||
package com.databasir.core.domain.document.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class DatabaseDocumentResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String databaseName;
|
||||
|
||||
private String productName;
|
||||
|
||||
private String productVersion;
|
||||
|
||||
private Integer documentVersion;
|
||||
|
||||
@Builder.Default
|
||||
private List<TableDocumentResponse> tables = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public static class TableDocumentResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
private String comment;
|
||||
|
||||
@Builder.Default
|
||||
private List<ColumnDocumentResponse> columns = new ArrayList<>();
|
||||
|
||||
@Builder.Default
|
||||
private List<IndexDocumentResponse> indexes = new ArrayList<>();
|
||||
|
||||
@Builder.Default
|
||||
private List<TriggerDocumentResponse> triggers = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public static class ColumnDocumentResponse {
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
private Integer size;
|
||||
|
||||
private Integer decimalDigits;
|
||||
|
||||
private String comment;
|
||||
|
||||
private String nullable;
|
||||
|
||||
private String autoIncrement;
|
||||
|
||||
private String defaultValue;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public static class IndexDocumentResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Boolean isPrimary;
|
||||
|
||||
private Boolean isUnique;
|
||||
|
||||
@Builder.Default
|
||||
private List<String> columnNames = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public static class TriggerDocumentResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String timing;
|
||||
|
||||
private String manipulation;
|
||||
|
||||
private String statement;
|
||||
|
||||
private String triggerCreateAt;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.databasir.core.domain.document.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class DatabaseDocumentVersionResponse {
|
||||
|
||||
private Long version;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
}
|
@@ -0,0 +1,186 @@
|
||||
package com.databasir.core.domain.document.service;
|
||||
|
||||
import com.databasir.core.Databasir;
|
||||
import com.databasir.core.DatabasirConfig;
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.document.converter.DocumentHistoryPojoConverter;
|
||||
import com.databasir.core.domain.document.converter.DocumentPojoConverter;
|
||||
import com.databasir.core.domain.document.converter.DocumentResponseConverter;
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentVersionResponse;
|
||||
import com.databasir.core.infrastructure.connection.DatabaseConnectionService;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.core.meta.data.DatabaseMeta;
|
||||
import com.databasir.dao.impl.*;
|
||||
import com.databasir.dao.tables.pojos.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class DocumentService {
|
||||
|
||||
private final ProjectDao projectDao;
|
||||
|
||||
private final ProjectSyncRuleDao projectSyncRuleDao;
|
||||
|
||||
private final DataSourceDao dataSourceDao;
|
||||
|
||||
private final DataSourcePropertyDao dataSourcePropertyDao;
|
||||
|
||||
private final SysKeyDao sysKeyDao;
|
||||
|
||||
private final DatabaseConnectionService databaseConnectionService;
|
||||
|
||||
private final DatabaseDocumentDao databaseDocumentDao;
|
||||
|
||||
private final TableDocumentDao tableDocumentDao;
|
||||
|
||||
private final TableColumnDocumentDao tableColumnDocumentDao;
|
||||
|
||||
private final TableIndexDocumentDao tableIndexDocumentDao;
|
||||
|
||||
private final TableTriggerDocumentDao tableTriggerDocumentDao;
|
||||
|
||||
private final DatabaseDocumentHistoryDao databaseDocumentHistoryDao;
|
||||
|
||||
private final DocumentPojoConverter documentPojoConverter;
|
||||
|
||||
private final DocumentResponseConverter documentResponseConverter;
|
||||
|
||||
private final DocumentHistoryPojoConverter documentHistoryPojoConverter;
|
||||
|
||||
private final JsonConverter jsonConverter;
|
||||
|
||||
@Transactional
|
||||
public void syncByProjectId(Integer projectId) {
|
||||
ProjectPojo project = projectDao.selectOptionalById(projectId)
|
||||
.orElseThrow(DomainErrors.PROJECT_NOT_FOUND::exception);
|
||||
DatabaseMeta meta = retrieveDatabaseMeta(projectId);
|
||||
Optional<DatabaseDocumentPojo> historyDocumentOpt = databaseDocumentDao.selectOptionalByProjectId(projectId);
|
||||
if (historyDocumentOpt.isPresent()) {
|
||||
DatabaseDocumentPojo historyDocument = historyDocumentOpt.get();
|
||||
Integer previousDocumentId = historyDocument.getId();
|
||||
saveAsHistory(historyDocument);
|
||||
deleteDeprecatedDocument(previousDocumentId);
|
||||
saveNewDocument(meta, historyDocument.getVersion() + 1, historyDocument.getProjectId(), previousDocumentId);
|
||||
} else {
|
||||
saveNewDocument(meta, 1L, projectId, null);
|
||||
}
|
||||
}
|
||||
|
||||
private DatabaseMeta retrieveDatabaseMeta(Integer projectId) {
|
||||
ProjectSyncRulePojo rule = projectSyncRuleDao.selectByProjectId(projectId);
|
||||
DataSourcePojo dataSource = dataSourceDao.selectByProjectId(projectId);
|
||||
List<DataSourcePropertyPojo> properties = dataSourcePropertyDao.selectByDataSourceId(dataSource.getId());
|
||||
Connection jdbcConnection = databaseConnectionService.create(dataSource, properties);
|
||||
DatabasirConfig databasirConfig = new DatabasirConfig();
|
||||
databasirConfig.setIgnoreTableNameRegex(jsonConverter.fromJson(rule.getIgnoreTableNameRegexArray()));
|
||||
databasirConfig.setIgnoreTableColumnNameRegex(jsonConverter.fromJson(rule.getIgnoreColumnNameRegexArray()));
|
||||
return Databasir.of(databasirConfig)
|
||||
.get(jdbcConnection, dataSource.getDatabaseName())
|
||||
.orElseThrow(DomainErrors.DATABASE_META_NOT_FOUND::exception);
|
||||
}
|
||||
|
||||
private void saveAsHistory(DatabaseDocumentPojo databaseDocument) {
|
||||
// save history
|
||||
Integer projectId = databaseDocument.getProjectId();
|
||||
Integer databaseMetaId = databaseDocument.getId();
|
||||
DatabaseDocumentResponse databaseDocumentResponse = getOneByProjectId(projectId, null).orElse(null);
|
||||
Long currVersion = databaseDocument.getVersion();
|
||||
DatabaseDocumentHistoryPojo documentHistoryPojo =
|
||||
documentHistoryPojoConverter.of(databaseDocumentResponse, projectId, databaseMetaId, currVersion);
|
||||
databaseDocumentHistoryDao.insertAndReturnId(documentHistoryPojo);
|
||||
log.info("save old meta info to history success");
|
||||
}
|
||||
|
||||
private void deleteDeprecatedDocument(Integer databaseDocumentId) {
|
||||
// delete old meta info
|
||||
tableDocumentDao.deleteByDatabaseDocumentId(databaseDocumentId);
|
||||
tableColumnDocumentDao.deleteByDatabaseDocumentId(databaseDocumentId);
|
||||
tableIndexDocumentDao.deleteByDatabaseMetaId(databaseDocumentId);
|
||||
tableTriggerDocumentDao.deleteByDatabaseDocumentId(databaseDocumentId);
|
||||
log.info("delete old meta info success");
|
||||
}
|
||||
|
||||
private void saveNewDocument(DatabaseMeta meta,
|
||||
Long version,
|
||||
Integer projectId,
|
||||
Integer databaseDocumentId) {
|
||||
|
||||
Integer currentDatabaseDocumentId = databaseDocumentId;
|
||||
if (databaseDocumentId == null) {
|
||||
currentDatabaseDocumentId =
|
||||
databaseDocumentDao.insertAndReturnId(documentPojoConverter.toDatabasePojo(projectId, meta, 1L));
|
||||
} else {
|
||||
databaseDocumentDao.update(documentPojoConverter.toDatabasePojo(projectId, meta, databaseDocumentId, version));
|
||||
}
|
||||
|
||||
final Integer docId = currentDatabaseDocumentId;
|
||||
meta.getTables().forEach(table -> {
|
||||
TableDocumentPojo tableMeta =
|
||||
documentPojoConverter.toTablePojo(docId, table);
|
||||
Integer tableMetaId = tableDocumentDao.insertAndReturnId(tableMeta);
|
||||
List<TableColumnDocumentPojo> tableColumnMetas = documentPojoConverter.toColumnPojo(docId, tableMetaId, table.getColumns());
|
||||
tableColumnDocumentDao.batchInsert(tableColumnMetas);
|
||||
List<TableIndexDocumentPojo> tableIndexMetas = documentPojoConverter.toIndexPojo(docId, tableMetaId, table.getIndexes());
|
||||
tableIndexDocumentDao.batchInsert(tableIndexMetas);
|
||||
List<TableTriggerDocumentPojo> tableTriggerMetas = documentPojoConverter.toTriggerPojo(docId, tableMetaId, table.getTriggers());
|
||||
tableTriggerDocumentDao.batchInsert(tableTriggerMetas);
|
||||
});
|
||||
log.info("save new meta info success");
|
||||
}
|
||||
|
||||
public Optional<DatabaseDocumentResponse> getOneByProjectId(Integer projectId, Long version) {
|
||||
if (version == null) {
|
||||
return databaseDocumentDao.selectOptionalByProjectId(projectId)
|
||||
.map(document -> {
|
||||
Integer id = document.getId();
|
||||
List<TableDocumentPojo> tables = tableDocumentDao.selectByDatabaseDocumentId(id);
|
||||
List<TableColumnDocumentPojo> columns = tableColumnDocumentDao.selectByDatabaseDocumentId(id);
|
||||
List<TableIndexDocumentPojo> indexes = tableIndexDocumentDao.selectByDatabaseMetaId(id);
|
||||
List<TableTriggerDocumentPojo> triggers = tableTriggerDocumentDao.selectByDatabaseDocumentId(id);
|
||||
Map<Integer, List<TableColumnDocumentPojo>> columnsGroupByTableMetaId = columns.stream()
|
||||
.collect(Collectors.groupingBy(TableColumnDocumentPojo::getTableDocumentId));
|
||||
Map<Integer, List<TableIndexDocumentPojo>> indexesGroupByTableMetaId = indexes.stream()
|
||||
.collect(Collectors.groupingBy(TableIndexDocumentPojo::getTableDocumentId));
|
||||
Map<Integer, List<TableTriggerDocumentPojo>> triggersGroupByTableMetaId = triggers.stream()
|
||||
.collect(Collectors.groupingBy(TableTriggerDocumentPojo::getTableDocumentId));
|
||||
List<DatabaseDocumentResponse.TableDocumentResponse> tableDocumentResponseList = tables.stream()
|
||||
.map(table -> {
|
||||
List<TableColumnDocumentPojo> subColumns = columnsGroupByTableMetaId.getOrDefault(table.getId(), Collections.emptyList());
|
||||
List<TableIndexDocumentPojo> subIndexes = indexesGroupByTableMetaId.getOrDefault(table.getId(), Collections.emptyList());
|
||||
List<TableTriggerDocumentPojo> subTriggers = triggersGroupByTableMetaId.getOrDefault(table.getId(), Collections.emptyList());
|
||||
return documentResponseConverter.of(table, subColumns, subIndexes, subTriggers);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
return documentResponseConverter.of(document, tableDocumentResponseList);
|
||||
});
|
||||
} else {
|
||||
return databaseDocumentHistoryDao.selectOptionalByProjectIdAndVersion(projectId, version)
|
||||
.map(obj -> jsonConverter.of(obj.getDatabaseDocumentObject()));
|
||||
}
|
||||
}
|
||||
|
||||
public Page<DatabaseDocumentVersionResponse> getVersionsBySchemaSourceId(Integer projectId, Pageable page) {
|
||||
return databaseDocumentDao.selectOptionalByProjectId(projectId)
|
||||
.map(schemaMeta -> databaseDocumentHistoryDao.selectPageByDatabaseDocumentId(page, schemaMeta.getId())
|
||||
.map(history -> DatabaseDocumentVersionResponse.builder()
|
||||
.version(history.getVersion())
|
||||
.createAt(history.getCreateAt())
|
||||
.build()))
|
||||
.orElseGet(Page::empty);
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.databasir.core.domain.group.converter;
|
||||
|
||||
import com.databasir.core.domain.group.data.GroupCreateRequest;
|
||||
import com.databasir.core.domain.group.data.GroupUpdateRequest;
|
||||
import com.databasir.dao.tables.pojos.GroupPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface GroupPojoConverter {
|
||||
|
||||
GroupPojo of(GroupCreateRequest groupCreateRequest);
|
||||
|
||||
GroupPojo of(GroupUpdateRequest groupUpdateRequest);
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.databasir.core.domain.group.converter;
|
||||
|
||||
import com.databasir.core.domain.group.data.GroupMemberPageResponse;
|
||||
import com.databasir.core.domain.group.data.GroupPageResponse;
|
||||
import com.databasir.core.domain.group.data.GroupResponse;
|
||||
import com.databasir.dao.tables.pojos.GroupPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.value.GroupMemberDetailPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface GroupResponseConverter {
|
||||
|
||||
default List<GroupPageResponse> toResponse(List<GroupPojo> pojos,
|
||||
Map<Integer, List<String>> groupOwnerGroupByGroupId,
|
||||
Map<Integer, Integer> projectCountMapByGroupId) {
|
||||
return pojos.stream()
|
||||
.map(group -> {
|
||||
Integer groupId = group.getId();
|
||||
List<String> owners = groupOwnerGroupByGroupId.getOrDefault(groupId, new ArrayList<>());
|
||||
Integer projectCount = projectCountMapByGroupId.getOrDefault(groupId, 0);
|
||||
return toResponse(group, owners, projectCount);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
GroupPageResponse toResponse(GroupPojo groupPojo,
|
||||
List<String> groupOwnerNames,
|
||||
Integer projectCount);
|
||||
|
||||
GroupResponse toResponse(GroupPojo groupPojo, List<UserPojo> groupOwners);
|
||||
|
||||
GroupMemberPageResponse toResponse(GroupMemberDetailPojo data);
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class GroupCreateRequest {
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
private String description;
|
||||
|
||||
@NotEmpty
|
||||
@Size(min = 1, max = 20, message = "一个分组的组长最多为 20 人,最少为 1 人")
|
||||
private List<Integer> groupOwnerUserIds = new ArrayList<>();
|
||||
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GroupMemberCreateRequest {
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String role;
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.databasir.dao.Tables.USER;
|
||||
import static com.databasir.dao.Tables.USER_ROLE;
|
||||
|
||||
@Data
|
||||
public class GroupMemberPageCondition {
|
||||
|
||||
private String role;
|
||||
|
||||
private String nicknameOrUsernameOrEmailContains;
|
||||
|
||||
public Condition toCondition() {
|
||||
List<Condition> conditions = new ArrayList<>();
|
||||
if (role != null) {
|
||||
conditions.add(USER_ROLE.ROLE.eq(role));
|
||||
}
|
||||
if (nicknameOrUsernameOrEmailContains != null) {
|
||||
conditions.add(USER.USERNAME.contains(nicknameOrUsernameOrEmailContains)
|
||||
.or(USER.NICKNAME.contains(nicknameOrUsernameOrEmailContains))
|
||||
.or(USER.EMAIL.contains(nicknameOrUsernameOrEmailContains)));
|
||||
}
|
||||
return conditions.stream().reduce(Condition::and).orElse(DSL.trueCondition());
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class GroupMemberPageResponse {
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String role;
|
||||
|
||||
private String username;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String email;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
@Data
|
||||
public class GroupMemberRoleUpdateRequest {
|
||||
|
||||
@NotBlank
|
||||
private String role;
|
||||
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import com.databasir.dao.Tables;
|
||||
import lombok.Data;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
@Data
|
||||
public class GroupPageCondition {
|
||||
|
||||
private String groupNameContains;
|
||||
|
||||
public Condition toCondition() {
|
||||
if (groupNameContains != null) {
|
||||
return Tables.GROUP.NAME.contains(groupNameContains);
|
||||
}
|
||||
return DSL.trueCondition();
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class GroupPageResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private List<String> groupOwnerNames;
|
||||
|
||||
private Integer projectCount;
|
||||
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class GroupResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private List<GroupOwnerResponse> groupOwners = new ArrayList<>();
|
||||
|
||||
private LocalDateTime updateAt;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
@Data
|
||||
public static class GroupOwnerResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String email;
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.databasir.core.domain.group.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class GroupUpdateRequest {
|
||||
|
||||
@NotNull
|
||||
private Integer id;
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
private String description;
|
||||
|
||||
@NotEmpty
|
||||
private List<Integer> groupOwnerUserIds = new ArrayList<>();
|
||||
|
||||
}
|
@@ -0,0 +1,146 @@
|
||||
package com.databasir.core.domain.group.service;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.group.converter.GroupPojoConverter;
|
||||
import com.databasir.core.domain.group.converter.GroupResponseConverter;
|
||||
import com.databasir.core.domain.group.data.*;
|
||||
import com.databasir.dao.impl.GroupDao;
|
||||
import com.databasir.dao.impl.ProjectDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.impl.UserRoleDao;
|
||||
import com.databasir.dao.tables.pojos.GroupPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.tables.pojos.UserRolePojo;
|
||||
import com.databasir.dao.value.GroupMemberSimplePojo;
|
||||
import com.databasir.dao.value.GroupProjectCountPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GroupService {
|
||||
|
||||
private final GroupDao groupDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final UserRoleDao userRoleDao;
|
||||
|
||||
private final ProjectDao projectDao;
|
||||
|
||||
private final GroupPojoConverter groupPojoConverter;
|
||||
|
||||
private final GroupResponseConverter groupResponseConverter;
|
||||
|
||||
@Transactional
|
||||
public void create(GroupCreateRequest request) {
|
||||
GroupPojo groupPojo = groupPojoConverter.of(request);
|
||||
Integer groupId = groupDao.insertAndReturnId(groupPojo);
|
||||
List<UserRolePojo> roles = request.getGroupOwnerUserIds()
|
||||
.stream()
|
||||
.map(userId -> {
|
||||
UserRolePojo role = new UserRolePojo();
|
||||
role.setUserId(userId);
|
||||
role.setRole("GROUP_OWNER");
|
||||
role.setGroupId(groupId);
|
||||
return role;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
userRoleDao.batchInsert(roles);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void update(GroupUpdateRequest request) {
|
||||
GroupPojo groupPojo = groupPojoConverter.of(request);
|
||||
groupDao.updateById(groupPojo);
|
||||
userRoleDao.deleteByRoleAndGroupId("GROUP_OWNER", groupPojo.getId());
|
||||
List<UserRolePojo> roles = request.getGroupOwnerUserIds()
|
||||
.stream()
|
||||
.map(userId -> {
|
||||
UserRolePojo role = new UserRolePojo();
|
||||
role.setUserId(userId);
|
||||
role.setRole("GROUP_OWNER");
|
||||
role.setGroupId(groupPojo.getId());
|
||||
return role;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
userRoleDao.batchInsert(roles);
|
||||
}
|
||||
|
||||
public void delete(Integer groupId) {
|
||||
groupDao.deleteById(groupId);
|
||||
userRoleDao.deleteByGroupId(groupId);
|
||||
}
|
||||
|
||||
public Page<GroupPageResponse> list(Pageable pageable, GroupPageCondition condition) {
|
||||
Page<GroupPojo> page = groupDao.selectByPage(pageable, condition.toCondition());
|
||||
List<Integer> groupIdList = page.getContent()
|
||||
.stream()
|
||||
.map(GroupPojo::getId)
|
||||
.collect(Collectors.toList());
|
||||
Map<Integer, List<GroupMemberSimplePojo>> ownersGroupByGroupId = userRoleDao.selectOwnerNamesByGroupIdIn(groupIdList)
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(GroupMemberSimplePojo::getGroupId));
|
||||
Map<Integer, GroupProjectCountPojo> projectCountMapByGroupId = projectDao.selectCountByGroupIds(groupIdList)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(GroupProjectCountPojo::getGroupId, v -> v));
|
||||
return page.map(groupPojo -> {
|
||||
Integer groupId = groupPojo.getId();
|
||||
List<String> owners = ownersGroupByGroupId.getOrDefault(groupId, new ArrayList<>())
|
||||
.stream()
|
||||
.map(GroupMemberSimplePojo::getNickname)
|
||||
.collect(Collectors.toList());
|
||||
GroupProjectCountPojo countPojo = projectCountMapByGroupId.get(groupId);
|
||||
Integer projectCount = countPojo == null ? 0 : countPojo.getCount();
|
||||
return groupResponseConverter.toResponse(groupPojo, owners, projectCount);
|
||||
});
|
||||
}
|
||||
|
||||
public Page<GroupMemberPageResponse> listGroupMembers(Integer groupId,
|
||||
Pageable pageable,
|
||||
GroupMemberPageCondition condition) {
|
||||
return userDao.selectGroupMembers(groupId, pageable, condition.toCondition())
|
||||
.map(groupResponseConverter::toResponse);
|
||||
}
|
||||
|
||||
public GroupResponse get(Integer groupId) {
|
||||
GroupPojo groupPojo = groupDao.selectById(groupId);
|
||||
List<UserPojo> users = userDao.selectLimitUsersByRoleAndGroup(groupId, "GROUP_OWNER", 50);
|
||||
return groupResponseConverter.toResponse(groupPojo, users);
|
||||
}
|
||||
|
||||
public void removeMember(Integer groupId, Integer userId) {
|
||||
userRoleDao.deleteByUserIdAndGroupId(userId, groupId);
|
||||
}
|
||||
|
||||
public void addMember(Integer groupId, GroupMemberCreateRequest request) {
|
||||
if (userRoleDao.hasRole(request.getUserId(), groupId)) {
|
||||
throw DomainErrors.USER_ROLE_DUPLICATE.exception();
|
||||
}
|
||||
UserRolePojo pojo = new UserRolePojo();
|
||||
pojo.setGroupId(groupId);
|
||||
pojo.setUserId(request.getUserId());
|
||||
pojo.setRole(request.getRole());
|
||||
userRoleDao.insertAndReturnId(pojo);
|
||||
}
|
||||
|
||||
public void changeMemberRole(Integer groupId, Integer userId, String role) {
|
||||
if (!userRoleDao.hasRole(userId, groupId, role)) {
|
||||
// TODO 最多 20 个组长
|
||||
userRoleDao.deleteByUserIdAndGroupId(userId, groupId);
|
||||
UserRolePojo pojo = new UserRolePojo();
|
||||
pojo.setUserId(userId);
|
||||
pojo.setGroupId(groupId);
|
||||
pojo.setRole(role);
|
||||
userRoleDao.insertAndReturnId(pojo);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.domain.login.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class AccessTokenRefreshRequest {
|
||||
|
||||
@NotBlank
|
||||
private String refreshToken;
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.databasir.core.domain.login.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AccessTokenRefreshResponse {
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private Long accessTokenExpireAt;
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.databasir.core.domain.login.data;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@EqualsAndHashCode
|
||||
public class LoginKeyResponse {
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private LocalDateTime accessTokenExpireAt;
|
||||
|
||||
private String refreshToken;
|
||||
|
||||
private LocalDateTime refreshTokenExpireAt;
|
||||
|
||||
public boolean accessTokenIsExpired() {
|
||||
return accessTokenExpireAt.isBefore(LocalDateTime.now());
|
||||
}
|
||||
|
||||
public boolean refreshTokenIsExpired() {
|
||||
return refreshTokenExpireAt.isBefore(LocalDateTime.now());
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
package com.databasir.core.domain.login.service;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.login.data.AccessTokenRefreshRequest;
|
||||
import com.databasir.core.domain.login.data.AccessTokenRefreshResponse;
|
||||
import com.databasir.core.domain.login.data.LoginKeyResponse;
|
||||
import com.databasir.core.infrastructure.jwt.JwtTokens;
|
||||
import com.databasir.dao.impl.LoginDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.tables.pojos.LoginPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class LoginService {
|
||||
|
||||
private final LoginDao loginDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final JwtTokens jwtTokens;
|
||||
|
||||
public AccessTokenRefreshResponse refreshAccessTokens(AccessTokenRefreshRequest request) {
|
||||
LoginPojo login = loginDao.selectByRefreshToken(request.getRefreshToken())
|
||||
.orElseThrow(DomainErrors.ACCESS_TOKEN_REFRESH_INVALID::exception);
|
||||
// refresh-token 已过期
|
||||
if (login.getRefreshTokenExpireAt().isBefore(LocalDateTime.now())) {
|
||||
throw DomainErrors.REFRESH_TOKEN_EXPIRED.exception();
|
||||
}
|
||||
// access-token 未过期就开始刷新有可能是 refresh-token 泄露了,删除 refresh-token
|
||||
if (login.getAccessTokenExpireAt().isAfter(LocalDateTime.now())) {
|
||||
log.warn("invalid access token refresh operation: request = {}, login = {}", request, login);
|
||||
loginDao.deleteByUserId(login.getUserId());
|
||||
throw DomainErrors.ACCESS_TOKEN_REFRESH_INVALID.exception();
|
||||
}
|
||||
UserPojo user = userDao.selectById(login.getUserId());
|
||||
String accessToken = jwtTokens.accessToken(user.getEmail());
|
||||
LocalDateTime accessTokenExpireAt = jwtTokens.expireAt(accessToken);
|
||||
loginDao.updateAccessToken(accessToken, accessTokenExpireAt, user.getId());
|
||||
Instant instant = accessTokenExpireAt.atZone(ZoneId.systemDefault()).toInstant();
|
||||
long accessTokenExpireAtMilli = instant.toEpochMilli();
|
||||
return new AccessTokenRefreshResponse(accessToken, accessTokenExpireAtMilli);
|
||||
}
|
||||
|
||||
public Optional<LoginKeyResponse> getLoginKey(Integer userId) {
|
||||
return loginDao.selectByUserId(userId)
|
||||
.map(loginPojo -> LoginKeyResponse.builder()
|
||||
.accessToken(loginPojo.getAccessToken())
|
||||
.accessTokenExpireAt(loginPojo.getAccessTokenExpireAt())
|
||||
.refreshToken(loginPojo.getRefreshToken())
|
||||
.refreshTokenExpireAt(loginPojo.getRefreshTokenExpireAt())
|
||||
.build());
|
||||
}
|
||||
|
||||
public LoginKeyResponse generate(Integer userId) {
|
||||
UserPojo user = userDao.selectById(userId);
|
||||
String accessToken = jwtTokens.accessToken(user.getEmail());
|
||||
LocalDateTime accessTokenExpireAt = jwtTokens.expireAt(accessToken);
|
||||
String refreshToken = UUID.randomUUID().toString().replace("-", "");
|
||||
LocalDateTime refreshTokenExpireAt = LocalDateTime.now().plusDays(15);
|
||||
|
||||
LoginPojo loginPojo = new LoginPojo();
|
||||
loginPojo.setAccessToken(accessToken);
|
||||
loginPojo.setAccessTokenExpireAt(accessTokenExpireAt);
|
||||
loginPojo.setRefreshToken(refreshToken);
|
||||
loginPojo.setRefreshTokenExpireAt(refreshTokenExpireAt);
|
||||
loginPojo.setUserId(userId);
|
||||
loginDao.insertOnDuplicateKeyUpdate(loginPojo);
|
||||
|
||||
return LoginKeyResponse.builder()
|
||||
.userId(userId)
|
||||
.accessToken(accessToken)
|
||||
.accessTokenExpireAt(accessTokenExpireAt)
|
||||
.refreshToken(refreshToken)
|
||||
.refreshTokenExpireAt(refreshTokenExpireAt)
|
||||
.build();
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.databasir.core.domain.project.converter;
|
||||
|
||||
import com.databasir.core.domain.project.data.DataSourcePropertyValue;
|
||||
import com.databasir.core.domain.project.data.ProjectCreateRequest;
|
||||
import com.databasir.core.domain.project.data.ProjectUpdateRequest;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePojo;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePropertyPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface DataSourcePojoConverter {
|
||||
|
||||
@Mapping(target = "id", ignore = true)
|
||||
@Mapping(target = "createAt", ignore = true)
|
||||
@Mapping(target = "updateAt", ignore = true)
|
||||
@Mapping(target = "password", source = "password")
|
||||
DataSourcePojo of(ProjectCreateRequest.DataSourceCreateRequest request,
|
||||
String password,
|
||||
Integer projectId);
|
||||
|
||||
@Mapping(target = "updateAt", ignore = true)
|
||||
@Mapping(target = "createAt", ignore = true)
|
||||
@Mapping(target = "password", source = "password")
|
||||
DataSourcePojo of(ProjectUpdateRequest.DataSourceUpdateRequest request,
|
||||
String password,
|
||||
Integer projectId);
|
||||
|
||||
@Mapping(target = "id", ignore = true)
|
||||
@Mapping(target = "createAt", ignore = true)
|
||||
DataSourcePropertyPojo of(DataSourcePropertyValue propertyValues, Integer dataSourceId);
|
||||
|
||||
default List<DataSourcePropertyPojo> of(List<DataSourcePropertyValue> propertyValues,
|
||||
Integer dataSourceId) {
|
||||
return propertyValues.stream().map(value -> of(value, dataSourceId)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package com.databasir.core.domain.project.converter;
|
||||
|
||||
import com.databasir.core.domain.project.data.ProjectCreateRequest;
|
||||
import com.databasir.core.domain.project.data.ProjectDetailResponse;
|
||||
import com.databasir.core.domain.project.data.ProjectSimpleResponse;
|
||||
import com.databasir.core.domain.project.data.ProjectUpdateRequest;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePojo;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePropertyPojo;
|
||||
import com.databasir.dao.tables.pojos.ProjectPojo;
|
||||
import com.databasir.dao.tables.pojos.ProjectSyncRulePojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = JsonConverter.class)
|
||||
public interface ProjectPojoConverter {
|
||||
|
||||
ProjectPojo of(ProjectCreateRequest request);
|
||||
|
||||
ProjectPojo of(ProjectUpdateRequest request);
|
||||
|
||||
@Mapping(target = "ignoreTableNameRegexArray", source = "request.ignoreTableNameRegexes")
|
||||
@Mapping(target = "ignoreColumnNameRegexArray", source = "request.ignoreColumnNameRegexes")
|
||||
ProjectSyncRulePojo of(ProjectCreateRequest.ProjectSyncRuleCreateRequest request,
|
||||
Integer projectId);
|
||||
|
||||
@Mapping(target = "ignoreTableNameRegexArray", source = "request.ignoreTableNameRegexes")
|
||||
@Mapping(target = "ignoreColumnNameRegexArray", source = "request.ignoreColumnNameRegexes")
|
||||
ProjectSyncRulePojo of(ProjectUpdateRequest.ProjectSyncRuleUpdateRequest request,
|
||||
Integer projectId);
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package com.databasir.core.domain.project.converter;
|
||||
|
||||
import com.databasir.core.domain.project.data.ProjectDetailResponse;
|
||||
import com.databasir.core.domain.project.data.ProjectSimpleResponse;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePojo;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePropertyPojo;
|
||||
import com.databasir.dao.tables.pojos.ProjectPojo;
|
||||
import com.databasir.dao.tables.pojos.ProjectSyncRulePojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = JsonConverter.class)
|
||||
public interface ProjectResponseConverter {
|
||||
|
||||
@Mapping(target = "id", source = "database.id")
|
||||
@Mapping(target = "createAt", source = "database.createAt")
|
||||
ProjectDetailResponse toResponse(ProjectPojo database,
|
||||
ProjectDetailResponse.DataSourceResponse dataSource,
|
||||
ProjectDetailResponse.ProjectSyncRuleResponse projectSyncRule);
|
||||
|
||||
ProjectDetailResponse.DataSourceResponse toResponse(DataSourcePojo dataSource,
|
||||
List<DataSourcePropertyPojo> properties);
|
||||
|
||||
@Mapping(target = "ignoreTableNameRegexes", source = "ignoreTableNameRegexArray")
|
||||
@Mapping(target = "ignoreColumnNameRegexes", source = "ignoreColumnNameRegexArray")
|
||||
ProjectDetailResponse.ProjectSyncRuleResponse toResponse(ProjectSyncRulePojo rule);
|
||||
|
||||
@Mapping(target = "id", source = "project.id")
|
||||
@Mapping(target = "createAt", source = "project.createAt")
|
||||
ProjectSimpleResponse toSimple(ProjectPojo project, DataSourcePojo dataSource);
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class DataSourcePropertyValue {
|
||||
|
||||
@NotBlank
|
||||
private String key;
|
||||
|
||||
@NotBlank
|
||||
private String value;
|
||||
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ProjectCreateRequest {
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
private String description;
|
||||
|
||||
@NotNull
|
||||
private Integer groupId;
|
||||
|
||||
@NotNull
|
||||
private ProjectCreateRequest.DataSourceCreateRequest dataSource;
|
||||
|
||||
@NotNull
|
||||
private ProjectCreateRequest.ProjectSyncRuleCreateRequest projectSyncRule;
|
||||
|
||||
@Data
|
||||
public static class DataSourceCreateRequest {
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
@NotBlank
|
||||
private String url;
|
||||
|
||||
@NotBlank
|
||||
private String databaseName;
|
||||
|
||||
@NotBlank
|
||||
private String databaseType;
|
||||
|
||||
private List<DataSourcePropertyValue> properties = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ProjectSyncRuleCreateRequest {
|
||||
|
||||
private List<String> ignoreTableNameRegexes = new ArrayList<>();
|
||||
|
||||
private List<String> ignoreColumnNameRegexes = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ProjectDetailResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private DataSourceResponse dataSource;
|
||||
|
||||
private ProjectSyncRuleResponse projectSyncRule;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
@Data
|
||||
public static class DataSourceResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String url;
|
||||
|
||||
private String databaseName;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private List<DataSourcePropertyValue> properties = new ArrayList<>();
|
||||
|
||||
private LocalDateTime updateAt;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ProjectSyncRuleResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private List<String> ignoreTableNameRegexes = new ArrayList<>();
|
||||
|
||||
private List<String> ignoreColumnNameRegexes = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import com.databasir.dao.Tables;
|
||||
import lombok.Data;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ProjectListCondition {
|
||||
|
||||
private String nameContains;
|
||||
|
||||
private String databaseNameContains;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private Integer groupId;
|
||||
|
||||
public Condition toCondition() {
|
||||
List<Condition> conditions = new ArrayList<>();
|
||||
if (nameContains != null) {
|
||||
Condition condition = Tables.PROJECT.NAME.contains(nameContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (databaseNameContains != null) {
|
||||
Condition condition = Tables.DATA_SOURCE.DATABASE_NAME.contains(databaseNameContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (databaseType != null) {
|
||||
Condition condition = Tables.DATA_SOURCE.DATABASE_TYPE.eq(databaseType);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (groupId != null) {
|
||||
Condition condition = Tables.PROJECT.GROUP_ID.eq(groupId);
|
||||
conditions.add(condition);
|
||||
}
|
||||
conditions.add(Tables.PROJECT.DELETED.eq(false));
|
||||
return conditions.stream().reduce(Condition::and).orElse(DSL.trueCondition());
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class ProjectSimpleResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private String databaseName;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package com.databasir.core.domain.project.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ProjectUpdateRequest {
|
||||
|
||||
@NotNull
|
||||
private Integer id;
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
private String description;
|
||||
|
||||
@NotNull
|
||||
private ProjectUpdateRequest.DataSourceUpdateRequest dataSource;
|
||||
|
||||
@NotNull
|
||||
private ProjectUpdateRequest.ProjectSyncRuleUpdateRequest projectSyncRule;
|
||||
|
||||
@Data
|
||||
public static class DataSourceUpdateRequest {
|
||||
|
||||
@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<>();
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ProjectSyncRuleUpdateRequest {
|
||||
|
||||
private List<String> ignoreTableNameRegexes = new ArrayList<>();
|
||||
|
||||
private List<String> ignoreColumnNameRegexes = new ArrayList<>();
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,141 @@
|
||||
package com.databasir.core.domain.project.service;
|
||||
|
||||
import com.databasir.common.codec.Aes;
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
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.dao.impl.*;
|
||||
import com.databasir.dao.tables.pojos.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ProjectService {
|
||||
|
||||
private final ProjectDao projectDao;
|
||||
|
||||
private final ProjectSyncRuleDao projectSyncRuleDao;
|
||||
|
||||
private final DataSourceDao dataSourceDao;
|
||||
|
||||
private final SysKeyDao sysKeyDao;
|
||||
|
||||
private final DataSourcePropertyDao dataSourcePropertyDao;
|
||||
|
||||
private final DataSourcePojoConverter dataSourcePojoConverter;
|
||||
|
||||
private final ProjectPojoConverter projectPojoConverter;
|
||||
|
||||
private final ProjectResponseConverter projectResponseConverter;
|
||||
|
||||
public ProjectDetailResponse getOne(Integer id) {
|
||||
return projectDao.selectOptionalById(id)
|
||||
.map(schemaSource -> {
|
||||
DataSourcePojo dataSource = dataSourceDao.selectByProjectId(id);
|
||||
List<DataSourcePropertyPojo> properties = dataSourcePropertyDao.selectByDataSourceId(dataSource.getId());
|
||||
ProjectDetailResponse.DataSourceResponse dataSourceResponse = projectResponseConverter.toResponse(dataSource, properties);
|
||||
ProjectSyncRulePojo rule = projectSyncRuleDao.selectByProjectId(id);
|
||||
ProjectDetailResponse.ProjectSyncRuleResponse ruleResponse = projectResponseConverter.toResponse(rule);
|
||||
return projectResponseConverter.toResponse(schemaSource, dataSourceResponse, ruleResponse);
|
||||
})
|
||||
.orElseThrow(DomainErrors.PROJECT_NOT_FOUND::exception);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void create(ProjectCreateRequest request) {
|
||||
ProjectPojo project = projectPojoConverter.of(request);
|
||||
Integer projectId = null;
|
||||
try {
|
||||
projectId = projectDao.insertAndReturnId(project);
|
||||
} catch (DuplicateKeyException e) {
|
||||
throw DomainErrors.PROJECT_NOT_FOUND.exception();
|
||||
}
|
||||
|
||||
String newPassword = encryptPassword(request.getDataSource().getPassword()).get();
|
||||
DataSourcePojo dataSource = dataSourcePojoConverter.of(request.getDataSource(), newPassword, projectId);
|
||||
Integer dataSourceId = dataSourceDao.insertAndReturnId(dataSource);
|
||||
|
||||
List<DataSourcePropertyValue> propertyValues = request.getDataSource().getProperties();
|
||||
List<DataSourcePropertyPojo> properties = dataSourcePojoConverter.of(propertyValues, dataSourceId);
|
||||
dataSourcePropertyDao.batchInsert(properties);
|
||||
|
||||
// TODO redesign it
|
||||
ProjectSyncRulePojo syncRule = projectPojoConverter.of(request.getProjectSyncRule(), projectId);
|
||||
projectSyncRuleDao.insertAndReturnId(syncRule);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void update(Integer groupId, ProjectUpdateRequest request) {
|
||||
Integer projectId = request.getId();
|
||||
if (projectDao.exists(groupId, projectId)) {
|
||||
// update dataSource
|
||||
String newPassword = encryptPassword(request.getDataSource().getPassword()).orElse(null);
|
||||
DataSourcePojo dataSource = dataSourcePojoConverter.of(request.getDataSource(), newPassword, projectId);
|
||||
dataSourceDao.updateByProjectId(dataSource);
|
||||
|
||||
// update connection property
|
||||
Integer dataSourceId = dataSourceDao.selectByProjectId(projectId).getId();
|
||||
List<DataSourcePropertyValue> propertyValues = request.getDataSource().getProperties();
|
||||
List<DataSourcePropertyPojo> properties = dataSourcePojoConverter.of(propertyValues, dataSourceId);
|
||||
if (properties.isEmpty()) {
|
||||
dataSourcePropertyDao.deleteByDataSourceId(dataSourceId);
|
||||
} else {
|
||||
dataSourcePropertyDao.deleteByDataSourceId(dataSourceId);
|
||||
dataSourcePropertyDao.batchInsert(properties);
|
||||
}
|
||||
|
||||
// update project sync rule TODO redesign it
|
||||
ProjectSyncRulePojo syncRule = projectPojoConverter.of(request.getProjectSyncRule(), projectId);
|
||||
projectSyncRuleDao.updateByProjectId(syncRule);
|
||||
|
||||
// update project info
|
||||
ProjectPojo project = projectPojoConverter.of(request);
|
||||
projectDao.updateById(project);
|
||||
} else {
|
||||
throw DomainErrors.PROJECT_NOT_FOUND.exception();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<String> encryptPassword(String password) {
|
||||
if (!StringUtils.hasText(password)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
SysKeyPojo sysKey = sysKeyDao.selectTopOne();
|
||||
// String decryptedPassword = Rsa.decryptFromBase64DataByPrivateKey(password, sysKey.getRsaPrivateKey());
|
||||
return Optional.of(Aes.encryptToBase64Data(password, sysKey.getAesKey()));
|
||||
}
|
||||
|
||||
public void delete(Integer projectId) {
|
||||
projectDao.updateDeletedById(true, projectId);
|
||||
}
|
||||
|
||||
public Page<ProjectSimpleResponse> list(Pageable page, ProjectListCondition condition) {
|
||||
Page<ProjectPojo> pageData = projectDao.selectByCondition(page, condition.toCondition());
|
||||
List<Integer> projectIds = pageData.getContent()
|
||||
.stream()
|
||||
.map(ProjectPojo::getId)
|
||||
.collect(Collectors.toList());
|
||||
Map<Integer, DataSourcePojo> dataSourceMapByProjectId = dataSourceDao.selectInProjectIds(projectIds)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(DataSourcePojo::getProjectId, Function.identity()));
|
||||
return pageData.map(project -> {
|
||||
DataSourcePojo dataSource = dataSourceMapByProjectId.get(project.getId());
|
||||
return projectResponseConverter.toSimple(project, dataSource);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package com.databasir.core.domain.system.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class SystemEmailResponse {
|
||||
|
||||
private String username;
|
||||
|
||||
private String smtpHost;
|
||||
|
||||
private Integer smtpPort;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package com.databasir.core.domain.system.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
@Data
|
||||
public class SystemEmailUpdateRequest {
|
||||
|
||||
@NotBlank
|
||||
@Email
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
@NotBlank
|
||||
private String smtpHost;
|
||||
|
||||
@NotNull
|
||||
@Min(0L)
|
||||
@Max(65535L)
|
||||
private Integer smtpPort;
|
||||
|
||||
}
|
@@ -0,0 +1,106 @@
|
||||
package com.databasir.core.domain.system.service;
|
||||
|
||||
import com.databasir.common.codec.Aes;
|
||||
import com.databasir.common.codec.Rsa;
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.system.data.SystemEmailResponse;
|
||||
import com.databasir.core.domain.system.data.SystemEmailUpdateRequest;
|
||||
import com.databasir.dao.impl.SysKeyDao;
|
||||
import com.databasir.dao.impl.SysMailDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.impl.UserRoleDao;
|
||||
import com.databasir.dao.tables.pojos.SysKeyPojo;
|
||||
import com.databasir.dao.tables.pojos.SysMailPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.tables.pojos.UserRolePojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SystemService {
|
||||
|
||||
private final SysKeyDao sysKeyDao;
|
||||
|
||||
private final SysMailDao sysMailDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final UserRoleDao userRoleDao;
|
||||
|
||||
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
@PostConstruct
|
||||
public void postInit() {
|
||||
sysKeyDao.selectOptionTopOne()
|
||||
.orElseGet(() -> {
|
||||
SysKeyPojo pojo = new SysKeyPojo();
|
||||
pojo.setAesKey(Aes.randomBase64Key());
|
||||
Rsa.RsaBase64Key key = Rsa.generateBase64Key();
|
||||
pojo.setRsaPublicKey(key.getPublicBase64Key());
|
||||
pojo.setRsaPrivateKey(key.getPrivateBase64Key());
|
||||
sysKeyDao.insertAndReturnId(pojo);
|
||||
return pojo;
|
||||
});
|
||||
|
||||
String email = "admin@databasir.com";
|
||||
String username = "databasir";
|
||||
Optional<UserPojo> userOpt = userDao.selectByEmail(email);
|
||||
if (!userOpt.isPresent()) {
|
||||
UserPojo admin = new UserPojo();
|
||||
admin.setEmail(email);
|
||||
admin.setUsername(username);
|
||||
admin.setPassword(bCryptPasswordEncoder.encode(username));
|
||||
admin.setEnabled(true);
|
||||
admin.setNickname("Databasir Admin");
|
||||
Integer userId = userDao.insertAndReturnId(admin);
|
||||
UserRolePojo role = new UserRolePojo();
|
||||
role.setUserId(userId);
|
||||
role.setRole("SYS_OWNER");
|
||||
userRoleDao.insertAndReturnId(role);
|
||||
}
|
||||
}
|
||||
|
||||
public void renewKey() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public Optional<SystemEmailResponse> getEmailSetting() {
|
||||
return sysMailDao.selectOptionTopOne()
|
||||
.map(mail -> {
|
||||
SystemEmailResponse response = new SystemEmailResponse();
|
||||
response.setSmtpHost(mail.getSmtpHost());
|
||||
response.setSmtpPort(mail.getSmtpPort());
|
||||
response.setUsername(mail.getUsername());
|
||||
response.setCreateAt(mail.getCreateAt());
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
public void updateEmailSetting(SystemEmailUpdateRequest request) {
|
||||
Optional<Integer> idOpt = sysMailDao.selectOptionTopOne().map(SysMailPojo::getId);
|
||||
SysMailPojo sysMailPojo = new SysMailPojo();
|
||||
sysMailPojo.setSmtpHost(request.getSmtpHost());
|
||||
sysMailPojo.setSmtpPort(request.getSmtpPort());
|
||||
sysMailPojo.setUsername(request.getUsername());
|
||||
idOpt.ifPresent(sysMailPojo::setId);
|
||||
if (request.getPassword() != null) {
|
||||
// TODO encrypt password ?
|
||||
sysMailPojo.setPassword(request.getPassword());
|
||||
}
|
||||
|
||||
if (idOpt.isPresent()) {
|
||||
if (!StringUtils.hasText(request.getPassword())) {
|
||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception();
|
||||
}
|
||||
sysMailDao.updateById(sysMailPojo);
|
||||
} else {
|
||||
sysMailDao.insertAndReturnId(sysMailPojo);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.domain.user.converter;
|
||||
|
||||
import com.databasir.core.domain.user.data.UserCreateRequest;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface UserPojoConverter {
|
||||
|
||||
@Mapping(target = "password", source = "hashedPassword")
|
||||
UserPojo of(UserCreateRequest request, String hashedPassword);
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.databasir.core.domain.user.converter;
|
||||
|
||||
import com.databasir.core.domain.user.data.UserDetailResponse;
|
||||
import com.databasir.core.domain.user.data.UserPageResponse;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.tables.pojos.UserRolePojo;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface UserResponseConverter {
|
||||
|
||||
|
||||
default UserDetailResponse detailResponse(UserPojo user,
|
||||
List<UserRolePojo> userRoles,
|
||||
Map<Integer, String> groupNameMapById) {
|
||||
List<UserDetailResponse.UserRoleDetailResponse> roles = userRoles.stream()
|
||||
.map(pojo -> userRoleDetailResponse(pojo, groupNameMapById.get(pojo.getGroupId())))
|
||||
.collect(Collectors.toList());
|
||||
return detailResponse(user, roles);
|
||||
}
|
||||
|
||||
UserDetailResponse detailResponse(UserPojo pojo, List<UserDetailResponse.UserRoleDetailResponse> roles);
|
||||
|
||||
UserDetailResponse.UserRoleDetailResponse userRoleDetailResponse(UserRolePojo pojo, String groupName);
|
||||
|
||||
UserPageResponse pageResponse(UserPojo pojo, Boolean isSysOwner, List<Integer> inGroupIds);
|
||||
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class UserCreateRequest {
|
||||
|
||||
private String avatar;
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
|
||||
@NotBlank
|
||||
private String nickname;
|
||||
|
||||
@NotBlank
|
||||
private String email;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
@NotNull
|
||||
private Boolean enabled;
|
||||
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UserDetailResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String avatar;
|
||||
|
||||
private String email;
|
||||
|
||||
private Boolean enabled;
|
||||
|
||||
private List<UserRoleDetailResponse> roles = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
@Data
|
||||
public static class UserRoleDetailResponse {
|
||||
|
||||
private String role;
|
||||
|
||||
private Integer groupId;
|
||||
|
||||
private String groupName;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class UserLoginRequest {
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UserLoginResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String email;
|
||||
|
||||
private String username;
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private long accessTokenExpireAt;
|
||||
|
||||
private String refreshToken;
|
||||
|
||||
private List<RoleResponse> roles;
|
||||
|
||||
@Data
|
||||
public static class RoleResponse {
|
||||
private String role;
|
||||
private Integer groupId;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,15 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Data
|
||||
public class UserNicknameUpdateRequest {
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 32, min = 1)
|
||||
private String nickname;
|
||||
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.databasir.dao.Tables.USER;
|
||||
|
||||
@Data
|
||||
public class UserPageCondition {
|
||||
|
||||
private String nicknameContains;
|
||||
|
||||
private String usernameContains;
|
||||
|
||||
private String emailContains;
|
||||
|
||||
private String nicknameOrUsernameOrEmailContains;
|
||||
|
||||
private Boolean enabled;
|
||||
|
||||
public Condition toCondition() {
|
||||
List<Condition> conditions = new ArrayList<>();
|
||||
if (nicknameContains != null) {
|
||||
Condition condition = USER.NICKNAME.contains(nicknameContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (usernameContains != null) {
|
||||
Condition condition = USER.USERNAME.contains(usernameContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (emailContains != null) {
|
||||
Condition condition = USER.EMAIL.contains(emailContains);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (enabled != null) {
|
||||
Condition condition = USER.ENABLED.eq(enabled);
|
||||
conditions.add(condition);
|
||||
}
|
||||
if (nicknameOrUsernameOrEmailContains != null) {
|
||||
Condition condition = USER.EMAIL.contains(nicknameOrUsernameOrEmailContains)
|
||||
.or(USER.USERNAME.contains(nicknameOrUsernameOrEmailContains))
|
||||
.or(USER.EMAIL.contains(nicknameOrUsernameOrEmailContains));
|
||||
conditions.add(condition);
|
||||
}
|
||||
return conditions.stream().reduce(Condition::and).orElse(DSL.trueCondition());
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UserPageResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String email;
|
||||
|
||||
private Boolean enabled;
|
||||
|
||||
private Boolean isSysOwner;
|
||||
|
||||
private List<Integer> inGroupIds = new ArrayList<>();
|
||||
|
||||
private LocalDateTime createAt;
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Data
|
||||
public class UserPasswordUpdateRequest {
|
||||
|
||||
@NotBlank
|
||||
private String originPassword;
|
||||
|
||||
@NotBlank
|
||||
@Size(min = 6, max = 32)
|
||||
private String newPassword;
|
||||
|
||||
@NotBlank
|
||||
@Size(min = 6, max = 32)
|
||||
private String confirmNewPassword;
|
||||
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.databasir.core.domain.user.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserRoleAssignRequest {
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String role;
|
||||
|
||||
private Integer groupId;
|
||||
}
|
@@ -0,0 +1,146 @@
|
||||
package com.databasir.core.domain.user.service;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.user.converter.UserPojoConverter;
|
||||
import com.databasir.core.domain.user.converter.UserResponseConverter;
|
||||
import com.databasir.core.domain.user.data.*;
|
||||
import com.databasir.core.infrastructure.mail.MailSender;
|
||||
import com.databasir.dao.impl.GroupDao;
|
||||
import com.databasir.dao.impl.SysMailDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.impl.UserRoleDao;
|
||||
import com.databasir.dao.tables.pojos.GroupPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.tables.pojos.UserRolePojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.stream.Collectors.*;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserService {
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final UserRoleDao userRoleDao;
|
||||
|
||||
private final GroupDao groupDao;
|
||||
|
||||
private final SysMailDao sysMailDao;
|
||||
|
||||
private final UserPojoConverter userPojoConverter;
|
||||
|
||||
private final UserResponseConverter userResponseConverter;
|
||||
|
||||
private final MailSender mailSender;
|
||||
|
||||
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public Page<UserPageResponse> list(Pageable pageable, UserPageCondition condition) {
|
||||
Page<UserPojo> users = userDao.selectByPage(pageable, condition.toCondition());
|
||||
List<Integer> userIds = users.getContent()
|
||||
.stream()
|
||||
.map(UserPojo::getId)
|
||||
.collect(toList());
|
||||
List<UserRolePojo> userRoles = userRoleDao.selectByUserIds(userIds);
|
||||
Map<Integer, List<Integer>> groupIdMapByUserId = userRoles
|
||||
.stream()
|
||||
.filter(ur -> ur.getGroupId() != null)
|
||||
.collect(groupingBy(UserRolePojo::getUserId, mapping(UserRolePojo::getGroupId, toList())));
|
||||
Map<Integer, List<UserRolePojo>> sysOwnerGroupByUserId = userRoles.stream()
|
||||
.filter(ur -> ur.getRole().equals("SYS_OWNER"))
|
||||
.collect(groupingBy(UserRolePojo::getUserId));
|
||||
return users.map(user ->
|
||||
userResponseConverter.pageResponse(user, sysOwnerGroupByUserId.containsKey(user.getId()),
|
||||
groupIdMapByUserId.get(user.getId())));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void create(UserCreateRequest userCreateRequest) {
|
||||
userDao.selectByEmailOrUsername(userCreateRequest.getUsername()).ifPresent(data -> {
|
||||
throw DomainErrors.USERNAME_OR_EMAIL_DUPLICATE.exception();
|
||||
});
|
||||
String hashedPassword = bCryptPasswordEncoder.encode(userCreateRequest.getPassword());
|
||||
UserPojo pojo = userPojoConverter.of(userCreateRequest, hashedPassword);
|
||||
try {
|
||||
userDao.insertAndReturnId(pojo);
|
||||
} catch (DuplicateKeyException e) {
|
||||
throw DomainErrors.USERNAME_OR_EMAIL_DUPLICATE.exception();
|
||||
}
|
||||
}
|
||||
|
||||
public UserDetailResponse get(Integer userId) {
|
||||
UserPojo pojo = userDao.selectById(userId);
|
||||
List<UserRolePojo> roles = userRoleDao.selectByUserIds(Collections.singletonList(userId));
|
||||
List<Integer> groupIds = roles.stream()
|
||||
.map(UserRolePojo::getGroupId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(toList());
|
||||
Map<Integer, String> groupNameMapById = groupDao.selectInIds(groupIds)
|
||||
.stream()
|
||||
.collect(toMap(GroupPojo::getId, GroupPojo::getName));
|
||||
return userResponseConverter.detailResponse(pojo, roles, groupNameMapById);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public String renewPassword(Integer userId) {
|
||||
UserPojo userPojo = userDao.selectById(userId);
|
||||
String randomPassword = UUID.randomUUID().toString()
|
||||
.replace("-", "")
|
||||
.substring(0, 8);
|
||||
String hashedPassword = bCryptPasswordEncoder.encode(randomPassword);
|
||||
userDao.updatePassword(userId, hashedPassword);
|
||||
sysMailDao.selectOptionTopOne()
|
||||
.ifPresent(mailPojo -> {
|
||||
String subject = "Databasir 密码重置提醒";
|
||||
String message = "您的密码已被重置,新密码为:" + randomPassword;
|
||||
mailSender.send(mailPojo, userPojo.getEmail(), subject, message);
|
||||
});
|
||||
return randomPassword;
|
||||
}
|
||||
|
||||
public void switchEnableStatus(Integer userId, Boolean enable) {
|
||||
userDao.updateEnabledByUserId(userId, enable);
|
||||
}
|
||||
|
||||
public void removeSysOwnerFrom(Integer userId) {
|
||||
if (userRoleDao.hasRole(userId, "SYS_OWNER")) {
|
||||
userRoleDao.deleteRole(userId, "SYS_OWNER");
|
||||
}
|
||||
}
|
||||
|
||||
public void addSysOwnerTo(Integer userId) {
|
||||
if (!userRoleDao.hasRole(userId, "SYS_OWNER")) {
|
||||
UserRolePojo role = new UserRolePojo();
|
||||
role.setUserId(userId);
|
||||
role.setRole("SYS_OWNER");
|
||||
userRoleDao.insertAndReturnId(role);
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePassword(Integer userId, UserPasswordUpdateRequest request) {
|
||||
if (!Objects.equals(request.getNewPassword(), request.getConfirmNewPassword())) {
|
||||
throw DomainErrors.UPDATE_PASSWORD_CONFIRM_FAILED.exception();
|
||||
}
|
||||
UserPojo userPojo = userDao.selectById(userId);
|
||||
if (!bCryptPasswordEncoder.matches(request.getOriginPassword(), userPojo.getPassword())) {
|
||||
throw DomainErrors.ORIGIN_PASSWORD_NOT_CORRECT.exception();
|
||||
}
|
||||
String newHashedPassword = bCryptPasswordEncoder.encode(request.getNewPassword());
|
||||
userDao.updatePassword(userId, newHashedPassword);
|
||||
}
|
||||
|
||||
public void updateNickname(Integer userId, UserNicknameUpdateRequest request) {
|
||||
UserPojo userPojo = userDao.selectById(userId);
|
||||
userPojo.setNickname(request.getNickname());
|
||||
userDao.updateById(userPojo);
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
public interface DatabaseConnectionFactory {
|
||||
|
||||
boolean support(String databaseType);
|
||||
|
||||
Connection getConnection(String username,
|
||||
String password,
|
||||
String url,
|
||||
String schema,
|
||||
Properties properties);
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
import com.databasir.common.codec.Aes;
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.dao.impl.SysKeyDao;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePojo;
|
||||
import com.databasir.dao.tables.pojos.DataSourcePropertyPojo;
|
||||
import com.databasir.dao.tables.pojos.SysKeyPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DatabaseConnectionService {
|
||||
|
||||
private final List<DatabaseConnectionFactory> factories;
|
||||
|
||||
private final SysKeyDao sysKeyDao;
|
||||
|
||||
public Connection create(DataSourcePojo dataSource,
|
||||
List<DataSourcePropertyPojo> dataSourceProperties) {
|
||||
SysKeyPojo sysKey = sysKeyDao.selectTopOne();
|
||||
String username = dataSource.getUsername();
|
||||
String password = Aes.decryptFromBase64Data(dataSource.getPassword(), sysKey.getAesKey());
|
||||
String url = dataSource.getUrl();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
public interface DatabaseTypes {
|
||||
|
||||
String MYSQL = "mysql";
|
||||
|
||||
String POSTGRESQL = "postgresql";
|
||||
|
||||
String ORACLE = "oracle";
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
public class MysqlDatabaseConnectionFactory implements DatabaseConnectionFactory {
|
||||
|
||||
@Override
|
||||
public boolean support(String databaseType) {
|
||||
return DatabaseTypes.MYSQL.equalsIgnoreCase(databaseType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password, String url, String schema, Properties properties) {
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
Properties info = new Properties();
|
||||
info.put("user", username);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.databasir.core.infrastructure.connection;
|
||||
|
||||
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
public class PostgresqlDatabaseConnectionFactory implements DatabaseConnectionFactory {
|
||||
|
||||
@Override
|
||||
public boolean support(String databaseType) {
|
||||
return DatabaseTypes.POSTGRESQL.equalsIgnoreCase(databaseType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password, String url, String schema, Properties properties) {
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
Properties info = new Properties();
|
||||
info.put("user", username);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
package com.databasir.core.infrastructure.converter;
|
||||
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.jooq.JSON;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class JsonConverter {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public JSON toJson(List<String> array) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(array);
|
||||
return JSON.valueOf(json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> fromJson(JSON json) {
|
||||
String data = json.data();
|
||||
if (data == null) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
try {
|
||||
return objectMapper.readValue(data.getBytes(StandardCharsets.UTF_8),
|
||||
new TypeReference<List<String>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSON toJson(DatabaseDocumentResponse response) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(response);
|
||||
return JSON.valueOf(json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public DatabaseDocumentResponse of(JSON json) {
|
||||
try {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
return objectMapper.readValue(json.data().getBytes(StandardCharsets.UTF_8), DatabaseDocumentResponse.class);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
package com.databasir.core.infrastructure.jwt;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.interfaces.JWTVerifier;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class JwtTokens {
|
||||
|
||||
// 15 minutes
|
||||
private static final long ACCESS_EXPIRE_TIME = 1000 * 60 * 15;
|
||||
|
||||
public static final String TOKEN_PREFIX = "Bearer ";
|
||||
|
||||
private static final String ISSUER = "Databasir";
|
||||
|
||||
private static final String SECRET = "Databasir2022";
|
||||
|
||||
public String accessToken(String username) {
|
||||
Algorithm algorithm = Algorithm.HMAC256(SECRET);
|
||||
|
||||
return JWT.create()
|
||||
.withExpiresAt(new Date(new Date().getTime() + ACCESS_EXPIRE_TIME))
|
||||
.withIssuer(ISSUER)
|
||||
.withClaim("username", username)
|
||||
.sign(algorithm);
|
||||
}
|
||||
|
||||
public boolean verify(String token) {
|
||||
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withIssuer(ISSUER)
|
||||
.build();
|
||||
try {
|
||||
verifier.verify(token);
|
||||
return true;
|
||||
} catch (JWTVerificationException e) {
|
||||
log.warn("verify jwt token failed " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String getUsername(String token) {
|
||||
return JWT.decode(token).getClaim("username").asString();
|
||||
}
|
||||
|
||||
public LocalDateTime expireAt(String token) {
|
||||
long time = JWT.decode(token).getExpiresAt().getTime();
|
||||
return Instant.ofEpochMilli(time)
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
package com.databasir.core.infrastructure.mail;
|
||||
|
||||
import com.databasir.dao.tables.pojos.SysMailPojo;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Component
|
||||
public class MailSender {
|
||||
|
||||
public void send(SysMailPojo mail, String to, String subject, String content) {
|
||||
JavaMailSender sender = initJavaMailSender(mail);
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(mail.getUsername());
|
||||
message.setTo(to);
|
||||
message.setSubject(subject);
|
||||
message.setText(content);
|
||||
sender.send(message);
|
||||
}
|
||||
|
||||
private JavaMailSender initJavaMailSender(SysMailPojo properties) {
|
||||
JavaMailSenderImpl sender = new JavaMailSenderImpl();
|
||||
sender.setHost(properties.getSmtpHost());
|
||||
if (properties.getSmtpPort() != null) {
|
||||
sender.setPort(properties.getSmtpPort());
|
||||
}
|
||||
sender.setUsername(properties.getUsername());
|
||||
sender.setPassword(properties.getPassword());
|
||||
sender.setProtocol("smtp");
|
||||
sender.setDefaultEncoding(StandardCharsets.UTF_8.name());
|
||||
return sender;
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package com.databasir.core.infrastructure.meta;
|
||||
|
||||
public class DatabaseMetaResolver {
|
||||
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
package com.databasir.core.meta.pojo;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class ColumnMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
private String comment;
|
||||
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* if default value is empty string, will be converted to ''.
|
||||
*/
|
||||
private String defaultValue;
|
||||
|
||||
private Integer size;
|
||||
|
||||
private Integer decimalDigits;
|
||||
|
||||
private Boolean isNullable;
|
||||
|
||||
private Boolean isAutoIncrement;
|
||||
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
package com.databasir.core.meta.pojo;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class DatabaseMeta {
|
||||
|
||||
/**
|
||||
* product_name
|
||||
*/
|
||||
private String productName;
|
||||
|
||||
/**
|
||||
* product_version
|
||||
*/
|
||||
private String productVersion;
|
||||
|
||||
/**
|
||||
* driver_name
|
||||
*/
|
||||
private String driverName;
|
||||
|
||||
/**
|
||||
* driver_version
|
||||
*/
|
||||
private String driverVersion;
|
||||
|
||||
/**
|
||||
* database_name
|
||||
*/
|
||||
private String databaseName;
|
||||
|
||||
private String remark;
|
||||
|
||||
@Builder.Default
|
||||
private List<TableMeta> tables = Collections.emptyList();
|
||||
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
package com.databasir.core.meta.pojo;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class IndexMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
@Builder.Default
|
||||
private List<String> columnNames = Collections.emptyList();
|
||||
|
||||
private Boolean isPrimaryKey;
|
||||
|
||||
private Boolean isUniqueKey;
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
package com.databasir.core.meta.pojo;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class TableMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
private String comment;
|
||||
|
||||
@Builder.Default
|
||||
private List<ColumnMeta> columns = Collections.emptyList();
|
||||
|
||||
@Builder.Default
|
||||
private List<TriggerMeta> triggers = Collections.emptyList();
|
||||
|
||||
@Builder.Default
|
||||
private List<IndexMeta> indexes = Collections.emptyList();
|
||||
|
||||
private String remark;
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
package com.databasir.core.meta.pojo;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* now: only support mysql, postgresql.
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class TriggerMeta {
|
||||
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* example: BEFORE, AFTER
|
||||
*/
|
||||
private String timing;
|
||||
|
||||
/**
|
||||
* example: INSERT, UPDATE
|
||||
*/
|
||||
private String manipulation;
|
||||
|
||||
private String statement;
|
||||
|
||||
private String createAt;
|
||||
}
|
@@ -1,80 +0,0 @@
|
||||
package com.databasir.core.meta.provider;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* TODO use to extension repository
|
||||
*/
|
||||
public interface SqlProvider {
|
||||
|
||||
Default DEFAULT = new Default();
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* generate sql to select database information, should return the follow columns
|
||||
* </p>
|
||||
*
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th> column name </th>
|
||||
* <th> column type </th>
|
||||
* <th> description </th>
|
||||
* <th> nullable </th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td> TABLE_CAT </td>
|
||||
* <td> String </td>
|
||||
* <td> catalog name </td>
|
||||
* <td> NO </td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* <br>
|
||||
*
|
||||
* @param databaseName
|
||||
* @return
|
||||
*/
|
||||
default Optional<String> databaseMetaSql(String databaseName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* generate sql to select table information, should return the follow columns
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th> column name </th>
|
||||
* <th> column type </th>
|
||||
* <th> description </th>
|
||||
* <th> nullable </th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td> TABLE_CAT </td>
|
||||
* <td> String </td>
|
||||
* <td> catalog name </td>
|
||||
* <td> NO </td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* @param databaseName
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
default Optional<String> tableMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<String> tableColumnMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<String> tableIndexMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<String> tableTriggerMetaSql(String databaseName, String tableName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
class Default implements SqlProvider {
|
||||
|
||||
}
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.pojo.ColumnMeta;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ColumnMetaRepository {
|
||||
|
||||
List<ColumnMeta> selectColumns(Connection connection, TableCondition condition);
|
||||
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface DatabaseMetaRepository {
|
||||
|
||||
Optional<DatabaseMeta> select(Connection connection, Condition condition);
|
||||
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.pojo.IndexMeta;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface IndexMetaRepository {
|
||||
|
||||
List<IndexMeta> selectIndexes(Connection connection, TableCondition condition);
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.pojo.TableMeta;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface TableMetaRepository {
|
||||
|
||||
List<TableMeta> selectTables(Connection connection, Condition condition);
|
||||
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package com.databasir.core.meta.repository;
|
||||
|
||||
import com.databasir.core.meta.pojo.TriggerMeta;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
public interface TriggerMetaRepository {
|
||||
|
||||
List<TriggerMeta> selectTriggers(Connection connection, TableCondition condition);
|
||||
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
package com.databasir.core.meta.repository.condition;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class Condition {
|
||||
|
||||
@NonNull
|
||||
private String databaseName;
|
||||
|
||||
@Builder.Default
|
||||
private Collection<String> ignoreTableNameRegex = Collections.emptyList();
|
||||
|
||||
@Builder.Default
|
||||
private Collection<String> ignoreTableColumnNameRegex = Collections.emptyList();
|
||||
|
||||
public boolean tableIsIgnored(String tableName) {
|
||||
return ignoreTableNameRegex.stream().anyMatch(regex -> Pattern.matches(regex, tableName));
|
||||
}
|
||||
|
||||
public boolean columnIsIgnored(String column) {
|
||||
return ignoreTableColumnNameRegex.stream().anyMatch(regex -> Pattern.matches(regex, column));
|
||||
}
|
||||
}
|
@@ -1,24 +0,0 @@
|
||||
package com.databasir.core.meta.repository.condition;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TableCondition extends Condition {
|
||||
|
||||
@NonNull
|
||||
private String tableName;
|
||||
|
||||
public static TableCondition of(Condition condition, String tableName) {
|
||||
return TableCondition.builder()
|
||||
.databaseName(condition.getDatabaseName())
|
||||
.tableName(tableName)
|
||||
.ignoreTableNameRegex(condition.getIgnoreTableNameRegex())
|
||||
.ignoreTableColumnNameRegex(condition.getIgnoreTableColumnNameRegex())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,69 +0,0 @@
|
||||
package com.databasir.core.meta.repository.impl.extension;
|
||||
|
||||
import com.databasir.core.meta.pojo.TriggerMeta;
|
||||
import com.databasir.core.meta.repository.TriggerMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class MysqlTableTriggerMetaRepository implements TriggerMetaRepository {
|
||||
|
||||
@Override
|
||||
public List<TriggerMeta> selectTriggers(Connection connection, TableCondition condition) {
|
||||
String sql = "SELECT TRIGGER_CATALOG,\n" +
|
||||
" TRIGGER_SCHEMA,\n" +
|
||||
" TRIGGER_NAME,\n" +
|
||||
" EVENT_MANIPULATION,\n" +
|
||||
" EVENT_OBJECT_CATALOG,\n" +
|
||||
" EVENT_OBJECT_SCHEMA,\n" +
|
||||
" EVENT_OBJECT_TABLE,\n" +
|
||||
" ACTION_ORDER,\n" +
|
||||
" ACTION_CONDITION,\n" +
|
||||
" ACTION_STATEMENT,\n" +
|
||||
" ACTION_ORIENTATION,\n" +
|
||||
" ACTION_TIMING,\n" +
|
||||
" ACTION_REFERENCE_OLD_TABLE,\n" +
|
||||
" ACTION_REFERENCE_NEW_TABLE,\n" +
|
||||
" ACTION_REFERENCE_OLD_ROW,\n" +
|
||||
" ACTION_REFERENCE_NEW_ROW,\n" +
|
||||
" CREATED,\n" +
|
||||
" SQL_MODE,\n" +
|
||||
" DEFINER\n " +
|
||||
"FROM information_schema.TRIGGERS WHERE EVENT_OBJECT_SCHEMA = ? AND EVENT_OBJECT_TABLE = ?";
|
||||
try {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setObject(1, condition.getDatabaseName());
|
||||
preparedStatement.setObject(2, condition.getTableName());
|
||||
ResultSet results = preparedStatement.executeQuery();
|
||||
List<TriggerMeta> triggers = new ArrayList<>();
|
||||
while (results.next()) {
|
||||
String name = results.getString("TRIGGER_NAME");
|
||||
String statement = results.getString("ACTION_STATEMENT");
|
||||
String timing = results.getString("ACTION_TIMING");
|
||||
String manipulation = results.getString("EVENT_MANIPULATION");
|
||||
String created = results.getString("CREATED");
|
||||
TriggerMeta meta = TriggerMeta.builder()
|
||||
.name(name)
|
||||
.manipulation(manipulation)
|
||||
.timing(timing)
|
||||
.statement(statement)
|
||||
.createAt(created)
|
||||
.build();
|
||||
triggers.add(meta);
|
||||
}
|
||||
return triggers;
|
||||
} catch (SQLException e) {
|
||||
log.warn("create trigger doc failed", e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,73 +0,0 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.pojo.ColumnMeta;
|
||||
import com.databasir.core.meta.repository.ColumnMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class JdbcColumnMetaRepository implements ColumnMetaRepository {
|
||||
|
||||
@Override
|
||||
public List<ColumnMeta> selectColumns(Connection connection, TableCondition tableCondition) {
|
||||
try {
|
||||
return doSelect(connection, tableCondition);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ColumnMeta> doSelect(Connection connection, TableCondition tableCondition) throws SQLException {
|
||||
List<ColumnMeta> columnDocs = new ArrayList<>();
|
||||
String databaseName = tableCondition.getDatabaseName();
|
||||
String tableName = tableCondition.getTableName();
|
||||
ResultSet columnsResult;
|
||||
try {
|
||||
columnsResult = connection.getMetaData().getColumns(databaseName, null, tableName, null);
|
||||
} catch (SQLException e) {
|
||||
log.warn("warn: ignore columns in " + databaseName + "." + tableName);
|
||||
return columnDocs;
|
||||
}
|
||||
while (columnsResult.next()) {
|
||||
String columnName = columnsResult.getString("COLUMN_NAME");
|
||||
if (tableCondition.columnIsIgnored(columnName)) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("ignore column: " + columnName);
|
||||
}
|
||||
} else {
|
||||
String columnType = columnsResult.getString("TYPE_NAME");
|
||||
Integer columnSize = columnsResult.getInt("COLUMN_SIZE");
|
||||
Integer decimalDigits = columnsResult.getInt("DECIMAL_DIGITS");
|
||||
String defaultValue = columnsResult.getString("COLUMN_DEF");
|
||||
boolean isNullable = Objects.equals("YES", columnsResult.getString("IS_NULLABLE"));
|
||||
boolean isAutoIncrement = Objects.equals("YES", columnsResult.getString("IS_AUTOINCREMENT"));
|
||||
String columnComment = columnsResult.getString("REMARKS");
|
||||
if (defaultValue != null && defaultValue.trim().equals("")) {
|
||||
defaultValue = "'" + defaultValue + "'";
|
||||
}
|
||||
ColumnMeta columnMeta = ColumnMeta.builder()
|
||||
.name(columnName)
|
||||
.type(columnType)
|
||||
.size(columnSize)
|
||||
.decimalDigits(decimalDigits)
|
||||
.isNullable(isNullable)
|
||||
.isAutoIncrement(isAutoIncrement)
|
||||
.comment(columnComment)
|
||||
.defaultValue(defaultValue)
|
||||
.build();
|
||||
columnDocs.add(columnMeta);
|
||||
}
|
||||
|
||||
}
|
||||
return columnDocs;
|
||||
}
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import com.databasir.core.meta.pojo.TableMeta;
|
||||
import com.databasir.core.meta.repository.DatabaseMetaRepository;
|
||||
import com.databasir.core.meta.repository.TableMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class JdbcDatabaseMetaRepository implements DatabaseMetaRepository {
|
||||
|
||||
private final TableMetaRepository tableMetaRepository;
|
||||
|
||||
@Override
|
||||
public Optional<DatabaseMeta> select(Connection connection, Condition condition) {
|
||||
try {
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
ResultSet catalogs = metaData.getCatalogs();
|
||||
while (catalogs.next()) {
|
||||
String catalogName = catalogs.getString("TABLE_CAT");
|
||||
if (Objects.equals(condition.getDatabaseName(), catalogName)) {
|
||||
List<TableMeta> tableDocs = tableMetaRepository.selectTables(connection, condition);
|
||||
DatabaseMeta meta = DatabaseMeta.builder()
|
||||
.productName(metaData.getDatabaseProductName())
|
||||
.productVersion(metaData.getDatabaseProductVersion())
|
||||
.databaseName(catalogName)
|
||||
.tables(tableDocs)
|
||||
.build();
|
||||
return Optional.of(meta);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.pojo.IndexMeta;
|
||||
import com.databasir.core.meta.repository.IndexMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class JdbcIndexMetaRepository implements IndexMetaRepository {
|
||||
@Override
|
||||
public List<IndexMeta> selectIndexes(Connection connection, TableCondition condition) {
|
||||
try {
|
||||
return doCreateIndexDocs(connection, condition);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<IndexMeta> doCreateIndexDocs(Connection connection, TableCondition condition)
|
||||
throws SQLException {
|
||||
String databaseName = condition.getDatabaseName();
|
||||
String tableName = condition.getTableName();
|
||||
List<IndexMeta> indexMetas = new ArrayList<>();
|
||||
ResultSet indexResults;
|
||||
try {
|
||||
indexResults = connection.getMetaData().getIndexInfo(databaseName, null, tableName, false, false);
|
||||
} catch (SQLException e) {
|
||||
log.warn("warn: ignore " + databaseName + "." + tableName);
|
||||
return indexMetas;
|
||||
}
|
||||
|
||||
Map<String, IndexMeta> pojoGroupByName = new HashMap<>();
|
||||
while (indexResults.next()) {
|
||||
Boolean nonUnique = indexResults.getBoolean("NON_UNIQUE");
|
||||
String indexName = indexResults.getString("INDEX_NAME");
|
||||
String columnName = indexResults.getString("COLUMN_NAME");
|
||||
if (pojoGroupByName.containsKey(indexName)) {
|
||||
pojoGroupByName.get(indexName).getColumnNames().add(columnName);
|
||||
} else {
|
||||
List<String> columns = new ArrayList<>();
|
||||
columns.add(columnName);
|
||||
IndexMeta indexMeta = IndexMeta.builder()
|
||||
.name(indexName)
|
||||
.columnNames(columns)
|
||||
.isPrimaryKey(Objects.equals("PRIMARY", indexName))
|
||||
.isUniqueKey(Objects.equals(nonUnique, false))
|
||||
.build();
|
||||
pojoGroupByName.put(indexName, indexMeta);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(pojoGroupByName.values());
|
||||
}
|
||||
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.pojo.TableMeta;
|
||||
import com.databasir.core.meta.repository.ColumnMetaRepository;
|
||||
import com.databasir.core.meta.repository.IndexMetaRepository;
|
||||
import com.databasir.core.meta.repository.TableMetaRepository;
|
||||
import com.databasir.core.meta.repository.TriggerMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.Condition;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdbcTableMetaRepository implements TableMetaRepository {
|
||||
|
||||
private final ColumnMetaRepository columnMetaRepository;
|
||||
|
||||
private final IndexMetaRepository indexMetaRepository;
|
||||
|
||||
private final TriggerMetaRepository triggerMetaRepository;
|
||||
|
||||
@Override
|
||||
public List<TableMeta> selectTables(Connection connection, Condition condition) {
|
||||
try {
|
||||
return doSelect(connection, condition);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<TableMeta> doSelect(Connection connection, Condition condition) throws SQLException {
|
||||
List<TableMeta> tableMetas = new ArrayList<>();
|
||||
String databaseName = condition.getDatabaseName();
|
||||
ResultSet tablesResult = connection.getMetaData().getTables(databaseName, null, null, null);
|
||||
while (tablesResult.next()) {
|
||||
String tableName = tablesResult.getString("TABLE_NAME");
|
||||
if (condition.tableIsIgnored(tableName)) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("ignored table: " + databaseName + "." + tableName);
|
||||
}
|
||||
} else {
|
||||
String tableType = tablesResult.getString("TABLE_TYPE");
|
||||
String tableComment = tablesResult.getString("REMARKS");
|
||||
TableCondition tableCondition = TableCondition.of(condition, tableName);
|
||||
TableMeta tableMeta = TableMeta.builder()
|
||||
.name(tableName)
|
||||
.type(tableType)
|
||||
.comment(tableComment)
|
||||
.columns(columnMetaRepository.selectColumns(connection, tableCondition))
|
||||
.indexes(indexMetaRepository.selectIndexes(connection, tableCondition))
|
||||
.triggers(triggerMetaRepository.selectTriggers(connection, tableCondition))
|
||||
.build();
|
||||
tableMetas.add(tableMeta);
|
||||
}
|
||||
}
|
||||
return tableMetas;
|
||||
}
|
||||
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
package com.databasir.core.meta.repository.impl.jdbc;
|
||||
|
||||
import com.databasir.core.meta.pojo.TriggerMeta;
|
||||
import com.databasir.core.meta.repository.TriggerMetaRepository;
|
||||
import com.databasir.core.meta.repository.condition.TableCondition;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class JdbcTriggerMetaRepository implements TriggerMetaRepository {
|
||||
|
||||
@Override
|
||||
public List<TriggerMeta> selectTriggers(Connection connection, TableCondition condition) {
|
||||
// note: jdbc not support get triggers
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
package com.databasir.core.render;
|
||||
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import com.databasir.core.render.markdown.MarkdownTemplateRender;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface Render {
|
||||
|
||||
void rendering(DatabaseMeta meta, OutputStream outputStream) throws IOException;
|
||||
|
||||
static Render markdownRender(RenderConfig configuration) {
|
||||
return new MarkdownTemplateRender(configuration);
|
||||
}
|
||||
|
||||
}
|
@@ -1,75 +0,0 @@
|
||||
package com.databasir.core.render;
|
||||
|
||||
import com.databasir.core.meta.pojo.ColumnMeta;
|
||||
import com.databasir.core.meta.pojo.IndexMeta;
|
||||
import com.databasir.core.meta.pojo.TriggerMeta;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Data
|
||||
public class RenderConfig {
|
||||
|
||||
private Boolean renderTables = true;
|
||||
|
||||
private Boolean renderColumns = true;
|
||||
|
||||
private Boolean renderIndexes = true;
|
||||
|
||||
private Boolean renderTriggers = true;
|
||||
|
||||
private LinkedHashMap<String, Function<ColumnMeta, String>> columnTitleAndValueMapping = columnTitleAndValueMapping();
|
||||
|
||||
private LinkedHashMap<String, Function<IndexMeta, String>> indexTitleAndValueMapping = indexTitleAndValueMapping();
|
||||
|
||||
private LinkedHashMap<String, Function<TriggerMeta, String>> triggerTitleAndValueMapping = triggerTitleAndValueMapping();
|
||||
|
||||
protected LinkedHashMap<String, Function<ColumnMeta, String>> columnTitleAndValueMapping() {
|
||||
LinkedHashMap<String, Function<ColumnMeta, String>> mapping = new LinkedHashMap<>();
|
||||
mapping.put("Name", ColumnMeta::getName);
|
||||
mapping.put("Type", column -> {
|
||||
String type;
|
||||
if (column.getDecimalDigits() == null || column.getDecimalDigits().equals(0)) {
|
||||
type = column.getType()
|
||||
+ "(" + column.getSize().toString() + ")";
|
||||
} else {
|
||||
type = column.getType()
|
||||
+ "(" + column.getSize().toString() + ", " + column.getDecimalDigits().toString() + ")";
|
||||
}
|
||||
return type;
|
||||
});
|
||||
mapping.put("Not Null", column -> column.getIsNullable() ? "" : "YES");
|
||||
mapping.put("Auto Increment", column -> column.getIsAutoIncrement() ? "YES" : "");
|
||||
mapping.put("Default", column -> {
|
||||
if (column.getDefaultValue() == null) {
|
||||
return "";
|
||||
}
|
||||
if (column.getDefaultValue().trim().equals("")) {
|
||||
return "'" + column.getDefaultValue() + "'";
|
||||
}
|
||||
return column.getDefaultValue();
|
||||
});
|
||||
mapping.put("Comment", ColumnMeta::getComment);
|
||||
return mapping;
|
||||
}
|
||||
|
||||
protected LinkedHashMap<String, Function<IndexMeta, String>> indexTitleAndValueMapping() {
|
||||
LinkedHashMap<String, Function<IndexMeta, String>> mapping = new LinkedHashMap<>();
|
||||
mapping.put("Name", IndexMeta::getName);
|
||||
mapping.put("IsPrimary", index -> index.getIsPrimaryKey() ? "YES" : "");
|
||||
mapping.put("IsUnique", index -> index.getIsUniqueKey() ? "YES" : "");
|
||||
mapping.put("Columns", index -> String.join(", ", index.getColumnNames()));
|
||||
return mapping;
|
||||
}
|
||||
|
||||
protected LinkedHashMap<String, Function<TriggerMeta, String>> triggerTitleAndValueMapping() {
|
||||
LinkedHashMap<String, Function<TriggerMeta, String>> mapping = new LinkedHashMap<>();
|
||||
mapping.put("Name", TriggerMeta::getName);
|
||||
mapping.put("Timing", trigger -> trigger.getTiming() + " " + trigger.getManipulation());
|
||||
mapping.put("Statement", trigger -> trigger.getStatement().replace("\n", " ")
|
||||
.replace("\r", " "));
|
||||
mapping.put("Create At", TriggerMeta::getCreateAt);
|
||||
return mapping;
|
||||
}
|
||||
}
|
@@ -1,108 +0,0 @@
|
||||
package com.databasir.core.render.markdown;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MarkdownBuilder {
|
||||
|
||||
private static final String LINE = "\n";
|
||||
|
||||
private static final String DOUBLE_LINE = LINE + LINE;
|
||||
|
||||
private StringBuilder builder = new StringBuilder(1024);
|
||||
|
||||
private MarkdownBuilder() {
|
||||
}
|
||||
|
||||
public static MarkdownBuilder builder() {
|
||||
return new MarkdownBuilder();
|
||||
}
|
||||
|
||||
public MarkdownBuilder primaryTitle(String title) {
|
||||
builder.append("# ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder secondTitle(String title) {
|
||||
builder.append("## ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder thirdTitle(String title) {
|
||||
builder.append("### ").append(title).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder text(String text) {
|
||||
builder.append(text).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder table(List<String> titles, List<List<String>> rows) {
|
||||
if (titles == null || titles.isEmpty()) {
|
||||
throw new IllegalArgumentException("titles must not be null or empty");
|
||||
}
|
||||
// build titles
|
||||
builder.append("| ");
|
||||
for (String title : titles) {
|
||||
builder.append(title).append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
|
||||
// build separators
|
||||
builder.append("| ");
|
||||
for (String title : titles) {
|
||||
builder.append("------").append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
|
||||
// build rows
|
||||
for (List<String> row : rows) {
|
||||
builder.append("| ");
|
||||
for (String column : row) {
|
||||
builder.append(column).append(" | ");
|
||||
}
|
||||
builder.append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder orderedList(List<String> list) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
builder.append(i + 1).append(". ").append(list.get(i)).append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder unorderedList(List<String> list) {
|
||||
for (String item : list) {
|
||||
builder.append("- ").append(item).append(LINE);
|
||||
}
|
||||
builder.append(LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder blockquotes(String content) {
|
||||
builder.append("> ").append(content).append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder code(String languageType, String statement) {
|
||||
builder.append("```").append(languageType).append(LINE)
|
||||
.append(statement)
|
||||
.append("```")
|
||||
.append(DOUBLE_LINE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkdownBuilder link(String text, String link) {
|
||||
builder.append("[").append(text).append("]")
|
||||
.append("(").append(link).append(")");
|
||||
return this;
|
||||
}
|
||||
|
||||
public String build() {
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
@@ -1,114 +0,0 @@
|
||||
package com.databasir.core.render.markdown;
|
||||
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import com.databasir.core.meta.pojo.TableMeta;
|
||||
import com.databasir.core.render.Render;
|
||||
import com.databasir.core.render.RenderConfig;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MarkdownRender implements Render {
|
||||
|
||||
@Getter
|
||||
private final RenderConfig config;
|
||||
|
||||
protected MarkdownRender(RenderConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static MarkdownRender of(RenderConfig config) {
|
||||
return new MarkdownRender(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rendering(DatabaseMeta meta,
|
||||
OutputStream outputStream) throws IOException {
|
||||
MarkdownBuilder contentBuilder = MarkdownBuilder.builder();
|
||||
contentBuilder.primaryTitle(meta.getDatabaseName());
|
||||
if (config.getRenderTables()) {
|
||||
for (TableMeta table : meta.getTables()) {
|
||||
buildTableName(contentBuilder, table);
|
||||
if (config.getRenderColumns()) {
|
||||
buildColumns(contentBuilder, table);
|
||||
}
|
||||
if (config.getRenderIndexes()) {
|
||||
buildIndexes(contentBuilder, table);
|
||||
}
|
||||
if (config.getRenderTriggers()) {
|
||||
buildTriggers(contentBuilder, table);
|
||||
}
|
||||
}
|
||||
}
|
||||
outputStream.write(contentBuilder.build().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void buildTableName(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
String tableName;
|
||||
if (table.getComment() == null || table.getComment().trim().isEmpty()) {
|
||||
tableName = table.getName();
|
||||
} else {
|
||||
tableName = table.getName() + "(" + table.getComment() + ")";
|
||||
}
|
||||
contentBuilder.secondTitle(tableName);
|
||||
}
|
||||
|
||||
private void buildColumns(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
contentBuilder.unorderedList(Collections.singletonList("columns"));
|
||||
List<List<String>> allColumnRows = table.getColumns()
|
||||
.stream()
|
||||
.map(column -> config.getColumnTitleAndValueMapping()
|
||||
.values()
|
||||
.stream()
|
||||
.map(mapping -> mapping.apply(column))
|
||||
.collect(Collectors.toList()))
|
||||
.collect(Collectors.toList());
|
||||
contentBuilder.table(tableTitles(), allColumnRows);
|
||||
}
|
||||
|
||||
private void buildIndexes(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
contentBuilder.unorderedList(Collections.singletonList("indexes"));
|
||||
List<List<String>> allIndexRows = table.getIndexes().stream()
|
||||
.map(index -> config.getIndexTitleAndValueMapping()
|
||||
.values()
|
||||
.stream()
|
||||
.map(mapping -> mapping.apply(index))
|
||||
.collect(Collectors.toList()))
|
||||
.collect(Collectors.toList());
|
||||
contentBuilder.table(indexTitles(), allIndexRows);
|
||||
}
|
||||
|
||||
private void buildTriggers(MarkdownBuilder contentBuilder, TableMeta table) {
|
||||
if (table.getTriggers() == null || table.getTriggers().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
contentBuilder.unorderedList(Collections.singletonList("triggers"));
|
||||
List<List<String>> allRows = table.getTriggers().stream()
|
||||
.map(trigger -> config.getTriggerTitleAndValueMapping()
|
||||
.values()
|
||||
.stream()
|
||||
.map(mapping -> mapping.apply(trigger))
|
||||
.collect(Collectors.toList()))
|
||||
.collect(Collectors.toList());
|
||||
contentBuilder.table(triggerTitles(), allRows);
|
||||
}
|
||||
|
||||
private List<String> tableTitles() {
|
||||
return new ArrayList<>(config.getColumnTitleAndValueMapping().keySet());
|
||||
}
|
||||
|
||||
private List<String> indexTitles() {
|
||||
return new ArrayList<>(config.getIndexTitleAndValueMapping().keySet());
|
||||
}
|
||||
|
||||
private List<String> triggerTitles() {
|
||||
return new ArrayList<>(config.getTriggerTitleAndValueMapping().keySet());
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
package com.databasir.core.render.markdown;
|
||||
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import com.databasir.core.render.Render;
|
||||
import com.databasir.core.render.RenderConfig;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* use freemarker template to render markdown
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class MarkdownTemplateRender implements Render {
|
||||
|
||||
private final RenderConfig renderConfig;
|
||||
|
||||
private String templatePath = "template/render/markdown/markdown.ftlh";
|
||||
|
||||
public MarkdownTemplateRender(RenderConfig config, String templatePath) {
|
||||
this(config);
|
||||
this.templatePath = templatePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rendering(DatabaseMeta meta, OutputStream outputStream) throws IOException {
|
||||
doRendering(meta, outputStream);
|
||||
}
|
||||
|
||||
public void doRendering(DatabaseMeta meta, OutputStream outputStream) throws IOException {
|
||||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
|
||||
cfg.setClassForTemplateLoading(getClass(), "/");
|
||||
cfg.setDefaultEncoding("UTF-8");
|
||||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
cfg.setLogTemplateExceptions(false);
|
||||
cfg.setWrapUncheckedExceptions(true);
|
||||
cfg.setFallbackOnNullLoopVariable(false);
|
||||
|
||||
Map<String, Object> root = new HashMap<>();
|
||||
root.put("meta", meta);
|
||||
root.put("config", renderConfig);
|
||||
Template template = cfg.getTemplate(templatePath);
|
||||
try {
|
||||
template.process(root, new OutputStreamWriter(outputStream));
|
||||
} catch (TemplateException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
# ${meta.databaseName}
|
||||
|
||||
| seq | name | comment |
|
||||
| ---- | --------------- | ------ |
|
||||
<#list meta.tables as table>
|
||||
| ${table_index+1} | [${table.name}](#${table.name}) | ${table.comment!'N/A'} |
|
||||
</#list>
|
||||
|
||||
<#if config.renderTables>
|
||||
<#list meta.tables as table>
|
||||
## ${table.name}
|
||||
|
||||
<#if config.renderColumns>
|
||||
### Columns
|
||||
|
||||
| seq | name | type | nullable | auto increment| default | comment |
|
||||
| ---- | ------ | ------ | ------ | -------- | ------ | ------ |
|
||||
<#list table.columns as column>
|
||||
| ${column_index+1} | ${column.name} | ${column.type} | ${column.isNullable?then('YES','NO')} | ${column.isAutoIncrement?then('YES', 'NO')} | ${column.isNullable?then(column.defaultValue!'NULL', column.defaultValue!'')} | ${column.comment!''} |
|
||||
</#list>
|
||||
</#if>
|
||||
|
||||
<#if config.renderIndexes>
|
||||
### Indexes
|
||||
|
||||
| seq | name | unique | primary key | columns |
|
||||
| ---- | ---- | -------- | -------- | ------ |
|
||||
<#list table.indexes as index>
|
||||
| ${index_index+1} | ${index.name} | ${index.isUniqueKey?then('YES', 'NO')} | ${index.isPrimaryKey?then('YES','NO')} | ${index.columnNames?join(', ')} |
|
||||
</#list>
|
||||
</#if>
|
||||
|
||||
<#if config.renderTriggers>
|
||||
### Triggers
|
||||
|
||||
| seq | name | timing | statement | created |
|
||||
| ---- | ---- | -------- | --------- | -------- |
|
||||
<#list table.triggers as trigger>
|
||||
| ${trigger_index} | ${trigger.name} | ${trigger.timing + " " + trigger.manipulation } | ${trigger.statement?replace("\n", "<br>")?replace("\r", " ")} | ${trigger.createAt} |
|
||||
</#list>
|
||||
</#if>
|
||||
|
||||
</#list>
|
||||
</#if>
|
@@ -1,39 +0,0 @@
|
||||
import com.databasir.core.Databasir;
|
||||
import com.databasir.core.meta.pojo.DatabaseMeta;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class App {
|
||||
|
||||
@Test
|
||||
public void testRenderAsMarkdown() throws SQLException, ClassNotFoundException {
|
||||
try (FileOutputStream out = new FileOutputStream("user.md")) {
|
||||
Connection connection = getJdbcConnection();
|
||||
Databasir databasir = Databasir.of();
|
||||
DatabaseMeta doc = databasir.get(connection, "user").orElseThrow();
|
||||
databasir.renderAsMarkdown(doc, out);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection getJdbcConnection() throws SQLException, ClassNotFoundException {
|
||||
// get database connection
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
Properties info = new Properties();
|
||||
info.put("user", "root");
|
||||
info.put("password", "123456");
|
||||
// this config is used by mysql
|
||||
info.put("useInformationSchema", "true");
|
||||
|
||||
String url = "jdbc:mysql://localhost:3306/patient?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true";
|
||||
return DriverManager.getConnection(url, info);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user