This is an automated email from the ASF dual-hosted git repository.
wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git
The following commit(s) were added to refs/heads/main by this push:
new 25b43669 BIGTOP-4303: Remove findByCondition in BaseDao (#126)
25b43669 is described below
commit 25b436690715c098d8ad768ba09ae23208eebd6a
Author: timyuer <[email protected]>
AuthorDate: Sat Dec 14 15:32:19 2024 +0800
BIGTOP-4303: Remove findByCondition in BaseDao (#126)
---
.../manager/dao/annotations/QueryCondition.java | 54 ---------
.../apache/bigtop/manager/dao/enums/QueryType.java | 37 -------
.../bigtop/manager/dao/repository/BaseDao.java | 6 -
.../bigtop/manager/dao/sql/BaseSqlProvider.java | 10 --
.../apache/bigtop/manager/dao/sql/SQLBuilder.java | 121 ---------------------
5 files changed, 228 deletions(-)
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 @@ public interface BaseDao<Entity> {
@SelectProvider(type = BaseSqlProvider.class, method = "selectAll")
List<Entity> findAll();
- /**
- * Query all entities.
- */
- @SelectProvider(type = BaseSqlProvider.class, method = "findByCondition")
- <Condition> List<Entity> 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 class BaseSqlProvider {
return SQLBuilder.selectAll(tableMetaData, databaseId);
}
- public <Condition> 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 class SQLBuilder {
return sql.toString();
}
- public static <Condition> 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 @@ public class SQLBuilder {
|| field.isAnnotationPresent(UpdateBy.class)
|| field.isAnnotationPresent(UpdateTime.class);
}
-
- private static <Condition> SQL mysqlCondition(Condition condition,
TableMetaData tableMetaData)
- throws IllegalAccessException {
-
- Class<?> loadClass;
- try {
- loadClass = condition.getClass();
- } catch (Exception e) {
- throw new RuntimeException("Get class Error!!!");
- }
-
- List<Field> 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<String, String> 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;
- }
}