com.github.chengyuxing.sql.dsl.clause.ColumnHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rabbit-sql Show documentation
Show all versions of rabbit-sql Show documentation
Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql
file.
package com.github.chengyuxing.sql.dsl.clause;
import com.github.chengyuxing.sql.dsl.types.FieldReference;
import com.github.chengyuxing.sql.dsl.types.Operator;
import com.github.chengyuxing.sql.dsl.types.StandardOperator;
import com.github.chengyuxing.sql.utils.EntityUtil;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.Set;
public abstract class ColumnHelper {
protected final Class clazz;
protected ColumnHelper(@NotNull Class clazz) {
this.clazz = clazz;
}
protected abstract Set columnWhiteList();
protected abstract Set operatorWhiteList();
protected @NotNull String getColumnName(@NotNull FieldReference fieldReference) {
String fieldName = EntityUtil.getFieldNameWithCache(fieldReference);
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return EntityUtil.getColumnName(field);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
protected boolean isIllegalColumn(String column) {
if (Objects.isNull(columnWhiteList()) || columnWhiteList().isEmpty()) {
return true;
}
return !columnWhiteList().contains(column);
}
protected boolean isIllegalOperator(@NotNull Operator operator) {
if (operator instanceof StandardOperator) {
return false;
}
if (Objects.isNull(operatorWhiteList()) || operatorWhiteList().isEmpty()) {
return true;
}
return !operatorWhiteList().contains(operator.getValue());
}
}