From 25b436690715c098d8ad768ba09ae23208eebd6a Mon Sep 17 00:00:00 2001 From: timyuer <524860213@qq.com> Date: Sat, 14 Dec 2024 15:32:19 +0800 Subject: [PATCH] BIGTOP-4303: Remove findByCondition in BaseDao (#126) --- .../dao/annotations/QueryCondition.java | 54 -------- .../bigtop/manager/dao/enums/QueryType.java | 37 ------ .../manager/dao/repository/BaseDao.java | 6 - .../manager/dao/sql/BaseSqlProvider.java | 10 -- .../bigtop/manager/dao/sql/SQLBuilder.java | 121 ------------------ 5 files changed, 228 deletions(-) delete mode 100644 bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/QueryCondition.java delete mode 100644 bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/enums/QueryType.java diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/QueryCondition.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/QueryCondition.java deleted file mode 100644 index 8e562693..00000000 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/QueryCondition.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.bigtop.manager.dao.annotations; - -import org.apache.bigtop.manager.dao.enums.QueryType; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface QueryCondition { - - /** - * queryType - */ - QueryType queryType() default QueryType.EQ; - - /** - * queryKey - */ - String queryKey() default ""; - - /** - * pairDelimiter - * e.g. 1-2 - */ - String pairDelimiter() default "-"; - - /** - * multipleDelimiter - * e.g. 1,2,3 - */ - String multipleDelimiter() default ","; -} diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/enums/QueryType.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/enums/QueryType.java deleted file mode 100644 index 7943239a..00000000 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/enums/QueryType.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.bigtop.manager.dao.enums; - -public enum QueryType { - EQ, - NOT_EQ, - IN, - NOT_IN, - GT, - GTE, - LT, - LTE, - BETWEEN, - PREFIX_LIKE, - SUFFIX_LIKE, - LIKE, - NOT_LIKE, - NULL, - NOT_NULL, -} diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/repository/BaseDao.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/repository/BaseDao.java index 62b6c7c1..aab72d5b 100644 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/repository/BaseDao.java +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/repository/BaseDao.java @@ -109,12 +109,6 @@ default Optional findOptionalById(Serializable id) { @SelectProvider(type = BaseSqlProvider.class, method = "selectAll") List findAll(); - /** - * Query all entities. - */ - @SelectProvider(type = BaseSqlProvider.class, method = "findByCondition") - List findByCondition(Condition condition); - /** * Delete the entity by primary key. */ diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/BaseSqlProvider.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/BaseSqlProvider.java index c76eef05..ad26b27d 100644 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/BaseSqlProvider.java +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/BaseSqlProvider.java @@ -109,16 +109,6 @@ public String selectAll(ProviderContext context) { return SQLBuilder.selectAll(tableMetaData, databaseId); } - public String findByCondition(Condition condition, ProviderContext context) - throws IllegalAccessException { - String databaseId = context.getDatabaseId(); - - Class entityClass = getEntityClass(context); - TableMetaData tableMetaData = TableMetaData.forClass(entityClass); - - return SQLBuilder.findByCondition(tableMetaData, databaseId, condition); - } - public String deleteById(Serializable id, ProviderContext context) { String databaseId = context.getDatabaseId(); diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/SQLBuilder.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/SQLBuilder.java index 5b06e379..19171d4b 100644 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/SQLBuilder.java +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/SQLBuilder.java @@ -19,10 +19,8 @@ package org.apache.bigtop.manager.dao.sql; -import org.apache.bigtop.manager.common.utils.ClassUtils; import org.apache.bigtop.manager.dao.annotations.CreateBy; import org.apache.bigtop.manager.dao.annotations.CreateTime; -import org.apache.bigtop.manager.dao.annotations.QueryCondition; import org.apache.bigtop.manager.dao.annotations.UpdateBy; import org.apache.bigtop.manager.dao.annotations.UpdateTime; import org.apache.bigtop.manager.dao.enums.DBType; @@ -420,26 +418,6 @@ public static String deleteByIds( return sql.toString(); } - public static String findByCondition( - TableMetaData tableMetaData, String databaseId, Condition condition) throws IllegalAccessException { - String tableName = tableMetaData.getTableName(); - log.info("databaseId: {}", databaseId); - SQL sql = new SQL(); - switch (DBType.toType(databaseId)) { - case POSTGRESQL: - tableName = "\"" + tableName + "\""; - case MYSQL: { - sql = mysqlCondition(condition, tableMetaData); - break; - } - default: { - log.error("Unsupported data source"); - } - } - - return sql.toString(); - } - private static String keywordsFormat(String keyword, DBType dbType) { return switch (dbType) { case MYSQL -> "`" + keyword + "`"; @@ -462,103 +440,4 @@ private static boolean checkBaseField(Field field) { || field.isAnnotationPresent(UpdateBy.class) || field.isAnnotationPresent(UpdateTime.class); } - - private static SQL mysqlCondition(Condition condition, TableMetaData tableMetaData) - throws IllegalAccessException { - - Class loadClass; - try { - loadClass = condition.getClass(); - } catch (Exception e) { - throw new RuntimeException("Get class Error!!!"); - } - - List fieldList = ClassUtils.getFields(loadClass); - /* Prepare SQL */ - SQL sql = new SQL(); - sql.SELECT("*"); - sql.FROM(tableMetaData.getTableName()); - for (Field field : fieldList) { - field.setAccessible(true); - String fieldName = field.getName(); - log.debug("[requestField] {}, [requestValue] {}", fieldName, field.get(condition)); - if (field.isAnnotationPresent(QueryCondition.class) && Objects.nonNull(field.get(condition))) { - QueryCondition annotation = field.getAnnotation(QueryCondition.class); - - String property = fieldName; - if (!annotation.queryKey().isEmpty()) { - property = annotation.queryKey(); - } - - Object value = field.get(condition); - Map fieldColumnMap = tableMetaData.getFieldColumnMap(); - - if (value != null && fieldColumnMap.containsKey(property)) { - String columnName = fieldColumnMap.get(property); - - log.info( - "[queryKey] {}, [queryType] {}, [queryValue] {}", - property, - annotation.queryType().toString(), - field.get(condition)); - switch (annotation.queryType()) { - case EQ: - sql.WHERE(getEquals(columnName, fieldName)); - break; - case NOT_EQ: - sql.WHERE(columnName + " != " + getTokenParam(fieldName)); - break; - case IN: - sql.WHERE(columnName + " IN ( REPLACE( " + getTokenParam(fieldName) + ", '" - + annotation.multipleDelimiter() + "', ',') )"); - break; - case NOT_IN: - sql.WHERE(columnName + " NOT IN ( REPLACE( " + getTokenParam(fieldName) + ", '" - + annotation.multipleDelimiter() + "', ',') )"); - break; - case GT: - sql.WHERE(columnName + " > " + getTokenParam(fieldName)); - break; - case GTE: - sql.WHERE(columnName + " >= " + getTokenParam(fieldName)); - break; - case LT: - sql.WHERE(columnName + " < " + getTokenParam(fieldName)); - break; - case LTE: - sql.WHERE(columnName + " <= " + getTokenParam(fieldName)); - break; - case BETWEEN: - sql.WHERE(columnName + " BETWEEN SUBSTRING_INDEX( " + getTokenParam(fieldName) + ", '" - + annotation.pairDelimiter() + "', 1) AND SUBSTRING_INDEX( " - + getTokenParam(fieldName) + ", '" - + annotation.pairDelimiter() + "', 2)"); - break; - case PREFIX_LIKE: - sql.WHERE(columnName + " LIKE CONCAT( " + getTokenParam(fieldName) + ", '%')"); - break; - case SUFFIX_LIKE: - sql.WHERE(columnName + " LIKE CONCAT('%', " + getTokenParam(fieldName) + ")"); - break; - case LIKE: - sql.WHERE(columnName + " LIKE CONCAT('%', " + getTokenParam(fieldName) + ", '%')"); - break; - case NOT_LIKE: - sql.WHERE(columnName + " NOT LIKE CONCAT('%', " + getTokenParam(fieldName) + ", '%')"); - break; - case NULL: - sql.WHERE(columnName + " IS NULL"); - break; - case NOT_NULL: - sql.WHERE(columnName + " IS NOT NULL"); - break; - default: - log.warn("Unknown query type: {}", annotation.queryType()); - } - } - } - } - - return sql; - } }