Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL Exists statement Support #1760

Merged
merged 5 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/io/aiven/klaw/dao/Acl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.aiven.klaw.dao;

import io.aiven.klaw.helpers.AclIPPrincipleTypeConverter;
import io.aiven.klaw.helpers.AivenAclIdConverter;
import io.aiven.klaw.model.enums.AclIPPrincipleType;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -72,5 +73,6 @@ public class Acl implements Serializable {
private Map<String, String> jsonParams;

@Column(name = "aclipprincipletype")
@Convert(converter = AclIPPrincipleTypeConverter.class)
private AclIPPrincipleType aclIpPrincipleType;
}
10 changes: 3 additions & 7 deletions core/src/main/java/io/aiven/klaw/dao/AclRequests.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package io.aiven.klaw.dao;

import io.aiven.klaw.helpers.AclIPPrincipleTypeConverter;
import io.aiven.klaw.helpers.AivenAclIdConverter;
import io.aiven.klaw.model.enums.AclIPPrincipleType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
Expand Down Expand Up @@ -107,6 +102,7 @@ public class AclRequests implements Serializable {
private Map<String, String> jsonParams;

@Column(name = "aclipprincipletype")
@Convert(converter = AclIPPrincipleTypeConverter.class)
private AclIPPrincipleType aclIpPrincipleType;

@Transient private String totalNoPages;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.aiven.klaw.helpers;

import io.aiven.klaw.model.enums.AclIPPrincipleType;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter
public class AclIPPrincipleTypeConverter
implements AttributeConverter<AclIPPrincipleType, Integer> {

@Override
public Integer convertToDatabaseColumn(AclIPPrincipleType aclIPPrincipleType) {
if (aclIPPrincipleType == null) {
return null;
}

return aclIPPrincipleType.ordinal();
}

@Override
public AclIPPrincipleType convertToEntityAttribute(Integer ordinalAclIpPrincipalType) {
if (ordinalAclIpPrincipalType == null) {
return null;
}
return switch (ordinalAclIpPrincipalType) {
case 0 -> AclIPPrincipleType.IP_ADDRESS;
case 1 -> AclIPPrincipleType.PRINCIPAL;
case 2 -> AclIPPrincipleType.USERNAME;
default -> null;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ public List<Acl> getUniqueConsumerGroups(int tenantId) {

public boolean validateIfConsumerGroupUsedByAnotherTeam(
Integer teamId, int tenantId, String consumerGroup) {
return aclRepo.validateIfConsumerGroupUsedByAnotherTeam(teamId, tenantId, consumerGroup);
return aclRepo.existsByTeamIdNotAndTenantIdAndConsumergroup(teamId, tenantId, consumerGroup);
}

public Team selectTeamDetails(Integer teamId, int tenantId) {
Expand Down Expand Up @@ -1499,43 +1499,50 @@ public boolean existsSchemaComponentsForEnv(String env, int tenantId) {
public boolean existsComponentsCountForTeam(Integer teamId, int tenantId) {
return Stream.<Supplier<Boolean>>of(
() -> {
boolean res = schemaRequestRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res =
schemaRequestRepo.existsByTeamIdAndTenantIdAndRequestStatus(
teamId, tenantId, RequestStatus.CREATED.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding request status parameter intentional ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it was hard coded in the native query for the requests it is checking if there is an open request for that team

log.debug("For team {} Active Schema Requests {}", teamId, res);
return res;
},
() -> {
boolean res = messageSchemaRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res = messageSchemaRepo.existsByTeamIdAndTenantId(teamId, tenantId);
log.debug("For team {} number of Schemas in DB {}", teamId, res);
return res;
},
() -> {
boolean res = kafkaConnectorRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res = kafkaConnectorRepo.existsByTeamIdAndTenantId(teamId, tenantId);
log.debug("For team {} Active Connector Requests {}", teamId, res);
return res;
},
() -> {
boolean res =
kafkaConnectorRequestsRepo.existsRecordsCountForTeamId(teamId, tenantId);
kafkaConnectorRequestsRepo.existsByTeamIdAndTenantIdAndRequestStatus(
teamId, tenantId, RequestStatus.CREATED.value);
log.debug("For team {} number of Connector in DB {}", teamId, res);
return res;
},
() -> {
boolean res = topicRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res = topicRepo.existsByTeamIdAndTenantId(teamId, tenantId);
log.debug("For team {} Active Topic Requests {}", teamId, res);
return res;
},
() -> {
boolean res = topicRequestsRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res =
topicRequestsRepo.existsByTeamIdAndTenantIdAndRequestStatus(
teamId, tenantId, RequestStatus.CREATED.value);
log.debug("For team {} number of Topic in DB {}", teamId, res);
return res;
},
() -> {
boolean res = aclRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res = aclRepo.existsByTeamIdAndTenantId(teamId, tenantId);
log.debug("For team {} Active ACL Requests {}", teamId, res);
return res;
},
() -> {
boolean res = aclRequestsRepo.existsRecordsCountForTeamId(teamId, tenantId);
boolean res =
aclRequestsRepo.existsByTeamIdAndTenantIdAndRequestStatus(
teamId, tenantId, RequestStatus.CREATED.value);
log.debug("For team {} number of ACL in DB {}", teamId, res);
return res;
})
Expand All @@ -1544,10 +1551,18 @@ public boolean existsComponentsCountForTeam(Integer teamId, int tenantId) {

public boolean existsComponentsCountForUser(String userId, int tenantId) {
return Stream.<Supplier<Boolean>>of(
() -> schemaRequestRepo.existsRecordsCountForUserId(userId, tenantId),
() -> kafkaConnectorRequestsRepo.existsRecordsCountForUserId(userId, tenantId),
() -> topicRequestsRepo.existsRecordsCountForUserId(userId, tenantId),
() -> aclRequestsRepo.existsRecordsCountForUserId(userId, tenantId))
() ->
schemaRequestRepo.existsByRequestorAndTenantIdAndRequestStatus(
userId, tenantId, RequestStatus.CREATED.value),
() ->
kafkaConnectorRequestsRepo.existsByRequestorAndTenantIdAndRequestStatus(
userId, tenantId, RequestStatus.CREATED.value),
() ->
topicRequestsRepo.existsByRequestorAndTenantIdAndRequestStatus(
userId, tenantId, RequestStatus.CREATED.value),
() ->
aclRequestsRepo.existsByRequestorAndTenantIdAndRequestStatus(
userId, tenantId, RequestStatus.CREATED.value))
.anyMatch(Supplier::get);
}

Expand Down
15 changes: 3 additions & 12 deletions core/src/main/java/io/aiven/klaw/repository/AclRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ List<Acl> findAllByEnvironmentAndAclPatternTypeAndTenantId(

List<Acl> findAllByTenantId(int tenantId);

@Query(
value =
"select exists(select 1 from kwacls where teamid != :teamId and tenantid = :tenantId and consumergroup = :consumerGroup)",
nativeQuery = true)
boolean validateIfConsumerGroupUsedByAnotherTeam(
@Param("teamId") Integer teamId,
@Param("tenantId") Integer tenantId,
@Param("consumerGroup") String consumerGroup);
boolean existsByTeamIdNotAndTenantIdAndConsumergroup(
Integer teamId, Integer tenantId, String consumerGroup);

boolean existsByEnvironmentAndTenantId(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);
Expand All @@ -46,10 +40,7 @@ boolean existsByEnvironmentAndTenantId(
List<Object[]> findAllAclsCountForEnv(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);

@Query(
value = "select exists(select 1 from kwacls where teamid = :teamId and tenantid = :tenantId)",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
boolean existsByTeamIdAndTenantId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);

@Query(
Expand Down
16 changes: 4 additions & 12 deletions core/src/main/java/io/aiven/klaw/repository/AclRequestsRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ boolean existsByTenantIdAndEnvironmentAndRequestStatus(
boolean existsByEnvironmentAndTenantId(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwaclrequests where teamid = :teamId and tenantid = :tenantId and topicstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantIdAndRequestStatus(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId, String requestStatus);

@Query(
value =
Expand All @@ -38,12 +34,8 @@ boolean existsRecordsCountForTeamId(
List<Object[]> findAllRecordsCountForUserId(
@Param("userId") String userId, @Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwaclrequests where (requestor = :userId) and tenantid = :tenantId and topicstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForUserId(
@Param("userId") String userId, @Param("tenantId") Integer tenantId);
boolean existsByRequestorAndTenantIdAndRequestStatus(
String requestor, Integer tenantId, String requestStatus);

@Query(
value = "select max(aclid) from kwaclrequests where tenantid = :tenantId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ List<KwKafkaConnector> findAllByConnectorNameAndEnvironmentAndTenantId(
boolean existsByEnvironmentAndTenantId(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwkafkaconnector where teamid = :teamId and tenantid = :tenantId)",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantId(Integer teamId, Integer tenantId);

@Query(
value = "select max(connectorid) from kwkafkaconnector where tenantid = :tenantId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,11 @@ boolean existsByEnvironmentAndTenantIdAndRequestStatus(
nativeQuery = true)
Integer getNextConnectorRequestId(@Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwkafkaconnectorrequests where teamid = :teamId and tenantid = :tenantId and connectorstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantIdAndRequestStatus(
Integer teamId, Integer tenantId, String requestStatus);

@Query(
value =
"select exists(select 1 from kwkafkaconnectorrequests where (requestor = :userId) and tenantid = :tenantId and connectorstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForUserId(
@Param("userId") String userId, @Param("tenantId") Integer tenantId);
boolean existsByRequestorAndTenantIdAndRequestStatus(
String requestor, Integer tenantId, String requestStatus);

@Query(
value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ List<MessageSchema> findAllByTenantIdAndTopicnameAndSchemaversionAndEnvironment(
boolean existsMessageSchemaByEnvironmentAndTenantId(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwavroschemas where teamid = :teamId and tenantid = :tenantId)",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantId(Integer teamId, Integer tenantId);

@Query(
value = "select max(avroschemaid) from kwavroschemas where tenantid = :tenantId",
Expand Down
16 changes: 4 additions & 12 deletions core/src/main/java/io/aiven/klaw/repository/SchemaRequestRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,11 @@ boolean existsSchemaRequestByEnvironmentAndTenantId(
boolean existsSchemaRequestByEnvironmentAndTenantIdAndRequestStatus(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not part of this change, but an unused method, can be deleted.

String envId, Integer tenantId, String requestStatus);

@Query(
value =
"select exists(select 1 from kwschemarequests where teamid = :teamId and tenantid = :tenantId and topicstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantIdAndRequestStatus(
Integer teamId, Integer tenantId, String topicStatus);

@Query(
value =
"select exists(select 1 from kwschemarequests where (requestor = :userId) and tenantid = :tenantId and topicstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForUserId(
@Param("userId") String userId, @Param("tenantId") Integer tenantId);
boolean existsByRequestorAndTenantIdAndRequestStatus(
String requestor, Integer tenantId, String requestStatus);

List<SchemaRequest> findAllByTenantId(int tenantId);

Expand Down
7 changes: 1 addition & 6 deletions core/src/main/java/io/aiven/klaw/repository/TopicRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ List<Object[]> findAllTopicsForTeam(
boolean existsByEnvironmentAndTenantId(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwtopics where teamid = :teamId and tenantid = :tenantId)",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantId(Integer teamId, Integer tenantId);

@Query(
value =
Expand Down
16 changes: 4 additions & 12 deletions core/src/main/java/io/aiven/klaw/repository/TopicRequestsRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,11 @@ List<TopicRequest> findAllByRequestStatusAndTopicnameAndEnvironmentAndTenantId(
boolean existsByEnvironmentAndTenantId(
@Param("envId") String envId, @Param("tenantId") Integer tenantId);

@Query(
value =
"select exists(select 1 from kwtopicrequests where teamid = :teamId and tenantid = :tenantId and topicstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForTeamId(
@Param("teamId") Integer teamId, @Param("tenantId") Integer tenantId);
boolean existsByTeamIdAndTenantIdAndRequestStatus(
Integer teamId, Integer tenantId, String requestStatus);

@Query(
value =
"select exists(select 1 from kwtopicrequests where (requestor = :userId) and tenantid = :tenantId and topicstatus='created')",
nativeQuery = true)
boolean existsRecordsCountForUserId(
@Param("userId") String userId, @Param("tenantId") Integer tenantId);
boolean existsByRequestorAndTenantIdAndRequestStatus(
String requestor, Integer tenantId, String requestStatus);

@Query(
value = "select max(topicid) from kwtopicrequests where tenantid = :tenantId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import io.aiven.klaw.UtilMethods;
import io.aiven.klaw.dao.AclRequests;
import io.aiven.klaw.dao.UserInfo;
import io.aiven.klaw.model.enums.AclType;
import io.aiven.klaw.model.enums.RequestMode;
import io.aiven.klaw.model.enums.RequestOperationType;
import io.aiven.klaw.model.enums.RequestStatus;
import io.aiven.klaw.model.enums.*;
import io.aiven.klaw.repository.AclRequestsRepo;
import io.aiven.klaw.repository.KwKafkaConnectorRequestsRepo;
import io.aiven.klaw.repository.SchemaRequestRepo;
Expand Down Expand Up @@ -806,6 +803,7 @@ private void generateData(
acl.setRequestOperationType(aclType.value);
acl.setRequestOperationType(requestOperationType.value); // Create/Delete ..
acl.setAclType(aclType.value);
acl.setAclIpPrincipleType(AclIPPrincipleType.IP_ADDRESS);
if (status != null) {
acl.setRequestStatus(status);
}
Expand Down