Skip to content

Commit

Permalink
BIGTOP-4230: Add some methods for BaseDao (apache#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq authored Sep 20, 2024
1 parent 103b8cf commit 4cce064
Show file tree
Hide file tree
Showing 33 changed files with 442 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public Object intercept(Invocation invocation) throws Throwable {
Collection<Object> objects;
if (parameter instanceof MapperMethod.ParamMap) {
MapperMethod.ParamMap<Object> paramMap = ((MapperMethod.ParamMap<Object>) parameter);
if (paramMap.get("param1") instanceof Collection) {
if (!paramMap.containsKey("param1") && paramMap.containsKey("arg0")) {
objects = ((Collection<Object>) paramMap.get("arg0"));
} else if (paramMap.get("param1") instanceof Collection) {
objects = ((Collection<Object>) paramMap.get("param1"));
} else {
objects = Collections.singletonList(paramMap.get("param1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public class ChatMessagePO extends BasePO implements Serializable {
@Column(name = "message", nullable = false, length = 255)
private String message;

@Column(name = "sender")
@Column(name = "sender", nullable = false)
private String sender;

@Column(name = "user_id")
@Column(name = "user_id", nullable = false)
private Long userId;

@Column(name = "thread_id")
@Column(name = "thread_id", nullable = false)
private Long threadId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public class ChatThreadPO extends BasePO implements Serializable {
@Column(name = "model", nullable = false, length = 255)
private String model;

@Column(name = "thread_info", columnDefinition = "json", nullable = false)
@Column(name = "thread_info", columnDefinition = "json")
private Map<String, String> threadInfo;

@Column(name = "user_id")
@Column(name = "user_id", nullable = false)
private Long userId;

@Column(name = "platform_id")
@Column(name = "platform_id", nullable = false)
private Long platformId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class HostPO extends BasePO implements Serializable {
@Column(name = "state")
private String state;

@Column(name = "cluster_id")
@Column(name = "cluster_id", nullable = false)
private Long clusterId;

@Transient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class JobPO extends BasePO implements Serializable {
@Column(name = "id")
private Long id;

@Column(name = "state")
@Column(name = "state", nullable = false)
private String state;

@Column(name = "name")
private String name;

@Lob
@Column(name = "context")
@Column(name = "context", nullable = false)
private String context;

@Column(name = "cluster_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public class PlatformAuthorizedPO extends BasePO implements Serializable {
@Column(name = "credentials", columnDefinition = "json", nullable = false)
private Map<String, String> credentials;

@Column(name = "platform_id")
@Column(name = "platform_id", nullable = false)
private Long platformId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public class RepoPO extends BasePO implements Serializable {
@Column(name = "repo_type")
private String repoType;

@Column(name = "cluster_id")
@Column(name = "cluster_id", nullable = false)
private Long clusterId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class StackPO extends BasePO implements Serializable {
@Column(name = "id")
private Long id;

@Column(name = "stack_name")
@Column(name = "stack_name", nullable = false)
private String stackName;

@Column(name = "stack_version")
@Column(name = "stack_version", nullable = false)
private String stackVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public class StagePO extends BasePO implements Serializable {
@Column(name = "id")
private Long id;

@Column(name = "name")
@Column(name = "name", nullable = false)
private String name;

@Column(name = "state")
@Column(name = "state", nullable = false)
private String state;

@Column(name = "order")
Expand All @@ -55,7 +55,7 @@ public class StagePO extends BasePO implements Serializable {
@Column(name = "context")
private String context;

@Column(name = "job_id")
@Column(name = "job_id", nullable = false)
private Long jobId;

@Column(name = "cluster_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class TaskPO extends BasePO implements Serializable {
@Column(name = "name")
private String name;

@Column(name = "context")
@Column(name = "context", nullable = false)
private String context;

@Column(name = "state")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,36 @@ public interface BaseDao<Entity> {
int save(Entity entity);

/**
* Update the entity by primary key.
* Insert all entities.
*/
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@InsertProvider(type = BaseSqlProvider.class, method = "insertList")
int saveAll(List<Entity> entities);

/**
* Partially update the entity by primary key.
*/
@UpdateProvider(type = BaseSqlProvider.class, method = "partialUpdateById")
int partialUpdateById(Entity entity);

/**
* Fully update the entity by primary key.
*/
@UpdateProvider(type = BaseSqlProvider.class, method = "updateById")
int updateById(Entity entity);

/**
* Partially update the entities by primary key.
*/
@UpdateProvider(type = BaseSqlProvider.class, method = "partialUpdateByIds")
int partialUpdateByIds(List<Entity> entities);

/**
* Fully update the entities by primary key.
*/
@UpdateProvider(type = BaseSqlProvider.class, method = "updateByIds")
int updateByIds(List<Entity> entities);

/**
* Query the entity by primary key.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

public interface HostDao extends BaseDao<HostPO> {

int saveAll(@Param("hosts") List<HostPO> hosts);

HostPO findByHostname(@Param("hostname") String hostname);

List<HostPO> findAllByHostnameIn(@Param("hostnames") Collection<String> hostnames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ public interface RepoDao extends BaseDao<RepoPO> {

Optional<RepoPO> findByRepoName(@Param("repoName") String clusterName);

int saveAll(@Param("clusters") List<RepoPO> repos);

List<RepoPO> findAllByClusterId(@Param("clusterId") Long clusterId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

@Slf4j
public class BaseSqlProvider {
Expand All @@ -46,6 +47,43 @@ public <Entity> String insert(Entity entity, ProviderContext context) {
return SQLBuilder.insert(tableMetaData, entity, databaseId);
}

public <Entity> String insertList(List<Entity> entities, ProviderContext context) {
Assert.notNull(entities, "entities must not be null");
Assert.notEmpty(entities, "entities list must not be empty");

String databaseId = context.getDatabaseId();

Class<?> entityClass = entities.get(0).getClass();

TableMetaData tableMetaData = TableMetaData.forClass(entityClass);

return SQLBuilder.insertList(tableMetaData, entities, databaseId);
}

public <Entity> String partialUpdateById(Entity entity, ProviderContext context) {
Assert.notNull(entity, "entity must not null");

String databaseId = context.getDatabaseId();

Class<?> entityClass = entity.getClass();
TableMetaData tableMetaData = TableMetaData.forClass(entityClass);

return SQLBuilder.update(tableMetaData, entity, databaseId, true);
}

public <Entity> String partialUpdateByIds(List<Entity> entities, ProviderContext context) {
Assert.notNull(entities, "entities must not be null");
Assert.notEmpty(entities, "entities list must not be empty");

String databaseId = context.getDatabaseId();

Class<?> entityClass = entities.get(0).getClass();

TableMetaData tableMetaData = TableMetaData.forClass(entityClass);

return SQLBuilder.updateList(tableMetaData, entities, databaseId, true);
}

public <Entity> String updateById(Entity entity, ProviderContext context) {
Assert.notNull(entity, "entity must not null");

Expand All @@ -54,7 +92,20 @@ public <Entity> String updateById(Entity entity, ProviderContext context) {
Class<?> entityClass = entity.getClass();
TableMetaData tableMetaData = TableMetaData.forClass(entityClass);

return SQLBuilder.update(tableMetaData, entity, databaseId);
return SQLBuilder.update(tableMetaData, entity, databaseId, false);
}

public <Entity> String updateByIds(List<Entity> entities, ProviderContext context) {
Assert.notNull(entities, "entities must not be null");
Assert.notEmpty(entities, "entities list must not be empty");

String databaseId = context.getDatabaseId();

Class<?> entityClass = entities.get(0).getClass();

TableMetaData tableMetaData = TableMetaData.forClass(entityClass);

return SQLBuilder.updateList(tableMetaData, entities, databaseId, false);
}

public String selectById(Serializable id, ProviderContext context) {
Expand Down
Loading

0 comments on commit 4cce064

Please sign in to comment.