mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-09 16:44:35 +08:00
feat: support custom database types
This commit is contained in:
@@ -80,6 +80,20 @@ public abstract class BaseDao<R> {
|
||||
new DataNotExistsException("data not exists in " + table.getName() + " with id = " + id));
|
||||
}
|
||||
|
||||
public Optional<R> selectOptionalOne(Condition condition) {
|
||||
return getDslContext()
|
||||
.select(table.fields()).from(table).where(condition)
|
||||
.fetchOptionalInto(pojoType);
|
||||
}
|
||||
|
||||
public R selectOne(Condition condition) {
|
||||
return selectOptionalOne(condition)
|
||||
.orElseThrow(() -> new DataNotExistsException("data not exists in "
|
||||
+ table.getName()
|
||||
+ " with condition = "
|
||||
+ condition));
|
||||
}
|
||||
|
||||
public List<R> selectInIds(List<? extends Serializable> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
|
@@ -0,0 +1,61 @@
|
||||
package com.databasir.dao.impl;
|
||||
|
||||
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
|
||||
import lombok.Getter;
|
||||
import org.jooq.DSLContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.databasir.dao.Tables.DATABASE_TYPE;
|
||||
|
||||
@Repository
|
||||
public class DatabaseTypeDao extends BaseDao<DatabaseTypePojo> {
|
||||
|
||||
@Autowired
|
||||
@Getter
|
||||
private DSLContext dslContext;
|
||||
|
||||
public DatabaseTypeDao() {
|
||||
super(DATABASE_TYPE, DatabaseTypePojo.class);
|
||||
}
|
||||
|
||||
public boolean existsByDatabaseType(String databaseType) {
|
||||
return exists(DATABASE_TYPE.DATABASE_TYPE_.eq(databaseType)
|
||||
.and(DATABASE_TYPE.DELETED.eq(false)));
|
||||
}
|
||||
|
||||
public boolean existsById(Integer id) {
|
||||
return exists(DATABASE_TYPE.ID.eq(id)
|
||||
.and(DATABASE_TYPE.DELETED.eq(false)));
|
||||
}
|
||||
|
||||
public DatabaseTypePojo selectByDatabaseType(String databaseType) {
|
||||
return this.selectOne(DATABASE_TYPE.DATABASE_TYPE_.eq(databaseType)
|
||||
.and(DATABASE_TYPE.DELETED.eq(false)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DatabaseTypePojo> selectAll() {
|
||||
return this.getDslContext().selectFrom(DATABASE_TYPE)
|
||||
.where(DATABASE_TYPE.DELETED.eq(false))
|
||||
.orderBy(DATABASE_TYPE.ID.desc())
|
||||
.fetchInto(DatabaseTypePojo.class);
|
||||
}
|
||||
|
||||
public int deleteById(Integer id) {
|
||||
return this.getDslContext()
|
||||
.update(DATABASE_TYPE)
|
||||
.set(DATABASE_TYPE.DELETED, true)
|
||||
.set(DATABASE_TYPE.DELETED_TOKEN, DATABASE_TYPE.ID)
|
||||
.where(DATABASE_TYPE.ID.eq(id)
|
||||
.and(DATABASE_TYPE.DELETED.eq(false)))
|
||||
.execute();
|
||||
}
|
||||
|
||||
public Optional<DatabaseTypePojo> selectOptionalById(Integer id) {
|
||||
return super.selectOptionalOne(DATABASE_TYPE.DELETED.eq(false).and(DATABASE_TYPE.ID.eq(id)));
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ import com.databasir.dao.value.GroupProjectCountPojo;
|
||||
import lombok.Getter;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
@@ -14,6 +15,7 @@ import org.springframework.stereotype.Repository;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -100,4 +102,17 @@ public class ProjectDao extends BaseDao<ProjectPojo> {
|
||||
.where(PROJECT.GROUP_ID.eq(groupId).and(PROJECT.DELETED.eq(false)))
|
||||
.fetchInto(Integer.class);
|
||||
}
|
||||
|
||||
public Map<String, Integer> countByDatabaseTypes(List<String> databaseTypes) {
|
||||
if (databaseTypes == null || databaseTypes.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return getDslContext()
|
||||
.select(DSL.count(DATA_SOURCE), DATA_SOURCE.DATABASE_TYPE)
|
||||
.from(DATA_SOURCE)
|
||||
.innerJoin(PROJECT).on(PROJECT.ID.eq(DATA_SOURCE.PROJECT_ID))
|
||||
.where(PROJECT.DELETED.eq(false)).and(DATA_SOURCE.DATABASE_TYPE.in(databaseTypes))
|
||||
.groupBy(DATA_SOURCE.DATABASE_TYPE)
|
||||
.fetchMap(DATA_SOURCE.DATABASE_TYPE, DSL.count(DATA_SOURCE));
|
||||
}
|
||||
}
|
||||
|
@@ -13,4 +13,10 @@ CREATE TABLE IF NOT EXISTS database_type
|
||||
create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT uk_database_type_deleted_deleted_token UNIQUE (database_type, deleted, deleted_token)
|
||||
) CHARSET utf8mb4
|
||||
COLLATE utf8mb4_unicode_ci COMMENT 'customer database types';
|
||||
COLLATE utf8mb4_unicode_ci COMMENT 'customer database types';
|
||||
|
||||
REPLACE INTO databasir.database_type (id, database_type, icon, DESCRIPTION, jdbc_driver_file_url,
|
||||
jdbc_driver_class_name,
|
||||
jdbc_protocol)
|
||||
VALUES (1, 'mysql', '', 'system default mysql', 'N/A', 'com.mysql.cj.jdbc.Driver', 'jdbc:mysql'),
|
||||
(2, 'postgresql', '', 'system default postgresql', 'N/A', 'org.postgresql.Driver', 'jdbc:postgresql');
|
||||
|
Reference in New Issue
Block a user