Skip to content

Commit

Permalink
Merge pull request #11 from ClearXs/1.1.x
Browse files Browse the repository at this point in the history
merge commit
  • Loading branch information
ClearXs authored Mar 26, 2024
2 parents 9cfa192 + 3d4fff3 commit 76286d6
Show file tree
Hide file tree
Showing 55 changed files with 933 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static Class<?> getGenericType(@NonNull Object obj, @NonNull Class<?> sup
/**
* get specific generic type by index
*
* @param index determinate generic type index, ignore if indistinct
* @return index generic type or null
* @see #obtainGenericTypes(Class, Class)
*/
Expand Down Expand Up @@ -123,7 +124,11 @@ public static Class<?>[] obtainGenericTypes(Class<?> objClass, Class<?> superCla
if (superType instanceof ParameterizedType parameterizedSuperType) {
Type[] actualTypeArguments = parameterizedSuperType.getActualTypeArguments();
try {
return Arrays.stream(actualTypeArguments).map(Class.class::cast).toArray(Class[]::new);
return Arrays.stream(actualTypeArguments)
// t must be class type
.filter(t -> Class.class.isAssignableFrom(t.getClass()))
.map(Class.class::cast)
.toArray(Class[]::new);
} catch (Throwable ex) {
log.error("obtain generic type by obj class {} from super class {}, happen class cast error", objClass.getName(), superClassOrInterface.getName(), ex);
return new Class[0];
Expand Down Expand Up @@ -183,6 +188,5 @@ static List<ParameterizedType> findGenericTypes(Class<?> clazz) {
static class BinaryClassKey {
private final Class<?> cls1;
private final Class<?> cls2;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface ExpressionTemplate {
* @param v1 模板变量v
* @return 解析完成的字符串
*/
default String paresTemplate(String template, String k1, Object v1) {
default String parseTemplate(String template, String k1, Object v1) {
return parseTemplate(template, Tuples.of(k1, v1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ default Object getValueIfNull(Object originalValue) {
return Objects.requireNonNullElse(originalValue, ValueWrapper.EMPTY_VALUE);
}

/**
* according to operator type, this type maybe is subtype, so translate specifies operator type
* <p>rules</p>
* <ul>
* <li>current operator type must be class-type</li>
* <li>find operator type first interface if {@link Operator} </li>
* </ul>
*
* @param operatorType operatorType
* @return {@link Operator} or parent type
*/
static Class<? extends Operator<?>> getHireachialType(Class<? extends Operator<?>> operatorType) {
if (!operatorType.isInterface()) {
Class<?>[] interfaces = operatorType.getInterfaces();
for (Class<?> hireachial : interfaces) {
if (Operator.class.isAssignableFrom(hireachial)) {
return (Class<? extends Operator<?>>) hireachial;
}
}
}
return operatorType;
}

/**
* Operator 分组注解
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ default QueryOperator query() {
*/
QueryOperator query(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends QueryOperator> T getQueryOperator(Class<T> queryOperatorClass, OperatorKey operatorKey) {
return getOperator(queryOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -62,6 +69,13 @@ default InsertOperator insert() {
*/
InsertOperator insert(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends InsertOperator> T getInsertOperator(Class<T> insertOperatorClass, OperatorKey operatorKey) {
return getOperator(insertOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -86,6 +100,13 @@ default UpdateOperator update() {
*/
UpdateOperator update(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends UpdateOperator> T getUpdateOperator(Class<T> updateOperatorClass, OperatorKey operatorKey) {
return getOperator(updateOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -109,6 +130,13 @@ default DeleteOperator delete() {
*/
DeleteOperator delete(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends DeleteOperator> T getDeleteOperator(Class<T> deleteOperatorClass, OperatorKey operatorKey) {
return getOperator(deleteOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -135,6 +163,13 @@ default CreateTableOperator createTable() {
*/
CreateTableOperator createTable(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends CreateTableOperator> T getCreateTableOperator(Class<T> createTableOperatorClass, OperatorKey operatorKey) {
return getOperator(createTableOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -159,6 +194,13 @@ default DropTableOperator dropTable() {
*/
DropTableOperator dropTable(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends DropTableOperator> T getDropTableOperator(Class<T> dropTableOperatorClass, OperatorKey operatorKey) {
return getOperator(dropTableOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -183,6 +225,13 @@ default ExistTableOperator existTable() {
*/
ExistTableOperator existTable(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends ExistTableOperator> T getExistTableOperator(Class<T> existTableOperatorClass, OperatorKey operatorKey) {
return getOperator(existTableOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -207,6 +256,13 @@ default ShowColumnsOperator showColumns() {
*/
ShowColumnsOperator showColumns(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends ShowColumnsOperator> T getShowColumnsOperator(Class<T> showColumnsOperatorClass, OperatorKey operatorKey) {
return getOperator(showColumnsOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -231,6 +287,13 @@ default ShowTablesOperator showTables() {
*/
ShowTablesOperator showTables(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends ShowTablesOperator> T getShowTablesOperator(Class<T> showTablesOperatorClass, OperatorKey operatorKey) {
return getOperator(showTablesOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -253,6 +316,13 @@ default AlterTableOperator alterTables() {
*/
AlterTableOperator alterTables(DBType dbType);

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
static <T extends AlterTableOperator> T getAlterTableOperator(Class<T> alterTableOperatorClass, OperatorKey operatorKey) {
return getOperator(alterTableOperatorClass, operatorKey, null);
}

/**
* @see #getOperator(Class, OperatorKey, DBType)
*/
Expand All @@ -261,9 +331,9 @@ static <T extends AlterTableOperator> T getAlterTableOperator(Class<T> alterTabl
}

/**
* 获取当前系统的SQL Operator
* 获取当前系统的OperatorGroup
*
* @return OperatorMetadata or DruidOperatorMetadata
* @see #getOperatorGroup(OperatorKey)
*/
static OperatorGroup getSystemOperatorGroup() {
OperatorKey systemOperatorKey = OperatorKey.getSystemOperatorKey();
Expand All @@ -273,6 +343,11 @@ static OperatorGroup getSystemOperatorGroup() {
return getOperatorGroup(systemOperatorKey);
}

/**
* base on operator key gain {@link OperatorGroup}
*
* @return {@link OperatorGroup} or null
*/
static OperatorGroup getOperatorGroup(OperatorKey operatorKey) {
if (operatorKey != null) {
return new OperatorGroupImpl(operatorKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,42 @@ private static <T> T ifNullThenAgain(SupplierAction<T> action, VoidConsumer comp
/**
* 基于SPI加载{@link Operator}的类型,放入缓存
*/
private static void loadOperatorBySPI(Class<? extends Operator<?>> operatorClazz) {
ServiceLoader.load(operatorClazz, ClassLoader.getSystemClassLoader())
.stream()
.forEach(provider -> {
Class<? extends Operator<?>> type = provider.type();
Operator.Group group = AnnotationUtils.findAnnotation(type, Operator.Group.class);
if (group == null) {
throw new IllegalArgumentException(String.format("Operator %s without Annotation @SQLOperator.Group", type.getName()));
}
String groupKey = group.value();
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
OperatorTraitGroup operatorTraitGroup = OTG_CACHES.computeIfAbsent(OperatorKey.returnKey(groupKey), k -> new OperatorTraitGroup());
if (log.isDebugEnabled()) {
log.debug("Through SPI load Operator Type [{}]", type.getName());
private static void loadOperatorBySPI(Class<? extends Operator<?>> operatorClass) {
ServiceLoader<? extends Operator<?>> loader = ServiceLoader.load(operatorClass, ClassLoader.getSystemClassLoader());
boolean present = loader.stream().findAny().isPresent();
if (!present) {
Class<?>[] interfaces = operatorClass.getInterfaces();
for (Class<?> hierachical : interfaces) {
// recursion of find parent class
// requirement parent class must be interfaces and subtype of Operator
if (Operator.class.isAssignableFrom(hierachical)) {
loadOperatorBySPI((Class<? extends Operator<?>>) hierachical);
break;
}
}
} else {
loader.reload();
loader.stream()
.forEach(provider -> {
Class<? extends Operator<?>> type = provider.type();
Operator.Group group = AnnotationUtils.findAnnotation(type, Operator.Group.class);
if (group == null) {
throw new IllegalArgumentException(String.format("Operator %s without Annotation @SQLOperator.Group", type.getName()));
}
operatorTraitGroup.append(type);
} finally {
writeLock.unlock();
}
});
String groupKey = group.value();
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
OperatorTraitGroup operatorTraitGroup = OTG_CACHES.computeIfAbsent(OperatorKey.returnKey(groupKey), k -> new OperatorTraitGroup());
if (log.isDebugEnabled()) {
log.debug("Through SPI load Operator Type [{}]", type.getName());
}
operatorTraitGroup.append(type);
} finally {
writeLock.unlock();
}
});
}
}

static class OperatorTraitGroup {
Expand All @@ -148,17 +162,33 @@ public OperatorTraitGroup() {
this.traits = Sets.newConcurrentHashSet();
}

/**
* by the operator class find OperatorTrait
*
* @param operatorClazz operatorClazz
* @return OperatorTrait or null
*/
public OperatorTrait find(Class<? extends Operator<?>> operatorClazz) {
// find operator class parent
Class<? extends Operator<?>> parent = Operator.getHireachialType(operatorClazz);
return this.traits.stream()
.filter(trait -> trait.getSuperClazz().equals(operatorClazz))
.filter(trait -> trait.getParent().equals(parent))
.findFirst()
.orElse(null);
}

/**
* @see #append(OperatorTrait)
*/
public void append(Class<? extends Operator<?>> implClazz) {
this.append(new OperatorTrait(implClazz));
}

/**
* with group append {@link OperatorTrait} instance
*
* @param trait trait must not nul
*/
public void append(OperatorTrait trait) {
if (trait == null) {
throw new IllegalArgumentException("parameter OperatorTrait is null");
Expand All @@ -171,26 +201,27 @@ public void append(OperatorTrait trait) {
}

@Data
@EqualsAndHashCode(of = "superClazz")
@EqualsAndHashCode(of = "parent")
static class OperatorTrait {

private final Class<? extends Operator<?>> clazz;
private Class<? extends Operator<?>> superClazz;
private Class<? extends Operator<?>> parent;

public OperatorTrait(Class<? extends Operator<?>> clazz) {
this.clazz = clazz;
Class<?>[] interfaces = this.clazz.getInterfaces();
for (Class<?> in : interfaces) {
if (Operator.class.isAssignableFrom(in)) {
this.superClazz = (Class<? extends Operator<?>>) in;
break;
}
}
if (superClazz == null) {
throw new DSLException(String.format("operator impl %s not implement any SQLOperator", clazz.getName()));
this.parent = Operator.getHireachialType(clazz);
if (this.parent == null) {
throw new DSLException(String.format("operator impl %s not implement any Operator", clazz.getName()));
}
}

/**
* new Operator instance by dbtype
*
* @param dbType dbType
* @param <T> generic Operator type
* @return Operator instance
*/
public <T extends Operator<?>> T newInstance(DBType dbType) {
Operator<?> sqlOperator;
try {
Expand All @@ -200,7 +231,7 @@ public <T extends Operator<?>> T newInstance(DBType dbType) {
sqlOperator = ClassUtils.newInstance(clazz, dbType);
}
} catch (Throwable ex) {
throw new DSLException(String.format("Instance SQLOperator %s failed, please invoke method Is it right?", clazz.getName()), ex);
throw new DSLException(String.format("Instance Operator %s failed, please invoke method Is it right?", clazz.getName()), ex);
}
return (T) sqlOperator;
}
Expand Down
Loading

0 comments on commit 76286d6

Please sign in to comment.