Skip to content

Commit

Permalink
MySQL Exists statement Support (#1760) (#1769)
Browse files Browse the repository at this point in the history
* Remove custom exists queries and use jpql to have spring do queries based on the drivers which handle all details



* Run spotless:apply



* Update to recitfy exists statement that had a not clause in it.



* Use a converter to ensure that the data is converted correctly from the db to the app and vice versa



---------

Signed-off-by: Aindriu Lavelle <[email protected]>
  • Loading branch information
aindriu-aiven authored Sep 14, 2023
1 parent 693fd74 commit 487f684
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 102 deletions.
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 @@ -1047,7 +1047,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 @@ -1468,43 +1468,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);
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 @@ -1513,10 +1520,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 @@ -25,14 +25,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 @@ -43,10 +37,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 @@ -46,19 +46,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(
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 @@ -37,19 +37,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

0 comments on commit 487f684

Please sign in to comment.