diff --git a/uno-core/src/main/java/cc/allio/uno/core/type/Types.java b/uno-core/src/main/java/cc/allio/uno/core/type/Types.java index d76c935a..e84b3bc6 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/type/Types.java +++ b/uno-core/src/main/java/cc/allio/uno/core/type/Types.java @@ -288,6 +288,9 @@ public static int signum(Object value) { * @return String */ public static String toString(Object value) { + if (value == null) { + return StringPool.EMPTY; + } return Optional.ofNullable(TypeOperatorFactory.translator(value.getClass())) .map(operator -> operator.fromString(value)) .orElse(value.toString()); diff --git a/uno-core/src/main/java/cc/allio/uno/core/util/DateUtil.java b/uno-core/src/main/java/cc/allio/uno/core/util/DateUtil.java index 20a768be..6b9a40c8 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/util/DateUtil.java +++ b/uno-core/src/main/java/cc/allio/uno/core/util/DateUtil.java @@ -869,4 +869,12 @@ public static boolean isBetween(Date nowTime, Date startTime, Date endTime) { return nowDate.after(startDate) && nowTime.before(endTime); } + /** + * get computer epoch time. it is 1970-01-01 08:00:00 + * + * @return the epoch time + */ + public static Date getEpochTime() { + return new Date(0); + } } diff --git a/uno-core/src/main/java/cc/allio/uno/core/util/Values.java b/uno-core/src/main/java/cc/allio/uno/core/util/Values.java index a494bc49..5d82a88c 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/util/Values.java +++ b/uno-core/src/main/java/cc/allio/uno/core/util/Values.java @@ -4,6 +4,8 @@ import java.util.Arrays; import java.util.Collection; +import java.util.function.Consumer; +import java.util.function.Supplier; import java.util.stream.Stream; /** @@ -81,4 +83,17 @@ public static Stream streamExpand(V... values) { return Stream.of(v); }); } + + + /** + * functional mapping getter to setter + *

for example: {@code mapping(User::getName, User::setName)}

+ * + * @param getter the getter function object + * @param setter the setter function object + * @param the value type + */ + public static void mapping(Supplier getter, Consumer setter) { + setter.accept(getter.get()); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ColumnDef.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ColumnDef.java index f6e66cef..45723107 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ColumnDef.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ColumnDef.java @@ -1,33 +1,36 @@ package cc.allio.uno.data.orm.dsl; import cc.allio.uno.core.api.EqualsTo; +import cc.allio.uno.core.api.Self; import cc.allio.uno.core.type.TypeOperatorFactory; import cc.allio.uno.data.orm.dsl.type.DataType; -import lombok.Builder; -import lombok.Data; -import lombok.EqualsAndHashCode; +import cc.allio.uno.data.orm.dsl.type.JavaType; +import cc.allio.uno.data.orm.dsl.type.TypeRegistry; +import lombok.*; import java.util.Objects; import java.util.Optional; /** - * SQL字段定义 + * DSL字段定义 * * @author j.x * @date 2023/4/12 19:35 + * @see ColumnDefBuilder * @since 1.1.4 */ @Data -@Builder @EqualsAndHashCode(of = {"dslName", "dataType"}) -public class ColumnDef implements EqualsTo { +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ColumnDef implements EqualsTo, Meta { // 字段名称 private DSLName dslName; // 字段注释 private String comment; // 数据类型 - private cc.allio.uno.data.orm.dsl.type.DataType dataType; + private DataType dataType; // 默认值 private Object defaultValue; // 是否为主键 @@ -47,12 +50,6 @@ public class ColumnDef implements EqualsTo { // 已删除值 private Object deleted; - @Override - public boolean equalsTo(ColumnDef other) { - return Objects.equals(this.getDslName(), other.getDslName()) - && dataType.equalsTo(other.getDataType()); - } - /** * 给定字段值按照当前{@link cc.allio.uno.data.orm.dsl.type.DataType}进行转换 * @@ -64,11 +61,210 @@ public Object castValue(Object ori) { .map(DataType::getDslType) .flatMap(dslType -> { int jdbcType = dslType.getJdbcType(); - cc.allio.uno.data.orm.dsl.type.JavaType javaType = cc.allio.uno.data.orm.dsl.type.TypeRegistry.getInstance().findJavaType(jdbcType); + JavaType javaType = TypeRegistry.getInstance().findJavaType(jdbcType); return Optional.ofNullable(javaType); }) .map(j -> TypeOperatorFactory.translator(j.getJavaType())) .map(typeOperator -> typeOperator.convert(ori)) .orElse(null); } + + + @Override + public boolean equalsTo(ColumnDef other) { + return Objects.equals(this.getDslName(), other.getDslName()) + && dataType.equalsTo(other.getDataType()); + } + + /** + * get {@link ColumnDefBuilder} builder + * + * @return the {@link ColumnDefBuilder} instance + */ + public static ColumnDefBuilder builder() { + return new ColumnDefBuilder(); + } + + /** + * {@link ColumnDef} builder + */ + public static class ColumnDefBuilder implements Self { + // 字段名称 + private DSLName dslName; + // 字段注释 + private String comment; + // 数据类型 + private DataType dataType; + // 默认值 + private Object defaultValue; + // 是否为主键 + private boolean isPk; + // 是否为外键 + private boolean isFk; + // 是否不为null + private boolean isNonNull; + // 是否为null + private boolean isNull; + // 是否唯一 + private boolean isUnique; + // 是否是删除标识字段 + private boolean isDeleted; + // 未删除值 + private Object undeleted; + // 已删除值 + private Object deleted; + + /** + * build column name + * + * @param dslName the dsl name + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder dslName(DSLName dslName) { + this.dslName = dslName; + return self(); + } + + /** + * build column comment + * + * @param comment the comment + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder comment(String comment) { + this.comment = comment; + return self(); + } + + /** + * build column data type + * + * @param dataType the data type + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder dataType(DataType dataType) { + this.dataType = dataType; + return self(); + } + + /** + * build column default value + * + * @param defaultValue the default value + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder defaultValue(Object defaultValue) { + this.defaultValue = defaultValue; + return self(); + } + + /** + * build column is pk + * + * @param isPk the is pk + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder isPk(Boolean isPk) { + this.isPk = isPk; + return self(); + } + + /** + * build column is fk + * + * @param isFk the is fk + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder isFk(Boolean isFk) { + this.isFk = isFk; + return self(); + } + + /** + * build column is non null + * + * @param isNonNull the is non null + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder isNonNull(Boolean isNonNull) { + this.isNonNull = isNonNull; + return self(); + } + + /** + * build column is null + * + * @param isNull the is null + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder isNull(Boolean isNull) { + this.isNull = isNull; + return self(); + } + + /** + * build column is unique + * + * @param isUnique the is unique + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder isUnique(Boolean isUnique) { + this.isUnique = isUnique; + return self(); + } + + /** + * build column is deleted + * + * @param isDeleted the is deleted + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder isDeleted(Boolean isDeleted) { + this.isDeleted = isDeleted; + return self(); + } + + /** + * build undeleted + * + * @param undeleted the undeleted + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder undeleted(Object undeleted) { + this.undeleted = undeleted; + return self(); + } + + /** + * build deleted + * + * @param deleted the deleted + * @return {@link ColumnDefBuilder} + */ + public ColumnDefBuilder deleted(Object deleted) { + this.deleted = deleted; + return self(); + } + + /** + * build {@link ColumnDef} + * + * @return {@link ColumnDef} + */ + public ColumnDef build() { + return new ColumnDef( + dslName, + comment, + dataType, + defaultValue, + isPk, + isFk, + isNonNull, + isNull, + isUnique, + isDeleted, + undeleted, + deleted); + } + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java index 87b7a021..e4dbd9bc 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java @@ -5,6 +5,7 @@ import cc.allio.uno.core.util.StringUtils; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; import java.util.Objects; import java.util.regex.Pattern; @@ -17,8 +18,9 @@ * @since 1.1.4 */ @Getter +@Setter @EqualsAndHashCode(of = "name") -public class DSLName implements Comparable, EqualsTo { +public class DSLName implements Comparable, EqualsTo, Meta { // 当前存入的dsl name private String name; @@ -34,7 +36,7 @@ public class DSLName implements Comparable, EqualsTo { /** * 名称格式化key */ - private static final String NAME_FORMAT_KEY = "allio.uno.data.orm.sql.name.format"; + private static final String NAME_FORMAT_KEY = "allio.uno.data.orm.dsl.name.format"; // 名称以下划线 private static final String NAME_FORMAT_UNDERLINE = "underline"; // 名称以驼峰 @@ -52,6 +54,24 @@ public class DSLName implements Comparable, EqualsTo { public static final NameFeature LOWER_CASE_FEATURE = new LowerCaseFeature(); public static final NameFeature UPPER_CASE_FEATURE = new UpperCaseFeature(); + public DSLName() { + + } + + public DSLName(String name) { + this(name, getNameFeature()); + } + + public DSLName(String name, NameFeature nameFeature) { + this.name = name; + this.feature = nameFeature; + } + + public DSLName(DSLName dslName, NameFeature... nameFeatures) { + this.name = dslName.format(); + this.feature = NameFeature.aggregate(nameFeatures); + } + private void setFeature(NameFeature... features) { this.feature = new AggregateNameFeature(features); } @@ -93,31 +113,10 @@ public boolean equalsTo(DSLName other) { } /** - * 获取name feature - * - * @return NameFeature or default {@link #PLAIN_FEATURE} - */ - public static NameFeature getNameFeature() { - String nameFormatKey = Envs.getProperty(NAME_FORMAT_KEY); - if (NAME_FORMAT_UNDERLINE.equals(nameFormatKey)) { - return UNDERLINE_FEATURE; - } else if (NAME_FORM_HUMP.equals(nameFormatKey)) { - return HUMP_FEATURE; - } else if (NAME_FORM_PLAIN.equals(nameFormatKey)) { - return PLAIN_FEATURE; - } else if (NAME_FORM_LOWER_CASE.equals(nameFormatKey)) { - return LOWER_CASE_FEATURE; - } else if (NAME_FORM_UPPER_CASE.equals(nameFormatKey)) { - return UPPER_CASE_FEATURE; - } - return UNDERLINE_FEATURE; - } - - /** - * 创建SQLName实例 + * 创建{@link DSLName}实例 * * @param name name - * @return SQLName + * @return {@link DSLName} */ public static DSLName of(String name) { return of(name, getNameFeature()); @@ -171,6 +170,27 @@ public static String toUnderline(String hump) { return DSLName.of(hump, UNDERLINE_FEATURE).format(); } + /** + * 获取name feature + * + * @return NameFeature or default {@link #PLAIN_FEATURE} + */ + public static NameFeature getNameFeature() { + String nameFormatKey = Envs.getProperty(NAME_FORMAT_KEY); + if (NAME_FORMAT_UNDERLINE.equals(nameFormatKey)) { + return UNDERLINE_FEATURE; + } else if (NAME_FORM_HUMP.equals(nameFormatKey)) { + return HUMP_FEATURE; + } else if (NAME_FORM_PLAIN.equals(nameFormatKey)) { + return PLAIN_FEATURE; + } else if (NAME_FORM_LOWER_CASE.equals(nameFormatKey)) { + return LOWER_CASE_FEATURE; + } else if (NAME_FORM_UPPER_CASE.equals(nameFormatKey)) { + return UPPER_CASE_FEATURE; + } + return UNDERLINE_FEATURE; + } + /** * 名称特性 */ diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Database.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Database.java index f6b9a5a1..20eb88b0 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Database.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Database.java @@ -1,6 +1,5 @@ package cc.allio.uno.data.orm.dsl; -import lombok.AllArgsConstructor; import lombok.Data; /** @@ -11,12 +10,30 @@ * @since 1.1.7 */ @Data -@AllArgsConstructor(staticName = "of") -public class Database { +public class Database implements Meta { /** * 数据库名 */ private DSLName name; + public Database() { + + } + + public Database(DSLName dslName) { + this.name = dslName; + } + + /** + * create {@link Database} instance + * + * @param dslName the dsl name + * @return {@link Database} instance + */ + public static Database of(DSLName dslName) { + Database database = new Database(); + database.setName(dslName); + return database; + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Meta.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Meta.java new file mode 100644 index 00000000..2267eb60 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Meta.java @@ -0,0 +1,59 @@ +package cc.allio.uno.data.orm.dsl; + +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.util.ClassUtils; + +/** + * definition 'Meta data' + * + * @author j.x + * @date 2024/4/16 15:05 + * @since 1.1.8 + */ +public interface Meta> { + + /** + * meta accept {@link MetaAcceptor} + * + * @param acceptor the acceptor instance + */ + default void accept(MetaAcceptor acceptor) { + if (acceptor != null) { + acceptor.onAccept((T) this); + } + } + + /** + * dynamic create meta instance by {@link Class} metaclass and args + * + * @param metaClass the metaclass + * @param args the args + * @param

the meta subtype + * @return {@link Meta} instance + */ + static

> P create(Class

metaClass, Object... args) { + return ClassUtils.newInstance(metaClass, args); + } + + /** + * dynamic create meta instance by {@link Class} {@link Meta} and args. + * this method will be through {@link MetaAcceptor} enhance meta data + * + * @param metaClass the metaclass + * @param metaAcceptorSet the {@link MetaAcceptorSet} instance + * @param args the args + * @param

the meta subtype + * @return {@link Meta} instance + * @throws NullPointerException if create metaclass is empty + */ + static

> P create(Class

metaClass, MetaAcceptorSet metaAcceptorSet, Object... args) { + P meta = ClassUtils.newInstance(metaClass, args); + if (meta == null) { + throw Exceptions.unNull(String.format("Failed to create %s meta instance", metaClass.getName())); + } + if (metaAcceptorSet != null) { + metaAcceptorSet.adapt(meta); + } + return meta; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/MetaAcceptor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/MetaAcceptor.java new file mode 100644 index 00000000..b4f6ee41 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/MetaAcceptor.java @@ -0,0 +1,25 @@ +package cc.allio.uno.data.orm.dsl; + +/** + * definition about dsl meta data acceptor + *

the meta data contains

+ *
    + *
  • {@link Table}
  • + *
  • {@link Database}
  • + *
  • {@link ColumnDef}
  • + *
  • {@link DSLName}
  • + *
+ * + * @author j.x + * @date 2024/4/16 15:03 + * @since 1.1.8 + */ +public interface MetaAcceptor> { + + /** + * trigger accept event + * + * @param meta the meta + */ + void onAccept(T meta); +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/MetaAcceptorSet.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/MetaAcceptorSet.java new file mode 100644 index 00000000..0ce51dc1 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/MetaAcceptorSet.java @@ -0,0 +1,132 @@ +package cc.allio.uno.data.orm.dsl; + +import java.util.Collection; + +/** + * {@link MetaAcceptor} Set + * + * @author j.x + * @date 2024/4/16 15:36 + * @since 1.1.8 + */ +public interface MetaAcceptorSet { + + /** + * obtain {@link ColumnDef} {@link MetaAcceptor} + * + * @return the {@link MetaAcceptor} + */ + default MetaAcceptor obtainColumnDefAcceptor() { + return customizeMetaAcceptorGetter(ColumnDef.class); + } + + /** + * set {@link ColumnDef} {@link MetaAcceptor} + * + * @param columnDefAcceptor the column def acceptor + */ + default void setColumnDefAcceptor(MetaAcceptor columnDefAcceptor) { + customizeMetaAcceptorSetter(ColumnDef.class, columnDefAcceptor); + } + + /** + * obtain {@link Database} {@link MetaAcceptor} + * + * @return the {@link MetaAcceptor} + */ + default MetaAcceptor obtainDatabaseAcceptor() { + return customizeMetaAcceptorGetter(Database.class); + } + + /** + * set {@link Database} {@link MetaAcceptor} + * + * @param databaseDefAcceptor the database acceptor + */ + default void setDatabaseDefAcceptor(MetaAcceptor databaseDefAcceptor) { + customizeMetaAcceptorSetter(Database.class, databaseDefAcceptor); + } + + /** + * obtain {@link DSLName} {@link MetaAcceptor} + * + * @return the {@link MetaAcceptor} + */ + default MetaAcceptor obtainDSLNameAcceptor() { + return customizeMetaAcceptorGetter(DSLName.class); + } + + /** + * set {@link DSLName} {@link MetaAcceptor} + * + * @param dslNameMetaAcceptor the dsl name acceptor + */ + default void setDSLNameAcceptor(MetaAcceptor dslNameMetaAcceptor) { + customizeMetaAcceptorSetter(DSLName.class, dslNameMetaAcceptor); + } + + /** + * obtain {@link Table} {@link MetaAcceptor} + * + * @return the {@link MetaAcceptor} + */ + default MetaAcceptor obtainTableAcceptor() { + return customizeMetaAcceptorGetter(Table.class); + } + + /** + * set {@link Table} {@link MetaAcceptor} + * + * @param tableAcceptor the table acceptor + */ + default void setTableAcceptor(MetaAcceptor
tableAcceptor) { + customizeMetaAcceptorSetter(Table.class, tableAcceptor); + } + + /** + * customize meta acceptor setter. + * + * @param metaClass the meta class + * @param metaAcceptor the meta acceptor + * @param the sub {@link Meta} + */ + > void customizeMetaAcceptorSetter(Class metaClass, MetaAcceptor metaAcceptor); + + /** + * customize meta acceptor getter + * + * @param metaClass the metaclass + * @param the meta subtype + * @return the {@link MetaAcceptor} instance + */ + > MetaAcceptor customizeMetaAcceptorGetter(Class metaClass); + + /** + * batch handle {@link Meta} {@link MetaAcceptor} + * + * @param meta the {@link Meta} instance + * @param the {@link Meta} type + * @see #adapt(Meta) + */ + default > void adapt(Collection meta) { + meta.forEach(this::adapt); + } + + /** + * choose adaption {@link Meta} {@link MetaAcceptor} + * + * @param meta the {@link Meta} instance + * @param the {@link Meta} type + */ + default > void adapt(T meta) { + if (meta != null) { + MetaAcceptor acceptor = customizeMetaAcceptorGetter(meta.getClass()); + meta.accept(acceptor); + } + } + + /** + * clear all {@link MetaAcceptor} + */ + void clear(); +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java index c91713d7..a1bf1c6e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Operator.java @@ -1,11 +1,14 @@ package cc.allio.uno.data.orm.dsl; +import cc.allio.uno.core.api.Self; import cc.allio.uno.core.bean.ValueWrapper; +import cc.allio.uno.data.orm.dsl.opeartorgroup.WrapperOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import java.lang.annotation.*; import java.util.Objects; import java.util.function.Supplier; +import java.util.function.UnaryOperator; /** * DSL操作 @@ -14,7 +17,7 @@ * @date 2023/4/12 19:44 * @since 1.1.4 */ -public interface Operator> { +public interface Operator> extends Self { /** * 获取DSL字符串 @@ -48,6 +51,14 @@ default T customize(String dsl) { return parse(dsl); } + /** + * support operatorFunc make be able customization. + * + * @param operatorFunc create new self operatorFunc, and invoker customize some operate.. + * @return self + */ + T customize(UnaryOperator operatorFunc); + /** * reset operator */ @@ -85,9 +96,24 @@ default Object getValueIfNull(Object originalValue) { * @throws ClassCastException failed to cast to specifies reality type */ default > O castReality(Class realityType) { + if (this instanceof WrapperOperator wrapperOperator) { + T actual = wrapperOperator.getActual(); + return realityType.cast(actual); + } return realityType.cast(this); } + /** + * obtain {@link MetaAcceptorSet}, the default is empty. + *

probable implement adopt proxy way.

+ *

otherwise will be adopt customize implementation

+ * + * @return the {@link MetaAcceptorSet} instance, default is null + */ + default MetaAcceptorSet obtainMetaAcceptorSet() { + return null; + } + /** * according to operator type, this type maybe is subtype, so translate specifies operator type *

rules

diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/OperatorGroup.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/OperatorGroup.java deleted file mode 100644 index 2dd95757..00000000 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/OperatorGroup.java +++ /dev/null @@ -1,462 +0,0 @@ -package cc.allio.uno.data.orm.dsl; - -import cc.allio.uno.data.orm.dsl.ddl.*; -import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; -import cc.allio.uno.data.orm.dsl.dml.InsertOperator; -import cc.allio.uno.data.orm.dsl.dml.QueryOperator; -import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; -import cc.allio.uno.data.orm.dsl.type.DBType; -import lombok.AllArgsConstructor; -import lombok.Data; - -/** - * 操作管理接口 - * - * @author j.x - * @date 2023/4/13 18:52 - * @see Operator - * @since 1.1.4 - */ -public interface OperatorGroup { - - // ======================== DML ======================== - - /** - * 获取查询操作 - * - * @return QueryOperator - */ - default QueryOperator query() { - return query(DBType.getSystemDbType()); - } - - /** - * 获取查询操作 - * - * @param dbType dbType - * @return QueryOperator - */ - QueryOperator query(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getQueryOperator(Class queryOperatorClass, OperatorKey operatorKey) { - return getOperator(queryOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getQueryOperator(Class queryOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(queryOperatorClass, operatorKey, dbType); - } - - /** - * 获取insert操作 - * - * @return InsertOperator - */ - default InsertOperator insert() { - return insert(DBType.getSystemDbType()); - } - - /** - * 获取insert操作 - * - * @param dbType dbType - * @return InsertOperator - */ - InsertOperator insert(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getInsertOperator(Class insertOperatorClass, OperatorKey operatorKey) { - return getOperator(insertOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getInsertOperator(Class insertOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(insertOperatorClass, operatorKey, dbType); - } - - /** - * 获取update操作 - * - * @return SQLUpdateOperator - */ - default UpdateOperator update() { - return update(DBType.getSystemDbType()); - } - - /** - * 获取update操作 - * - * @param dbType dbType - * @return SQLUpdateOperator - */ - UpdateOperator update(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getUpdateOperator(Class updateOperatorClass, OperatorKey operatorKey) { - return getOperator(updateOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getUpdateOperator(Class updateOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(updateOperatorClass, operatorKey, dbType); - } - - /** - * 获取delete操作 - * - * @return SQLDeleteOperator - */ - default DeleteOperator delete() { - return delete(DBType.getSystemDbType()); - } - - /** - * 获取delete操作 - * - * @return SQLDeleteOperator - */ - DeleteOperator delete(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getDeleteOperator(Class deleteOperatorClass, OperatorKey operatorKey) { - return getOperator(deleteOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getDeleteOperator(Class deleteOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(deleteOperatorClass, operatorKey, dbType); - } - - // ======================== DDL ======================== - - /** - * registry table操作 - * - * @return CreateTableOperator - */ - default CreateTableOperator createTable() { - return createTable(DBType.getSystemDbType()); - } - - /** - * registry table操作 - * - * @param dbType dbType - * @return CreateTableOperator - */ - CreateTableOperator createTable(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getCreateTableOperator(Class createTableOperatorClass, OperatorKey operatorKey) { - return getOperator(createTableOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getCreateTableOperator(Class createTableOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(createTableOperatorClass, operatorKey, dbType); - } - - /** - * drop xxxx - * - * @return DropTableOperator - */ - default DropTableOperator dropTable() { - return dropTable(DBType.getSystemDbType()); - } - - /** - * drop xxxx - * - * @param dbType dbType - * @return DropTableOperator - */ - DropTableOperator dropTable(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getDropTableOperator(Class dropTableOperatorClass, OperatorKey operatorKey) { - return getOperator(dropTableOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getDropTableOperator(Class dropTableOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(dropTableOperatorClass, operatorKey, dbType); - } - - /** - * exist xxxx - * - * @return ExistTableOperator - */ - default ExistTableOperator existTable() { - return existTable(DBType.getSystemDbType()); - } - - /** - * exist xxxx - * - * @param dbType dbType - * @return ExistTableOperator - */ - ExistTableOperator existTable(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getExistTableOperator(Class existTableOperatorClass, OperatorKey operatorKey) { - return getOperator(existTableOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getExistTableOperator(Class existTableOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(existTableOperatorClass, operatorKey, dbType); - } - - /** - * show columns for xxxx - * - * @return ShowColumnsOperator - */ - default ShowColumnsOperator showColumns() { - return showColumns(DBType.getSystemDbType()); - } - - /** - * show columns for xxxx - * - * @param dbType dbType - * @return ShowColumnsOperator - */ - ShowColumnsOperator showColumns(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getShowColumnsOperator(Class showColumnsOperatorClass, OperatorKey operatorKey) { - return getOperator(showColumnsOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getShowColumnsOperator(Class showColumnsOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(showColumnsOperatorClass, operatorKey, dbType); - } - - /** - * show tables - * - * @return ShowTablesOperator - */ - default ShowTablesOperator showTables() { - return showTables(DBType.getSystemDbType()); - } - - /** - * show tables - * - * @param dbType dbType - * @return ShowTablesOperator - */ - ShowTablesOperator showTables(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getShowTablesOperator(Class showTablesOperatorClass, OperatorKey operatorKey) { - return getOperator(showTablesOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getShowTablesOperator(Class showTablesOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(showTablesOperatorClass, operatorKey, dbType); - } - - /** - * @see #alterTables(DBType) - */ - default AlterTableOperator alterTables() { - return alterTables(DBType.getSystemDbType()); - } - - /** - * alert tables - * - * @param dbType dbType - * @return AlterTableOperator - */ - AlterTableOperator alterTables(DBType dbType); - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getAlterTableOperator(Class alterTableOperatorClass, OperatorKey operatorKey) { - return getOperator(alterTableOperatorClass, operatorKey, null); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static T getAlterTableOperator(Class alterTableOperatorClass, OperatorKey operatorKey, DBType dbType) { - return getOperator(alterTableOperatorClass, operatorKey, dbType); - } - - /** - * 获取当前系统的OperatorGroup - * - * @see #getOperatorGroup(OperatorKey) - */ - static OperatorGroup getSystemOperatorGroup() { - OperatorKey systemOperatorKey = OperatorKey.getSystemOperatorKey(); - if (systemOperatorKey == null) { - return null; - } - 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); - } - return null; - } - - /** - * 根据operator的class获取指定operator实例. - *
    - *
  • operator key = {@link OperatorKey#getSystemOperatorKey()}
  • - *
  • dbtype = {@link DBType#getSystemDbType()}
  • - *
- * - * @param operatorClass operatorClass - * @param SQLOperator - * @return SQLOperator - * @throws IllegalArgumentException operatorClass is null - * @throws NullPointerException An operation that does not exist - */ - static > T getOperator(Class operatorClass) { - return getOperator(operatorClass, OperatorKey.getSystemOperatorKey(), DBType.getSystemDbType()); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static > T getOperator(Class operatorClass, OperatorKey operatorKey) { - return getOperator(operatorClass, operatorKey, DBType.getSystemDbType()); - } - - /** - * @see #getOperator(Class, OperatorKey, DBType) - */ - static > T getOperator(Class operatorClass, DBType dbType) { - return getOperator(operatorClass, OperatorKey.getSystemOperatorKey(), dbType); - } - - /** - * 根据operator的class获取指定operator实例 - * - * @param operatorClass operatorClass - * @param operatorKey operatorKey - * @param dbType dbtype - * @param generic of Operator type - * @return Operator - * @throws IllegalArgumentException operatorClass is null - * @throws NullPointerException An operation that does not exist - * @see OperatorKey - * @see SPIOperatorHelper#lazyGet(Class, OperatorKey, DBType) - */ - static > T getOperator(Class operatorClass, OperatorKey operatorKey, DBType dbType) { - return SPIOperatorHelper.lazyGet(operatorClass, operatorKey, dbType); - } - - @Data - @AllArgsConstructor - class OperatorGroupImpl implements OperatorGroup { - - private final OperatorKey key; - - @Override - public QueryOperator query(DBType dbType) { - return SPIOperatorHelper.lazyGet(QueryOperator.class, key, dbType); - } - - @Override - public InsertOperator insert(DBType dbType) { - return SPIOperatorHelper.lazyGet(InsertOperator.class, key, dbType); - } - - @Override - public UpdateOperator update(DBType dbType) { - return SPIOperatorHelper.lazyGet(UpdateOperator.class, key, dbType); - } - - @Override - public DeleteOperator delete(DBType dbType) { - return SPIOperatorHelper.lazyGet(DeleteOperator.class, key, dbType); - } - - @Override - public CreateTableOperator createTable(DBType dbType) { - return SPIOperatorHelper.lazyGet(CreateTableOperator.class, key, dbType); - } - - @Override - public DropTableOperator dropTable(DBType dbType) { - return SPIOperatorHelper.lazyGet(DropTableOperator.class, key, dbType); - } - - @Override - public ExistTableOperator existTable(DBType dbType) { - return SPIOperatorHelper.lazyGet(ExistTableOperator.class, key, dbType); - } - - @Override - public ShowColumnsOperator showColumns(DBType dbType) { - return SPIOperatorHelper.lazyGet(ShowColumnsOperator.class, key, dbType); - } - - @Override - public ShowTablesOperator showTables(DBType dbType) { - return SPIOperatorHelper.lazyGet(ShowTablesOperator.class, key, dbType); - } - - @Override - public AlterTableOperator alterTables(DBType dbType) { - return SPIOperatorHelper.lazyGet(AlterTableOperator.class, key, dbType); - } - } -} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/PrepareOperatorImpl.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/PrepareOperatorImpl.java index 62688bb1..d1329f61 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/PrepareOperatorImpl.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/PrepareOperatorImpl.java @@ -14,6 +14,7 @@ * @since 1.1.4 */ public abstract class PrepareOperatorImpl> implements PrepareOperator { + private static final int DEFAULT_CAPACITY = 0; protected PrepareValue[] prepareValues; protected int prepareIndex; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java index 2ade0907..5695d7bc 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/SPIOperatorHelper.java @@ -63,9 +63,11 @@ public static > T lazyGet(Class operatorClass, Operator readLock.lock(); operatorTrait = operatorTraitGroup.find(operatorClass); if (operatorTrait == null) { - throw new DSLException(String.format("On the basis of SPI load operator %s and %s, " + - "but not found counterpart Impl, " + - "Please Check the Impl Has Annotation @AutoService", operatorKey.key(), operatorClass.getName())); + throw new DSLException( + String.format( + "On the basis of SPI load operator %s and %s, " + + "but not found counterpart Impl, " + + "Please Check the Impl Has Annotation @AutoService", operatorKey.key(), operatorClass.getName())); } } finally { readLock.unlock(); diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Table.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Table.java index 2f006353..afd08e18 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Table.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/Table.java @@ -12,7 +12,7 @@ */ @Data @Accessors(chain = true) -public class Table { +public class Table implements Meta
{ /** * 表名 @@ -28,6 +28,27 @@ public class Table { private String type; private String comment; + public Table() { + } + + public Table(String name) { + this.name = DSLName.of(name); + } + + public Table(String name, String alias) { + this.name = DSLName.of(name); + this.alias = alias; + } + + public Table(DSLName dslName) { + this.name = dslName; + } + + public Table(DSLName dslName, String alias) { + this.name = dslName; + this.alias = alias; + } + /** * 根据表名创建{@link Table}实例 * @@ -39,22 +60,40 @@ public static Table of(String name) { return table; } + /** + * create {@link Table} by table name and table alias + * + * @param name the name + * @param alias the alias + * @return table instance + */ public static Table of(String name, String alias) { - Table table = new Table(); - table.setName(DSLName.of(name)); + Table table = of(name); table.setAlias(alias); return table; } + /** + * create {@link Table} by table {@link DSLName} + * + * @param name the dsl name + * @return table instance + */ public static Table of(DSLName name) { return of(name, null); } + /** + * creat {@link Table} by table {@link DSLName} and table alias + * + * @param name the name + * @param alias the alias + * @return table instance + */ public static Table of(DSLName name, String alias) { Table table = new Table(); table.setName(name); table.setAlias(alias); return table; } - -} +} \ No newline at end of file diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/TableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/TableOperator.java index d09b352d..9f15575c 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/TableOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/TableOperator.java @@ -1,6 +1,5 @@ package cc.allio.uno.data.orm.dsl; -import cc.allio.uno.core.api.Self; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; import java.util.function.UnaryOperator; @@ -12,16 +11,17 @@ * @date 2023/4/16 18:07 * @since 1.1.4 */ -public interface TableOperator> extends Self { +public interface TableOperator> extends Operator { /** * FROM XX * - * @param name xxxx name + * @param entityClass entityClass * @return Operator */ - default T from(String name) { - return from(Table.of(name)); + default

T from(Class

entityClass) { + String table = PojoWrapper.findTable(entityClass); + return from(table); } /** @@ -30,29 +30,32 @@ default T from(String name) { * @param name xxxx name * @return Operator */ - default T from(DSLName name) { - return from(Table.of(name)); + default T from(String name) { + Table table = Table.of(name); + return from(table); } /** * FROM XX * - * @param name xxxx name - * @param alias xxxx alias + * @param name xxxx name * @return Operator */ - default T from(String name, String alias) { - return from(Table.of(name, alias)); + default T from(DSLName name) { + Table table = Table.of(name); + return from(table); } /** * FROM XX * - * @param entityClass entityClass + * @param name xxxx name + * @param alias xxxx alias * @return Operator */ - default

T from(Class

entityClass) { - return from(PojoWrapper.findTable(entityClass)); + default T from(String name, String alias) { + Table table = Table.of(name, alias); + return from(table); } /** @@ -62,7 +65,8 @@ default

T from(Class

entityClass) { * @return Operator */ default T from(UnaryOperator

func) { - return from(func.apply(new Table())); + Table table = new Table(); + return from(func.apply(table)); } /** diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/WhereOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/WhereOperator.java index b97b544b..2f777229 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/WhereOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/WhereOperator.java @@ -4,7 +4,32 @@ import cc.allio.uno.core.function.lambda.MethodReferenceColumn; /** - * where eq1=eq2... + * where condition, contains following operate: + *
    + *
  1. {@link #gt(DSLName, Object)}
  2. + *
  3. {@link #gte(DSLName, Object)}
  4. + *
  5. {@link #lt(DSLName, Object)}
  6. + *
  7. {@link #lte(DSLName, Object)}
  8. + *
  9. {@link #eq(DSLName, Object)}
  10. + *
  11. {@link #neq(DSLName, Object)}
  12. + *
  13. {@link #notNull(DSLName)}
  14. + *
  15. {@link #isNull(DSLName)}
  16. + *
  17. {@link #in(DSLName, Object[])}
  18. + *
  19. {@link #notIn(DSLName, Object[])}
  20. + *
  21. {@link #between(DSLName, Object, Object)}
  22. + *
  23. {@link #notBetween(DSLName, Object, Object)}
  24. + *
  25. {@link #like(DSLName, Object)}
  26. + *
  27. {@link #$like(DSLName, Object)}
  28. + *
  29. {@link #like$(DSLName, Object)}
  30. + *
  31. {@link #$like$(DSLName, Object)}
  32. + *
  33. {@link #notLike(DSLName, Object)}
  34. + *
  35. {@link #$notLike(DSLName, Object)}
  36. + *
  37. {@link #notLike$(DSLName, Object)}
  38. + *
  39. {@link #$notLike$(DSLName, Object)}
  40. + *
  41. {@link #or()}
  42. + *
  43. {@link #and()}
  44. + *
  45. {@link #nor()}
  46. + *
* * @author j.x * @date 2023/4/16 18:14 @@ -17,7 +42,7 @@ public interface WhereOperator> extends Self { * * @param reference 方法引用 * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T gt(MethodReferenceColumn reference, Object value) { return gt(reference.getColumn(), value); @@ -28,7 +53,7 @@ default T gt(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T gt(String fieldName, Object value) { return gt(DSLName.of(fieldName), value); @@ -39,7 +64,7 @@ default T gt(String fieldName, Object value) { * * @param dslName dslName * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ T gt(DSLName dslName, Object value); @@ -48,7 +73,7 @@ default T gt(String fieldName, Object value) { * * @param reference 方法引用 * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T gte(MethodReferenceColumn reference, Object value) { return gte(reference.getColumn(), value); @@ -59,7 +84,7 @@ default T gte(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T gte(String fieldName, Object value) { return gte(DSLName.of(fieldName), value); @@ -70,7 +95,7 @@ default T gte(String fieldName, Object value) { * * @param dslName dslName * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ T gte(DSLName dslName, Object value); @@ -79,7 +104,7 @@ default T gte(String fieldName, Object value) { * * @param reference 方法引用 * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T lt(MethodReferenceColumn reference, Object value) { return lt(reference.getColumn(), value); @@ -90,7 +115,7 @@ default T lt(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T lt(String fieldName, Object value) { return lt(DSLName.of(fieldName), value); @@ -101,7 +126,7 @@ default T lt(String fieldName, Object value) { * * @param dslName dslName * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ T lt(DSLName dslName, Object value); @@ -110,7 +135,7 @@ default T lt(String fieldName, Object value) { * * @param reference 方法引用 * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T lte(MethodReferenceColumn reference, Object value) { return lte(reference.getColumn(), value); @@ -121,7 +146,7 @@ default T lte(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T lte(String fieldName, Object value) { return lte(DSLName.of(fieldName), value); @@ -132,7 +157,7 @@ default T lte(String fieldName, Object value) { * * @param dslName dslName * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ T lte(DSLName dslName, Object value); @@ -141,7 +166,7 @@ default T lte(String fieldName, Object value) { * * @param reference 方法引用 * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T eq(MethodReferenceColumn reference, Object value) { return eq(reference.getColumn(), value); @@ -152,7 +177,7 @@ default T eq(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T eq(String fieldName, Object value) { return eq(DSLName.of(fieldName), value); @@ -163,7 +188,7 @@ default T eq(String fieldName, Object value) { * * @param dslName dslName * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ T eq(DSLName dslName, Object value); @@ -172,7 +197,7 @@ default T eq(String fieldName, Object value) { * * @param reference 方法引用 * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T neq(MethodReferenceColumn reference, Object value) { return neq(reference.getColumn(), value); @@ -183,7 +208,7 @@ default T neq(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ default T neq(String fieldName, Object value) { return neq(DSLName.of(fieldName), value); @@ -194,7 +219,7 @@ default T neq(String fieldName, Object value) { * * @param dslName dslName * @param value 比较数据值 - * @return SQLWhereOperator + * @return self */ T neq(DSLName dslName, Object value); @@ -202,7 +227,7 @@ default T neq(String fieldName, Object value) { * is not null condition * * @param reference 方法引用 - * @return SQLWhereOperator + * @return self */ default T notNull(MethodReferenceColumn reference) { return notNull(reference.getColumn()); @@ -212,7 +237,7 @@ default T notNull(MethodReferenceColumn reference) { * is not null condition * * @param fieldName java variable name - * @return SQLWhereOperator + * @return self */ default T notNull(String fieldName) { return notNull(DSLName.of(fieldName)); @@ -222,7 +247,7 @@ default T notNull(String fieldName) { * is not null condition * * @param dslName dslName - * @return SQLWhereOperator + * @return self */ T notNull(DSLName dslName); @@ -267,7 +292,7 @@ default T in(String fieldName, V... values) { * * @param dslName dslName * @param values 数值数据 - * @return SQLWhereOperator + * @return self */ T in(DSLName dslName, V... values); @@ -290,7 +315,7 @@ default T notIn(String fieldName, V... values) { * * @param dslName dslName * @param values 数值数据 - * @return SQLWhereOperator + * @return self */ T notIn(DSLName dslName, V... values); @@ -300,7 +325,7 @@ default T notIn(String fieldName, V... values) { * @param reference 方法引用 * @param withValue between起始值 * @param endValue between结束值 - * @return SQLWhereOperator + * @return self */ default T between(MethodReferenceColumn reference, Object withValue, Object endValue) { return between(reference.getColumn(), withValue, endValue); @@ -312,7 +337,7 @@ default T between(MethodReferenceColumn reference, Object withValue, Obje * @param fieldName java variable name * @param withValue between起始值 * @param endValue between结束值 - * @return SQLWhereOperator + * @return self */ default T between(String fieldName, Object withValue, Object endValue) { return between(DSLName.of(fieldName), withValue, endValue); @@ -324,7 +349,7 @@ default T between(String fieldName, Object withValue, Object endValue) { * @param dslName dslName * @param withValue between起始值 * @param endValue between结束值 - * @return SQLWhereOperator + * @return self */ T between(DSLName dslName, Object withValue, Object endValue); @@ -334,7 +359,7 @@ default T between(String fieldName, Object withValue, Object endValue) { * @param reference 方法引用 * @param withValue between起始值 * @param endValue between结束值 - * @return SQLWhereOperator + * @return self */ default T notBetween(MethodReferenceColumn reference, Object withValue, Object endValue) { return notBetween(reference.getColumn(), withValue, endValue); @@ -346,7 +371,7 @@ default T notBetween(MethodReferenceColumn reference, Object withValue, O * @param fieldName java variable name * @param withValue between起始值 * @param endValue between结束值 - * @return SQLWhereOperator + * @return self */ default T notBetween(String fieldName, Object withValue, Object endValue) { return notBetween(DSLName.of(fieldName), withValue, endValue); @@ -358,7 +383,7 @@ default T notBetween(String fieldName, Object withValue, Object endValue) { * @param dslName dslName * @param withValue between起始值 * @param endValue between结束值 - * @return SQLWhereOperator + * @return self */ T notBetween(DSLName dslName, Object withValue, Object endValue); @@ -367,7 +392,7 @@ default T notBetween(String fieldName, Object withValue, Object endValue) { * * @param reference 方法引用 * @param value like值 - * @return SQLWhereOperator + * @return self */ default T like(MethodReferenceColumn reference, Object value) { return like(reference.getColumn(), value); @@ -378,7 +403,7 @@ default T like(MethodReferenceColumn reference, Object value) { * * @param reference 方法引用 * @param value like值 - * @return SQLWhereOperator + * @return self */ default T $like(MethodReferenceColumn reference, Object value) { return $like(reference.getColumn(), value); @@ -389,7 +414,7 @@ default T like(MethodReferenceColumn reference, Object value) { * * @param fieldName java variable name * @param value like值 - * @return SQLWhereOperator + * @return self */ default T like(String fieldName, Object value) { return like(DSLName.of(fieldName), value); @@ -400,7 +425,7 @@ default T like(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T like(DSLName dslName, Object value); @@ -409,7 +434,7 @@ default T like(String fieldName, Object value) { * * @param fieldName java variable name * @param value like值 - * @return SQLWhereOperator + * @return self */ default T $like(String fieldName, Object value) { return $like(DSLName.of(fieldName), value); @@ -420,7 +445,7 @@ default T like(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T $like(DSLName dslName, Object value); @@ -429,7 +454,7 @@ default T like(String fieldName, Object value) { * * @param reference 方法引用 * @param value like值 - * @return SQLWhereOperator + * @return self */ default T like$(MethodReferenceColumn reference, Object value) { return like$(reference.getColumn(), value); @@ -440,7 +465,7 @@ default T like(String fieldName, Object value) { * * @param fieldName java variable name * @param value like值 - * @return SQLWhereOperator + * @return self */ default T like$(String fieldName, Object value) { return like$(DSLName.of(fieldName), value); @@ -451,7 +476,7 @@ default T like(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T like$(DSLName dslName, Object value); @@ -461,7 +486,7 @@ default T like(String fieldName, Object value) { * * @param reference 方法引用 * @param value like值 - * @return SQLWhereOperator + * @return self */ default T $like$(MethodReferenceColumn reference, Object value) { return $like$(reference.getColumn(), value); @@ -472,7 +497,7 @@ default T like(String fieldName, Object value) { * * @param fieldName java variable name * @param value like值 - * @return SQLWhereOperator + * @return self */ default T $like$(String fieldName, Object value) { return $like$(DSLName.of(fieldName), value); @@ -483,7 +508,7 @@ default T like(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T $like$(DSLName dslName, Object value); @@ -506,7 +531,7 @@ default T notLike(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T notLike(DSLName dslName, Object value); @@ -529,7 +554,7 @@ default T notLike(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T $notLike(DSLName dslName, Object value); @@ -552,7 +577,7 @@ default T notLike(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T notLike$(DSLName dslName, Object value); @@ -575,7 +600,7 @@ default T notLike(String fieldName, Object value) { * * @param dslName dslName * @param value like值 - * @return SQLWhereOperator + * @return self */ T $notLike$(DSLName dslName, Object value); diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/AlterTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/AlterTableOperator.java index 708a335e..1a3f5245 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/AlterTableOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/AlterTableOperator.java @@ -3,6 +3,7 @@ import cc.allio.uno.core.api.Self; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import java.util.Arrays; import java.util.Collection; @@ -13,22 +14,22 @@ * * @author j.x * @date 2023/6/8 19:21 - * @since 1.1.4 * @see OperatorGroup + * @since 1.1.4 */ -public interface AlterTableOperator extends Operator, Self, TableOperator { +public interface AlterTableOperator> extends Operator, Self, TableOperator { /** * @see #alertColumns(Collection) */ - default AlterTableOperator alertColumn(ColumnDef columnDef) { + default T alertColumn(ColumnDef columnDef) { return alertColumns(columnDef); } /** * @see #alertColumns(Collection) */ - default AlterTableOperator alertColumns(ColumnDef... columnDefs) { + default T alertColumns(ColumnDef... columnDefs) { return alertColumns(Arrays.stream(columnDefs).toList()); } @@ -36,21 +37,21 @@ default AlterTableOperator alertColumns(ColumnDef... columnDefs) { * 修改列 * * @param columnDefs columnDefs - * @return AlterTableOperator + * @return self */ - AlterTableOperator alertColumns(Collection columnDefs); + T alertColumns(Collection columnDefs); /** * @see #addColumns(Collection) */ - default AlterTableOperator addColumn(ColumnDef columnDef) { + default T addColumn(ColumnDef columnDef) { return addColumns(columnDef); } /** * @see #addColumns(Collection) */ - default AlterTableOperator addColumns(ColumnDef... columnDefs) { + default T addColumns(ColumnDef... columnDefs) { return addColumns(Arrays.stream(columnDefs).toList()); } @@ -58,28 +59,28 @@ default AlterTableOperator addColumns(ColumnDef... columnDefs) { * 添加column * * @param columnDefs columnDefs - * @return AlterTableOperator + * @return self */ - AlterTableOperator addColumns(Collection columnDefs); + T addColumns(Collection columnDefs); /** * @see #deleteColumn(DSLName) */ - default AlterTableOperator deleteColumn(String column) { + default T deleteColumn(String column) { return deleteColumn(DSLName.of(column)); } /** * @see #deleteColumns(Collection) */ - default AlterTableOperator deleteColumn(DSLName column) { + default T deleteColumn(DSLName column) { return deleteColumns(column); } /** * @see #deleteColumns(Collection) */ - default AlterTableOperator deleteColumns(DSLName... columns) { + default T deleteColumns(DSLName... columns) { return deleteColumns(Arrays.stream(columns).toList()); } @@ -87,35 +88,35 @@ default AlterTableOperator deleteColumns(DSLName... columns) { * 删除列 * * @param columns columns - * @return AlterTableOperator + * @return self */ - AlterTableOperator deleteColumns(Collection columns); + T deleteColumns(Collection columns); /** * @see #rename(Table) */ - default AlterTableOperator rename(String table) { + default T rename(String table) { return rename(Table.of(table)); } /** * @see #rename(Table) */ - default AlterTableOperator rename(DSLName table) { + default T rename(DSLName table) { return rename(Table.of(table)); } /** * @see #rename(Table) */ - default AlterTableOperator rename(Class pojoClass) { + default T rename(Class pojoClass) { return rename(PojoWrapper.findTable(pojoClass)); } /** * @see #rename(Table) */ - default AlterTableOperator rename(UnaryOperator
func) { + default T rename(UnaryOperator
func) { return rename(func.apply(new Table())); } @@ -123,7 +124,7 @@ default AlterTableOperator rename(UnaryOperator
func) { * 重命名表名 * * @param to to - * @return AlterTableOperator + * @return self */ - AlterTableOperator rename(Table to); + T rename(Table to); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/CreateTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/CreateTableOperator.java index 7f87b7b9..4c36a77b 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/CreateTableOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/CreateTableOperator.java @@ -1,7 +1,7 @@ package cc.allio.uno.data.orm.dsl.ddl; import cc.allio.uno.core.util.CollectionUtils; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; import cc.allio.uno.data.orm.dsl.ColumnDef; import cc.allio.uno.data.orm.dsl.Operator; @@ -15,19 +15,19 @@ * * @author j.x * @date 2023/4/12 19:42 - * @since 1.1.4 * @see OperatorGroup + * @since 1.1.4 */ -public interface CreateTableOperator extends Operator, TableOperator { +public interface CreateTableOperator> extends Operator, TableOperator { /** * 根据pojo的clas创表实例 * * @param pojoClass the pojoClass - * @return SQLCreateTableOperator + * @return self */ - default CreateTableOperator fromPojo(Class pojoClass) { - PojoWrapper pojoWrapper = PojoWrapper.getInstance(pojoClass); + default

T fromPojo(Class

pojoClass) { + PojoWrapper

pojoWrapper = PojoWrapper.getInstance(pojoClass); List columnDefs = pojoWrapper.getColumnDefs(); return from(pojoWrapper.getTable()).columns(columnDefs); } @@ -36,9 +36,9 @@ default CreateTableOperator fromPojo(Class pojoClass) { * 字段 * * @param columnDefs 集合 - * @return CreateTableOperator + * @return self */ - default CreateTableOperator columns(ColumnDef... columnDefs) { + default T columns(ColumnDef... columnDefs) { if (columnDefs != null) { for (ColumnDef columnDef : columnDefs) { column(columnDef); @@ -51,9 +51,9 @@ default CreateTableOperator columns(ColumnDef... columnDefs) { * 字段 * * @param columnDefs 集合 - * @return CreateTableOperator + * @return self */ - default CreateTableOperator columns(List columnDefs) { + default T columns(List columnDefs) { if (CollectionUtils.isNotEmpty(columnDefs)) { for (ColumnDef columnDef : columnDefs) { column(columnDef); @@ -66,9 +66,9 @@ default CreateTableOperator columns(List columnDefs) { * 字段 * * @param builder the builder - * @return CreateTableOperator + * @return self */ - default CreateTableOperator column(UnaryOperator builder) { + default T column(UnaryOperator builder) { return column(builder.apply(ColumnDef.builder()).build()); } @@ -76,15 +76,15 @@ default CreateTableOperator column(UnaryOperator bui * 字段 * * @param columnDef SQLColumnDef - * @return CreateTableOperator + * @return self */ - CreateTableOperator column(ColumnDef columnDef); + T column(ColumnDef columnDef); /** * 注释 * * @param comment 注释 - * @return CreateTableOperator + * @return self */ - CreateTableOperator comment(String comment); + T comment(String comment); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/DropTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/DropTableOperator.java index e2c6bff7..6e039d85 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/DropTableOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/DropTableOperator.java @@ -1,7 +1,7 @@ package cc.allio.uno.data.orm.dsl.ddl; import cc.allio.uno.data.orm.dsl.Operator; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.TableOperator; /** @@ -9,16 +9,16 @@ * * @author j.x * @date 2023/4/16 12:52 - * @since 1.1.4 * @see OperatorGroup + * @since 1.1.4 */ -public interface DropTableOperator extends Operator, TableOperator { +public interface DropTableOperator> extends Operator, TableOperator { /** * Drop xxxx if exist * * @param ifExist ifExist - * @return SQLDropTableOperator + * @return self */ - DropTableOperator ifExist(Boolean ifExist); + T ifExist(Boolean ifExist); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ExistTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ExistTableOperator.java index 8bee3236..e9b0b43e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ExistTableOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ExistTableOperator.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.ddl; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.PrepareOperator; import cc.allio.uno.data.orm.dsl.TableOperator; @@ -12,6 +12,5 @@ * @since 1.1.4 * @see OperatorGroup */ -public interface ExistTableOperator extends PrepareOperator, TableOperator { - +public interface ExistTableOperator> extends PrepareOperator, TableOperator { } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowColumnsOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowColumnsOperator.java index 27e768ef..627e4623 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowColumnsOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowColumnsOperator.java @@ -1,11 +1,10 @@ package cc.allio.uno.data.orm.dsl.ddl; import cc.allio.uno.data.orm.dsl.DataBaseOperator; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.PrepareOperator; import cc.allio.uno.data.orm.dsl.TableOperator; -import cc.allio.uno.data.query.stream.SQLCommandExecutorStream; /** * Show Columns @@ -15,8 +14,7 @@ * @see OperatorGroup * @since 1.1.4 */ -public interface ShowColumnsOperator - extends PrepareOperator, TableOperator, DataBaseOperator { +public interface ShowColumnsOperator> extends PrepareOperator, TableOperator, DataBaseOperator { String TABLE_CATALOG_FIELD = "TABLE_CATALOG"; String TABLE_SCHEMA_FILED = "TABLE_SCHEMA"; @@ -35,7 +33,7 @@ public interface ShowColumnsOperator /** * 转换为{@link QueryOperator} * - * @return SQLQueryOperator for instance + * @return {@link QueryOperator} for instance */ - QueryOperator toQueryOperator(); + QueryOperator toQueryOperator(); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java index 0d124c83..5e4343cc 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java @@ -2,18 +2,17 @@ import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; /** * Show Tables Operator * * @author j.x * @date 2024/1/4 16:56 - * @since 1.1.7 * @see OperatorGroup + * @since 1.1.7 */ -public interface ShowTablesOperator - extends TableOperator, - PrepareOperator, DataBaseOperator { +public interface ShowTablesOperator> extends TableOperator, PrepareOperator, DataBaseOperator { String TABLE_CATALOG_FILED = "TABLE_CATALOG"; String TABLE_SCHEMA_FILED = "TABLE_SCHEMA"; @@ -23,24 +22,24 @@ public interface ShowTablesOperator /** * 转换为{@link QueryOperator} * - * @return SQLQueryOperator for instance + * @return {@link QueryOperator} */ - QueryOperator toQueryOperator(); + QueryOperator toQueryOperator(); /** * schema * * @param schema schema - * @return ShowTablesOperator + * @return self */ - ShowTablesOperator schema(String schema); + T schema(String schema); /** * 作为库表查询的过滤,可以多次使用,如果多次则查询这多个信息 * * @param table xxxx - * @return ShowTablesOperator + * @return self */ @Override - ShowTablesOperator from(Table table); + T from(Table table); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/DeleteOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/DeleteOperator.java index 0b32c781..8cf60ac0 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/DeleteOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/DeleteOperator.java @@ -1,10 +1,9 @@ package cc.allio.uno.data.orm.dsl.dml; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.PrepareOperator; import cc.allio.uno.data.orm.dsl.TableOperator; import cc.allio.uno.data.orm.dsl.WhereOperator; -import cc.allio.uno.data.orm.dsl.type.DBType; /** * DeleteOperator @@ -14,6 +13,5 @@ * @see OperatorGroup * @since 1.1.4 */ -public interface DeleteOperator extends - PrepareOperator, TableOperator, WhereOperator { +public interface DeleteOperator> extends PrepareOperator, TableOperator, WhereOperator { } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/InsertOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/InsertOperator.java index e1c0d51f..fe58e0e0 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/InsertOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/InsertOperator.java @@ -4,6 +4,7 @@ import cc.allio.uno.core.util.id.IdGenerator; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import reactor.util.function.Tuple2; @@ -21,16 +22,16 @@ * @see OperatorGroup * @since 1.1.4 */ -public interface InsertOperator extends PrepareOperator, TableOperator { +public interface InsertOperator> extends PrepareOperator, TableOperator { /** * INSERT VALUES * * @param reference key * @param value value - * @return InsertOperator + * @return self */ - default InsertOperator insert(MethodReferenceColumn reference, Object value) { + default T insert(MethodReferenceColumn reference, Object value) { return insert(reference.getColumn(), value); } @@ -39,9 +40,9 @@ default InsertOperator insert(MethodReferenceColumn reference, Object val * * @param fieldName fieldName * @param value value - * @return InsertOperator + * @return self */ - default InsertOperator insert(String fieldName, Object value) { + default T insert(String fieldName, Object value) { return insert(Map.of(fieldName, getValueIfNull(value))); } @@ -50,9 +51,9 @@ default InsertOperator insert(String fieldName, Object value) { * * @param f1 f1 * @param v1 v1 - * @return InsertOperator + * @return self */ - default InsertOperator insert(String f1, Object v1, String f2, Object v2) { + default T insert(String f1, Object v1, String f2, Object v2) { return insert(Tuples.of(f1, getValueIfNull(v1)), Tuples.of(f2, getValueIfNull(v2))); } @@ -63,9 +64,9 @@ default InsertOperator insert(String f1, Object v1, String f2, Object v2) { * @param v1 v1 * @param f2 f2 * @param v2 v2 - * @return InsertOperator + * @return self */ - default InsertOperator insert(String f1, Object v1, String f2, Object v2, String f3, Object v3) { + default T insert(String f1, Object v1, String f2, Object v2, String f3, Object v3) { return insert( Tuples.of(f1, getValueIfNull(v1)), Tuples.of(f2, getValueIfNull(v2)), @@ -83,9 +84,9 @@ default InsertOperator insert(String f1, Object v1, String f2, Object v2, String * @param v3 v3 * @param f4 f4 * @param v4 v4 - * @return InsertOperator + * @return self */ - default InsertOperator insert(String f1, Object v1, String f2, Object v2, String f3, Object v3, String f4, Object v4) { + default T insert(String f1, Object v1, String f2, Object v2, String f3, Object v3, String f4, Object v4) { return insert( Tuples.of(f1, getValueIfNull(v1)), Tuples.of(f2, getValueIfNull(v2)), @@ -97,9 +98,9 @@ default InsertOperator insert(String f1, Object v1, String f2, Object v2, String * INSERT VALUES * * @param tuple2s Key value - * @return InsertOperator + * @return self */ - default InsertOperator insert(Tuple2... tuple2s) { + default T insert(Tuple2... tuple2s) { Map values = Maps.newHashMap(); for (Tuple2 tuple2 : tuple2s) { values.put(tuple2.getT1(), tuple2.getT2()); @@ -111,10 +112,10 @@ default InsertOperator insert(Tuple2... tuple2s) { * 基于POJO实体动态构建INSERT VALUES,如果该实体某个字段没有值将不会加入到INSERT中 * * @param pojo pojo - * @return SQLInsertOperator + * @return self */ - default InsertOperator insertPojo(T pojo) { - PojoWrapper pojoWrapper = PojoWrapper.getInstance(pojo); + default

T insertPojo(P pojo) { + PojoWrapper

pojoWrapper = PojoWrapper.getInstance(pojo); String pkName = pojoWrapper.getPkColumn().getDslName().format(DSLName.HUMP_FEATURE); ColumnDef pkColumn = pojoWrapper.getPkColumn(); Object pkValue = pkColumn.castValue(IdGenerator.defaultGenerator().getNextId()); @@ -128,9 +129,9 @@ default InsertOperator insertPojo(T pojo) { * INSERT VALUES * * @param values Key value - * @return InsertOperator + * @return self */ - default InsertOperator insert(Map values) { + default T insert(Map values) { Map inserts = Maps.newLinkedHashMap(); for (Map.Entry entry : values.entrySet()) { inserts.put(DSLName.of(entry.getKey()), getValueIfNull(entry.getValue())); @@ -142,9 +143,9 @@ default InsertOperator insert(Map values) { * INSERT VALUES * * @param values Key value - * @return InsertOperator + * @return self */ - default InsertOperator inserts(Map values) { + default T inserts(Map values) { Set sqlNames = values.keySet(); Collection value = values.values(); return columns(sqlNames.stream().toList()).values(Lists.newArrayList(value)); @@ -154,10 +155,10 @@ default InsertOperator inserts(Map values) { * 基于POJO实体动态构建INSERT VALUES,如果该实体某个字段没有值将不会加入到INSERT中 * * @param pojos pojos list - * @return SQLInsertOperator + * @return self */ - default InsertOperator batchInsertPojos(List pojos) { - for (T pojo : pojos) { + default

T batchInsertPojos(List

pojos) { + for (P pojo : pojos) { insertPojo(pojo); } return self(); @@ -167,9 +168,9 @@ default InsertOperator batchInsertPojos(List pojos) { * INSERT VALUES,VALUES,VALUES * * @param values values - * @return SQLInsertOperator + * @return self */ - default InsertOperator batchInsert(List columns, List> values) { + default T batchInsert(List columns, List> values) { return batchInserts(columns.stream().map(DSLName::of).toList(), values); } @@ -177,9 +178,9 @@ default InsertOperator batchInsert(List columns, List> valu * INSERT VALUES,VALUES,VALUES * * @param values values - * @return SQLInsertOperator + * @return self */ - default InsertOperator batchInserts(List columns, List> values) { + default T batchInserts(List columns, List> values) { columns(columns); for (List value : values) { values(value); @@ -190,7 +191,7 @@ default InsertOperator batchInserts(List columns, List> va /** * @see #strictFill(String, Supplier) */ - default InsertOperator strictFill(String f, Object v) { + default T strictFill(String f, Object v) { return strictFill(f, () -> v); } @@ -201,17 +202,17 @@ default InsertOperator strictFill(String f, Object v) { * * @param f 字段名 * @param v 字段值 - * @return SQLInsertOperator + * @return self */ - InsertOperator strictFill(String f, Supplier v); + T strictFill(String f, Supplier v); /** * 提供insert column * * @param columns columns - * @return SQLInsertOperator + * @return self */ - default InsertOperator columns(String... columns) { + default T columns(String... columns) { return columns(Lists.newArrayList(columns).stream().map(DSLName::of).toList()); } @@ -219,9 +220,9 @@ default InsertOperator columns(String... columns) { * 提供insert column * * @param columns columns - * @return SQLInsertOperator + * @return self */ - default InsertOperator columns(DSLName... columns) { + default T columns(DSLName... columns) { return columns(Lists.newArrayList(columns)); } @@ -229,14 +230,14 @@ default InsertOperator columns(DSLName... columns) { * 提供insert column * * @param columns columns - * @return SQLInsertOperator + * @return self */ - InsertOperator columns(Collection columns); + T columns(Collection columns); /** * @see #values(List) */ - default InsertOperator values(Object... values) { + default T values(Object... values) { return values(Lists.newArrayList(values)); } @@ -251,9 +252,9 @@ default InsertOperator values(Object... values) { * * * @param values values - * @return SQLInsertOperator + * @return self */ - InsertOperator values(List values); + T values(List values); /** * 是否是批量插入 diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java index f00ce6fd..7fe449b7 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/QueryOperator.java @@ -5,6 +5,7 @@ import cc.allio.uno.data.orm.dsl.Func; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.word.Distinct; import com.google.common.collect.Lists; @@ -19,7 +20,7 @@ * @see OperatorGroup * @since 1.1.4 */ -public interface QueryOperator extends PrepareOperator, TableOperator, WhereOperator { +public interface QueryOperator> extends PrepareOperator, TableOperator, WhereOperator { // ============================== SELECT PART ============================== @@ -29,7 +30,7 @@ public interface QueryOperator extends PrepareOperator, TableOper * @param reference 方法引用 * @return self */ - default QueryOperator select(MethodReferenceColumn reference) { + default T select(MethodReferenceColumn reference) { return select(reference.getColumn()); } @@ -40,7 +41,7 @@ default QueryOperator select(MethodReferenceColumn reference) { * @param alias alias * @return self */ - default QueryOperator select(MethodReferenceColumn reference, String alias) { + default T select(MethodReferenceColumn reference, String alias) { return select(reference.getColumn(), alias); } @@ -49,7 +50,7 @@ default QueryOperator select(MethodReferenceColumn reference, String alia * * @return Select */ - default QueryOperator selectAll() { + default T selectAll() { return select(StringPool.ASTERISK); } @@ -59,7 +60,7 @@ default QueryOperator selectAll() { * @param fieldName java variable name * @return self */ - default QueryOperator select(String fieldName) { + default T select(String fieldName) { return select(DSLName.of(fieldName)); } @@ -69,7 +70,7 @@ default QueryOperator select(String fieldName) { * @param dslName dslName * @return self */ - QueryOperator select(DSLName dslName); + T select(DSLName dslName); /** * the select field @@ -78,7 +79,7 @@ default QueryOperator select(String fieldName) { * @param alias alias * @return self */ - default QueryOperator select(String fieldName, String alias) { + default T select(String fieldName, String alias) { return select(DSLName.of(fieldName), alias); } @@ -89,7 +90,7 @@ default QueryOperator select(String fieldName, String alias) { * @param alias alias * @return self */ - QueryOperator select(DSLName dslName, String alias); + T select(DSLName dslName, String alias); /** @@ -98,7 +99,7 @@ default QueryOperator select(String fieldName, String alias) { * @param fieldNames java variable name * @return self */ - default QueryOperator select(String[] fieldNames) { + default T select(String[] fieldNames) { return select(Lists.newArrayList(fieldNames)); } @@ -108,7 +109,7 @@ default QueryOperator select(String[] fieldNames) { * @param fieldNames java variable name * @return self */ - default QueryOperator select(Collection fieldNames) { + default T select(Collection fieldNames) { return selects(fieldNames.stream().map(DSLName::of).toList()); } @@ -118,7 +119,7 @@ default QueryOperator select(Collection fieldNames) { * @param entityType the entity type * @return self */ - default QueryOperator select(Class entityType) { + default

T select(Class

entityType) { Collection columns = PojoWrapper.findColumns(entityType); return selects(columns); } @@ -127,9 +128,9 @@ default QueryOperator select(Class entityType) { * 批量条件'SELECT'条件 * * @param dslNames dslNames - * @return Select + * @return self */ - QueryOperator selects(Collection dslNames); + T selects(Collection dslNames); /** * obtain select columns @@ -139,18 +140,18 @@ default QueryOperator select(Class entityType) { /** * 添加 distinct * - * @return Select + * @return self */ - QueryOperator distinct(); + T distinct(); /** * 添加 distinct on * * @param reference 方法引用 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator distinctOn(MethodReferenceColumn reference) { + default T distinctOn(MethodReferenceColumn reference) { return distinctOn(reference.getColumn()); } @@ -160,9 +161,9 @@ default QueryOperator distinctOn(MethodReferenceColumn reference) { * @param reference 方法引用 * @param alias 别名 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator distinctOn(MethodReferenceColumn reference, String alias) { + default T distinctOn(MethodReferenceColumn reference, String alias) { return distinctOn(reference.getColumn(), alias); } @@ -170,9 +171,9 @@ default QueryOperator distinctOn(MethodReferenceColumn reference, String * 添加 distinct on * * @param fieldName java variable name - * @return Select + * @return self */ - default QueryOperator distinctOn(String fieldName) { + default T distinctOn(String fieldName) { return distinctOn(fieldName, fieldName); } @@ -181,29 +182,29 @@ default QueryOperator distinctOn(String fieldName) { * * @param fieldName java variable name * @param alias 别名 - * @return Select + * @return self */ - default QueryOperator distinctOn(String fieldName, String alias) { + default T distinctOn(String fieldName, String alias) { return distinctOn(DSLName.of(fieldName), alias); } /** * 添加 distinct on * - * @param sqlName sqlName + * @param dslName dslName * @param alias 别名 - * @return Select + * @return self */ - QueryOperator distinctOn(DSLName sqlName, String alias); + T distinctOn(DSLName dslName, String alias); /** * 添加 min(field) alias * * @param reference 方法引用 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator min(MethodReferenceColumn reference) { + default T min(MethodReferenceColumn reference) { return min(reference.getColumn()); } @@ -213,9 +214,9 @@ default QueryOperator min(MethodReferenceColumn reference) { * @param reference 方法引用 * @param alias 别名 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator min(MethodReferenceColumn reference, String alias) { + default T min(MethodReferenceColumn reference, String alias) { return min(reference.getColumn(), alias); } @@ -223,9 +224,9 @@ default QueryOperator min(MethodReferenceColumn reference, String alias) * 添加 min(field) alias * * @param fieldName java variable name - * @return Select + * @return self */ - default QueryOperator min(String fieldName) { + default T min(String fieldName) { return min(fieldName, null); } @@ -234,9 +235,9 @@ default QueryOperator min(String fieldName) { * * @param fieldName java variable name * @param alias 别名 - * @return Select + * @return self */ - default QueryOperator min(String fieldName, String alias) { + default T min(String fieldName, String alias) { return aggregate(Func.MIN_FUNCTION.getName(), fieldName, alias, null); } @@ -245,9 +246,9 @@ default QueryOperator min(String fieldName, String alias) { * * @param reference 方法引用 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator max(MethodReferenceColumn reference) { + default T max(MethodReferenceColumn reference) { return max(reference.getColumn()); } @@ -257,9 +258,9 @@ default QueryOperator max(MethodReferenceColumn reference) { * @param reference 方法应用 * @param alias 别名 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator max(MethodReferenceColumn reference, String alias) { + default T max(MethodReferenceColumn reference, String alias) { return max(reference.getColumn(), alias); } @@ -267,9 +268,9 @@ default QueryOperator max(MethodReferenceColumn reference, String alias) * 添加 max(field) alias * * @param fieldName java variable name - * @return Select + * @return self */ - default QueryOperator max(String fieldName) { + default T max(String fieldName) { return max(fieldName, fieldName); } @@ -278,9 +279,9 @@ default QueryOperator max(String fieldName) { * * @param fieldName java variable name * @param alias 别名 - * @return Select + * @return self */ - default QueryOperator max(String fieldName, String alias) { + default T max(String fieldName, String alias) { return aggregate(Func.MAX_FUNCTION.getName(), fieldName, alias, null); } @@ -289,9 +290,9 @@ default QueryOperator max(String fieldName, String alias) { * * @param reference 方法引用 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator avg(MethodReferenceColumn reference) { + default T avg(MethodReferenceColumn reference) { return avg(reference.getColumn()); } @@ -301,9 +302,9 @@ default QueryOperator avg(MethodReferenceColumn reference) { * @param reference 方法应用 * @param alias 别名 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator avg(MethodReferenceColumn reference, String alias) { + default T avg(MethodReferenceColumn reference, String alias) { return avg(reference.getColumn(), alias); } @@ -311,9 +312,9 @@ default QueryOperator avg(MethodReferenceColumn reference, String alias) * 添加 avg(field) alias * * @param fieldName java variable name - * @return Select + * @return self */ - default QueryOperator avg(String fieldName) { + default T avg(String fieldName) { return avg(fieldName, fieldName); } @@ -322,18 +323,18 @@ default QueryOperator avg(String fieldName) { * * @param fieldName java variable name * @param alias 别名 - * @return Select + * @return self */ - default QueryOperator avg(String fieldName, String alias) { + default T avg(String fieldName, String alias) { return aggregate(Func.AVG_FUNCTION.getName(), fieldName, alias, null); } /** * 添加 count(field) alias * - * @return Select + * @return self */ - default QueryOperator count() { + default T count() { return count(StringPool.ASTERISK, "count"); } @@ -342,9 +343,9 @@ default QueryOperator count() { * * @param reference 方法应用 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator count(MethodReferenceColumn reference) { + default T count(MethodReferenceColumn reference) { return count(reference.getColumn(), null); } @@ -354,9 +355,9 @@ default QueryOperator count(MethodReferenceColumn reference) { * @param reference 方法引用 * @param alias 别名 * @param 实体类型 - * @return Select + * @return self */ - default QueryOperator count(MethodReferenceColumn reference, String alias) { + default T count(MethodReferenceColumn reference, String alias) { return count(reference.getColumn(), alias); } @@ -364,9 +365,9 @@ default QueryOperator count(MethodReferenceColumn reference, String alias * 添加 count(field) alias * * @param fieldName java variable name - * @return Select + * @return self */ - default QueryOperator count(String fieldName) { + default T count(String fieldName) { return count(fieldName, null); } @@ -375,9 +376,9 @@ default QueryOperator count(String fieldName) { * * @param fieldName java variable name * @param alias 别名 - * @return Select + * @return self */ - default QueryOperator count(String fieldName, String alias) { + default T count(String fieldName, String alias) { return aggregate(Func.COUNT_FUNCTION.getName(), fieldName, alias, null); } @@ -388,10 +389,10 @@ default QueryOperator count(String fieldName, String alias) { * @param fieldName java variable name * @param alias 别名 * @param distinct distinct - * @return Select + * @return self * @see Func */ - default QueryOperator aggregate(String syntax, String fieldName, String alias, Distinct distinct) { + default T aggregate(String syntax, String fieldName, String alias, Distinct distinct) { Func func = Func.of(syntax); if (func != null) { return aggregate(func, fieldName, alias, distinct); @@ -406,10 +407,10 @@ default QueryOperator aggregate(String syntax, String fieldName, String alias, D * @param fieldName java variable name * @param alias 别名 * @param distinct distinct - * @return Select + * @return self * @see Func */ - default QueryOperator aggregate(Func syntax, String fieldName, String alias, Distinct distinct) { + default T aggregate(Func syntax, String fieldName, String alias, Distinct distinct) { return aggregate(syntax, DSLName.of(fieldName), alias, distinct); } @@ -417,13 +418,13 @@ default QueryOperator aggregate(Func syntax, String fieldName, String alias, Dis * Select 函数 * * @param syntax 函数语法 - * @param sqlName sqlName + * @param dslName dslName * @param alias 别名 * @param distinct distinct - * @return Select + * @return self * @see Func */ - QueryOperator aggregate(Func syntax, DSLName sqlName, String alias, Distinct distinct); + T aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct); // ============================== FROM PART ============================== @@ -432,9 +433,9 @@ default QueryOperator aggregate(Func syntax, String fieldName, String alias, Dis * * @param fromTable QueryOperator实例 * @param alias 查询别名 - * @return QueryOperator + * @return self */ - QueryOperator from(QueryOperator fromTable, String alias); + T from(QueryOperator fromTable, String alias); /** * FROM t1 left join t2 on t1.xx = t2.xx @@ -442,9 +443,9 @@ default QueryOperator aggregate(Func syntax, String fieldName, String alias, Dis * @param left left * @param right right * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator leftJoin(Table left, Table right, BinaryCondition condition) { + default T leftJoin(Table left, Table right, BinaryCondition condition) { return join(left, JoinType.LEFT_OUTER_JOIN, right, condition); } @@ -454,9 +455,9 @@ default QueryOperator leftJoin(Table left, Table right, BinaryCondition conditio * @param left left * @param right right * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator rightJoin(Table left, Table right, BinaryCondition condition) { + default T rightJoin(Table left, Table right, BinaryCondition condition) { return join(left, JoinType.RIGHT_OUTER_JOIN, right, condition); } @@ -466,9 +467,9 @@ default QueryOperator rightJoin(Table left, Table right, BinaryCondition conditi * @param leftAlias leftAlias * @param rightName rightName * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator leftJoinThen(String leftAlias, String rightName, BinaryCondition condition) { + default T leftJoinThen(String leftAlias, String rightName, BinaryCondition condition) { return leftJoinThen(leftAlias, rightName, rightName, condition); } @@ -479,9 +480,9 @@ default QueryOperator leftJoinThen(String leftAlias, String rightName, BinaryCon * @param rightName rightName * @param rightAlias rightAlias * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator leftJoinThen(String leftAlias, String rightName, String rightAlias, BinaryCondition condition) { + default T leftJoinThen(String leftAlias, String rightName, String rightAlias, BinaryCondition condition) { return joinThen(leftAlias, JoinType.LEFT_OUTER_JOIN, Table.of(rightName, rightAlias), condition); } @@ -491,9 +492,9 @@ default QueryOperator leftJoinThen(String leftAlias, String rightName, String ri * @param leftAlias right * @param rightName rightName * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator rightJoinThen(String leftAlias, String rightName, BinaryCondition condition) { + default T rightJoinThen(String leftAlias, String rightName, BinaryCondition condition) { return joinThen(leftAlias, JoinType.RIGHT_OUTER_JOIN, Table.of(rightName, rightName), condition); } @@ -504,9 +505,9 @@ default QueryOperator rightJoinThen(String leftAlias, String rightName, BinaryCo * @param rightName rightName * @param rightAlias rightAlias * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator rightJoinThen(String leftAlias, String rightName, String rightAlias, BinaryCondition condition) { + default T rightJoinThen(String leftAlias, String rightName, String rightAlias, BinaryCondition condition) { return joinThen(leftAlias, JoinType.RIGHT_OUTER_JOIN, Table.of(rightName, rightAlias), condition); } @@ -516,9 +517,9 @@ default QueryOperator rightJoinThen(String leftAlias, String rightName, String r * @param joinType 连接 * @param right right * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator joinThen(JoinType joinType, Table right, BinaryCondition condition) { + default T joinThen(JoinType joinType, Table right, BinaryCondition condition) { return joinThen("empty", joinType, right, condition); } @@ -529,9 +530,9 @@ default QueryOperator joinThen(JoinType joinType, Table right, BinaryCondition c * @param joinType 连接 * @param right right * @param condition 条件 - * @return QueryOperator + * @return self */ - default QueryOperator joinThen(String leftAlias, JoinType joinType, Table right, BinaryCondition condition) { + default T joinThen(String leftAlias, JoinType joinType, Table right, BinaryCondition condition) { return join(Table.of(leftAlias, leftAlias), joinType, right, condition); } @@ -542,9 +543,9 @@ default QueryOperator joinThen(String leftAlias, JoinType joinType, Table right, * @param joinType 连接 * @param right right * @param condition 条件 - * @return QueryOperator + * @return self */ - QueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition); + T join(Table left, JoinType joinType, Table right, BinaryCondition condition); // ============================== ORDER PART ============================== @@ -552,9 +553,9 @@ default QueryOperator joinThen(String leftAlias, JoinType joinType, Table right, * 默认Order排序规则 * * @param fieldName java variable name - * @return Order对象 + * @return self */ - default QueryOperator by(String fieldName) { + default T by(String fieldName) { return byDesc(fieldName); } @@ -562,9 +563,9 @@ default QueryOperator by(String fieldName) { * 默认Order排序规则 * * @param reference 方法引用 - * @return Order对象 + * @return self */ - default QueryOperator by(MethodReferenceColumn reference) { + default T by(MethodReferenceColumn reference) { return by(reference.getColumn()); } @@ -572,9 +573,9 @@ default QueryOperator by(MethodReferenceColumn reference) { * 添加ORDER ASC * * @param reference 方法引用 - * @return Order对象 + * @return self */ - default QueryOperator byAsc(MethodReferenceColumn reference) { + default T byAsc(MethodReferenceColumn reference) { return byAsc(reference.getColumn()); } @@ -582,9 +583,9 @@ default QueryOperator byAsc(MethodReferenceColumn reference) { * 添加ORDER ASC * * @param fieldName java variable name - * @return Order对象 + * @return self */ - default QueryOperator byAsc(String fieldName) { + default T byAsc(String fieldName) { return orderBy(fieldName, OrderCondition.ASC); } @@ -592,9 +593,9 @@ default QueryOperator byAsc(String fieldName) { * 添加ORDER DESC * * @param reference 方法引用 - * @return Order对象 + * @return self */ - default QueryOperator byDesc(MethodReferenceColumn reference) { + default T byDesc(MethodReferenceColumn reference) { return byDesc(reference.getColumn()); } @@ -602,9 +603,9 @@ default QueryOperator byDesc(MethodReferenceColumn reference) { * 添加ORDER DESC * * @param fieldName java variable name - * @return Order对象 + * @return self */ - default QueryOperator byDesc(String fieldName) { + default T byDesc(String fieldName) { return orderBy(fieldName, OrderCondition.DESC); } @@ -613,9 +614,9 @@ default QueryOperator byDesc(String fieldName) { * * @param reference 方法引用 * @param order 排序 - * @return Order对象 + * @return self */ - default QueryOperator orderBy(MethodReferenceColumn reference, String order) { + default T orderBy(MethodReferenceColumn reference, String order) { return orderBy(reference.getColumn(), order); } @@ -624,9 +625,9 @@ default QueryOperator orderBy(MethodReferenceColumn reference, String order) * * @param fieldName java variable name * @param order 排序 - * @return Order对象 + * @return self */ - default QueryOperator orderBy(String fieldName, String order) { + default T orderBy(String fieldName, String order) { return orderBy(fieldName, OrderCondition.valueOf(order)); } @@ -635,9 +636,9 @@ default QueryOperator orderBy(String fieldName, String order) { * * @param reference 方法引用 * @param orderCondition 排序条件 - * @return Order对象 + * @return self */ - default QueryOperator orderBy(MethodReferenceColumn reference, OrderCondition orderCondition) { + default T orderBy(MethodReferenceColumn reference, OrderCondition orderCondition) { return orderBy(reference.getColumn(), orderCondition); } @@ -646,20 +647,20 @@ default QueryOperator orderBy(MethodReferenceColumn reference, OrderCondi * * @param fieldName java variable name * @param orderCondition 排序条件 - * @return Order对象 + * @return self */ - default QueryOperator orderBy(String fieldName, OrderCondition orderCondition) { + default T orderBy(String fieldName, OrderCondition orderCondition) { return orderBy(DSLName.of(fieldName), orderCondition); } /** * 添加Order语句 * - * @param sqlName sqlName + * @param dslName dslName * @param orderCondition 排序条件 - * @return Order对象 + * @return self */ - QueryOperator orderBy(DSLName sqlName, OrderCondition orderCondition); + T orderBy(DSLName dslName, OrderCondition orderCondition); // ============================== LIMIT PART ============================== @@ -668,9 +669,9 @@ default QueryOperator orderBy(String fieldName, OrderCondition orderCondition) { * * @param current 当前页 * @param pageSize 页大小 - * @return Limit + * @return self */ - default QueryOperator page(Long current, Long pageSize) { + default T page(Long current, Long pageSize) { return limit(pageSize, (current - 1) * pageSize); } @@ -679,20 +680,19 @@ default QueryOperator page(Long current, Long pageSize) { * * @param limit 限制查询的数量 * @param offset 偏移数 - * @return Limit + * @return self */ - QueryOperator limit(Long limit, Long offset); + T limit(Long limit, Long offset); // ============================== WHERE PART ============================== - /** * 由某一个字段进行分组 * * @param reference 方法引用 - * @return Group对象 + * @return self */ - default QueryOperator groupByOne(MethodReferenceColumn reference) { + default T groupByOne(MethodReferenceColumn reference) { return groupByOnes(reference.getColumn()); } @@ -702,9 +702,9 @@ default QueryOperator groupByOne(MethodReferenceColumn reference) { * 由某一个字段进行分组 * * @param fieldName java variable name - * @return Group对象 + * @return self */ - default QueryOperator groupByOne(String fieldName) { + default T groupByOne(String fieldName) { return groupByOnes(fieldName); } @@ -712,9 +712,9 @@ default QueryOperator groupByOne(String fieldName) { * 由某一类字段进行分组 * * @param fieldNames java variable name数组 - * @return Group对象 + * @return self */ - default QueryOperator groupByOnes(String... fieldNames) { + default T groupByOnes(String... fieldNames) { return groupByOne(Lists.newArrayList(fieldNames)); } @@ -722,9 +722,9 @@ default QueryOperator groupByOnes(String... fieldNames) { * 由某一类字段进行分组 * * @param fieldNames java variable name集合 - * @return Group对象 + * @return self */ - default QueryOperator groupByOne(Collection fieldNames) { + default T groupByOne(Collection fieldNames) { return groupByOnes(fieldNames.stream().map(DSLName::of).toList()); } @@ -734,7 +734,7 @@ default QueryOperator groupByOne(Collection fieldNames) { * @param fieldNames java variable name集合 * @return Group对象 */ - QueryOperator groupByOnes(Collection fieldNames); + T groupByOnes(Collection fieldNames); // ====================== advanced method ====================== @@ -746,6 +746,5 @@ default QueryOperator groupByOne(Collection fieldNames) { * @param subQuery the build tree query for sub query operator * @return self */ - QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery); - -} + T tree(QueryOperator baseQuery, QueryOperator subQuery); +} \ No newline at end of file diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/UpdateOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/UpdateOperator.java index 32d72e0c..fb15aee9 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/UpdateOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/dml/UpdateOperator.java @@ -1,8 +1,10 @@ package cc.allio.uno.data.orm.dsl.dml; +import cc.allio.uno.core.api.Self; import cc.allio.uno.core.function.lambda.MethodReferenceColumn; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import com.google.common.collect.Maps; import reactor.util.function.Tuple2; import reactor.util.function.Tuples; @@ -19,16 +21,16 @@ * @see OperatorGroup * @since 1.1.4 */ -public interface UpdateOperator extends PrepareOperator, TableOperator, WhereOperator { +public interface UpdateOperator> extends PrepareOperator, TableOperator, WhereOperator, Self { /** * UPDATE VALUES * * @param reference key * @param value value - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(MethodReferenceColumn reference, Object value) { + default T update(MethodReferenceColumn reference, Object value) { return update(reference.getColumn(), getValueIfNull(value)); } @@ -37,9 +39,9 @@ default UpdateOperator update(MethodReferenceColumn reference, Object val * * @param fieldName fieldName * @param value value - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(String fieldName, Object value) { + default T update(String fieldName, Object value) { return update(Tuples.of(fieldName, getValueIfNull(value))); } @@ -48,9 +50,9 @@ default UpdateOperator update(String fieldName, Object value) { * * @param f1 f1 * @param v1 v1 - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(String f1, Object v1, String f2, Object v2) { + default T update(String f1, Object v1, String f2, Object v2) { return update(Tuples.of(f1, getValueIfNull(v1)), Tuples.of(f2, getValueIfNull(v2))); } @@ -61,9 +63,9 @@ default UpdateOperator update(String f1, Object v1, String f2, Object v2) { * @param v1 v1 * @param f2 f2 * @param v2 v2 - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(String f1, Object v1, String f2, Object v2, String f3, Object v3) { + default T update(String f1, Object v1, String f2, Object v2, String f3, Object v3) { return update( Tuples.of(f1, getValueIfNull(v1)), Tuples.of(f2, getValueIfNull(v2)), @@ -81,9 +83,9 @@ default UpdateOperator update(String f1, Object v1, String f2, Object v2, String * @param v3 v3 * @param f4 f4 * @param v4 v4 - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(String f1, Object v1, String f2, Object v2, String f3, Object v3, String f4, Object v4) { + default T update(String f1, Object v1, String f2, Object v2, String f3, Object v3, String f4, Object v4) { return update( Tuples.of(f1, getValueIfNull(v1)), Tuples.of(f2, getValueIfNull(v2)), @@ -95,9 +97,9 @@ default UpdateOperator update(String f1, Object v1, String f2, Object v2, String * UPDATE VALUES * * @param tuple2s Key value - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(Tuple2... tuple2s) { + default T update(Tuple2... tuple2s) { Map values = Maps.newHashMap(); for (Tuple2 tuple2 : tuple2s) { values.put(tuple2.getT1(), tuple2.getT2()); @@ -109,9 +111,9 @@ default UpdateOperator update(Tuple2... tuple2s) { * UPDATE VALUES * * @param pojo pojo - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator updatePojo(Object pojo) { + default T updatePojo(Object pojo) { PojoWrapper pojoWrapper = PojoWrapper.getInstance(pojo); List notPkColumns = pojoWrapper.getNotPkColumns(); Map values = Maps.newLinkedHashMap(); @@ -126,9 +128,9 @@ default UpdateOperator updatePojo(Object pojo) { * UPDATE VALUES,将会过滤为空的值 * * @param values Key value - * @return SQLUpdateOperator + * @return self */ - default UpdateOperator update(Map values) { + default T update(Map values) { Map updates = Maps.newLinkedHashMap(); for (Map.Entry entry : values.entrySet()) { updates.put(DSLName.of(entry.getKey()), entry.getValue()); @@ -140,14 +142,14 @@ default UpdateOperator update(Map values) { * UPDATE VALUES * * @param values Key value - * @return SQLUpdateOperator + * @return self */ - UpdateOperator updates(Map values); + T updates(Map values); /** * @see #strictFill(String, Supplier) */ - default UpdateOperator strictFill(String f, Object v) { + default T strictFill(String f, Object v) { return strictFill(f, () -> v); } @@ -159,5 +161,5 @@ default UpdateOperator strictFill(String f, Object v) { * @param v 字段值 * @return SQLInsertOperator */ - UpdateOperator strictFill(String f, Supplier v); + T strictFill(String f, Supplier v); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/helper/PojoWrapper.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/helper/PojoWrapper.java index affb5aa2..7cc23a1e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/helper/PojoWrapper.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/helper/PojoWrapper.java @@ -181,7 +181,7 @@ private Table obtainTable() { TableResolver tableResolver = TABLE_RESOLVES.computeIfAbsent( pojoClass, - k -> { + _ -> { Class tableResolverClass = tableResolve.value(); return Optional.ofNullable(tableResolverClass) .map(ClassUtils::newInstance) @@ -244,7 +244,7 @@ private List obtainColumnDefs() { ColumnDefListResolver columnDefListResolver = CLASS_COLUMN_LIST_RESOLVERS.computeIfAbsent( pojoClass, - k -> { + _ -> { Class columnDefResolverClass = columnDefListResolve.value(); return Optional.ofNullable(columnDefResolverClass) .map(ClassUtils::newInstance) @@ -302,7 +302,7 @@ private ColumnDef createColumnDef(Field field) { .flatMap(c -> { ColumnDefResolver columnDefResolver = COLUMN_RESOLVES.computeIfAbsent( field, - k -> { + _ -> { Class columnResolverClass = columnDefResolve.value(); return Optional.ofNullable(columnResolverClass) .map(ClassUtils::newInstance) @@ -500,7 +500,7 @@ private static PojoInspect getPojoInspection(Class pojoClass) { return Optional.ofNullable(pojoInspection) .map(p -> { Class pojoInspectClass = p.value(); - return POJO_INSPECTS.computeIfAbsent(pojoClass, k -> Optional.ofNullable(pojoInspectClass).map(ClassUtils::newInstance).orElse(null)); + return POJO_INSPECTS.computeIfAbsent(pojoClass, _ -> Optional.ofNullable(pojoInspectClass).map(ClassUtils::newInstance).orElse(null)); }) .orElse(DEFAULT_POJO_INSPECT); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/logical/BiLogical.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/logical/BiLogical.java new file mode 100644 index 00000000..f243b30f --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/logical/BiLogical.java @@ -0,0 +1,26 @@ +package cc.allio.uno.data.orm.dsl.logical; + +/** + * the binary of logical + * + * @author j.x + * @date 2024/3/12 01:39 + * @since 1.1.7 + */ +public interface BiLogical { + + /** + * doAccept method + * + * @param p argument + * @return the R result + */ + R doAccept(T p); + + /** + * get logical predicate + * + * @return logical + */ + Logical getLogical(); +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorAlterTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorAlterTableOperator.java new file mode 100644 index 00000000..22a975fd --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorAlterTableOperator.java @@ -0,0 +1,107 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.ddl.AlterTableOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.Collection; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link AlterTableOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:30 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorAlterTableOperator implements AlterTableOperator, WrapperOperator { + + private AlterTableOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public MetaAcceptorAlterTableOperator alertColumns(Collection columnDefs) { + metaAcceptorSet.adapt(columnDefs); + actual.alertColumns(columnDefs); + return self(); + } + + @Override + public MetaAcceptorAlterTableOperator addColumns(Collection columnDefs) { + metaAcceptorSet.adapt(columnDefs); + actual.addColumns(columnDefs); + return self(); + } + + @Override + public MetaAcceptorAlterTableOperator deleteColumns(Collection columns) { + metaAcceptorSet.adapt(columns); + actual.deleteColumns(columns); + return self(); + } + + @Override + public MetaAcceptorAlterTableOperator rename(Table to) { + metaAcceptorSet.adapt(to); + actual.rename(to); + return self(); + } + + @Override + public MetaAcceptorAlterTableOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorAlterTableOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorAlterTableOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorCreateTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorCreateTableOperator.java new file mode 100644 index 00000000..525afbe7 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorCreateTableOperator.java @@ -0,0 +1,94 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.ColumnDef; +import cc.allio.uno.data.orm.dsl.MetaAcceptorSet; +import cc.allio.uno.data.orm.dsl.Operator; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link CreateTableOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:14 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorCreateTableOperator implements CreateTableOperator, WrapperOperator { + + private CreateTableOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public MetaAcceptorCreateTableOperator column(ColumnDef columnDef) { + metaAcceptorSet.adapt(columnDef); + actual.column(columnDef); + return self(); + } + + @Override + public MetaAcceptorCreateTableOperator comment(String comment) { + actual.comment(comment); + return self(); + } + + @Override + public MetaAcceptorCreateTableOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorCreateTableOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorCreateTableOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorDeleteOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorDeleteOperator.java new file mode 100644 index 00000000..c535135c --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorDeleteOperator.java @@ -0,0 +1,247 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link DeleteOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:11 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorDeleteOperator implements DeleteOperator, WrapperOperator { + + private DeleteOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public MetaAcceptorDeleteOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorDeleteOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorDeleteOperator gt(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.gt(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator gte(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.gte(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator lt(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.lt(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator lte(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.lte(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator eq(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.eq(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator neq(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.neq(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator notNull(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.notNull(dslName); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator isNull(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.isNull(dslName); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator in(DSLName dslName, V... values) { + metaAcceptorSet.adapt(dslName); + actual.in(dslName, values); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator notIn(DSLName dslName, V... values) { + metaAcceptorSet.adapt(dslName); + actual.notIn(dslName, values); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator between(DSLName dslName, Object withValue, Object endValue) { + metaAcceptorSet.adapt(dslName); + actual.between(dslName, withValue, endValue); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator notBetween(DSLName dslName, Object withValue, Object endValue) { + metaAcceptorSet.adapt(dslName); + actual.notBetween(dslName, withValue, endValue); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator like(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.like(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator $like(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$like(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator like$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.like$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator $like$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$like$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator notLike(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.notLike(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator $notLike(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$notLike(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator notLike$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.notLike$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator $notLike$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$notLike$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator or() { + actual.or(); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator and() { + actual.and(); + return self(); + } + + @Override + public MetaAcceptorDeleteOperator nor() { + actual.nor(); + return self(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorDropTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorDropTableOperator.java new file mode 100644 index 00000000..dd61073d --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorDropTableOperator.java @@ -0,0 +1,86 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.MetaAcceptorSet; +import cc.allio.uno.data.orm.dsl.Operator; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.ddl.DropTableOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link DropTableOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:17 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorDropTableOperator implements DropTableOperator, WrapperOperator { + + private DropTableOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public MetaAcceptorDropTableOperator ifExist(Boolean ifExist) { + actual.ifExist(ifExist); + return self(); + } + + @Override + public MetaAcceptorDropTableOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorDropTableOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorDropTableOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorExistTableOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorExistTableOperator.java new file mode 100644 index 00000000..2292d0db --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorExistTableOperator.java @@ -0,0 +1,92 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.MetaAcceptorSet; +import cc.allio.uno.data.orm.dsl.Operator; +import cc.allio.uno.data.orm.dsl.PrepareValue; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.ddl.ExistTableOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link ExistTableOperator} + * + * @author j.x + * @date 2024/4/21 16:19 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorExistTableOperator implements ExistTableOperator, WrapperOperator { + + private ExistTableOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public MetaAcceptorExistTableOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorExistTableOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorExistTableOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorInsertOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorInsertOperator.java new file mode 100644 index 00000000..94ca717d --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorInsertOperator.java @@ -0,0 +1,115 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.InsertOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.Collection; +import java.util.List; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link InsertOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:05 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorInsertOperator implements InsertOperator, WrapperOperator { + + private InsertOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public MetaAcceptorInsertOperator strictFill(String f, Supplier v) { + actual.strictFill(f, v); + return self(); + } + + @Override + public MetaAcceptorInsertOperator columns(Collection columns) { + metaAcceptorSet.adapt(columns); + actual.columns(columns); + return self(); + } + + @Override + public MetaAcceptorInsertOperator values(List values) { + actual.values(values); + return self(); + } + + @Override + public boolean isBatched() { + return actual.isBatched(); + } + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public MetaAcceptorInsertOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorInsertOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorInsertOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorQueryOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorQueryOperator.java new file mode 100644 index 00000000..ef17756c --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorQueryOperator.java @@ -0,0 +1,335 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.dsl.word.Distinct; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.Collection; +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link QueryOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 15:54 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorQueryOperator implements QueryOperator, WrapperOperator { + + private QueryOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public MetaAcceptorQueryOperator select(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.select(dslName); + return self(); + } + + @Override + public MetaAcceptorQueryOperator select(DSLName dslName, String alias) { + metaAcceptorSet.adapt(dslName); + actual.select(dslName, alias); + return self(); + } + + @Override + public MetaAcceptorQueryOperator selects(Collection dslNames) { + metaAcceptorSet.adapt(dslNames); + actual.selects(dslNames); + return self(); + } + + @Override + public List obtainSelectColumns() { + return actual.obtainSelectColumns(); + } + + @Override + public MetaAcceptorQueryOperator distinct() { + actual.distinct(); + return self(); + } + + @Override + public MetaAcceptorQueryOperator distinctOn(DSLName dslName, String alias) { + metaAcceptorSet.adapt(dslName); + actual.distinctOn(dslName, alias); + return self(); + } + + @Override + public MetaAcceptorQueryOperator aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct) { + metaAcceptorSet.adapt(dslName); + actual.aggregate(syntax, dslName, alias, distinct); + return self(); + } + + @Override + public MetaAcceptorQueryOperator from(QueryOperator fromTable, String alias) { + actual.from(fromTable, alias); + return self(); + } + + @Override + public MetaAcceptorQueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { + metaAcceptorSet.adapt(left); + metaAcceptorSet.adapt(right); + actual.join(left, joinType, right, condition); + return self(); + } + + @Override + public MetaAcceptorQueryOperator orderBy(DSLName dslName, OrderCondition orderCondition) { + metaAcceptorSet.adapt(dslName); + actual.orderBy(dslName, orderCondition); + return self(); + } + + @Override + public MetaAcceptorQueryOperator limit(Long limit, Long offset) { + actual.limit(limit, offset); + return self(); + } + + @Override + public MetaAcceptorQueryOperator groupByOnes(Collection fieldNames) { + metaAcceptorSet.adapt(fieldNames); + actual.groupByOnes(fieldNames); + return self(); + } + + @Override + public MetaAcceptorQueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + actual.tree(baseQuery, subQuery); + return self(); + } + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public MetaAcceptorQueryOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorQueryOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorQueryOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorQueryOperator gt(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.gt(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator gte(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.gte(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator lt(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.lt(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator lte(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.lte(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator eq(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.eq(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator neq(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.neq(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator notNull(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.notNull(dslName); + return self(); + } + + @Override + public MetaAcceptorQueryOperator isNull(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.isNull(dslName); + return self(); + } + + @Override + public MetaAcceptorQueryOperator in(DSLName dslName, V... values) { + metaAcceptorSet.adapt(dslName); + actual.in(dslName, values); + return self(); + } + + @Override + public MetaAcceptorQueryOperator notIn(DSLName dslName, V... values) { + metaAcceptorSet.adapt(dslName); + actual.notIn(dslName, values); + return self(); + } + + @Override + public MetaAcceptorQueryOperator between(DSLName dslName, Object withValue, Object endValue) { + metaAcceptorSet.adapt(dslName); + actual.between(dslName, withValue, endValue); + return self(); + } + + @Override + public MetaAcceptorQueryOperator notBetween(DSLName dslName, Object withValue, Object endValue) { + metaAcceptorSet.adapt(dslName); + actual.notBetween(dslName, withValue, endValue); + return self(); + } + + @Override + public MetaAcceptorQueryOperator like(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.like(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator $like(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$like(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator like$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.like$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator $like$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$like$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator notLike(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.notLike(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator $notLike(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$notLike(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator notLike$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.notLike$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator $notLike$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$notLike$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorQueryOperator or() { + actual.or(); + return self(); + } + + @Override + public MetaAcceptorQueryOperator and() { + actual.and(); + return self(); + } + + @Override + public MetaAcceptorQueryOperator nor() { + actual.nor(); + return self(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorSetOperatorGroup.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorSetOperatorGroup.java new file mode 100644 index 00000000..c816dc8b --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorSetOperatorGroup.java @@ -0,0 +1,94 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.ddl.*; +import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; +import cc.allio.uno.data.orm.dsl.dml.InsertOperator; +import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import lombok.Data; + +/** + * base on aspectj implementation {@link OperatorGroup}. + *

enhance {@link Operator#obtainMetaAcceptorSet()} load by proxy

+ * + * @author j.x + * @date 2024/4/21 15:31 + * @since 1.1.8 + */ +@Data +public class MetaAcceptorSetOperatorGroup implements OperatorGroup { + + private final OperatorKey key; + private MetaAcceptorSet metaAcceptorSet; + + public MetaAcceptorSetOperatorGroup(OperatorKey key) { + this(key, null); + } + + public MetaAcceptorSetOperatorGroup(OperatorKey key, MetaAcceptorSet metaAcceptorSet) { + this.key = key; + this.metaAcceptorSet = metaAcceptorSet; + } + + @Override + public QueryOperator query(DBType dbType) { + QueryOperator queryOperator = SPIOperatorHelper.lazyGet(QueryOperator.class, key, dbType); + return new MetaAcceptorQueryOperator(queryOperator, metaAcceptorSet); + } + + @Override + public InsertOperator insert(DBType dbType) { + InsertOperator insertOperator = SPIOperatorHelper.lazyGet(InsertOperator.class, key, dbType); + return new MetaAcceptorInsertOperator(insertOperator, metaAcceptorSet); + } + + @Override + public UpdateOperator update(DBType dbType) { + UpdateOperator updateOperator = SPIOperatorHelper.lazyGet(UpdateOperator.class, key, dbType); + return new MetaAcceptorUpdateOperator(updateOperator, metaAcceptorSet); + } + + @Override + public DeleteOperator delete(DBType dbType) { + DeleteOperator deleteOperator = SPIOperatorHelper.lazyGet(DeleteOperator.class, key, dbType); + return new MetaAcceptorDeleteOperator(deleteOperator, metaAcceptorSet); + } + + @Override + public CreateTableOperator createTable(DBType dbType) { + CreateTableOperator createTableOperator = SPIOperatorHelper.lazyGet(CreateTableOperator.class, key, dbType); + return new MetaAcceptorCreateTableOperator(createTableOperator, metaAcceptorSet); + } + + @Override + public DropTableOperator dropTable(DBType dbType) { + DropTableOperator dropTableOperator = SPIOperatorHelper.lazyGet(DropTableOperator.class, key, dbType); + return new MetaAcceptorDropTableOperator(dropTableOperator, metaAcceptorSet); + } + + @Override + public ExistTableOperator existTable(DBType dbType) { + ExistTableOperator existTableOperator = SPIOperatorHelper.lazyGet(ExistTableOperator.class, key, dbType); + return new MetaAcceptorExistTableOperator(existTableOperator, metaAcceptorSet); + } + + @Override + public ShowColumnsOperator showColumns(DBType dbType) { + ShowColumnsOperator showColumnsOperator = SPIOperatorHelper.lazyGet(ShowColumnsOperator.class, key, dbType); + return new MetaAcceptorShowColumnsOperator(showColumnsOperator, metaAcceptorSet); + } + + @Override + public ShowTablesOperator showTables(DBType dbType) { + ShowTablesOperator showTablesOperator = SPIOperatorHelper.lazyGet(ShowTablesOperator.class, key, dbType); + return new MetaAcceptorShowTablesOperator(showTablesOperator, metaAcceptorSet); + } + + @Override + public AlterTableOperator alterTables(DBType dbType) { + AlterTableOperator alterTableOperator = SPIOperatorHelper.lazyGet(AlterTableOperator.class, key, dbType); + return new MetaAcceptorAlterTableOperator(alterTableOperator, metaAcceptorSet); + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorShowColumnsOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorShowColumnsOperator.java new file mode 100644 index 00000000..fd557b9c --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorShowColumnsOperator.java @@ -0,0 +1,102 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.ddl.ShowColumnsOperator; +import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link ShowColumnsOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:21 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorShowColumnsOperator implements ShowColumnsOperator, WrapperOperator { + + private ShowColumnsOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public QueryOperator toQueryOperator() { + return actual.toQueryOperator(); + } + + @Override + public MetaAcceptorShowColumnsOperator database(Database database) { + metaAcceptorSet.adapt(database); + actual.database(database); + return self(); + } + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public MetaAcceptorShowColumnsOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorShowColumnsOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorShowColumnsOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorShowTablesOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorShowTablesOperator.java new file mode 100644 index 00000000..9a47fd45 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorShowTablesOperator.java @@ -0,0 +1,108 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.ddl.ShowTablesOperator; +import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link ShowTablesOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:23 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorShowTablesOperator implements ShowTablesOperator, WrapperOperator { + + private ShowTablesOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public QueryOperator toQueryOperator() { + return actual.toQueryOperator(); + } + + @Override + public MetaAcceptorShowTablesOperator schema(String schema) { + actual.schema(schema); + return self(); + } + + @Override + public MetaAcceptorShowTablesOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public MetaAcceptorShowTablesOperator database(Database database) { + metaAcceptorSet.adapt(database); + actual.database(database); + return self(); + } + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorShowTablesOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorShowTablesOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorUpdateOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorUpdateOperator.java new file mode 100644 index 00000000..35d886ba --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/MetaAcceptorUpdateOperator.java @@ -0,0 +1,261 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +/** + * wrapper for {@link UpdateOperator} from {@link MetaAcceptorSet} + * + * @author j.x + * @date 2024/4/21 16:08 + * @since 1.1.8 + */ +@AllArgsConstructor +public class MetaAcceptorUpdateOperator implements UpdateOperator, WrapperOperator { + + private UpdateOperator actual; + @NotNull + private final MetaAcceptorSet metaAcceptorSet; + + @Override + public MetaAcceptorUpdateOperator updates(Map values) { + actual.updates(values); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator strictFill(String f, Supplier v) { + actual.strictFill(f, v); + return self(); + } + + @Override + public String getPrepareDSL() { + return actual.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return actual.getPrepareValues(); + } + + @Override + public MetaAcceptorUpdateOperator from(Table table) { + metaAcceptorSet.adapt(table); + actual.from(table); + return self(); + } + + @Override + public Table getTable() { + return actual.getTable(); + } + + @Override + public String getDSL() { + return actual.getDSL(); + } + + @Override + public MetaAcceptorUpdateOperator parse(String dsl) { + this.actual = actual.parse(dsl); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator customize(UnaryOperator operatorFunc) { + this.actual = actual.customize(operatorFunc); + return self(); + } + + @Override + public void reset() { + actual.reset(); + } + + @Override + public void setDBType(DBType dbType) { + actual.setDBType(dbType); + } + + @Override + public DBType getDBType() { + return actual.getDBType(); + } + + @Override + public MetaAcceptorUpdateOperator gt(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.gt(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator gte(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.gte(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator lt(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.lt(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator lte(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.lte(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator eq(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.eq(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator neq(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.neq(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator notNull(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.notNull(dslName); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator isNull(DSLName dslName) { + metaAcceptorSet.adapt(dslName); + actual.isNull(dslName); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator in(DSLName dslName, V... values) { + metaAcceptorSet.adapt(dslName); + actual.in(dslName, values); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator notIn(DSLName dslName, V... values) { + metaAcceptorSet.adapt(dslName); + actual.notIn(dslName, values); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator between(DSLName dslName, Object withValue, Object endValue) { + metaAcceptorSet.adapt(dslName); + actual.between(dslName, withValue, endValue); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator notBetween(DSLName dslName, Object withValue, Object endValue) { + metaAcceptorSet.adapt(dslName); + actual.notBetween(dslName, withValue, endValue); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator like(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.like(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator $like(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$like(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator like$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.like$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator $like$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$like$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator notLike(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.notLike(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator $notLike(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$notLike(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator notLike$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.notLike$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator $notLike$(DSLName dslName, Object value) { + metaAcceptorSet.adapt(dslName); + actual.$notLike$(dslName, value); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator or() { + actual.or(); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator and() { + actual.and(); + return self(); + } + + @Override + public MetaAcceptorUpdateOperator nor() { + actual.nor(); + return self(); + } + + @Override + public MetaAcceptorSet obtainMetaAcceptorSet() { + return metaAcceptorSet; + } + + @Override + public > T getActual() { + return (T) actual; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/OperatorGroup.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/OperatorGroup.java new file mode 100644 index 00000000..4c152deb --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/OperatorGroup.java @@ -0,0 +1,397 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.MetaAcceptorSet; +import cc.allio.uno.data.orm.dsl.Operator; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.SPIOperatorHelper; +import cc.allio.uno.data.orm.dsl.ddl.*; +import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; +import cc.allio.uno.data.orm.dsl.dml.InsertOperator; +import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; + +/** + * 操作管理接口 + * + * @author j.x + * @date 2023/4/13 18:52 + * @see Operator + * @since 1.1.4 + */ +public interface OperatorGroup { + + // ======================== DML ======================== + + /** + * base on {@link DBType#getSystemDbType()} get {@link QueryOperator} + * + * @return the {@link QueryOperator} instance + */ + default QueryOperator query() { + return query(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link QueryOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link QueryOperator} instance + */ + QueryOperator query(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getQueryOperator(Class queryOperatorClass, OperatorKey operatorKey) { + return getOperator(queryOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getQueryOperator(Class queryOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(queryOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link InsertOperator} + * + * @return the {@link InsertOperator} instance + */ + default InsertOperator insert() { + return insert(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link InsertOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link InsertOperator} instance + */ + InsertOperator insert(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getInsertOperator(Class insertOperatorClass, OperatorKey operatorKey) { + return getOperator(insertOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getInsertOperator(Class insertOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(insertOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link UpdateOperator} + * + * @return the {@link UpdateOperator} instance + */ + default UpdateOperator update() { + return update(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link UpdateOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link UpdateOperator} instance + */ + UpdateOperator update(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getUpdateOperator(Class updateOperatorClass, OperatorKey operatorKey) { + return getOperator(updateOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getUpdateOperator(Class updateOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(updateOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link DeleteOperator} + * + * @return the {@link DeleteOperator} instance + */ + default DeleteOperator delete() { + return delete(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link DeleteOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link DeleteOperator} instance + */ + DeleteOperator delete(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getDeleteOperator(Class deleteOperatorClass, OperatorKey operatorKey) { + return getOperator(deleteOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getDeleteOperator(Class deleteOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(deleteOperatorClass, operatorKey, dbType); + } + + // ======================== DDL ======================== + + /** + * base on {@link DBType#getSystemDbType()} get {@link CreateTableOperator} + * + * @return the {@link CreateTableOperator} instance + */ + default CreateTableOperator createTable() { + return createTable(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link CreateTableOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link CreateTableOperator} instance + */ + CreateTableOperator createTable(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getCreateTableOperator(Class createTableOperatorClass, OperatorKey operatorKey) { + return getOperator(createTableOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getCreateTableOperator(Class createTableOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(createTableOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link DropTableOperator} + * + * @return the {@link DropTableOperator} instance + */ + default DropTableOperator dropTable() { + return dropTable(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link DropTableOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link DropTableOperator} instance + */ + DropTableOperator dropTable(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getDropTableOperator(Class dropTableOperatorClass, OperatorKey operatorKey) { + return getOperator(dropTableOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getDropTableOperator(Class dropTableOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(dropTableOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link ExistTableOperator} + * + * @return the {@link ExistTableOperator} instance + */ + default ExistTableOperator existTable() { + return existTable(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link ExistTableOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link ExistTableOperator} instance + */ + ExistTableOperator existTable(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getExistTableOperator(Class existTableOperatorClass, OperatorKey operatorKey) { + return getOperator(existTableOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getExistTableOperator(Class existTableOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(existTableOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link ShowColumnsOperator} + * + * @return the {@link ShowColumnsOperator} instance + */ + default ShowColumnsOperator showColumns() { + return showColumns(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link ShowColumnsOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link ShowColumnsOperator} instance + */ + ShowColumnsOperator showColumns(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getShowColumnsOperator(Class showColumnsOperatorClass, OperatorKey operatorKey) { + return getOperator(showColumnsOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getShowColumnsOperator(Class showColumnsOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(showColumnsOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link ShowTablesOperator} + * + * @return the {@link ShowTablesOperator} instance + */ + default ShowTablesOperator showTables() { + return showTables(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link ShowTablesOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link ShowTablesOperator} instance + */ + ShowTablesOperator showTables(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getShowTablesOperator(Class showTablesOperatorClass, OperatorKey operatorKey) { + return getOperator(showTablesOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getShowTablesOperator(Class showTablesOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(showTablesOperatorClass, operatorKey, dbType); + } + + /** + * base on {@link DBType#getSystemDbType()} get {@link AlterTableOperator} + * + * @return the {@link AlterTableOperator} instance + */ + default AlterTableOperator alterTables() { + return alterTables(DBType.getSystemDbType()); + } + + /** + * base on {@link DBType} get {@link AlterTableOperator} + * + * @param dbType the {@link DBType} instance + * @return the {@link AlterTableOperator} instance + */ + AlterTableOperator alterTables(DBType dbType); + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getAlterTableOperator(Class alterTableOperatorClass, OperatorKey operatorKey) { + return getOperator(alterTableOperatorClass, operatorKey, null); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getAlterTableOperator(Class alterTableOperatorClass, OperatorKey operatorKey, DBType dbType) { + return getOperator(alterTableOperatorClass, operatorKey, dbType); + } + + /** + * base on operator key gain {@link OperatorGroup} + * + * @return {@link OperatorGroup} or null + */ + static OperatorGroup getOperatorGroup(OperatorKey operatorKey, MetaAcceptorSet metaAcceptorSet) { + if (operatorKey != null) { + return new MetaAcceptorSetOperatorGroup(operatorKey, metaAcceptorSet); + } + return null; + } + + /** + * 根据operator的class获取指定operator实例. + *
    + *
  • operator key = {@link OperatorKey#getSystemOperatorKey()}
  • + *
  • dbtype = {@link DBType#getSystemDbType()}
  • + *
+ * + * @param operatorClass operatorClass + * @param SQLOperator + * @return SQLOperator + * @throws IllegalArgumentException operatorClass is null + * @throws NullPointerException An operation that does not exist + */ + static > T getOperator(Class operatorClass) { + return getOperator(operatorClass, OperatorKey.getSystemOperatorKey(), DBType.getSystemDbType()); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getOperator(Class operatorClass, OperatorKey operatorKey) { + return getOperator(operatorClass, operatorKey, DBType.getSystemDbType()); + } + + /** + * @see #getOperator(Class, OperatorKey, DBType) + */ + static > T getOperator(Class operatorClass, DBType dbType) { + return getOperator(operatorClass, OperatorKey.getSystemOperatorKey(), dbType); + } + + /** + * 根据operator的class获取指定operator实例 + * + * @param operatorClass operatorClass + * @param operatorKey operatorKey + * @param dbType dbtype + * @param generic of Operator type + * @return Operator + * @throws IllegalArgumentException operatorClass is null + * @throws NullPointerException An operation that does not exist + * @see OperatorKey + * @see SPIOperatorHelper#lazyGet(Class, OperatorKey, DBType) + */ + static > T getOperator(Class operatorClass, OperatorKey operatorKey, DBType dbType) { + return SPIOperatorHelper.lazyGet(operatorClass, operatorKey, dbType); + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/WrapperOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/WrapperOperator.java new file mode 100644 index 00000000..0677b008 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/opeartorgroup/WrapperOperator.java @@ -0,0 +1,21 @@ +package cc.allio.uno.data.orm.dsl.opeartorgroup; + +import cc.allio.uno.data.orm.dsl.Operator; + +/** + * describe how to wrapper {@link Operator} + * + * @author j.x + * @date 2024/4/21 17:24 + * @since 1.1.8 + */ +public interface WrapperOperator { + + /** + * get actual {@link Operator} + * + * @param the {@link Operator} type + * @return actual operator + */ + > T getActual(); +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DBType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DBType.java index 78c120bf..ec7fb7ec 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DBType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DBType.java @@ -44,7 +44,7 @@ public interface DBType { DBType MARIADB = new DefaultDBType("MariaDB", DatabaseDriver.MARIADB.getDriverClassName(), DBCategory.RELATIONAL, "jdbc:mariadb://" + IP_PORTS_TEMPLATE + "/" + DATABASE_NAME_TEMPLATE); DBType SQLITE = new DefaultDBType("SQLite", DatabaseDriver.SQLITE.getDriverClassName(), DBCategory.RELATIONAL, "jdbc:sqlite::memory:"); DBType H2 = new DefaultDBType("H2", DatabaseDriver.H2.getDriverClassName(), DBCategory.RELATIONAL, "jdbc:h2:mem:" + DATABASE_NAME_TEMPLATE + ";IGNORECASE=TRUE"); - DBType ELASTIC_SEARCH = new DefaultDBType("ElasticSearch", StringPool.EMPTY, DBCategory.SEARCH_ENGINES, ""); + DBType ELASTICSEARCH = new DefaultDBType("ElasticSearch", StringPool.EMPTY, DBCategory.SEARCH_ENGINES, ""); DBType MONGODB = new DefaultDBType("Mongodb", "org.mongodb.Driver", DBCategory.DOCUMENT, ""); DBType TD_ENGINE = new DefaultDBType("TDEngine", StringPool.EMPTY, DBCategory.TIME_SERIES, ""); DBType INFLUXDB = new DefaultDBType("Influxdb", StringPool.EMPTY, DBCategory.TIME_SERIES, ""); @@ -54,7 +54,7 @@ public interface DBType { /** * 类型集合 */ - List ALL_DB_TYPES = Lists.newArrayList(MYSQL, POSTGRESQL, SQLSERVER, ORACLE, OPEN_GAUSS, DB2, MARIADB, SQLITE, H2, ELASTIC_SEARCH, MONGODB, TD_ENGINE, INFLUXDB, NEO4J, REDIS); + List ALL_DB_TYPES = Lists.newArrayList(MYSQL, POSTGRESQL, SQLSERVER, ORACLE, OPEN_GAUSS, DB2, MARIADB, SQLITE, H2, ELASTICSEARCH, MONGODB, TD_ENGINE, INFLUXDB, NEO4J, REDIS); /** * 获取数据库类型名称 diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/AbstractCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/AbstractCommandExecutor.java index 1eacde07..3494aadd 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/AbstractCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/AbstractCommandExecutor.java @@ -54,7 +54,7 @@ public boolean bool(Operator operator, CommandType commandType, ResultSetHand * @throws DSLException invoke process has error */ protected boolean doBool(Operator operator, CommandType commandType, ResultSetHandler resultSetHandler) { - InnerCommandExecutorManager manager = getManager(); + InnerCommandExecutorManager manager = getInnerCommandExecutorManager(); if (manager == null) { throw new DSLException("inner command executor manager is null, can't execute operator"); } @@ -98,7 +98,7 @@ public List queryList(Operator queryOperator, CommandType commandType, * @throws DSLException query failed throw */ protected List doQueryList(Operator operator, CommandType commandType, ListResultSetHandler resultSetHandler) { - InnerCommandExecutorManager manager = getManager(); + InnerCommandExecutorManager manager = getInnerCommandExecutorManager(); if (manager == null) { throw new DSLException("inner command executor manager is null, can't execute operator"); } @@ -152,7 +152,5 @@ T aspect(Operator operator, CommandType commandType, Supplier operate) * * @return InnerCommandExecutorManager */ - protected InnerCommandExecutorManager getManager() { - return null; - } + protected abstract InnerCommandExecutorManager getInnerCommandExecutorManager(); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutor.java index ef537d23..96da6996 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutor.java @@ -3,8 +3,8 @@ import cc.allio.uno.core.env.Envs; import cc.allio.uno.core.util.CollectionUtils; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.executor.handler.*; import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; @@ -102,21 +102,21 @@ default T checkCollectionIsOneAndGet(Collection collection) { /** * 获取执行器key * - * @return ExecutorKey + * @return the {@link ExecutorKey} instance */ ExecutorKey getKey(); /** * 获取DSL操作元数据。 * - * @return OperatorMetadata实例 + * @return the {@link OperatorGroup} instance */ OperatorGroup getOperatorGroup(); /** * 获取执行器参数 * - * @return ExecutorOptions + * @return the {@link ExecutorOptions} instance */ ExecutorOptions getOptions(); diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorFactory.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorFactory.java index eeb02bfd..b57db13c 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorFactory.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorFactory.java @@ -27,7 +27,7 @@ private CommandExecutorFactory() { * @return command executor */ public static T register(T commandExecutor) { - return getRegistry().registerCommandExecutor(commandExecutor.getOptions(), () -> commandExecutor, true); + return getRegistry().register(commandExecutor.getOptions(), () -> commandExecutor, true); } /** diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistry.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistry.java index 82c0b7d6..8d5672b0 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistry.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistry.java @@ -17,6 +17,15 @@ */ public interface CommandExecutorRegistry { + /** + * 注册 command executor,如果存在则不在进行设置 + * + * @param executorOptions executorOptions + * @param commandExecutorSupplier commandExecutorSupplier + * @param ifPresent if true 如果已经存在则进行覆盖 否则不进行覆盖 + */ + T register(ExecutorOptions executorOptions, Supplier commandExecutorSupplier, boolean ifPresent); + /** * 基于给定的{@link ExecutorOptions}从{@link CommandExecutorLoader} * @@ -35,15 +44,6 @@ public interface CommandExecutorRegistry { */ T createAndRegister(@NotNull ExecutorOptions executorOptions); - /** - * 注册 command executor,如果存在则不在进行设置 - * - * @param executorOptions executorOptions - * @param commandExecutorSupplier commandExecutorSupplier - * @param ifPresent if true 如果已经存在则进行覆盖 否则不进行覆盖 - */ - T registerCommandExecutor(ExecutorOptions executorOptions, Supplier commandExecutorSupplier, boolean ifPresent); - /** * 基于最佳匹配原则获取最可能存在的{@link ExecutorOptions},在基于此获取{@link AggregateCommandExecutor}实例 * diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistryImpl.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistryImpl.java index 4dda1350..3bb96ead 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistryImpl.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandExecutorRegistryImpl.java @@ -29,7 +29,9 @@ public class CommandExecutorRegistryImpl implements CommandExecutorRegistry { private final ReadWriteLock lock = new ReentrantReadWriteLock(); - // 以唯一标识作为key的Map + /** + * base on {@link ExecutorKey#key()} as the map key, the map value is {@link AggregateCommandExecutor} + */ private final Map commandExecutorMap = Maps.newConcurrentMap(); // 以option作为key,获取commandExecutor的的唯一标识 // option的equals是根据 DBType ExecutorKey OperatorKey @@ -70,11 +72,11 @@ public T createAndRegister(@NotNull Executo if (executorLoader == null) { return null; } - return (T) registerCommandExecutor(executorOptions, () -> executorLoader.load(executorOptions), true); + return (T) register(executorOptions, () -> executorLoader.load(executorOptions), true); } @Override - public T registerCommandExecutor(ExecutorOptions executorOptions, Supplier commandExecutorSupplier, boolean ifPresent) { + public T register(ExecutorOptions executorOptions, Supplier commandExecutorSupplier, boolean ifPresent) { Lock writeLock = lock.writeLock(); writeLock.lock(); try { @@ -84,11 +86,11 @@ public T registerCommandExecutor(ExecutorOp } T commandExecutor; if (ifPresent) { - commandExecutor = (T) commandExecutorMap.compute(key, (k, v) -> commandExecutorSupplier.get()); - optionsKeyMap.compute(executorOptions, (k, v) -> key); + commandExecutor = (T) commandExecutorMap.compute(key, (_, _) -> commandExecutorSupplier.get()); + optionsKeyMap.compute(executorOptions, (_, _) -> key); } else { - commandExecutor = (T) commandExecutorMap.computeIfAbsent(key, k -> commandExecutorSupplier.get()); - optionsKeyMap.computeIfAbsent(executorOptions, k -> key); + commandExecutor = (T) commandExecutorMap.computeIfAbsent(key, _ -> commandExecutorSupplier.get()); + optionsKeyMap.computeIfAbsent(executorOptions, _ -> key); } boolean systemDefault = executorOptions.isSystemDefault(); if (systemDefault) { diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java index 06d299aa..5f33d839 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/CommandType.java @@ -32,7 +32,7 @@ public enum CommandType { SHOW_COLUMNS(ShowColumnsOperator.class), SHOW_TABLES(ShowTablesOperator.class); - private final Class> operatorClass; + private final Class operatorClass; /** * base on operator class get command type, if {@link CommandType} self operatorClass is null, return null @@ -46,7 +46,7 @@ public static > CommandType getByOperatorClass(Class o) return null; } // try get hirachical - Class> hireachialType = Operator.getHierarchicalType(o); + Class> hireachialType = cc.allio.uno.data.orm.dsl.Operator.getHierarchicalType(o); for (CommandType commandType : values()) { if (commandType == UNKNOWN || commandType == FLUSH) { continue; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DDLCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DDLCommandExecutor.java index fc1e892c..c7d5749e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DDLCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DDLCommandExecutor.java @@ -31,21 +31,21 @@ public interface DDLCommandExecutor extends CommandExecutor { * * @see #alertTable(AlterTableOperator) */ - default boolean alertTable(Class entityType, UnaryOperator func) { + default boolean alertTable(Class entityType, UnaryOperator> func) { return alertTable(func.apply(getOperatorGroup().alterTables().from(entityType))); } /** * @see #alertTable(AlterTableOperator) */ - default boolean alertTable(UnaryOperator func) { + default boolean alertTable(UnaryOperator> func) { return alertTable(func.apply(getOperatorGroup().alterTables())); } /** * @see #bool(Operator, CommandType) */ - default boolean alertTable(AlterTableOperator alterTableOperator) { + default boolean alertTable(AlterTableOperator alterTableOperator) { return bool(alterTableOperator, CommandType.ALERT_TABLE); } @@ -56,7 +56,7 @@ default boolean alertTable(AlterTableOperator alterTableOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean alertTable(AlterTableOperator alterTableOperator, ResultSetHandler resultSetHandler) { + default boolean alertTable(AlterTableOperator alterTableOperator, ResultSetHandler resultSetHandler) { return bool(alterTableOperator, CommandType.ALERT_TABLE, resultSetHandler); } @@ -77,7 +77,7 @@ default

boolean createTable(Class

pojoClass) { * @param func the func * @return true 成功 false 失败 */ - default boolean createTable(UnaryOperator func) { + default boolean createTable(UnaryOperator> func) { return createTable(func.apply(getOperatorGroup().createTable(getOptions().getDbType()))); } @@ -87,7 +87,7 @@ default boolean createTable(UnaryOperator func) { * @param createTableOperator CreateTableOperator * @return true 成功 false 失败 */ - default boolean createTable(CreateTableOperator createTableOperator) { + default boolean createTable(CreateTableOperator createTableOperator) { return bool(createTableOperator, CommandType.CREATE_TABLE); } @@ -98,7 +98,7 @@ default boolean createTable(CreateTableOperator createTableOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean createTable(CreateTableOperator createTableOperator, ResultSetHandler resultSetHandler) { + default boolean createTable(CreateTableOperator createTableOperator, ResultSetHandler resultSetHandler) { return bool(createTableOperator, CommandType.CREATE_TABLE, resultSetHandler); } @@ -149,7 +149,7 @@ default

boolean dropTable(Class

pojoClass) { * @param func func * @return true 成功 false 失败 */ - default boolean dropTable(UnaryOperator func) { + default boolean dropTable(UnaryOperator> func) { return dropTable(func.apply(getOperatorGroup().dropTable(getOptions().getDbType()))); } @@ -159,7 +159,7 @@ default boolean dropTable(UnaryOperator func) { * @param dropTableOperator dropTableOperator * @return true 成功 false 失败 */ - default boolean dropTable(DropTableOperator dropTableOperator) { + default boolean dropTable(DropTableOperator dropTableOperator) { return bool(dropTableOperator, CommandType.DELETE_TABLE); } @@ -170,7 +170,7 @@ default boolean dropTable(DropTableOperator dropTableOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean dropTable(DropTableOperator dropTableOperator, ResultSetHandler resultSetHandler) { + default boolean dropTable(DropTableOperator dropTableOperator, ResultSetHandler resultSetHandler) { return bool(dropTableOperator, CommandType.DELETE, resultSetHandler); } @@ -211,7 +211,7 @@ default

boolean existTable(Class

pojoClass) { * @param func the func * @return true 成功 false 失败 */ - default boolean existTable(UnaryOperator func) { + default boolean existTable(UnaryOperator> func) { return existTable(func.apply(getOperatorGroup().existTable(getOptions().getDbType()))); } @@ -221,7 +221,7 @@ default boolean existTable(UnaryOperator func) { * @param existTableOperator ExistTableOperator * @return true 成功 false 失败 */ - default boolean existTable(ExistTableOperator existTableOperator) { + default boolean existTable(ExistTableOperator existTableOperator) { return bool(existTableOperator, CommandType.EXIST_TABLE); } @@ -232,7 +232,7 @@ default boolean existTable(ExistTableOperator existTableOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean existTable(ExistTableOperator existTableOperator, ResultSetHandler resultSetHandler) { + default boolean existTable(ExistTableOperator existTableOperator, ResultSetHandler resultSetHandler) { return bool(existTableOperator, CommandType.EXIST_TABLE, resultSetHandler); } @@ -273,7 +273,7 @@ default List showColumns(Table table) { * @param func the func * @return ColumnDef */ - default List showColumns(UnaryOperator func) { + default List showColumns(UnaryOperator> func) { return showColumns(func.apply(getOperatorGroup().showColumns(getOptions().getDbType()))); } @@ -283,7 +283,7 @@ default List showColumns(UnaryOperator func) { * @param showColumnsOperator showColumnsOperator * @return ColumnDef */ - default List showColumns(ShowColumnsOperator showColumnsOperator) { + default List showColumns(ShowColumnsOperator showColumnsOperator) { return queryList(showColumnsOperator, CommandType.SHOW_COLUMNS, getOptions().obtainColumnDefListResultSetHandler()); } @@ -322,7 +322,7 @@ default List

showTables(Database database) { * @param func func * @return List
*/ - default List
showTables(UnaryOperator func) { + default List
showTables(UnaryOperator> func) { return showTables(func.apply(getOperatorGroup().showTables(getOptions().getDbType()))); } @@ -332,7 +332,7 @@ default List
showTables(UnaryOperator func) { * @param showTablesOperator showTablesOperator * @return List
*/ - default List
showTables(ShowTablesOperator showTablesOperator) { + default List
showTables(ShowTablesOperator showTablesOperator) { return queryList(showTablesOperator, CommandType.SHOW_TABLES, getOptions().obtainTableListResultSetHandler()); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java index b69c005f..4d7271da 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java @@ -73,8 +73,8 @@ default

boolean batchInsertPojos(List

pojos) { * @param func the func * @return true 成功 false 失败 */ - default boolean insert(Class pojoClass, UnaryOperator func) { - InsertOperator insertOperator = getOperatorGroup().insert(getOptions().getDbType()); + default boolean insert(Class pojoClass, UnaryOperator> func) { + InsertOperator insertOperator = getOperatorGroup().insert(getOptions().getDbType()); return insert(func.apply(insertOperator.from(pojoClass))); } @@ -84,7 +84,7 @@ default boolean insert(Class pojoClass, UnaryOperator fun * @param func the func * @return true 成功 false 失败 */ - default boolean insert(UnaryOperator func) { + default boolean insert(UnaryOperator> func) { return insert(func.apply(getOperatorGroup().insert(getOptions().getDbType()))); } @@ -94,7 +94,7 @@ default boolean insert(UnaryOperator func) { * @param insertOperator insertOperator * @return true 成功 false 失败 */ - default boolean insert(InsertOperator insertOperator) { + default boolean insert(InsertOperator insertOperator) { return bool(insertOperator, CommandType.INSERT); } @@ -105,7 +105,7 @@ default boolean insert(InsertOperator insertOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean insert(InsertOperator insertOperator, ResultSetHandler resultSetHandler) { + default boolean insert(InsertOperator insertOperator, ResultSetHandler resultSetHandler) { return bool(insertOperator, CommandType.INSERT, resultSetHandler); } @@ -128,7 +128,7 @@ default

boolean updatePojo(P pojo) { default boolean updatePojoById(P pojo, ID id) { PojoWrapper

pojoWrapper = PojoWrapper.getInstance(pojo); ColumnDef theId = pojoWrapper.getPkColumn(); - return updatePojoByCondition(pojo, condition -> condition.eq(theId.getDslName(), id)); + return updatePojoByCondition(pojo, condition -> (WhereOperator) condition.eq(theId.getDslName(), id)); } /** @@ -137,10 +137,10 @@ default boolean updatePojoById(P pojo, ID id) { * @param pojo the pojo * @return true 成功 false 失败 */ - default

boolean updatePojoByCondition(P pojo, UnaryOperator> condition) { + default

boolean updatePojoByCondition(P pojo, UnaryOperator> condition) { return update(o -> { PojoWrapper

pojoWrapper = PojoWrapper.getInstance(pojo); - UpdateOperator updateOperator = o.from(pojoWrapper.getTable()).updatePojo(pojo); + UpdateOperator updateOperator = o.from(pojoWrapper.getTable()).updatePojo(pojo); condition.apply(updateOperator); return updateOperator; }); @@ -154,8 +154,8 @@ default

boolean updatePojoByCondition(P pojo, UnaryOperator boolean update(Class pojoClass, UnaryOperator func) { - UpdateOperator updateOperator = getOperatorGroup().update(getOptions().getDbType()); + default boolean update(Class pojoClass, UnaryOperator> func) { + UpdateOperator updateOperator = getOperatorGroup().update(getOptions().getDbType()); return update(func.apply(updateOperator.from(pojoClass))); } @@ -165,7 +165,7 @@ default boolean update(Class pojoClass, UnaryOperator fun * @param func the func * @return true 成功 false 失败 */ - default boolean update(UnaryOperator func) { + default boolean update(UnaryOperator> func) { return update(func.apply(getOperatorGroup().update(getOptions().getDbType()))); } @@ -175,7 +175,7 @@ default boolean update(UnaryOperator func) { * @param updateOperator updateOperator * @return true 成功 false 失败 */ - default boolean update(UpdateOperator updateOperator) { + default boolean update(UpdateOperator updateOperator) { return bool(updateOperator, CommandType.UPDATE); } @@ -186,7 +186,7 @@ default boolean update(UpdateOperator updateOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean update(UpdateOperator updateOperator, ResultSetHandler resultSetHandler) { + default boolean update(UpdateOperator updateOperator, ResultSetHandler resultSetHandler) { return bool(updateOperator, CommandType.UPDATE, resultSetHandler); } @@ -217,7 +217,7 @@ default boolean deleteById(Class pojoClass, ID i if (pkColumn == null) { throw new DSLException("Can not find the primary key column"); } - UpdateOperator update = getOperatorGroup().update(getOptions().getDbType()); + UpdateOperator update = getOperatorGroup().update(getOptions().getDbType()); return bool(update.from(pojoClass).eq(pkColumn.getDslName(), id), CommandType.DELETE); } @@ -233,7 +233,7 @@ default boolean deleteAll(Class pojoClass) { if (pkColumn == null) { throw new DSLException("Can not find the primary key column"); } - UpdateOperator update = getOperatorGroup().update(getOptions().getDbType()); + UpdateOperator update = getOperatorGroup().update(getOptions().getDbType()); return bool(update.from(pojoClass), CommandType.DELETE); } @@ -270,7 +270,7 @@ default boolean deleteAllById(Class pojoClass, I if (pkColumn == null) { throw new DSLException("Can not find the primary key column"); } - UpdateOperator update = getOperatorGroup().update(getOptions().getDbType()); + UpdateOperator update = getOperatorGroup().update(getOptions().getDbType()); return bool(update.from(pojoClass).in(pkColumn.getDslName(), ids), CommandType.DELETE); } @@ -281,8 +281,8 @@ default boolean deleteAllById(Class pojoClass, I * @param pojoClass pojoClass * @return true 成功 false 失败 */ - default boolean delete(Class pojoClass, UnaryOperator func) { - DeleteOperator deleteOperator = getOperatorGroup().delete(getOptions().getDbType()); + default boolean delete(Class pojoClass, UnaryOperator> func) { + DeleteOperator deleteOperator = getOperatorGroup().delete(getOptions().getDbType()); return delete(func.apply(deleteOperator.from(pojoClass))); } @@ -292,7 +292,7 @@ default boolean delete(Class pojoClass, UnaryOperator fun * @param func the func * @return true 成功 false 失败 */ - default boolean delete(UnaryOperator func) { + default boolean delete(UnaryOperator> func) { return delete(func.apply(getOperatorGroup().delete(getOptions().getDbType()))); } @@ -302,7 +302,7 @@ default boolean delete(UnaryOperator func) { * @param deleteOperator deleteOperator * @return true 成功 false 失败 */ - default boolean delete(DeleteOperator deleteOperator) { + default boolean delete(DeleteOperator deleteOperator) { return bool(deleteOperator, CommandType.DELETE); } @@ -313,7 +313,7 @@ default boolean delete(DeleteOperator deleteOperator) { * @param resultSetHandler resultSetHandler * @return true 成功 false 失败 */ - default boolean delete(DeleteOperator deleteOperator, ResultSetHandler resultSetHandler) { + default boolean delete(DeleteOperator deleteOperator, ResultSetHandler resultSetHandler) { return bool(deleteOperator, CommandType.DELETE, resultSetHandler); } @@ -355,7 +355,7 @@ default

boolean saveOrUpdate(P pojo, BooleanSupplier dataExist, MethodRefere return saveOrUpdate( dataExist, () -> { - UpdateOperator updateOperator = getOperatorGroup().update(getOptions().getDbType()); + UpdateOperator updateOperator = getOperatorGroup().update(getOptions().getDbType()); updateOperator.from(pojoWrapper.getTable()); String column = eqUpdate.getColumn(); // update for eq @@ -368,7 +368,7 @@ default

boolean saveOrUpdate(P pojo, BooleanSupplier dataExist, MethodRefere return updateOperator; }, () -> { - InsertOperator insertOperator = getOperatorGroup().insert(getOptions().getDbType()); + InsertOperator insertOperator = getOperatorGroup().insert(getOptions().getDbType()); insertOperator.from(pojoWrapper.getTable()); insertOperator.insertPojo(pojoWrapper.getPojo()); return insertOperator; @@ -383,7 +383,7 @@ default

boolean saveOrUpdate(P pojo, BooleanSupplier dataExist, MethodRefere * @param f3 如果数据不存在则insert * @return true 成功 false 失败 */ - default boolean saveOrUpdate(BooleanSupplier f1, UnaryOperator f2, UnaryOperator f3) { + default boolean saveOrUpdate(BooleanSupplier f1, UnaryOperator> f2, UnaryOperator> f3) { return saveOrUpdate(f1, () -> f2.apply(getOperatorGroup().update(getOptions().getDbType())), () -> f3.apply(getOperatorGroup().insert(getOptions().getDbType()))); } @@ -395,7 +395,7 @@ default boolean saveOrUpdate(BooleanSupplier f1, UnaryOperator f * @param insertOperator 如果数据不存在则insert * @return true 成功 false 失败 */ - default boolean saveOrUpdate(BooleanSupplier dataExist, UpdateOperator updateOperator, InsertOperator insertOperator) { + default boolean saveOrUpdate(BooleanSupplier dataExist, UpdateOperator updateOperator, InsertOperator insertOperator) { return saveOrUpdate(dataExist, () -> updateOperator, () -> insertOperator); } @@ -408,7 +408,7 @@ default boolean saveOrUpdate(BooleanSupplier dataExist, UpdateOperator updateOpe * @param insertOperator 如果数据不存在则insert * @return true 成功 false 失败 */ - default boolean saveOrUpdate(BooleanSupplier dataExist, Supplier updateOperator, Supplier insertOperator) { + default boolean saveOrUpdate(BooleanSupplier dataExist, Supplier> updateOperator, Supplier> insertOperator) { boolean isExist = dataExist.getAsBoolean(); if (!isExist) { return insert(insertOperator.get()); @@ -450,7 +450,7 @@ default T queryOne(Class entityClass) { * @param 实体类型 * @return 实体 or null */ - default T queryOne(Class entityClass, UnaryOperator func) { + default T queryOne(Class entityClass, UnaryOperator> func) { return queryOne(func.apply(getOperatorGroup().query(getOptions().getDbType())), getOptions().obtainBeanResultSetHandler(entityClass)); } @@ -462,7 +462,7 @@ default T queryOne(Class entityClass, UnaryOperator func) * @param 实体类型 * @return 实体 or null */ - default T queryOne(Class entityClass, QueryOperator queryOperator) { + default T queryOne(Class entityClass, QueryOperator queryOperator) { return queryOne(queryOperator, getOptions().obtainBeanResultSetHandler(entityClass)); } @@ -472,7 +472,7 @@ default T queryOne(Class entityClass, QueryOperator queryOperator) { * @param func the func * @return ResultGroup */ - default ResultGroup queryOne(UnaryOperator func) { + default ResultGroup queryOne(UnaryOperator> func) { return queryOne(func.apply(getOperatorGroup().query(getOptions().getDbType()))); } @@ -482,7 +482,7 @@ default ResultGroup queryOne(UnaryOperator func) { * @param func the func * @return map */ - default Map queryMap(UnaryOperator func) { + default Map queryMap(UnaryOperator> func) { return queryMap(func.apply(getOperatorGroup().query(getOptions().getDbType()))); } @@ -492,7 +492,7 @@ default Map queryMap(UnaryOperator func) { * @param queryOperator queryOperator * @return map */ - default Map queryMap(QueryOperator queryOperator) { + default Map queryMap(QueryOperator queryOperator) { return queryOne(queryOperator, getOptions().obtainMapResultSetHandler()); } @@ -502,7 +502,7 @@ default Map queryMap(QueryOperator queryOperator) { * @param queryOperator queryOperator * @return ResultGroup */ - default ResultGroup queryOne(QueryOperator queryOperator) { + default ResultGroup queryOne(QueryOperator queryOperator) { return queryOne(queryOperator, getOptions().obtainDefaultResultSetHandler()); } @@ -513,7 +513,7 @@ default ResultGroup queryOne(QueryOperator queryOperator) { * @param 结果集对象 * @return ResultGroup */ - default R queryOne(UnaryOperator func, ResultSetHandler resultSetHandler) { + default R queryOne(UnaryOperator> func, ResultSetHandler resultSetHandler) { return queryOne(func.apply(getOperatorGroup().query(getOptions().getDbType())), resultSetHandler); } @@ -525,7 +525,7 @@ default R queryOne(UnaryOperator func, ResultSetHandler re * @return ResultGroup * @throws DSLException 当结果集大于1时抛出 */ - default R queryOne(QueryOperator queryOperator, ResultSetHandler resultSetHandler) { + default R queryOne(QueryOperator queryOperator, ResultSetHandler resultSetHandler) { List resultGroups = queryList(queryOperator); ResultGroup resultGroup = checkCollectionIsOneAndGet(resultGroups); return Optional.ofNullable(resultGroup).map(resultSetHandler).orElse(null); @@ -566,7 +566,7 @@ default List queryListByIds(Class entityClass * @param 类型 * @return list */ - default List queryList(Class entityClass, UnaryOperator func) { + default List queryList(Class entityClass, UnaryOperator> func) { return queryList( entityClass, func.apply( @@ -586,7 +586,7 @@ default List queryList(Class entityClass, UnaryOperator * @param 类型 * @return list */ - default List queryList(Class entityClass, QueryOperator queryOperator) { + default List queryList(Class entityClass, QueryOperator queryOperator) { return queryList(queryOperator, getOptions().obtainListBeanResultSetHandler(entityClass)); } @@ -596,7 +596,7 @@ default List queryList(Class entityClass, QueryOperator queryOperator) * @param func the func * @return list map */ - default List> queryListMap(UnaryOperator func) { + default List> queryListMap(UnaryOperator> func) { return queryListMap(func.apply(getOperatorGroup().query(getOptions().getDbType()))); } @@ -606,7 +606,7 @@ default List> queryListMap(UnaryOperator func * @param queryOperator queryOperator * @return list map */ - default List> queryListMap(QueryOperator queryOperator) { + default List> queryListMap(QueryOperator queryOperator) { return queryList(queryOperator, getOptions().obtainListMapResultHandler()); } @@ -617,7 +617,7 @@ default List> queryListMap(QueryOperator queryOperator) { * @return List * @throws DSLException query failed throw */ - default List queryList(UnaryOperator func) { + default List queryList(UnaryOperator> func) { return queryList(func.apply(getOperatorGroup().query(getOptions().getDbType()))); } @@ -628,7 +628,7 @@ default List queryList(UnaryOperator func) { * @return List * @throws DSLException query failed throw */ - default List queryList(QueryOperator queryOperator) { + default List queryList(QueryOperator queryOperator) { return queryList(queryOperator, getOptions().obtainDefaultListResultSetHandler()); } @@ -641,7 +641,7 @@ default List queryList(QueryOperator queryOperator) { * @return List * @throws DSLException query failed throw */ - default List queryList(UnaryOperator func, ListResultSetHandler resultSetHandler) { + default List queryList(UnaryOperator> func, ListResultSetHandler resultSetHandler) { return queryList(func.apply(getOperatorGroup().query(getOptions().getDbType())), resultSetHandler); } @@ -654,7 +654,7 @@ default List queryList(UnaryOperator func, ListResultSetHa * @return List * @throws DSLException query failed throw */ - default List queryList(QueryOperator queryOperator, ListResultSetHandler resultSetHandler) { + default List queryList(QueryOperator queryOperator, ListResultSetHandler resultSetHandler) { return queryList(queryOperator, CommandType.SELECT, resultSetHandler); } @@ -668,14 +668,15 @@ default List queryList(QueryOperator queryOperator, ListResultSetHandler< * @return List * @throws DSLException query failed throw */ - default IPage queryPage(IPage page, UnaryOperator func, Class entityClass) { + default IPage queryPage(IPage page, UnaryOperator> func, Class entityClass) { PojoWrapper pojoWrapper = PojoWrapper.getInstance(entityClass); return queryPage( page, - func.apply(getOperatorGroup() - .query(getOptions().getDbType()) - .selects(pojoWrapper.getColumnDSLName()) - .from(pojoWrapper.getTable())), + func.apply( + getOperatorGroup() + .query(getOptions().getDbType()) + .selects(pojoWrapper.getColumnDSLName()) + .from(pojoWrapper.getTable())), entityClass); } @@ -688,7 +689,7 @@ default IPage queryPage(IPage page, UnaryOperator func, * @return List * @throws DSLException query failed throw */ - default IPage queryPage(IPage page, QueryOperator queryOperator, Class entityClass) { + default IPage queryPage(IPage page, QueryOperator queryOperator, Class entityClass) { return queryPage(page, queryOperator, getOptions().obtainListBeanResultSetHandler(entityClass)); } @@ -700,7 +701,7 @@ default IPage queryPage(IPage page, QueryOperator queryOperator, Class * @return List * @throws DSLException query failed throw */ - default IPage> queryPageMap(IPage page, UnaryOperator func) { + default IPage> queryPageMap(IPage page, UnaryOperator> func) { return queryPageMap(page, func.apply(getOperatorGroup().query(getOptions().getDbType()))); } @@ -712,7 +713,7 @@ default IPage> queryPageMap(IPage page, UnaryOperator> queryPageMap(IPage page, QueryOperator queryOperator) { + default IPage> queryPageMap(IPage page, QueryOperator queryOperator) { return queryPage(page, queryOperator, getOptions().obtainListMapResultHandler()); } @@ -724,7 +725,7 @@ default IPage> queryPageMap(IPage page, QueryOperator que * @return List * @throws DSLException query failed throw */ - default IPage queryPage(IPage page, UnaryOperator func) { + default IPage queryPage(IPage page, UnaryOperator> func) { return queryPage(page, func.apply(getOperatorGroup().query(getOptions().getDbType()))); } @@ -736,7 +737,7 @@ default IPage queryPage(IPage page, UnaryOperator * @return List * @throws DSLException query failed throw */ - default IPage queryPage(IPage page, QueryOperator queryOperator) { + default IPage queryPage(IPage page, QueryOperator queryOperator) { return queryPage(page, queryOperator, getOptions().obtainDefaultListResultSetHandler()); } @@ -751,7 +752,7 @@ default IPage queryPage(IPage page, QueryOperator queryOperator) * @throws DSLException query failed throw * @see #queryList(QueryOperator, ListResultSetHandler) */ - default IPage queryPage(IPage page, UnaryOperator func, ListResultSetHandler resultSetHandler) { + default IPage queryPage(IPage page, UnaryOperator> func, ListResultSetHandler resultSetHandler) { return queryPage(page, func.apply(getOperatorGroup().query(getOptions().getDbType())), resultSetHandler); } @@ -766,7 +767,7 @@ default IPage queryPage(IPage page, UnaryOperator func, * @throws DSLException query failed throw * @see #queryList(QueryOperator, ListResultSetHandler) */ - default IPage queryPage(IPage page, QueryOperator queryOperator, ListResultSetHandler resultSetHandler) { + default IPage queryPage(IPage page, QueryOperator queryOperator, ListResultSetHandler resultSetHandler) { queryOperator.page(page.getCurrent(), page.getSize()); List r = queryList(queryOperator, resultSetHandler); Long count = queryOne( diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/ResultSet.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/ResultSet.java index c5c50d83..aa3103fa 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/ResultSet.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/ResultSet.java @@ -2,6 +2,7 @@ import lombok.Setter; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -12,12 +13,12 @@ * @date 2023/5/28 23:11 * @since 1.1.4 */ +@Setter public class ResultSet implements Iterable { /** * 结果数据 */ - @Setter private List resultGroups; @Override @@ -25,6 +26,6 @@ public Iterator iterator() { if (resultGroups != null) { return resultGroups.iterator(); } - throw new NullPointerException("resultGroups for list is empty"); + return Collections.emptyIterator(); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutor.java index 5305e9a9..43be0963 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutor.java @@ -2,6 +2,7 @@ import cc.allio.uno.data.orm.dsl.Operator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.WrapperOperator; import com.google.common.collect.Lists; import org.slf4j.Logger; @@ -16,7 +17,7 @@ * @modify 1.1.7 * @since 1.1.4 */ -public interface InnerCommandExecutor, H> { +public interface InnerCommandExecutor { /** * 执行类型 @@ -52,14 +53,19 @@ default R exec(Operator operator, H handler) throws Throwable { */ static > O castTo(Operator original, Class castFor) { try { - return castFor.cast(original); + if (original instanceof WrapperOperator wrapperOperator) { + Operator actual = wrapperOperator.getActual(); + return castFor.cast(actual); + } else { + return castFor.cast(original); + } } catch (ClassCastException ex) { throw new DSLException(String.format("original %s cast to %s failed", original.getClass().getName(), castFor.getName()), ex); } } /** - * sub-class support reality operator type + * subclass support reality operator type * * @return the operator type */ diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutorManager.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutorManager.java index 4c24f657..1938c870 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutorManager.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerCommandExecutorManager.java @@ -7,7 +7,6 @@ import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; import cc.allio.uno.data.orm.executor.CommandType; -import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; import com.google.common.collect.Maps; import java.util.Map; @@ -36,7 +35,7 @@ public class InnerCommandExecutorManager { * * @param operatorType the operatorType is not null * @param executor the executor is not null - * @param {@link InnerCommandExecutor#exec(Operator, ResultSetHandler)} return type + * @param {@link InnerCommandExecutor#exec(Operator, Object)} return type * @param {@link InnerCommandExecutor} operator */ public , H> void set(Class operatorType, InnerCommandExecutor executor) { diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerDefaultCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerDefaultCommandExecutor.java index 11ff63ee..bdbc631c 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerDefaultCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerDefaultCommandExecutor.java @@ -10,5 +10,5 @@ * @date 2024/3/15 06:42 * @since 1.1.7 */ -public interface InnerDefaultCommandExecutor> extends InnerCommandExecutor> { +public interface InnerDefaultCommandExecutor extends InnerCommandExecutor> { } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerListCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerListCommandExecutor.java index 960daa72..5787489b 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerListCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/internal/InnerListCommandExecutor.java @@ -12,5 +12,5 @@ * @date 2024/3/15 06:39 * @since 1.1.7 */ -public interface InnerListCommandExecutor> extends InnerCommandExecutor, O, ListResultSetHandler> { +public interface InnerListCommandExecutor extends InnerCommandExecutor, O, ListResultSetHandler> { } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptions.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptions.java index fe8b8898..015d8965 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptions.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptions.java @@ -1,6 +1,7 @@ package cc.allio.uno.data.orm.executor.options; import cc.allio.uno.core.api.OptionalContext; +import cc.allio.uno.data.orm.dsl.MetaAcceptorSet; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.data.orm.executor.handler.ExecutorResultHandlerSet; @@ -16,7 +17,7 @@ * @date 2024/2/14 21:07 * @since 1.1.7 */ -public interface ExecutorOptions extends ExecutorResultHandlerSet, OptionalContext { +public interface ExecutorOptions extends ExecutorResultHandlerSet, MetaAcceptorSet, OptionalContext { String KEY_MARK = "key"; String DB_TYPE_MARK = "dbType"; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptionsImpl.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptionsImpl.java index d86ccebc..41e7218e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptionsImpl.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/options/ExecutorOptionsImpl.java @@ -2,7 +2,7 @@ import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.core.util.id.IdGenerator; -import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.executor.interceptor.Interceptor; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.data.orm.executor.internal.SPIInnerCommandScanner; @@ -31,6 +31,8 @@ public class ExecutorOptionsImpl extends SPIExecutorOptionsResultHandlerSet impl @Getter private final SPIInnerCommandScanner scanner; + private final Map>, MetaAcceptor>> acceptorSet; + public ExecutorOptionsImpl(@NotNull DBType dbType, @NotNull ExecutorKey executorKey, @NotNull OperatorKey operatorKey) { this(IdGenerator.defaultGenerator().getNextIdAsString(), dbType, executorKey, operatorKey); } @@ -42,6 +44,7 @@ public ExecutorOptionsImpl(@NotNull String key, @NotNull DBType dbType, @NotNull putAttribute(EXECUTOR_KEY_MARK, executorKey); putAttribute(OPERATOR_KEY_MARK, operatorKey); this.scanner = new SPIInnerCommandScanner(executorKey); + this.acceptorSet = Maps.newConcurrentMap(); } /** @@ -89,7 +92,6 @@ public Map getAll() { return properties; } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -102,4 +104,19 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hashCode(getDbType(), getExecutorKey(), getOperatorKey()); } + + @Override + public > void customizeMetaAcceptorSetter(Class metaClass, MetaAcceptor metaAcceptor) { + acceptorSet.put(metaClass, metaAcceptor); + } + + @Override + public > MetaAcceptor customizeMetaAcceptorGetter(Class metaClass) { + return (MetaAcceptor) acceptorSet.get(metaClass); + } + + @Override + public void clear() { + acceptorSet.clear(); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java index 731ed5f4..2de5ec1c 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/query/BaseQueryFilter.java @@ -3,11 +3,13 @@ import cc.allio.uno.data.orm.dsl.Func; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.word.Distinct; import cc.allio.uno.data.orm.dsl.type.DBType; import java.util.Collection; import java.util.List; +import java.util.function.UnaryOperator; /** * 基于{@link QueryOperator}实现的{@link QueryFilter} @@ -16,13 +18,17 @@ * @date 2023/4/17 18:21 * @since 1.1.4 */ -public class BaseQueryFilter implements QueryFilter, QueryOperator { +public class BaseQueryFilter implements QueryFilter, QueryOperator { private QueryWrapper queryWrapper; - private final QueryOperator queryOperator; + private final QueryOperator queryOperator; + private final DBType dbType; + private final OperatorKey operatorMetadataKey; public BaseQueryFilter(DBType dbType, OperatorKey operatorMetadataKey) { - queryOperator = OperatorGroup.getOperator(QueryOperator.class, operatorMetadataKey, dbType); + this.dbType = dbType; + this.operatorMetadataKey = operatorMetadataKey; + this.queryOperator = OperatorGroup.getOperator(QueryOperator.class, operatorMetadataKey, dbType); } @Override @@ -31,8 +37,15 @@ public String getDSL() { } @Override - public QueryOperator parse(String dsl) { - return queryOperator.parse(dsl); + public BaseQueryFilter parse(String dsl) { + queryOperator.parse(dsl); + return self(); + } + + @Override + public BaseQueryFilter customize(UnaryOperator operatorFunc) { + // TODO will be implementation #customize + return self(); } @Override @@ -42,12 +55,12 @@ public void reset() { @Override public void setDBType(DBType dbType) { - + queryOperator.setDBType(dbType); } @Override public DBType getDBType() { - return null; + return queryOperator.getDBType(); } @Override @@ -71,8 +84,9 @@ public List getPrepareValues() { } @Override - public QueryOperator from(Table table) { - return queryOperator.from(table); + public BaseQueryFilter from(Table table) { + queryOperator.from(table); + return self(); } @Override @@ -81,133 +95,159 @@ public Table getTable() { } @Override - public QueryOperator gt(DSLName sqlName, Object value) { - return queryOperator.gt(sqlName, value); + public BaseQueryFilter gt(DSLName sqlName, Object value) { + queryOperator.gt(sqlName, value); + return self(); } @Override - public QueryOperator gte(DSLName sqlName, Object value) { - return queryOperator.gte(sqlName, value); + public BaseQueryFilter gte(DSLName sqlName, Object value) { + queryOperator.gte(sqlName, value); + return self(); } @Override - public QueryOperator lt(DSLName sqlName, Object value) { - return queryOperator.lt(sqlName, value); + public BaseQueryFilter lt(DSLName sqlName, Object value) { + queryOperator.lt(sqlName, value); + return self(); } @Override - public QueryOperator lte(DSLName sqlName, Object value) { - return queryOperator.lte(sqlName, value); + public BaseQueryFilter lte(DSLName sqlName, Object value) { + queryOperator.lte(sqlName, value); + return self(); } @Override - public QueryOperator eq(DSLName sqlName, Object value) { - return queryOperator.eq(sqlName, value); + public BaseQueryFilter eq(DSLName sqlName, Object value) { + queryOperator.eq(sqlName, value); + return self(); } @Override - public QueryOperator neq(DSLName sqlName, Object value) { - return queryOperator.neq(sqlName, value); + public BaseQueryFilter neq(DSLName sqlName, Object value) { + queryOperator.neq(sqlName, value); + return self(); } @Override - public QueryOperator notNull(DSLName sqlName) { - return queryOperator.notNull(sqlName); + public BaseQueryFilter notNull(DSLName sqlName) { + queryOperator.notNull(sqlName); + return self(); } @Override - public QueryOperator isNull(DSLName sqlName) { - return queryOperator.isNull(sqlName); + public BaseQueryFilter isNull(DSLName sqlName) { + queryOperator.isNull(sqlName); + return self(); } @Override - public QueryOperator in(DSLName sqlName, Object... values) { - return queryOperator.in(sqlName, values); + public BaseQueryFilter in(DSLName sqlName, Object... values) { + queryOperator.in(sqlName, values); + return self(); } @Override - public QueryOperator notIn(DSLName sqlName, Object... values) { - return queryOperator.notIn(sqlName, values); + public BaseQueryFilter notIn(DSLName sqlName, Object... values) { + queryOperator.notIn(sqlName, values); + return self(); } @Override - public QueryOperator between(DSLName sqlName, Object withValue, Object endValue) { - return queryOperator.between(sqlName, withValue, endValue); + public BaseQueryFilter between(DSLName sqlName, Object withValue, Object endValue) { + queryOperator.between(sqlName, withValue, endValue); + return self(); } @Override - public QueryOperator notBetween(DSLName sqlName, Object withValue, Object endValue) { - return queryOperator.notBetween(sqlName, withValue, endValue); + public BaseQueryFilter notBetween(DSLName sqlName, Object withValue, Object endValue) { + queryOperator.notBetween(sqlName, withValue, endValue); + return self(); } @Override - public QueryOperator like(DSLName sqlName, Object value) { - return queryOperator.like(sqlName, value); + public BaseQueryFilter like(DSLName sqlName, Object value) { + queryOperator.like(sqlName, value); + return self(); } @Override - public QueryOperator $like(DSLName sqlName, Object value) { - return queryOperator.$like(sqlName, value); + public BaseQueryFilter $like(DSLName sqlName, Object value) { + queryOperator.$like(sqlName, value); + return self(); } @Override - public QueryOperator like$(DSLName sqlName, Object value) { - return queryOperator.like$(sqlName, value); + public BaseQueryFilter like$(DSLName sqlName, Object value) { + queryOperator.like$(sqlName, value); + return self(); } @Override - public QueryOperator $like$(DSLName sqlName, Object value) { - return queryOperator.$like$(sqlName, value); + public BaseQueryFilter $like$(DSLName sqlName, Object value) { + queryOperator.$like$(sqlName, value); + return self(); } @Override - public QueryOperator notLike(DSLName sqlName, Object value) { - return queryOperator.notLike(sqlName, value); + public BaseQueryFilter notLike(DSLName sqlName, Object value) { + queryOperator.notLike(sqlName, value); + return self(); } @Override - public QueryOperator $notLike(DSLName sqlName, Object value) { - return queryOperator.$notLike(sqlName, value); + public BaseQueryFilter $notLike(DSLName sqlName, Object value) { + queryOperator.$notLike(sqlName, value); + return self(); } @Override - public QueryOperator notLike$(DSLName sqlName, Object value) { - return queryOperator.notLike$(sqlName, value); + public BaseQueryFilter notLike$(DSLName sqlName, Object value) { + queryOperator.notLike$(sqlName, value); + return self(); } @Override - public QueryOperator $notLike$(DSLName sqlName, Object value) { - return queryOperator.$notLike$(sqlName, value); + public BaseQueryFilter $notLike$(DSLName sqlName, Object value) { + queryOperator.$notLike$(sqlName, value); + return self(); } @Override - public QueryOperator or() { - return queryOperator.or(); + public BaseQueryFilter or() { + queryOperator.or(); + return self(); } @Override - public QueryOperator and() { - return queryOperator.and(); + public BaseQueryFilter and() { + queryOperator.and(); + return self(); } @Override - public QueryOperator nor() { - return queryOperator.nor(); + public BaseQueryFilter nor() { + queryOperator.nor(); + return self(); } @Override - public QueryOperator select(DSLName dslName) { - return queryOperator.select(dslName); + public BaseQueryFilter select(DSLName dslName) { + queryOperator.select(dslName); + return self(); } @Override - public QueryOperator select(DSLName dslName, String alias) { - return queryOperator.select(dslName, alias); + public BaseQueryFilter select(DSLName dslName, String alias) { + queryOperator.select(dslName, alias); + return self(); } @Override - public QueryOperator selects(Collection dslNames) { - return queryOperator.selects(dslNames); + public BaseQueryFilter selects(Collection dslNames) { + queryOperator.selects(dslNames); + return self(); } @Override @@ -216,52 +256,56 @@ public List obtainSelectColumns() { } @Override - public QueryOperator distinct() { - return queryOperator.distinct(); - } - - @Override - public QueryOperator distinctOn(DSLName sqlName, String alias) { - return queryOperator.distinctOn(sqlName, alias); + public BaseQueryFilter distinct() { + queryOperator.distinct(); + return self(); } @Override - public QueryOperator aggregate(Func syntax, DSLName sqlName, String alias, Distinct distinct) { - return queryOperator.aggregate(syntax, sqlName, alias, distinct); + public BaseQueryFilter distinctOn(DSLName dslName, String alias) { + queryOperator.distinctOn(dslName, alias); + return self(); } @Override - public QueryOperator from(QueryOperator fromTable, String alias) { - return queryOperator.from(fromTable, alias); + public BaseQueryFilter aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct) { + queryOperator.aggregate(syntax, dslName, alias, distinct); + return self(); } @Override - public QueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { - return queryOperator.join(left, joinType, right, condition); + public BaseQueryFilter from(QueryOperator fromTable, String alias) { + queryOperator.from(fromTable, alias); + return self(); } @Override - public QueryOperator orderBy(DSLName sqlName, OrderCondition orderCondition) { - return queryOperator.orderBy(sqlName, orderCondition); + public BaseQueryFilter join(Table left, JoinType joinType, Table right, BinaryCondition condition) { + queryOperator.join(left, joinType, right, condition); + return self(); } @Override - public QueryOperator limit(Long limit, Long offset) { - return queryOperator.limit(limit, offset); + public BaseQueryFilter orderBy(DSLName dslName, OrderCondition orderCondition) { + queryOperator.orderBy(dslName, orderCondition); + return self(); } @Override - public QueryOperator groupByOnes(Collection fieldNames) { - return queryOperator.groupByOnes(fieldNames); + public BaseQueryFilter limit(Long limit, Long offset) { + queryOperator.limit(limit, offset); + return self(); } @Override - public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { - return queryOperator.tree(baseQuery, subQuery); + public BaseQueryFilter groupByOnes(Collection fieldNames) { + queryOperator.groupByOnes(fieldNames); + return self(); } @Override - public QueryOperator self() { - return queryOperator; + public BaseQueryFilter tree(QueryOperator baseQuery, QueryOperator subQuery) { + queryOperator.tree(baseQuery, subQuery); + return self(); } -} +} \ No newline at end of file diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/ADruidSQLCreateOperator.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/ADruidSQLCreateOperator.java index 6939e17d..8f245d38 100644 --- a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/ADruidSQLCreateOperator.java +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/ADruidSQLCreateOperator.java @@ -4,16 +4,24 @@ import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; import cc.allio.uno.data.orm.dsl.type.DBType; +import java.util.function.UnaryOperator; + @AutoService(CreateTableOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class ADruidSQLCreateOperator implements CreateTableOperator { +public class ADruidSQLCreateOperator implements CreateTableOperator { + @Override public String getDSL() { return null; } @Override - public CreateTableOperator parse(String dsl) { + public ADruidSQLCreateOperator parse(String dsl) { + return null; + } + + @Override + public ADruidSQLCreateOperator customize(UnaryOperator operatorFunc) { return null; } @@ -33,7 +41,7 @@ public DBType getDBType() { } @Override - public CreateTableOperator from(Table table) { + public ADruidSQLCreateOperator from(Table table) { return null; } @@ -43,12 +51,12 @@ public Table getTable() { } @Override - public CreateTableOperator column(ColumnDef columnDef) { + public ADruidSQLCreateOperator column(ColumnDef columnDef) { return null; } @Override - public CreateTableOperator comment(String comment) { + public ADruidSQLCreateOperator comment(String comment) { return null; } } diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/BDruidSQLCreateOperator.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/BDruidSQLCreateOperator.java index 852d18f2..36ac628f 100644 --- a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/BDruidSQLCreateOperator.java +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/BDruidSQLCreateOperator.java @@ -4,16 +4,23 @@ import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; import cc.allio.uno.data.orm.dsl.type.DBType; +import java.util.function.UnaryOperator; + @AutoService(CreateTableOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class BDruidSQLCreateOperator implements CreateTableOperator { +public class BDruidSQLCreateOperator implements CreateTableOperator { @Override public String getDSL() { return null; } @Override - public CreateTableOperator parse(String dsl) { + public BDruidSQLCreateOperator parse(String dsl) { + return null; + } + + @Override + public BDruidSQLCreateOperator customize(UnaryOperator operatorFunc) { return null; } @@ -33,7 +40,7 @@ public DBType getDBType() { } @Override - public CreateTableOperator from(Table table) { + public BDruidSQLCreateOperator from(Table table) { return null; } @@ -43,12 +50,12 @@ public Table getTable() { } @Override - public CreateTableOperator column(ColumnDef columnDef) { + public BDruidSQLCreateOperator column(ColumnDef columnDef) { return null; } @Override - public CreateTableOperator comment(String comment) { + public BDruidSQLCreateOperator comment(String comment) { return null; } } diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/DruidShowColumnsOperator.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/DruidShowColumnsOperator.java index b201301c..eca053fe 100644 --- a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/DruidShowColumnsOperator.java +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/DruidShowColumnsOperator.java @@ -6,17 +6,24 @@ import cc.allio.uno.data.orm.dsl.type.DBType; import java.util.List; +import java.util.function.UnaryOperator; @AutoService(ShowColumnsOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class DruidShowColumnsOperator implements ShowColumnsOperator { +public class DruidShowColumnsOperator implements ShowColumnsOperator { + @Override public String getDSL() { return null; } @Override - public ShowColumnsOperator parse(String dsl) { + public DruidShowColumnsOperator parse(String dsl) { + return null; + } + + @Override + public DruidShowColumnsOperator customize(UnaryOperator operatorFunc) { return null; } @@ -46,7 +53,7 @@ public List getPrepareValues() { } @Override - public ShowColumnsOperator from(Table table) { + public DruidShowColumnsOperator from(Table table) { return null; } @@ -56,12 +63,12 @@ public Table getTable() { } @Override - public QueryOperator toQueryOperator() { + public QueryOperator toQueryOperator() { return null; } @Override - public ShowColumnsOperator database(Database database) { + public DruidShowColumnsOperator database(Database database) { return null; } } diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/MetaAcceptorSetImpl.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/MetaAcceptorSetImpl.java new file mode 100644 index 00000000..7bbde49d --- /dev/null +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/MetaAcceptorSetImpl.java @@ -0,0 +1,30 @@ +package cc.allio.uno.data.orm.dsl; + +import com.google.common.collect.Maps; + +import java.util.Map; + +public class MetaAcceptorSetImpl implements MetaAcceptorSet { + + private final Map>, MetaAcceptor>> cache; + + public MetaAcceptorSetImpl() { + this.cache = Maps.newConcurrentMap(); + } + + + @Override + public > void customizeMetaAcceptorSetter(Class metaClass, MetaAcceptor metaAcceptor) { + cache.put(metaClass, metaAcceptor); + } + + @Override + public > MetaAcceptor customizeMetaAcceptorGetter(Class metaClass) { + return (MetaAcceptor) cache.get(metaClass); + } + + @Override + public void clear() { + cache.clear(); + } +} diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/MetaTest.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/MetaTest.java new file mode 100644 index 00000000..380a3868 --- /dev/null +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/MetaTest.java @@ -0,0 +1,50 @@ +package cc.allio.uno.data.orm.dsl; + +import cc.allio.uno.test.BaseTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.function.Consumer; + +public class MetaTest extends BaseTestCase { + + private MetaAcceptorSet metaAcceptorSet; + + @BeforeEach + void init() { + this.metaAcceptorSet = new MetaAcceptorSetImpl(); + } + + @Test + void testCreateMetaByArgs() { + ConsumerDSLNameAcceptor dslNameAcceptor = new ConsumerDSLNameAcceptor(dslname -> dslname.setName("name1")); + metaAcceptorSet.setDSLNameAcceptor(dslNameAcceptor); + DSLName d1 = Meta.create(DSLName.class, metaAcceptorSet, "name"); + assertEquals("name1", d1.getName()); + + // clear set DSLName acceptor + metaAcceptorSet.clear(); + + d1 = Meta.create(DSLName.class, metaAcceptorSet, "name"); + assertEquals("name", d1.getName()); + } + + @Test + void testCreateColumnError() { + assertThrows(NullPointerException.class, () -> Meta.create(ColumnDef.class, metaAcceptorSet)); + } + + public static class ConsumerDSLNameAcceptor implements MetaAcceptor { + + private final Consumer consumer; + + public ConsumerDSLNameAcceptor(Consumer consumer) { + this.consumer = consumer; + } + + @Override + public void onAccept(DSLName meta) { + consumer.accept(meta); + } + } +} diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisInsertOperator.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisInsertOperator.java index ff797976..7d370a9e 100644 --- a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisInsertOperator.java +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisInsertOperator.java @@ -7,17 +7,23 @@ import java.util.Collection; import java.util.List; import java.util.function.Supplier; +import java.util.function.UnaryOperator; @AutoService(InsertOperator.class) @Operator.Group(OperatorKey.REDIS_LITERAL) -public class RedisInsertOperator implements InsertOperator { +public class RedisInsertOperator implements InsertOperator { @Override public String getDSL() { return null; } @Override - public InsertOperator parse(String dsl) { + public RedisInsertOperator parse(String dsl) { + return null; + } + + @Override + public RedisInsertOperator customize(UnaryOperator operatorFunc) { return null; } @@ -47,7 +53,7 @@ public List getPrepareValues() { } @Override - public InsertOperator from(Table table) { + public RedisInsertOperator from(Table table) { return null; } @@ -57,17 +63,17 @@ public Table getTable() { } @Override - public InsertOperator strictFill(String f, Supplier v) { + public RedisInsertOperator strictFill(String f, Supplier v) { return null; } @Override - public InsertOperator columns(Collection columns) { + public RedisInsertOperator columns(Collection columns) { return null; } @Override - public InsertOperator values(List values) { + public RedisInsertOperator values(List values) { return null; } diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisShowColumnsOperator.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisShowColumnsOperator.java index 47002f96..5f06f6e1 100644 --- a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisShowColumnsOperator.java +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/RedisShowColumnsOperator.java @@ -6,10 +6,11 @@ import cc.allio.uno.data.orm.dsl.type.DBType; import java.util.List; +import java.util.function.UnaryOperator; @AutoService(ShowColumnsOperator.class) @Operator.Group(OperatorKey.REDIS_LITERAL) -public class RedisShowColumnsOperator implements ShowColumnsOperator { +public class RedisShowColumnsOperator implements ShowColumnsOperator { @Override public String getDSL() { @@ -17,7 +18,12 @@ public String getDSL() { } @Override - public ShowColumnsOperator parse(String dsl) { + public RedisShowColumnsOperator parse(String dsl) { + return null; + } + + @Override + public RedisShowColumnsOperator customize(UnaryOperator operatorFunc) { return null; } @@ -47,7 +53,7 @@ public List getPrepareValues() { } @Override - public ShowColumnsOperator from(Table table) { + public RedisShowColumnsOperator from(Table table) { return null; } @@ -57,12 +63,12 @@ public Table getTable() { } @Override - public QueryOperator toQueryOperator() { + public QueryOperator toQueryOperator() { return null; } @Override - public ShowColumnsOperator database(Database database) { + public RedisShowColumnsOperator database(Database database) { return null; } } diff --git a/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/operatorgroup/OperatorGroupTest.java b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/operatorgroup/OperatorGroupTest.java new file mode 100644 index 00000000..a5b01b7c --- /dev/null +++ b/uno-data/uno-data-api/src/test/java/cc/allio/uno/data/orm/dsl/operatorgroup/OperatorGroupTest.java @@ -0,0 +1,29 @@ +package cc.allio.uno.data.orm.dsl.operatorgroup; + +import cc.allio.uno.data.orm.dsl.MetaAcceptorSet; +import cc.allio.uno.data.orm.dsl.MetaAcceptorSetImpl; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.dml.InsertOperator; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; +import cc.allio.uno.test.BaseTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class OperatorGroupTest extends BaseTestCase { + + private MetaAcceptorSet metaAcceptorSet; + + @BeforeEach + void init() { + this.metaAcceptorSet = new MetaAcceptorSetImpl(); + } + + @Test + void testObtainMetaAcceptor() { + OperatorGroup operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.REDIS, metaAcceptorSet); + InsertOperator insert = operatorGroup.insert(); + assertNotNull(insert); + MetaAcceptorSet selfMetaAcceptorSet = insert.obtainMetaAcceptorSet(); + assertNotNull(selfMetaAcceptorSet); + } +} diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbAutoConfiguration.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbAutoConfiguration.java index b747cf82..99c99b06 100644 --- a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbAutoConfiguration.java +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbAutoConfiguration.java @@ -1,27 +1,48 @@ package cc.allio.uno.data.orm.config.db; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.CommandExecutorRegistry; import cc.allio.uno.data.orm.executor.ExecutorInitializerAutoConfiguration; +import cc.allio.uno.data.orm.executor.ExecutorOptionsBuilder; +import cc.allio.uno.data.orm.executor.db.DbCommandExecutor; import cc.allio.uno.data.orm.executor.db.DbCommandExecutorLoader; import cc.allio.uno.data.orm.executor.db.DbCommandExecutorProcessor; import cc.allio.uno.data.orm.executor.db.DbMybatisConfiguration; +import cc.allio.uno.data.orm.executor.interceptor.Interceptor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import cc.allio.uno.data.orm.executor.options.ExecutorOptions; import com.alibaba.druid.pool.DruidDataSource; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.List; + +/** + * configuration for db + * + * @author j.x + * @date 2023/4/14 19:29 + * @since 1.1.4 + */ @Configuration(proxyBeanMethods = false) @AutoConfigureBefore(ExecutorInitializerAutoConfiguration.class) @AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class}) @ConditionalOnClass({DruidDataSource.class, HikariDataSource.class}) +@EnableConfigurationProperties(DbProperties.class) public class DbAutoConfiguration { @Bean @@ -36,4 +57,38 @@ public DbCommandExecutorLoader dbCommandExecutorLoader(SqlSessionFactory sqlSess public DbCommandExecutorProcessor dbExecutorProcessor() { return new DbCommandExecutorProcessor(); } -} + + @Bean + @ConditionalOnMissingBean + @ConditionalOnBean(CommandExecutorRegistry.class) + @ConditionalOnProperty(prefix = "allio.uno.data.db", name = "enabled", havingValue = "true") + public DbCommandExecutor defaultDbCommandExecutor(DbCommandExecutorLoader commandExecutorLoader, + DbProperties dbProperties, + CommandExecutorRegistry commandExecutorRegistry, + ObjectProvider> interceptorProvider) { + List interceptors = interceptorProvider.getIfAvailable(List::of); + DbProperties.DbType dbType = dbProperties.getDbType(); + DBType systemDBType = switch (dbType) { + case MYSQL -> DBType.MYSQL; + case ORACLE -> DBType.ORACLE; + case SQLITE -> DBType.SQLITE; + case MARIADB -> DBType.MARIADB; + case SQLSERVER -> DBType.SQLSERVER; + case DB2 -> DBType.DB2; + case POSTGRESQL -> DBType.POSTGRESQL; + case OPEN_GAUSS -> DBType.OPEN_GAUSS; + case null, default -> DBType.H2; + }; + ExecutorOptions executorOptions = + ExecutorOptionsBuilder.create(systemDBType, dbType.name()) + .address(dbProperties.getAddress()) + .username(dbProperties.getUsername()) + .password(dbProperties.getPassword()) + .executorKey(ExecutorKey.DB) + .operatorKey(OperatorKey.SQL) + .interceptors(interceptors) + .build(); + return commandExecutorRegistry.register(executorOptions, () -> commandExecutorLoader.load(executorOptions), false); + } + +} \ No newline at end of file diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbProperties.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbProperties.java new file mode 100644 index 00000000..62438761 --- /dev/null +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/config/db/DbProperties.java @@ -0,0 +1,63 @@ +package cc.allio.uno.data.orm.config.db; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * describe database command executor type properties + * + * @author j.x + * @date 2024/4/14 19:23 + * @since 1.1.8 + */ +@Data +@ConfigurationProperties(prefix = "allio.uno.data.db") +public class DbProperties { + + /** + * enable influxdb + */ + private Boolean enabled = false; + + /** + * database type + */ + private DbType dbType = DbType.H2; + + /** + * influxdb address, like as localhost:3306 + */ + private String address; + + /** + * auth for username + */ + private String username; + + /** + * auth for password + */ + private String password; + + /** + * database + */ + private String database; + + /** + * is system default command executor + */ + private Boolean systemDefault = true; + + public enum DbType { + MYSQL, + POSTGRESQL, + SQLSERVER, + ORACLE, + OPEN_GAUSS, + DB2, + MARIADB, + SQLITE, + H2 + } +} diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java index f3aae9b8..967246b4 100644 --- a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java @@ -8,12 +8,14 @@ import cc.allio.uno.data.orm.dsl.ddl.ShowTablesOperator; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ListResultSetHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.InnerCommandExecutorManager; import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; import cc.allio.uno.data.orm.executor.options.ExecutorOptionsImpl; @@ -74,7 +76,7 @@ public DbCommandExecutor(ExecutorOptions options, DbMybatisConfiguration configu this.executor = new SimpleExecutor(configuration, tx); this.languageDriver = new RawLanguageDriver(); this.sqlCommandAdapter = new MybatisSQLCommandAdapter(); - this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.SQL); + this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.SQL, options); } @Override @@ -150,13 +152,13 @@ protected boolean doBool(Operator operator, CommandType commandType, ResultSe @Override protected List doQueryList(Operator operator, CommandType commandType, ListResultSetHandler resultSetHandler) { - QueryOperator queryOperator; + QueryOperator queryOperator; if (commandType == CommandType.SELECT) { - queryOperator = (QueryOperator) operator; + queryOperator = (QueryOperator) operator; } else if (commandType == CommandType.SHOW_TABLES) { - queryOperator = ((ShowTablesOperator) operator).toQueryOperator(); + queryOperator = ((ShowTablesOperator) operator).toQueryOperator(); } else if (commandType == CommandType.SHOW_COLUMNS) { - queryOperator = ((ShowColumnsOperator) operator).toQueryOperator(); + queryOperator = ((ShowColumnsOperator) operator).toQueryOperator(); } else { throw new DSLException(String.format("un accept command to Db query list %s", commandType)); } @@ -195,6 +197,11 @@ protected List doQueryList(Operator operator, CommandType commandType, } } + @Override + protected InnerCommandExecutorManager getInnerCommandExecutorManager() { + return null; + } + @Override public boolean check() throws SocketTimeoutException { try { diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/ElasticSearchProperties.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/ElasticSearchProperties.java new file mode 100644 index 00000000..a0406e81 --- /dev/null +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/ElasticSearchProperties.java @@ -0,0 +1,46 @@ +package cc.allio.uno.data.orm.config.elasticsearch; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * describe elasticsearch properties + * + * @author j.x + * @date 2024/4/14 19:59 + * @since 1.1.8 + */ +@Data +@ConfigurationProperties(prefix = "allio.uno.data.elasticsearch") +public class ElasticSearchProperties { + + /** + * enable influxdb + */ + private Boolean enabled = false; + + /** + * influxdb address, like as localhost:27017 + */ + private String address = "localhost:9200"; + + /** + * auth for username + */ + private String username; + + /** + * auth for password + */ + private String password; + + /** + * database + */ + private String database; + + /** + * is system default command executor + */ + private Boolean systemDefault = true; +} diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/UDElasticSearchAutoConfiguration.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/UDElasticSearchAutoConfiguration.java deleted file mode 100644 index ff98e5f0..00000000 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/UDElasticSearchAutoConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package cc.allio.uno.data.orm.config.elasticsearch; - -import cc.allio.uno.data.orm.executor.ExecutorInitializerAutoConfiguration; -import cc.allio.uno.data.orm.executor.elasticsearch.EsCommandExecutorLoader; -import cc.allio.uno.data.orm.executor.elasticsearch.EsCommandExecutorProcessor; -import org.elasticsearch.client.RestClientBuilder; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; -import org.springframework.context.annotation.*; - -@Configuration(proxyBeanMethods = false) -@AutoConfigureBefore(ExecutorInitializerAutoConfiguration.class) -@AutoConfigureAfter(ElasticsearchRestClientAutoConfiguration.class) -@ConditionalOnClass(RestClientBuilder.class) -public class UDElasticSearchAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public EsCommandExecutorLoader esCommandExecutorLoader(RestClientBuilder builder) { - return new EsCommandExecutorLoader(builder); - } - - @Bean - @ConditionalOnBean(EsCommandExecutorLoader.class) - public EsCommandExecutorProcessor esExecutorProcessor() { - return new EsCommandExecutorProcessor(); - } -} diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/UnoElasticSearchAutoConfiguration.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/UnoElasticSearchAutoConfiguration.java new file mode 100644 index 00000000..3a60ee4d --- /dev/null +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/config/elasticsearch/UnoElasticSearchAutoConfiguration.java @@ -0,0 +1,74 @@ +package cc.allio.uno.data.orm.config.elasticsearch; + +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.CommandExecutorRegistry; +import cc.allio.uno.data.orm.executor.ExecutorInitializerAutoConfiguration; +import cc.allio.uno.data.orm.executor.ExecutorOptionsBuilder; +import cc.allio.uno.data.orm.executor.elasticsearch.EsCommandExecutor; +import cc.allio.uno.data.orm.executor.elasticsearch.EsCommandExecutorLoader; +import cc.allio.uno.data.orm.executor.elasticsearch.EsCommandExecutorProcessor; +import cc.allio.uno.data.orm.executor.interceptor.Interceptor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import cc.allio.uno.data.orm.executor.options.ExecutorOptions; +import org.elasticsearch.client.RestClientBuilder; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.*; + +import java.util.List; + +/** + * configuration for elasticsearch + * + * @author j.x + * @date 2023/3/14 20:01 + * @since 1.1.4 + */ +@Configuration(proxyBeanMethods = false) +@AutoConfigureBefore(ExecutorInitializerAutoConfiguration.class) +@AutoConfigureAfter(ElasticsearchRestClientAutoConfiguration.class) +@ConditionalOnClass(RestClientBuilder.class) +@EnableConfigurationProperties(ElasticSearchProperties.class) +public class UnoElasticSearchAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public EsCommandExecutorLoader esCommandExecutorLoader(RestClientBuilder builder) { + return new EsCommandExecutorLoader(builder); + } + + @Bean + @ConditionalOnBean(EsCommandExecutorLoader.class) + public EsCommandExecutorProcessor esExecutorProcessor() { + return new EsCommandExecutorProcessor(); + } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnBean(CommandExecutorRegistry.class) + @ConditionalOnProperty(prefix = "allio.uno.data.elasticsearch", name = "enabled", havingValue = "true") + public EsCommandExecutor defaultDbCommandExecutor(EsCommandExecutorLoader commandExecutorLoader, + ElasticSearchProperties elasticSearchProperties, + CommandExecutorRegistry commandExecutorRegistry, + ObjectProvider> interceptorProvider) { + List interceptors = interceptorProvider.getIfAvailable(List::of); + ExecutorOptions executorOptions = + ExecutorOptionsBuilder.create(DBType.ELASTICSEARCH) + .address(elasticSearchProperties.getAddress()) + .username(elasticSearchProperties.getUsername()) + .password(elasticSearchProperties.getPassword()) + .executorKey(ExecutorKey.ELASTICSEARCH) + .operatorKey(OperatorKey.ELASTICSEARCH) + .interceptors(interceptors) + .build(); + return commandExecutorRegistry.register(executorOptions, () -> commandExecutorLoader.load(executorOptions), false); + } +} diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsCreateIndexOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsCreateIndexOperator.java similarity index 87% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsCreateIndexOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsCreateIndexOperator.java index 1f956178..62128203 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsCreateIndexOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsCreateIndexOperator.java @@ -1,9 +1,8 @@ -package cc.allio.uno.data.orm.dsl.ddl.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.ddl; import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.type.DBType; import co.elastic.clients.elasticsearch._types.mapping.Property; import co.elastic.clients.elasticsearch._types.mapping.TypeMapping; @@ -13,6 +12,8 @@ import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; +import java.util.function.UnaryOperator; + /** * 创建索引 * @@ -23,19 +24,20 @@ */ @AutoService(CreateTableOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsCreateIndexOperator implements CreateTableOperator { +public class EsCreateIndexOperator implements CreateTableOperator { + private final EsPropertyAdapter elasticSearchPropertyAdapter; private final IndexSettings.Builder settingsBuilder; private final TypeMapping.Builder mappingBuilder; + private final DBType dbType; private CreateIndexRequest request; private CreateIndexRequest.Builder builder; private Table table; - private DBType dbType; private static final String ERROR_MSG = "elasticsearch registry operator not support that operator"; public EsCreateIndexOperator() { - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.builder = new CreateIndexRequest.Builder(); this.elasticSearchPropertyAdapter = new EsPropertyAdapter(); this.settingsBuilder = new IndexSettings.Builder(); @@ -57,7 +59,13 @@ public String getDSL() { @Override public EsCreateIndexOperator parse(String dsl) { - throw new DSLException(String.format("%s This operation is not supported", this.getClass().getName())); + // nothing to do + return self(); + } + + @Override + public EsCreateIndexOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsCreateIndexOperator()); } @Override @@ -80,7 +88,7 @@ public DBType getDBType() { public EsCreateIndexOperator from(Table table) { this.table = table; builder.index(table.getName().format()); - return (EsCreateIndexOperator) self(); + return self(); } @Override @@ -92,12 +100,13 @@ public Table getTable() { public EsCreateIndexOperator column(ColumnDef columnDef) { Property property = elasticSearchPropertyAdapter.adapt(columnDef.getDataType()); mappingBuilder.properties(columnDef.getDslName().format(), property); - return (EsCreateIndexOperator) self(); + return self(); } @Override public EsCreateIndexOperator comment(String comment) { - throw new DSLException(ERROR_MSG); + // nothing todo + return self(); } // =================== settings =================== @@ -107,7 +116,7 @@ public EsCreateIndexOperator comment(String comment) { */ public EsCreateIndexOperator maxResultWindow(Integer value) { settingsBuilder.maxResultWindow(value); - return (EsCreateIndexOperator) self(); + return self(); } /** @@ -116,7 +125,7 @@ public EsCreateIndexOperator maxResultWindow(Integer value) { */ public EsCreateIndexOperator numberOfShards(String value) { settingsBuilder.numberOfShards(value); - return (EsCreateIndexOperator) self(); + return self(); } /** @@ -125,7 +134,7 @@ public EsCreateIndexOperator numberOfShards(String value) { */ public EsCreateIndexOperator numberOfReplicas(String value) { settingsBuilder.numberOfReplicas(value); - return (EsCreateIndexOperator) self(); + return self(); } /** @@ -137,7 +146,7 @@ public EsCreateIndexOperator numberOfReplicas(String value) { */ public EsCreateIndexOperator codec(String value) { settingsBuilder.codec(value); - return (EsCreateIndexOperator) self(); + return self(); } /** @@ -145,7 +154,7 @@ public EsCreateIndexOperator codec(String value) { */ public EsCreateIndexOperator maxScriptFields(Integer value) { settingsBuilder.maxScriptFields(value); - return (EsCreateIndexOperator) self(); + return self(); } /** diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsDropIndexOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsDropIndexOperator.java similarity index 77% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsDropIndexOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsDropIndexOperator.java index 4074e364..7cc81a01 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsDropIndexOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsDropIndexOperator.java @@ -1,4 +1,4 @@ -package cc.allio.uno.data.orm.dsl.ddl.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.ddl; import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.dsl.OperatorKey; @@ -10,6 +10,8 @@ import cc.allio.uno.data.orm.dsl.Table; import cc.allio.uno.data.orm.dsl.ddl.DropTableOperator; +import java.util.function.UnaryOperator; + /** * 删除索引{@link DeleteIndexRequest} * @@ -19,16 +21,16 @@ */ @AutoService(DropTableOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsDropIndexOperator implements DropTableOperator { +public class EsDropIndexOperator implements DropTableOperator { - private DBType dbType; + private final DBType dbType; private DeleteIndexRequest deleteIndexRequest; private DeleteIndexRequest.Builder builder; private Table table; private static final String ERROR_MSG = "elasticsearch drop operator not support that operator"; public EsDropIndexOperator() { - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.builder = new DeleteIndexRequest.Builder(); } @@ -38,10 +40,15 @@ public String getDSL() { } @Override - public DropTableOperator parse(String dsl) { + public EsDropIndexOperator parse(String dsl) { return null; } + @Override + public EsDropIndexOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsDropIndexOperator()); + } + @Override public void reset() { deleteIndexRequest = null; @@ -59,7 +66,7 @@ public DBType getDBType() { } @Override - public DropTableOperator from(Table table) { + public EsDropIndexOperator from(Table table) { this.table = table; builder.index(table.getName().format()); return self(); @@ -71,7 +78,7 @@ public Table getTable() { } @Override - public DropTableOperator ifExist(Boolean ifExist) { + public EsDropIndexOperator ifExist(Boolean ifExist) { throw new DSLException(ERROR_MSG); } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsExistIndexOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsExistIndexOperator.java similarity index 69% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsExistIndexOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsExistIndexOperator.java index fff39ea3..e0ddbcbb 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsExistIndexOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsExistIndexOperator.java @@ -1,14 +1,14 @@ -package cc.allio.uno.data.orm.dsl.ddl.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.ddl; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.type.DBType; import co.elastic.clients.elasticsearch.core.SearchRequest; import cc.allio.uno.data.orm.dsl.ddl.ExistTableOperator; import java.util.List; +import java.util.function.UnaryOperator; /** * 判断索引是否存在{@link co.elastic.clients.elasticsearch.indices.ExistsRequest} @@ -19,26 +19,31 @@ */ @AutoService(ExistTableOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsExistIndexOperator implements ExistTableOperator { - private DBType dbType; - SearchRequest.Builder builder; +public class EsExistIndexOperator implements ExistTableOperator { + + private final DBType dbType; private SearchRequest searchRequest; private Table table; - private static final String ERROR_MSG = "elasticsearch drop operator not support that operator"; + SearchRequest.Builder builder; public EsExistIndexOperator() { - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.builder = new SearchRequest.Builder(); } @Override public String getDSL() { - throw new DSLException(ERROR_MSG); + return StringPool.EMPTY; + } + + @Override + public EsExistIndexOperator parse(String dsl) { + return self(); } @Override - public ExistTableOperator parse(String dsl) { - return null; + public EsExistIndexOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsExistIndexOperator()); } @Override @@ -49,7 +54,7 @@ public void reset() { @Override public void setDBType(DBType dbType) { - throw Exceptions.unOperate("setDBType"); + // nothing to do } @Override @@ -59,16 +64,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - throw new DSLException(ERROR_MSG); + return getDSL(); } @Override public List getPrepareValues() { - throw new DSLException(ERROR_MSG); + return List.of(); } @Override - public ExistTableOperator from(Table table) { + public EsExistIndexOperator from(Table table) { this.table = table; builder.index(table.getName().format()); return self(); @@ -90,5 +95,4 @@ public SearchRequest getSearchRequest() { } return searchRequest; } - -} +} \ No newline at end of file diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsPropertyAdapter.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsPropertyAdapter.java similarity index 98% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsPropertyAdapter.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsPropertyAdapter.java index d101ce41..49389776 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsPropertyAdapter.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsPropertyAdapter.java @@ -1,4 +1,4 @@ -package cc.allio.uno.data.orm.dsl.ddl.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.ddl; import cc.allio.uno.data.orm.dsl.type.DataType; import cc.allio.uno.data.orm.dsl.type.DataTypeAdapter; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsShowColumnsOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsShowColumnsOperator.java similarity index 72% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsShowColumnsOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsShowColumnsOperator.java index 89199a10..1df7bff3 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/EsShowColumnsOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/ddl/EsShowColumnsOperator.java @@ -1,6 +1,7 @@ -package cc.allio.uno.data.orm.dsl.ddl.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.ddl; import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.StringPool; import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; @@ -11,6 +12,7 @@ import com.google.common.collect.Lists; import java.util.List; +import java.util.function.UnaryOperator; /** * es 查询所有映射.{@link GetMappingRequest} @@ -21,26 +23,32 @@ */ @AutoService(ShowColumnsOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsShowColumnsOperator implements ShowColumnsOperator { +public class EsShowColumnsOperator implements ShowColumnsOperator { - private DBType dbType; + private final DBType dbType; private GetMappingRequest.Builder builder; private GetMappingRequest getMappingRequest; private Table table; public EsShowColumnsOperator() { - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.builder = new GetMappingRequest.Builder(); } @Override public String getDSL() { - throw new DSLException(String.format("%s This operation is not supported", this.getClass().getName())); + return StringPool.EMPTY; } @Override - public ShowColumnsOperator parse(String dsl) { - throw new DSLException(String.format("%s This operation is not supported", this.getClass().getName())); + public EsShowColumnsOperator parse(String dsl) { + // nothing TODO + return self(); + } + + @Override + public EsShowColumnsOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsShowColumnsOperator()); } @Override @@ -61,16 +69,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - throw new DSLException(String.format("%s This operation is not supported", this.getClass().getName())); + return getDSL(); } @Override public List getPrepareValues() { - throw new DSLException(String.format("%s This operation is not supported", this.getClass().getName())); + return List.of(); } @Override - public ShowColumnsOperator from(Table table) { + public EsShowColumnsOperator from(Table table) { this.table = table; builder = builder.index(Lists.newArrayList(table.getName().format())); return self(); @@ -82,7 +90,7 @@ public Table getTable() { } @Override - public QueryOperator toQueryOperator() { + public QueryOperator toQueryOperator() { throw new DSLException(String.format("%s This operation is not supported", this.getClass().getName())); } @@ -99,7 +107,8 @@ public GetMappingRequest getMappingRequest() { } @Override - public ShowColumnsOperator database(Database database) { - return null; + public EsShowColumnsOperator database(Database database) { + // nothing TODO + return self(); } } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsDeleteOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsDeleteOperator.java similarity index 61% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsDeleteOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsDeleteOperator.java index 953c8d23..89c58417 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsDeleteOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsDeleteOperator.java @@ -1,9 +1,7 @@ -package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.dml; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.type.DBType; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest; @@ -13,6 +11,7 @@ import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; /** * {@link DeleteByQueryRequest}实现删除数据 @@ -23,16 +22,15 @@ */ @AutoService(DeleteOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsDeleteOperator extends EsGenericWhereOperator implements DeleteOperator { +public class EsDeleteOperator extends EsWhereOperatorImpl implements DeleteOperator { - private DBType dbType; + private final DBType dbType; private DeleteByQueryRequest deleteRequest; private DeleteByQueryRequest.Builder deleteBuilder; private Table table; - private static final String ERROR_MSG = "elasticsearch delete operator not support that operator"; public EsDeleteOperator() { - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.deleteBuilder = new DeleteByQueryRequest.Builder(); } @@ -44,8 +42,14 @@ public String getDSL() { } @Override - public DeleteOperator parse(String dsl) { - return null; + public EsDeleteOperator parse(String dsl) { + // nothing todo + return self(); + } + + @Override + public EsDeleteOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsDeleteOperator()); } @Override @@ -57,7 +61,7 @@ public void reset() { @Override public void setDBType(DBType dbType) { - throw Exceptions.unOperate("setDBType"); + // nothing todo } @Override @@ -67,16 +71,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - return null; + return getDSL(); } @Override public List getPrepareValues() { - throw new DSLException(ERROR_MSG); + return List.of(); } @Override - public DeleteOperator from(Table table) { + public EsDeleteOperator from(Table table) { this.table = table; deleteBuilder.index(Collections.singletonList(table.getName().format())); return self(); @@ -99,39 +103,4 @@ public DeleteByQueryRequest getDeleteRequest() { } return deleteRequest; } - - @Override - public DeleteOperator neq(DSLName sqlName, Object value) { - return null; - } - - @Override - public DeleteOperator notIn(DSLName sqlName, Object... values) { - return null; - } - - @Override - public DeleteOperator notLike(DSLName sqlName, Object value) { - return null; - } - - @Override - public DeleteOperator $notLike(DSLName sqlName, Object value) { - return null; - } - - @Override - public DeleteOperator notLike$(DSLName sqlName, Object value) { - return null; - } - - @Override - public DeleteOperator $notLike$(DSLName sqlName, Object value) { - return null; - } - - @Override - public DeleteOperator nor() { - return null; - } } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsInsertOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsInsertOperator.java similarity index 79% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsInsertOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsInsertOperator.java index 74d58095..569a13d3 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsInsertOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsInsertOperator.java @@ -1,7 +1,6 @@ -package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.dml; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.type.DBType; @@ -20,6 +19,7 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; +import java.util.function.UnaryOperator; /** * 基于{@link BulkRequest}做数据创建 @@ -31,18 +31,16 @@ */ @AutoService(InsertOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsInsertOperator implements InsertOperator { +public class EsInsertOperator implements InsertOperator { - private DBType dbType; + private final DBType dbType; private BulkRequest bulkRequest; private BulkRequest.Builder batchBuilder; private final List bkps; private Table table; - private static final String ERROR_MSG = "elasticsearch insert operator not support that operator"; - public EsInsertOperator() { - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.batchBuilder = new BulkRequest.Builder(); this.bkps = Lists.newArrayList(); } @@ -55,8 +53,14 @@ public String getDSL() { } @Override - public InsertOperator parse(String dsl) { - return null; + public EsInsertOperator parse(String dsl) { + // nothing todo + return self(); + } + + @Override + public EsInsertOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsInsertOperator()); } @Override @@ -69,7 +73,7 @@ public void reset() { @Override public void setDBType(DBType dbType) { - throw Exceptions.unOperate("setDBType"); + // nothing todo } @Override @@ -79,16 +83,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - return null; + return getDSL(); } @Override public List getPrepareValues() { - throw new DSLException(ERROR_MSG); + return List.of(); } @Override - public InsertOperator from(Table table) { + public EsInsertOperator from(Table table) { this.table = table; return self(); } @@ -99,12 +103,12 @@ public Table getTable() { } @Override - public InsertOperator inserts(Map values) { + public EsInsertOperator inserts(Map values) { return batchInserts(values.keySet().stream().toList(), List.of((values.values().stream().toList()))); } @Override - public InsertOperator batchInserts(List columns, List> values) { + public EsInsertOperator batchInserts(List columns, List> values) { if (table == null) { throw new DSLException("ensure invoke #xxxx() given index"); } @@ -130,22 +134,22 @@ public InsertOperator batchInserts(List columns, List> val } @Override - public InsertOperator strictFill(String f, Object v) { + public EsInsertOperator strictFill(String f, Object v) { return null; } @Override - public InsertOperator strictFill(String f, Supplier v) { + public EsInsertOperator strictFill(String f, Supplier v) { return null; } @Override - public InsertOperator columns(Collection columns) { + public EsInsertOperator columns(Collection columns) { return null; } @Override - public InsertOperator values(List values) { + public EsInsertOperator values(List values) { return null; } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsQueryOperator.java similarity index 67% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsQueryOperator.java index 009f98ad..8a1d06eb 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsQueryOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsQueryOperator.java @@ -1,7 +1,6 @@ -package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.dml; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.Func; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.Operator; @@ -22,6 +21,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; /** * 基于{@link SearchRequest}的查询,SQL数据为elasticsearch dsl @@ -32,7 +32,7 @@ */ @AutoService(QueryOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsQueryOperator extends EsGenericWhereOperator implements QueryOperator { +public class EsQueryOperator extends EsWhereOperatorImpl implements QueryOperator { private final DBType dbType; private SearchRequest searchRequest; @@ -44,7 +44,7 @@ public class EsQueryOperator extends EsGenericWhereOperator imple public EsQueryOperator() { super(); - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.searchBuilder = new SearchRequest.Builder(); // 默认查询所有数据 this.searchBuilder.size(Integer.MAX_VALUE); @@ -57,12 +57,18 @@ public String getDSL() { String dsl = JsonpUtils.toString(request.query()); return dsl.substring(dsl.indexOf(StringPool.COLON + StringPool.SPACE) + 2); } - return null; + return StringPool.EMPTY; } @Override - public QueryOperator parse(String dsl) { - return null; + public EsQueryOperator parse(String dsl) { + // nothing to do + return self(); + } + + @Override + public EsQueryOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsQueryOperator()); } @Override @@ -75,7 +81,7 @@ public void reset() { @Override public void setDBType(DBType dbType) { - throw Exceptions.unOperate("setDBType"); + // nothing to do } @Override @@ -85,16 +91,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - throw new DSLException(ERROR_MSG); + return getDSL(); } @Override public List getPrepareValues() { - throw new DSLException(ERROR_MSG); + return List.of(); } @Override - public QueryOperator from(Table table) { + public EsQueryOperator from(Table table) { this.table = table; searchBuilder = searchBuilder.index(table.getName().format()); return self(); @@ -106,17 +112,17 @@ public Table getTable() { } @Override - public QueryOperator select(DSLName dslName) { + public EsQueryOperator select(DSLName dslName) { return this.selects(Collections.singleton(dslName)); } @Override - public QueryOperator select(DSLName dslName, String alias) { + public EsQueryOperator select(DSLName dslName, String alias) { return this.selects(Collections.singleton(dslName)); } @Override - public QueryOperator selects(Collection dslNames) { + public EsQueryOperator selects(Collection dslNames) { for (DSLName dslName : dslNames) { String name = dslName.format(); FieldAndFormat ff = FieldAndFormat.of(f -> f.field(name)); @@ -132,55 +138,55 @@ public List obtainSelectColumns() { } @Override - public QueryOperator distinct() { + public EsQueryOperator distinct() { throw new DSLException(ERROR_MSG); } @Override - public QueryOperator distinctOn(DSLName sqlName, String alias) { - Aggregation aggregation = Aggregation.of(a -> a.cardinality(v -> v.field(sqlName.format()))); + public EsQueryOperator distinctOn(DSLName dslName, String alias) { + Aggregation aggregation = Aggregation.of(a -> a.cardinality(v -> v.field(dslName.format()))); searchBuilder.aggregations(alias, aggregation); throw new DSLException(ERROR_MSG); } @Override - public QueryOperator aggregate(Func syntax, DSLName sqlName, String alias, Distinct distinct) { + public EsQueryOperator aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct) { Aggregation aggregation = Aggregation.of(a -> { switch (syntax) { case COUNT_FUNCTION: - return a.valueCount(v -> v.field(sqlName.format())); + return a.valueCount(v -> v.field(dslName.format())); case MIN_FUNCTION: - return a.min(m -> m.field(sqlName.format())); + return a.min(m -> m.field(dslName.format())); case AVG_FUNCTION: - return a.avg(m -> m.field(sqlName.format())); + return a.avg(m -> m.field(dslName.format())); case SUM_FUNCTION: - return a.sum(s -> s.field(sqlName.format())); + return a.sum(s -> s.field(dslName.format())); case MAX_FUNCTION: - return a.max(m -> m.field(sqlName.format())); + return a.max(m -> m.field(dslName.format())); default: return null; } }); searchBuilder.aggregations(alias, aggregation); if (distinct != null) { - Aggregation d = Aggregation.of(a -> a.cardinality(v -> v.field(sqlName.format()))); + Aggregation d = Aggregation.of(a -> a.cardinality(v -> v.field(dslName.format()))); searchBuilder.aggregations(alias + "d", d); } return self(); } @Override - public QueryOperator from(QueryOperator fromTable, String alias) { + public EsQueryOperator from(QueryOperator fromTable, String alias) { throw new DSLException(ERROR_MSG); } @Override - public QueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { + public EsQueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { throw new DSLException(ERROR_MSG); } @Override - public QueryOperator orderBy(DSLName sqlName, OrderCondition orderCondition) { + public EsQueryOperator orderBy(DSLName dslName, OrderCondition orderCondition) { SortOrder sortOrder; switch (orderCondition) { case ASC: @@ -190,26 +196,27 @@ public QueryOperator orderBy(DSLName sqlName, OrderCondition orderCondition) { default: sortOrder = SortOrder.Desc; } - SortOptions sortOptions = SortOptions.of(sp -> sp.field(fs -> fs.field(sqlName.format()).order(sortOrder))); + SortOptions sortOptions = SortOptions.of(sp -> sp.field(fs -> fs.field(dslName.format()).order(sortOrder))); searchBuilder.sort(Collections.singletonList(sortOptions)); return self(); } @Override - public QueryOperator limit(Long limit, Long offset) { + public EsQueryOperator limit(Long limit, Long offset) { searchBuilder.from(Math.toIntExact(limit)); searchBuilder.size(Math.toIntExact(offset)); return self(); } @Override - public QueryOperator groupByOnes(Collection fieldNames) { + public EsQueryOperator groupByOnes(Collection fieldNames) { throw new DSLException(ERROR_MSG); } @Override - public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { - throw Exceptions.unOperate("tree"); + public EsQueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + // nothing to do + return self(); } /** @@ -233,43 +240,10 @@ public synchronized SearchRequest getSearchRequest() { * */ private void addDefaultQuery() { - if (CollectionUtils.isEmpty(mustQuery) && CollectionUtils.isEmpty(mustNotQuery) && CollectionUtils.isEmpty(shouldQuery)) { + if (CollectionUtils.isEmpty(mustQuery) + && CollectionUtils.isEmpty(mustNotQuery) + && CollectionUtils.isEmpty(shouldQuery)) { matchAll(); } } - - @Override - public QueryOperator neq(DSLName sqlName, Object value) { - return null; - } - - @Override - public QueryOperator notIn(DSLName sqlName, Object... values) { - return null; - } - - @Override - public QueryOperator notLike(DSLName sqlName, Object value) { - return null; - } - - @Override - public QueryOperator $notLike(DSLName sqlName, Object value) { - return null; - } - - @Override - public QueryOperator notLike$(DSLName sqlName, Object value) { - return null; - } - - @Override - public QueryOperator $notLike$(DSLName sqlName, Object value) { - return null; - } - - @Override - public QueryOperator nor() { - return null; - } -} +} \ No newline at end of file diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsUpdateOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsUpdateOperator.java similarity index 67% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsUpdateOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsUpdateOperator.java index 696cc979..316994d5 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsUpdateOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsUpdateOperator.java @@ -1,9 +1,7 @@ -package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.dml; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.type.DBType; import co.elastic.clients.elasticsearch._types.query_dsl.Query; import co.elastic.clients.elasticsearch.core.UpdateByQueryRequest; @@ -16,6 +14,7 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; +import java.util.function.UnaryOperator; import java.util.stream.Collectors; /** @@ -27,17 +26,16 @@ */ @AutoService(UpdateOperator.class) @Operator.Group(OperatorKey.ELASTICSEARCH_LITERAL) -public class EsUpdateOperator extends EsGenericWhereOperator implements UpdateOperator { +public class EsUpdateOperator extends EsWhereOperatorImpl implements UpdateOperator { - private DBType dbType; + private final DBType dbType; private UpdateByQueryRequest updateRequest; private UpdateByQueryRequest.Builder updateBuilder; private Table table; - private static final String ERROR_MSG = "elasticsearch update operator not support that operator"; public EsUpdateOperator() { super(); - this.dbType = DBType.ELASTIC_SEARCH; + this.dbType = DBType.ELASTICSEARCH; this.updateBuilder = new UpdateByQueryRequest.Builder(); } @@ -49,8 +47,14 @@ public String getDSL() { } @Override - public UpdateOperator parse(String dsl) { - return null; + public EsUpdateOperator parse(String dsl) { + // nothing todo + return self(); + } + + @Override + public EsUpdateOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new EsUpdateOperator()); } @Override @@ -61,7 +65,7 @@ public void reset() { @Override public void setDBType(DBType dbType) { - throw Exceptions.unOperate("setDBType"); + // nothing todo } @Override @@ -71,16 +75,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - return null; + return getDSL(); } @Override public List getPrepareValues() { - throw new DSLException(ERROR_MSG); + return List.of(); } @Override - public UpdateOperator from(Table table) { + public EsUpdateOperator from(Table table) { this.table = table; this.updateBuilder = updateBuilder.index(Collections.singletonList(table.getName().format())); return self(); @@ -92,7 +96,7 @@ public Table getTable() { } @Override - public UpdateOperator updates(Map values) { + public EsUpdateOperator updates(Map values) { // source String source = values.keySet().stream().map(o -> "ctx._source['" + o.format() + "'] = params['" + o.format() + "']").collect(Collectors.joining(StringPool.SEMICOLON)); // params @@ -102,7 +106,7 @@ public UpdateOperator updates(Map values) { } @Override - public UpdateOperator strictFill(String f, Supplier v) { + public EsUpdateOperator strictFill(String f, Supplier v) { return null; } @@ -119,34 +123,4 @@ public UpdateByQueryRequest getUpdateRequest() { } return updateRequest; } - - @Override - public UpdateOperator notIn(DSLName sqlName, Object... values) { - return null; - } - - @Override - public UpdateOperator notLike(DSLName sqlName, Object value) { - return null; - } - - @Override - public UpdateOperator $notLike(DSLName sqlName, Object value) { - return null; - } - - @Override - public UpdateOperator notLike$(DSLName sqlName, Object value) { - return null; - } - - @Override - public UpdateOperator $notLike$(DSLName sqlName, Object value) { - return null; - } - - @Override - public UpdateOperator nor() { - return null; - } -} +} \ No newline at end of file diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsGenericWhereOperator.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsWhereOperatorImpl.java similarity index 90% rename from uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsGenericWhereOperator.java rename to uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsWhereOperatorImpl.java index d7f8210e..ff806375 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/EsGenericWhereOperator.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/dsl/elasticsearch/dml/EsWhereOperatorImpl.java @@ -1,4 +1,4 @@ -package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +package cc.allio.uno.data.orm.dsl.elasticsearch.dml; import cc.allio.uno.core.api.Self; import cc.allio.uno.data.orm.dsl.DSLName; @@ -23,7 +23,7 @@ * @date 2023/5/29 13:22 * @since 1.1.4 */ -public abstract class EsGenericWhereOperator> implements WhereOperator { +public abstract class EsWhereOperatorImpl> implements WhereOperator { protected BoolQuery.Builder boolBuilder; @@ -36,7 +36,7 @@ public abstract class EsGenericWhereOperator> implements Where private static final String MUST_NOT_CONDITION = "must_not"; private static final AtomicReference LOGIC_PREDICATE = new AtomicReference<>(MUST_CONDITION); - protected EsGenericWhereOperator() { + protected EsWhereOperatorImpl() { boolBuilder = new BoolQuery.Builder(); this.mustQuery = Lists.newArrayList(); this.shouldQuery = Lists.newArrayList(); @@ -132,6 +132,31 @@ public T like(DSLName sqlName, Object value) { return logicQuery(WildcardQuery.of(wq -> wq.field(sqlName.format()).value(StringPool.STAR + value.toString() + StringPool.STAR))._toQuery()); } + @Override + public T notIn(DSLName sqlName, Object... values) { + return self(); + } + + @Override + public T notLike(DSLName sqlName, Object value) { + return self(); + } + + @Override + public T $notLike(DSLName sqlName, Object value) { + return self(); + } + + @Override + public T notLike$(DSLName sqlName, Object value) { + return self(); + } + + @Override + public T $notLike$(DSLName sqlName, Object value) { + return self(); + } + @Override public T or() { LOGIC_PREDICATE.set(SHOULD_CONDITION); @@ -144,6 +169,11 @@ public T and() { return self(); } + @Override + public T nor() { + return self(); + } + /** * and not * diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutor.java index 27e1d30d..c9c2e91a 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutor.java @@ -1,6 +1,7 @@ package cc.allio.uno.data.orm.executor.elasticsearch; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.executor.internal.InnerCommandExecutorManager; @@ -38,7 +39,7 @@ public EsCommandExecutor(ExecutorOptions options, RestClientBuilder restClientBu this.restClient = restClientBuilder.build(); JsonpMapper mapper = new JacksonJsonpMapper(); RestClientTransport transport = new RestClientTransport(restClient, mapper); - this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.ELASTICSEARCH); + this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.ELASTICSEARCH, options); SPIInnerCommandScanner scanner = options.getScanner(); this.manager = scanner.scan(new ElasticsearchIndicesClient(transport), new ElasticsearchClient(transport)); } @@ -68,7 +69,7 @@ public void destroy() { } @Override - protected InnerCommandExecutorManager getManager() { + protected InnerCommandExecutorManager getInnerCommandExecutorManager() { return manager; } } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java index 206b32c3..332dfe28 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/EsCommandExecutorLoader.java @@ -31,7 +31,7 @@ public EsCommandExecutorLoader(RestClientBuilder restClientBuilder) { @Override public EsCommandExecutor onLoad(List interceptors) { - ExecutorOptions executorOptions = new ExecutorOptionsImpl(DBType.ELASTIC_SEARCH, ExecutorKey.ELASTICSEARCH, OperatorKey.ELASTICSEARCH); + ExecutorOptions executorOptions = new ExecutorOptionsImpl(DBType.ELASTICSEARCH, ExecutorKey.ELASTICSEARCH, OperatorKey.ELASTICSEARCH); executorOptions.addInterceptors(interceptors); return load(executorOptions); } @@ -43,6 +43,6 @@ public EsCommandExecutor onLoad(ExecutorOptions executorOptions) { @Override public boolean match(DBType dbType) { - return DBType.ELASTIC_SEARCH == dbType; + return DBType.ELASTICSEARCH == dbType; } } diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsCreateTableCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsCreateTableCommandExecutor.java index f54aea01..cf645315 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsCreateTableCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsCreateTableCommandExecutor.java @@ -3,7 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.ddl.elasticsearch.EsCreateIndexOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.ddl.EsCreateIndexOperator; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteCommandExecutor.java index 0d7c6e09..48020a93 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteCommandExecutor.java @@ -3,7 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.dml.elasticsearch.EsDeleteOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsDeleteOperator; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteTableCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteTableCommandExecutor.java index 436e3b2c..b6612b12 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteTableCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsDeleteTableCommandExecutor.java @@ -3,7 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.ddl.elasticsearch.EsDropIndexOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.ddl.EsDropIndexOperator; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsExistTableCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsExistTableCommandExecutor.java index 8308ef7e..7cb89046 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsExistTableCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsExistTableCommandExecutor.java @@ -3,7 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.ddl.elasticsearch.EsExistIndexOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.ddl.EsExistIndexOperator; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsInsertCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsInsertCommandExecutor.java index 907dee75..b6f5ac37 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsInsertCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsInsertCommandExecutor.java @@ -3,7 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.dml.elasticsearch.EsInsertOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsInsertOperator; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsQueryCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsQueryCommandExecutor.java index 1b1ed047..d90d48b7 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsQueryCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsQueryCommandExecutor.java @@ -2,7 +2,7 @@ import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.dml.elasticsearch.EsQueryOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsQueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.type.JavaType; import cc.allio.uno.data.orm.dsl.type.TypeRegistry; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsShowColumnCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsShowColumnCommandExecutor.java index f73fc334..e41413f2 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsShowColumnCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsShowColumnCommandExecutor.java @@ -2,8 +2,8 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.dsl.ColumnDef; -import cc.allio.uno.data.orm.dsl.ddl.elasticsearch.EsPropertyAdapter; -import cc.allio.uno.data.orm.dsl.ddl.elasticsearch.EsShowColumnsOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.ddl.EsPropertyAdapter; +import cc.allio.uno.data.orm.dsl.elasticsearch.ddl.EsShowColumnsOperator; import cc.allio.uno.data.orm.dsl.type.DataType; import cc.allio.uno.data.orm.executor.CommandExecutor; import cc.allio.uno.data.orm.executor.ResultGroup; diff --git a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsUpdateCommandExecutor.java b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsUpdateCommandExecutor.java index 894dcc40..0fbea494 100644 --- a/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsUpdateCommandExecutor.java +++ b/uno-data/uno-data-elasticsearch/src/main/java/cc/allio/uno/data/orm/executor/elasticsearch/internal/EsUpdateCommandExecutor.java @@ -3,7 +3,7 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.dml.elasticsearch.EsUpdateOperator; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsUpdateOperator; import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; diff --git a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/ElasticSearchShowColumnsOperatorTest.java b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/ElasticSearchShowColumnsOperatorTest.java index f2a46452..295271e1 100644 --- a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/ElasticSearchShowColumnsOperatorTest.java +++ b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/ddl/elasticsearch/ElasticSearchShowColumnsOperatorTest.java @@ -1,5 +1,6 @@ package cc.allio.uno.data.orm.dsl.ddl.elasticsearch; +import cc.allio.uno.data.orm.dsl.elasticsearch.ddl.EsShowColumnsOperator; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchInsertOperatorTest.java b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchInsertOperatorTest.java index af7d37b0..0176a839 100644 --- a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchInsertOperatorTest.java +++ b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchInsertOperatorTest.java @@ -1,5 +1,6 @@ package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsInsertOperator; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchUpdateOperatorTest.java b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchUpdateOperatorTest.java index 8ff5237e..2a82aa0d 100644 --- a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchUpdateOperatorTest.java +++ b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticSearchUpdateOperatorTest.java @@ -1,6 +1,7 @@ package cc.allio.uno.data.orm.dsl.dml.elasticsearch; import cc.allio.uno.core.util.JsonUtils; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsUpdateOperator; import cc.allio.uno.data.test.model.Bank; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticsearchQueryOperatorTest.java b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticsearchQueryOperatorTest.java index 07dc0b7d..3c45be2b 100644 --- a/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticsearchQueryOperatorTest.java +++ b/uno-data/uno-data-elasticsearch/src/test/java/cc/allio/uno/data/orm/dsl/dml/elasticsearch/ElasticsearchQueryOperatorTest.java @@ -1,5 +1,6 @@ package cc.allio.uno.data.orm.dsl.dml.elasticsearch; +import cc.allio.uno.data.orm.dsl.elasticsearch.dml.EsQueryOperator; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-influxdb/pom.xml b/uno-data/uno-data-influxdb/pom.xml index e7cb7e66..b1db52d7 100644 --- a/uno-data/uno-data-influxdb/pom.xml +++ b/uno-data/uno-data-influxdb/pom.xml @@ -17,6 +17,10 @@ cc.allio uno-data-api + + cc.allio + uno-data-sql + com.influxdb influxdb-client-java diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/InfluxdbProperties.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/InfluxdbProperties.java new file mode 100644 index 00000000..78194e4a --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/InfluxdbProperties.java @@ -0,0 +1,73 @@ +package cc.allio.uno.data.orm.config.influxdb; + +import com.influxdb.LogLevel; +import com.influxdb.client.domain.WritePrecision; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * describe influxdb properties + * + * @author j.x + * @date 2024/4/14 18:16 + * @since 1.1.8 + */ +@Data +@ConfigurationProperties(prefix = "allio.uno.data.influxdb") +public class InfluxdbProperties { + + public static final String ORGANIZATION = "organization"; + public static final String LOG_LEVEL = "logLevel"; + public static final String WRITE_PRECISION = "writePrecision"; + public static final String TOKEN = "token"; + + /** + * enable influxdb + */ + private Boolean enabled = false; + + /** + * is system default command executor + */ + private Boolean systemDefault = false; + + /** + * influxdb address, like as localhost:8086 + */ + private String address = "http://localhost:8086"; + + /** + * influxdb organization + */ + private String organization; + + /** + * influxdb bucket + */ + private String bucket; + + /** + * auth for username + */ + private String username; + + /** + * auth for password + */ + private String password; + + /** + * the authentication token + */ + private String token; + + /** + * log level + */ + private LogLevel logLevel = LogLevel.BASIC; + + /** + * write precision, default is seconds + */ + private WritePrecision writePrecision = WritePrecision.S; +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/UnoInfluxdbAutoConfiguration.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/UnoInfluxdbAutoConfiguration.java index 82c9baf9..04534a52 100644 --- a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/UnoInfluxdbAutoConfiguration.java +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/config/influxdb/UnoInfluxdbAutoConfiguration.java @@ -1,13 +1,34 @@ package cc.allio.uno.data.orm.config.influxdb; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.CommandExecutorRegistry; +import cc.allio.uno.data.orm.executor.ExecutorOptionsBuilder; +import cc.allio.uno.data.orm.executor.influxdb.InfluxdbCommandExecutor; import cc.allio.uno.data.orm.executor.influxdb.InfluxdbCommandExecutorLoader; import cc.allio.uno.data.orm.executor.influxdb.InfluxdbCommandExecutorProcessor; +import cc.allio.uno.data.orm.executor.interceptor.Interceptor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import cc.allio.uno.data.orm.executor.options.ExecutorOptions; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.List; + +/** + * configuration for uno influxdb + * + * @author j.x + * @date 2024/4/14 18:17 + * @since 1.1.8 + */ @Configuration +@EnableConfigurationProperties(InfluxdbProperties.class) public class UnoInfluxdbAutoConfiguration { @Bean @@ -21,4 +42,33 @@ public InfluxdbCommandExecutorLoader influxdbCommandExecutorLoader() { public InfluxdbCommandExecutorProcessor influxdbCommandExecutorProcessor() { return new InfluxdbCommandExecutorProcessor(); } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnBean(CommandExecutorRegistry.class) + @ConditionalOnProperty(prefix = "allio.uno.data.influxdb", name = "enabled", havingValue = "true") + public InfluxdbCommandExecutor defaultInfluxdbCommandExecutor(InfluxdbCommandExecutorLoader commandExecutorLoader, + InfluxdbProperties influxdbProperties, + CommandExecutorRegistry commandExecutorRegistry, + ObjectProvider> interceptorProvider) { + List interceptors = interceptorProvider.getIfAvailable(List::of); + // create command option + ExecutorOptions executorOptions = + ExecutorOptionsBuilder.create(DBType.INFLUXDB) + .address(influxdbProperties.getAddress()) + .password(influxdbProperties.getPassword()) + .database(influxdbProperties.getBucket()) + .username(influxdbProperties.getUsername()) + .password(influxdbProperties.getPassword()) + .executorKey(ExecutorKey.INFLUXDB) + .operatorKey(OperatorKey.INFLUXDB) + .interceptors(interceptors) + .systemDefault(influxdbProperties.getSystemDefault()) + .setProperty(InfluxdbProperties.ORGANIZATION, influxdbProperties.getOrganization()) + .setProperty(InfluxdbProperties.LOG_LEVEL, influxdbProperties.getLogLevel()) + .setProperty(InfluxdbProperties.WRITE_PRECISION, influxdbProperties.getWritePrecision()) + .setProperty(InfluxdbProperties.TOKEN, influxdbProperties.getToken()) + .build(); + return commandExecutorRegistry.register(executorOptions, () -> commandExecutorLoader.load(executorOptions), false); + } } diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/InfluxDbTableAcceptor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/InfluxDbTableAcceptor.java new file mode 100644 index 00000000..b9a658d1 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/InfluxDbTableAcceptor.java @@ -0,0 +1,20 @@ +package cc.allio.uno.data.orm.dsl.influxdb; + +import cc.allio.uno.core.StringPool; +import cc.allio.uno.data.orm.dsl.MetaAcceptor; +import cc.allio.uno.data.orm.dsl.Table; + +/** + * in influxdb {@link Table#getSchema()} is empty, but for compatible other data service. so this {@link MetaAcceptor} solution problem + * + * @author j.x + * @date 2024/4/20 19:30 + * @since 1.1.8 + */ +public class InfluxDbTableAcceptor implements MetaAcceptor

{ + + @Override + public void onAccept(Table table) { + table.setSchema(StringPool.EMPTY); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbAlterBucketOperator.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbAlterBucketOperator.java index 9c8d982c..4a40969c 100644 --- a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbAlterBucketOperator.java +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbAlterBucketOperator.java @@ -1,13 +1,18 @@ package cc.allio.uno.data.orm.dsl.influxdb.ddl; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.ddl.AlterTableOperator; import cc.allio.uno.data.orm.dsl.type.DBType; +import com.google.common.collect.Lists; +import com.influxdb.client.domain.BucketRetentionRules; import lombok.Getter; import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.function.UnaryOperator; /** * influxdb alter bucket(table) operator @@ -19,20 +24,27 @@ @Getter @AutoService(AlterTableOperator.class) @Operator.Group(OperatorKey.INFLUXDB_LITERAL) -public class InfluxdbAlterBucketOperator implements AlterTableOperator { +public class InfluxdbAlterBucketOperator implements AlterTableOperator { private Table fromBucket; private Table toBucket; private Long retention; + private List retentionRules; @Override public String getDSL() { - throw Exceptions.unOperate("getDSL"); + return StringPool.EMPTY; } @Override - public AlterTableOperator parse(String dsl) { - throw Exceptions.unOperate("parse"); + public InfluxdbAlterBucketOperator parse(String dsl) { + // nothing to do + return self(); + } + + @Override + public InfluxdbAlterBucketOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new InfluxdbAlterBucketOperator()); } @Override @@ -53,7 +65,7 @@ public DBType getDBType() { } @Override - public AlterTableOperator from(Table table) { + public InfluxdbAlterBucketOperator from(Table table) { this.fromBucket = toBucket; return self(); } @@ -64,22 +76,25 @@ public Table getTable() { } @Override - public AlterTableOperator alertColumns(Collection columnDefs) { - throw Exceptions.unOperate("alertColumns"); + public InfluxdbAlterBucketOperator alertColumns(Collection columnDefs) { + // nothing to do + return self(); } @Override - public AlterTableOperator addColumns(Collection columnDefs) { - throw Exceptions.unOperate("addColumns"); + public InfluxdbAlterBucketOperator addColumns(Collection columnDefs) { + // nothing to do + return self(); } @Override - public AlterTableOperator deleteColumns(Collection columns) { - throw Exceptions.unOperate("deleteColumns"); + public InfluxdbAlterBucketOperator deleteColumns(Collection columns) { + // nothing to do + return self(); } @Override - public AlterTableOperator rename(Table to) { + public InfluxdbAlterBucketOperator rename(Table to) { this.toBucket = to; return self(); } @@ -90,8 +105,14 @@ public AlterTableOperator rename(Table to) { * @param retention the long type retention * @return return {@link InfluxdbAlterBucketOperator} */ - public InfluxdbAlterBucketOperator retention(Long retention) { - this.retention = retention; - return this; + public InfluxdbAlterBucketOperator addRetentionRule(UnaryOperator func) { + BucketRetentionRules rules = Optional.ofNullable(func).map(f -> f.apply(new BucketRetentionRules())).orElse(null); + if (retentionRules == null) { + retentionRules = Lists.newArrayList(); + } + if (rules != null) { + retentionRules.add(rules); + } + return self(); } } diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbCreateBucketOperator.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbCreateBucketOperator.java new file mode 100644 index 00000000..13f4183e --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/ddl/InfluxdbCreateBucketOperator.java @@ -0,0 +1,149 @@ +package cc.allio.uno.data.orm.dsl.influxdb.ddl; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.StringPool; +import cc.allio.uno.data.orm.dsl.ColumnDef; +import cc.allio.uno.data.orm.dsl.Operator; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import com.google.common.collect.Lists; +import com.influxdb.client.domain.BucketRetentionRules; +import com.influxdb.client.domain.Label; +import com.influxdb.client.domain.SchemaType; +import lombok.Getter; + +import java.util.List; +import java.util.Optional; +import java.util.function.UnaryOperator; + +/** + * influxdb create bucket operator + * + * @author j.x + * @date 2024/4/13 17:07 + * @since 1.1.8 + */ +@Getter +@AutoService(CreateTableOperator.class) +@Operator.Group(OperatorKey.INFLUXDB_LITERAL) +public class InfluxdbCreateBucketOperator implements CreateTableOperator { + + private Table fromBucket; + @Getter + private List retentionRules; + @Getter + private SchemaType schemaType; + @Getter + private List
fromBuckets; + + @Override + public QueryOperator toQueryOperator() { + throw Exceptions.unOperate("toQueryOperator"); + } + + @Override + public InfluxdbShowBucketsOperator schema(String schema) { + // nothing to do + return self(); + } + + @Override + public InfluxdbShowBucketsOperator from(Table table) { + if (fromBuckets == null) { + this.fromBuckets = Lists.newArrayList(); + } + fromBuckets.add(table); + return self(); + } + + @Override + public Table getTable() { + throw Exceptions.unOperate("getTable"); + } + + @Override + public InfluxdbShowBucketsOperator database(Database database) { + // nothing to do + return self(); + } + + @Override + public String getPrepareDSL() { + return StringPool.EMPTY; + } + + @Override + public List getPrepareValues() { + return Collections.emptyList(); + } + + @Override + public String getDSL() { + return StringPool.EMPTY; + } + + @Override + public InfluxdbShowBucketsOperator parse(String dsl) { + // nothing to do + return self(); + } + + @Override + public InfluxdbShowBucketsOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new InfluxdbShowBucketsOperator()); + } + + @Override + public void reset() { + this.fromBuckets = null; + } + + @Override + public void setDBType(DBType dbType) { + // nothing to do + } + + @Override + public DBType getDBType() { + return DBType.INFLUXDB; + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbDeleteOperator.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbDeleteOperator.java new file mode 100644 index 00000000..d9495945 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbDeleteOperator.java @@ -0,0 +1,127 @@ +package cc.allio.uno.data.orm.dsl.influxdb.dml; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import lombok.Getter; + +import java.util.Date; +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * influx delete measurement operator. + * + * @author j.x + * @date 2024/4/13 20:14 + * @see api + * @since 1.1.8 + */ +@Getter +@AutoService(DeleteOperator.class) +@Operator.Group(OperatorKey.INFLUXDB_LITERAL) +public class InfluxdbDeleteOperator extends InfluxdbSQLWhereOperatorImpl implements DeleteOperator { + + private Table measurement; + private Date startTime; + private Date stopTime; + private String bucket; + + private static final String MEASUREMENT_NAME = "_measurement"; + + public InfluxdbDeleteOperator() { + super(); + } + + @Override + public String getPrepareDSL() { + return getDSL(); + } + + @Override + public List getPrepareValues() { + return List.of(); + } + + @Override + public String getDSL() { + return sqlQueryOperator.getWhereDSL(); + } + + @Override + public InfluxdbDeleteOperator parse(String dsl) { + return self(); + } + + @Override + public InfluxdbDeleteOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new InfluxdbDeleteOperator()); + } + + @Override + public void reset() { + this.measurement = null; + this.startTime = null; + this.stopTime = null; + this.bucket = null; + sqlQueryOperator.reset(); + } + + @Override + public void setDBType(DBType dbType) { + // nothing to do + } + + @Override + public DBType getDBType() { + return DBType.INFLUXDB; + } + + @Override + public InfluxdbDeleteOperator from(Table table) { + this.measurement = table; + // set predicate like as "_measurement="xxx"" + eq(MEASUREMENT_NAME, table.getName().format()); + return self(); + } + + @Override + public Table getTable() { + return measurement; + } + + + /** + * range to delete data of start time + * + * @param startTime the start time of {@link Date} + * @return self + */ + public InfluxdbDeleteOperator start(Date startTime) { + this.startTime = startTime; + return self(); + } + + /** + * range to delete data of stop time + * + * @param stopTime the stop time of {@link Date} + * @return self + */ + public InfluxdbDeleteOperator stop(Date stopTime) { + this.stopTime = stopTime; + return self(); + } + + /** + * set to bucket + * + * @param bucket the bucket + * @return self + */ + public InfluxdbDeleteOperator bucket(String bucket) { + this.bucket = bucket; + return self(); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbInsertOperator.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbInsertOperator.java new file mode 100644 index 00000000..a3da749a --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbInsertOperator.java @@ -0,0 +1,189 @@ +package cc.allio.uno.data.orm.dsl.influxdb.dml; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.StringPool; +import cc.allio.uno.core.util.CollectionUtils; +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.InsertOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import lombok.Getter; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +/** + * influx db insert operator + *

when invoke relevant {@link #columns(String...)} api, means create influxdb fields, if create tags, please use

+ * + * @author j.x + * @date 2024/4/14 15:19 + * @since 1.1.8 + */ +@AutoService(InsertOperator.class) +@Operator.Group(OperatorKey.INFLUXDB_LITERAL) +public class InfluxdbInsertOperator implements InsertOperator { + + private Table measurement; + + private List columns; + @Getter + private List> fields; + @Getter + private List> tags; + + public InfluxdbInsertOperator() { + this.columns = Lists.newArrayList(); + this.fields = Lists.newArrayList(); + this.tags = Lists.newArrayList(); + } + + @Override + public InfluxdbInsertOperator strictFill(String f, Supplier v) { + DSLName dslName = DSLName.of(f); + // set to field + for (Map field : fields) { + if (field.containsKey(dslName)) { + field.put(dslName, v.get()); + } + } + // set to tags + for (Map tag : tags) { + if (tag.containsKey(dslName)) { + tag.put(dslName, v.get()); + } + } + return self(); + } + + @Override + public InfluxdbInsertOperator columns(Collection columns) { + this.columns.addAll(columns); + return self(); + } + + @Override + public InfluxdbInsertOperator values(List values) { + if (CollectionUtils.isEmpty(values)) { + return self(); + } + Map field = Maps.newHashMap(); + for (int i = 0; i < columns.size(); i++) { + DSLName col = columns.get(i); + Object v = values.get(i); + field.put(col, v); + } + this.fields.add(field); + return self(); + } + + /** + * set to measurement tags + * + * @see #tags(Map) + */ + public InfluxdbInsertOperator tags(String f1, Object v1) { + return tags(Map.of(DSLName.of(f1), getValueIfNull(v1))); + } + + /** + * set to measurement tags + * + * @see #tags(Map) + */ + public InfluxdbInsertOperator tags(String f1, Object v1, String f2, Object v2) { + return tags(Map.of( + DSLName.of(f1), getValueIfNull(v1), + DSLName.of(f2), getValueIfNull(v2))); + } + + /** + * set to measurement tags + * + * @see #tags(Map) + */ + public InfluxdbInsertOperator tags(String f1, Object v1, String f2, Object v2, String f3, Object v3) { + return tags(Map.of( + DSLName.of(f1), getValueIfNull(v1), + DSLName.of(f2), getValueIfNull(v2), + DSLName.of(f3), getValueIfNull(v3))); + } + + /** + * set to measurement tags + * + * @param values the tag map value + * @return self + */ + public InfluxdbInsertOperator tags(Map values) { + if (CollectionUtils.isEmpty(values)) { + return self(); + } + this.tags.add(values); + return self(); + } + + @Override + public boolean isBatched() { + return !fields.isEmpty(); + } + + @Override + public String getPrepareDSL() { + return getDSL(); + } + + @Override + public List getPrepareValues() { + return List.of(); + } + + @Override + public String getDSL() { + return StringPool.EMPTY; + } + + @Override + public InfluxdbInsertOperator parse(String dsl) { + // nothing to do + return self(); + } + + @Override + public InfluxdbInsertOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new InfluxdbInsertOperator()); + } + + @Override + public void reset() { + this.measurement = null; + this.columns = Lists.newArrayList(); + this.fields = Lists.newArrayList(); + this.tags = Lists.newArrayList(); + } + + @Override + public void setDBType(DBType dbType) { + // nothing to do + } + + @Override + public DBType getDBType() { + return DBType.INFLUXDB; + } + + @Override + public InfluxdbInsertOperator from(Table table) { + this.measurement = table; + return self(); + } + + @Override + public Table getTable() { + return measurement; + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbQueryOperator.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbQueryOperator.java new file mode 100644 index 00000000..3b4426ad --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbQueryOperator.java @@ -0,0 +1,183 @@ +package cc.allio.uno.data.orm.dsl.influxdb.dml; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.dsl.word.Distinct; +import lombok.Getter; + +import java.util.Collection; +import java.util.List; +import java.util.function.UnaryOperator; + +/** + * influx query operator use by InfluxQL + * + * @author j.x + * @date 2024/4/14 16:51 + * @since 1.1.8 + */ +@AutoService(QueryOperator.class) +@Operator.Group(OperatorKey.INFLUXDB_LITERAL) +public class InfluxdbQueryOperator extends InfluxdbSQLWhereOperatorImpl implements QueryOperator { + + private Table measurement; + + @Getter + private boolean count = false; + + public InfluxdbQueryOperator() { + super(); + } + + /** + * like official website said: support: + *
    + *
  • field key
  • + *
  • tag key
  • + *
  • measurement
  • + *
  • ...
  • + *
+ */ + @Override + public InfluxdbQueryOperator select(DSLName dslName) { + sqlQueryOperator.select(dslName); + return self(); + } + + @Override + public InfluxdbQueryOperator select(DSLName dslName, String alias) { + sqlQueryOperator.select(dslName, alias); + return self(); + } + + @Override + public InfluxdbQueryOperator selects(Collection dslNames) { + sqlQueryOperator.selects(dslNames); + return self(); + } + + @Override + public List obtainSelectColumns() { + return sqlQueryOperator.obtainSelectColumns(); + } + + @Override + public InfluxdbQueryOperator distinct() { + throw Exceptions.unOperate("distinct"); + } + + @Override + public InfluxdbQueryOperator distinctOn(DSLName dslName, String alias) { + throw Exceptions.unOperate("distinct"); + } + + @Override + public InfluxdbQueryOperator aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct) { + if (Func.COUNT_FUNCTION == syntax) { + count = true; + } + return self(); + } + + @Override + public InfluxdbQueryOperator from(QueryOperator fromTable, String alias) { + sqlQueryOperator.from(fromTable, alias); + return self(); + } + + @Override + public InfluxdbQueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { + throw Exceptions.unOperate("join(Table left, JoinType joinType, Table right, BinaryCondition condition)"); + } + + @Override + public InfluxdbQueryOperator orderBy(DSLName dslName, OrderCondition orderCondition) { + sqlQueryOperator.orderBy(dslName, orderCondition); + return self(); + } + + /** + * limit and slimit (maybe exist compatibility) + * + * @see LIMIT and SLIMIT clauses + */ + @Override + public InfluxdbQueryOperator limit(Long limit, Long offset) { + sqlQueryOperator.limit(limit, offset); + return self(); + } + + /** + * group by tags + * + * @see GROUP BY clause + */ + @Override + public InfluxdbQueryOperator groupByOnes(Collection fieldNames) { + sqlQueryOperator.groupByOnes(fieldNames); + return self(); + } + + @Override + public InfluxdbQueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + throw Exceptions.unOperate("tree(QueryOperator baseQuery, QueryOperator subQuery)"); + } + + @Override + public String getPrepareDSL() { + return sqlQueryOperator.getPrepareDSL(); + } + + @Override + public List getPrepareValues() { + return sqlQueryOperator.getPrepareValues(); + } + + @Override + public String getDSL() { + return sqlQueryOperator.getDSL(); + } + + @Override + public InfluxdbQueryOperator parse(String dsl) { + sqlQueryOperator = sqlQueryOperator.parse(dsl); + return self(); + } + + @Override + public InfluxdbQueryOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new InfluxdbQueryOperator()); + } + + @Override + public void reset() { + this.measurement = null; + this.count = false; + this.sqlQueryOperator.reset(); + } + + @Override + public void setDBType(DBType dbType) { + // nothing todo + } + + @Override + public DBType getDBType() { + return DBType.INFLUXDB; + } + + @Override + public InfluxdbQueryOperator from(Table table) { + this.measurement = table; + this.sqlQueryOperator.from(table); + return self(); + } + + @Override + public Table getTable() { + return measurement; + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbSQLWhereOperatorImpl.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbSQLWhereOperatorImpl.java new file mode 100644 index 00000000..8bc498cc --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbSQLWhereOperatorImpl.java @@ -0,0 +1,161 @@ +package cc.allio.uno.data.orm.dsl.influxdb.dml; + +import cc.allio.uno.data.orm.dsl.DSLName; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.WhereOperator; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLQueryOperator; + +/** + * use SQL as for InfluxQL + * + * @author j.x + * @date 2024/4/14 15:14 + * @since 1.1.8 + */ +public class InfluxdbSQLWhereOperatorImpl> implements WhereOperator { + + protected SQLQueryOperator sqlQueryOperator; + + public InfluxdbSQLWhereOperatorImpl() { + this.sqlQueryOperator = OperatorGroup.getQueryOperator(SQLQueryOperator.class, OperatorKey.SQL); + } + + @Override + public T gt(DSLName dslName, Object value) { + sqlQueryOperator.gt(dslName, value); + return self(); + } + + @Override + public T gte(DSLName dslName, Object value) { + sqlQueryOperator.gte(dslName, value); + return self(); + } + + @Override + public T lt(DSLName dslName, Object value) { + sqlQueryOperator.lt(dslName, value); + return self(); + } + + @Override + public T lte(DSLName dslName, Object value) { + sqlQueryOperator.lte(dslName, value); + return self(); + } + + @Override + public T eq(DSLName dslName, Object value) { + sqlQueryOperator.eq(dslName, value); + return self(); + } + + @Override + public T neq(DSLName dslName, Object value) { + sqlQueryOperator.neq(dslName, value); + return self(); + } + + @Override + public T notNull(DSLName dslName) { + sqlQueryOperator.notNull(dslName); + return self(); + } + + @Override + public T isNull(DSLName dslName) { + sqlQueryOperator.isNull(dslName); + return self(); + } + + @Override + public T in(DSLName dslName, V... values) { + sqlQueryOperator.in(dslName, values); + return self(); + } + + @Override + public T notIn(DSLName dslName, V... values) { + sqlQueryOperator.notIn(dslName, values); + return self(); + } + + @Override + public T between(DSLName dslName, Object withValue, Object endValue) { + sqlQueryOperator.between(dslName, withValue, endValue); + return self(); + } + + @Override + public T notBetween(DSLName dslName, Object withValue, Object endValue) { + sqlQueryOperator.notBetween(dslName, withValue, endValue); + return self(); + } + + @Override + public T like(DSLName dslName, Object value) { + sqlQueryOperator.lte(dslName, value); + return self(); + } + + @Override + public T $like(DSLName dslName, Object value) { + sqlQueryOperator.$like(dslName, value); + return self(); + } + + @Override + public T like$(DSLName dslName, Object value) { + sqlQueryOperator.like$(dslName, value); + return self(); + } + + @Override + public T $like$(DSLName dslName, Object value) { + sqlQueryOperator.$like$(dslName, value); + return self(); + } + + @Override + public T notLike(DSLName dslName, Object value) { + sqlQueryOperator.notLike(dslName, value); + return self(); + } + + @Override + public T $notLike(DSLName dslName, Object value) { + sqlQueryOperator.$notLike(dslName, value); + return self(); + } + + @Override + public T notLike$(DSLName dslName, Object value) { + sqlQueryOperator.notLike$(dslName, value); + return self(); + } + + @Override + public T $notLike$(DSLName dslName, Object value) { + sqlQueryOperator.$notLike$(dslName, value); + return self(); + } + + @Override + public T or() { + sqlQueryOperator.or(); + return self(); + } + + @Override + public T and() { + sqlQueryOperator.and(); + return self(); + } + + @Override + public T nor() { + sqlQueryOperator.nor(); + return self(); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbUpdateOperator.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbUpdateOperator.java new file mode 100644 index 00000000..678d3f7a --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/dsl/influxdb/dml/InfluxdbUpdateOperator.java @@ -0,0 +1,140 @@ +package cc.allio.uno.data.orm.dsl.influxdb.dml; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.StringPool; +import cc.allio.uno.data.orm.dsl.*; +import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; +import cc.allio.uno.data.orm.dsl.type.DBType; + +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +/** + * influx db update operator (bridge to {@link InfluxdbInsertOperator}) + * + * @author j.x + * @date 2024/4/14 15:48 + * @since 1.1.8 + */ +@AutoService(UpdateOperator.class) +@Operator.Group(OperatorKey.INFLUXDB_LITERAL) +public class InfluxdbUpdateOperator extends InfluxdbSQLWhereOperatorImpl implements UpdateOperator { + + private Table measurement; + private InfluxdbInsertOperator insertOperator; + + public InfluxdbUpdateOperator() { + insertOperator = new InfluxdbInsertOperator(); + } + + @Override + public InfluxdbUpdateOperator updates(Map values) { + insertOperator.inserts(values); + return self(); + } + + @Override + public InfluxdbUpdateOperator strictFill(String f, Supplier v) { + insertOperator.strictFill(f, v); + return self(); + } + + /** + * set to measurement tags + * + * @see #tags(Map) + */ + public InfluxdbUpdateOperator tags(String f1, Object v1) { + return tags(Map.of(DSLName.of(f1), getValueIfNull(v1))); + } + + /** + * set to measurement tags + * + * @see #tags(Map) + */ + public InfluxdbUpdateOperator tags(String f1, Object v1, String f2, Object v2) { + return tags(Map.of( + DSLName.of(f1), getValueIfNull(v1), + DSLName.of(f2), getValueIfNull(v2))); + } + + /** + * set to measurement tags + * + * @see #tags(Map) + */ + public InfluxdbUpdateOperator tags(String f1, Object v1, String f2, Object v2, String f3, Object v3) { + return tags(Map.of( + DSLName.of(f1), getValueIfNull(v1), + DSLName.of(f2), getValueIfNull(v2), + DSLName.of(f3), getValueIfNull(v3))); + } + + /** + * set to measurement tags + * + * @param values the tag map value + * @return self + */ + public InfluxdbUpdateOperator tags(Map values) { + insertOperator.tags(values); + return self(); + } + + @Override + public String getPrepareDSL() { + return getDSL(); + } + + @Override + public List getPrepareValues() { + return List.of(); + } + + @Override + public String getDSL() { + return StringPool.EMPTY; + } + + @Override + public InfluxdbUpdateOperator parse(String dsl) { + // nothing to do + return self(); + } + + @Override + public InfluxdbUpdateOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new InfluxdbUpdateOperator()); + } + + @Override + public void reset() { + this.measurement = null; + this.sqlQueryOperator.reset(); + this.insertOperator.reset(); + } + + @Override + public void setDBType(DBType dbType) { + // nothing to do + } + + @Override + public DBType getDBType() { + return DBType.INFLUXDB; + } + + @Override + public InfluxdbUpdateOperator from(Table table) { + this.measurement = table; + return self(); + } + + @Override + public Table getTable() { + return measurement; + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutor.java index a17ea6b3..2084fc28 100644 --- a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutor.java +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutor.java @@ -1,18 +1,22 @@ package cc.allio.uno.data.orm.executor.influxdb; +import cc.allio.uno.core.StringPool; import cc.allio.uno.core.util.template.ExpressionTemplate; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.config.influxdb.InfluxdbProperties; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.InfluxDbTableAcceptor; import cc.allio.uno.data.orm.executor.AbstractCommandExecutor; import cc.allio.uno.data.orm.executor.AggregateCommandExecutor; +import cc.allio.uno.data.orm.executor.influxdb.internal.InfluxdbCommandExecutorAdaptation; import cc.allio.uno.data.orm.executor.internal.InnerCommandExecutorManager; import cc.allio.uno.data.orm.executor.internal.SPIInnerCommandScanner; import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; import com.influxdb.LogLevel; -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.InfluxDBClientFactory; -import com.influxdb.client.InfluxDBClientOptions; +import com.influxdb.client.*; +import com.influxdb.client.domain.WritePrecision; import java.net.SocketTimeoutException; @@ -38,19 +42,32 @@ public InfluxdbCommandExecutor(ExecutorOptions options) { String address = options.getAddress(); String connectionString = ExpressionTemplate.parse(CONNECTION_URL_TEMPLATE, "address", address); clientOptionsBuilder.connectionString(connectionString); - // organization - String organization = options.getDatabase(); - clientOptionsBuilder.org(organization); // auth String username = options.getUsername(); String password = options.getPassword(); clientOptionsBuilder.authenticate(username, password.toCharArray()); - clientOptionsBuilder.logLevel(LogLevel.BASIC); + // bucket + String bucket = options.getDatabase(); + clientOptionsBuilder.bucket(bucket); + // organization + String organization = options.get(InfluxdbProperties.ORGANIZATION, String.class).orElse(StringPool.EMPTY); + clientOptionsBuilder.org(organization); + // token + String token = options.get(InfluxdbProperties.TOKEN, String.class).orElse(StringPool.EMPTY); + clientOptionsBuilder.authenticateToken(token.toCharArray()); + // settings + LogLevel logLevel = options.get(InfluxdbProperties.LOG_LEVEL, LogLevel.class).orElse(LogLevel.BASIC); + clientOptionsBuilder.logLevel(logLevel); + WritePrecision writePrecision = options.get(InfluxdbProperties.WRITE_PRECISION, WritePrecision.class).orElse(WritePrecision.S); + clientOptionsBuilder.precision(writePrecision); InfluxDBClientOptions clientOptions = clientOptionsBuilder.build(); this.influxDBClient = InfluxDBClientFactory.create(clientOptions); SPIInnerCommandScanner scanner = options.getScanner(); - this.manager = scanner.scan(influxDBClient); - this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.INFLUXDB); + InfluxdbCommandExecutorAdaptation adaptation = new InfluxdbCommandExecutorAdaptation(influxDBClient, clientOptions); + this.manager = scanner.scan(influxDBClient, clientOptions, adaptation); + + this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.INFLUXDB, options); + options.customizeMetaAcceptorSetter(Table.class, new InfluxDbTableAcceptor()); } @Override @@ -78,7 +95,7 @@ public void destroy() { } @Override - protected InnerCommandExecutorManager getManager() { + protected InnerCommandExecutorManager getInnerCommandExecutorManager() { return manager; } } diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java index 8b7fa162..9b21b52a 100644 --- a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorLoader.java @@ -3,8 +3,10 @@ import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.data.orm.executor.BaseCommandExecutorLoader; +import cc.allio.uno.data.orm.executor.CommandExecutorLoader; import cc.allio.uno.data.orm.executor.interceptor.Interceptor; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; +import com.google.auto.service.AutoService; import java.util.List; @@ -15,6 +17,7 @@ * @date 2024/4/1 17:21 * @since 1.1.8 */ +@AutoService(CommandExecutorLoader.class) public class InfluxdbCommandExecutorLoader extends BaseCommandExecutorLoader { @Override diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbAlterBucketCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbAlterBucketCommandExecutor.java index 54c8af6f..01469da3 100644 --- a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbAlterBucketCommandExecutor.java +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbAlterBucketCommandExecutor.java @@ -11,13 +11,22 @@ import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; import cc.allio.uno.data.orm.executor.internal.ATOInnerCommandExecutor; import cc.allio.uno.data.orm.executor.options.ExecutorKey; -import com.google.common.collect.Lists; import com.influxdb.client.BucketsApi; import com.influxdb.client.InfluxDBClient; import com.influxdb.client.domain.Bucket; import com.influxdb.client.domain.BucketRetentionRules; import lombok.extern.slf4j.Slf4j; +import java.util.List; + +/** + * influxdb alter bucket command executor + * + * @author j.x + * @date 2024/4/13 17:15 + * @see InfluxdbAlterBucketOperator + * @since 1.1.8 + */ @Slf4j @AutoService(ATOInnerCommandExecutor.class) @CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) @@ -42,12 +51,9 @@ public Boolean doExec(InfluxdbAlterBucketOperator operator, ResultSetHandler retentionRules = operator.getRetentionRules(); + if (retentionRules != null) { + updateBucket.setRetentionRules(retentionRules); } ResultRow resultRow; try { diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbCommandExecutorAdaptation.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbCommandExecutorAdaptation.java new file mode 100644 index 00000000..93845724 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbCommandExecutorAdaptation.java @@ -0,0 +1,84 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.util.StringUtils; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientOptions; +import com.influxdb.client.OrganizationsApi; +import com.influxdb.client.OrganizationsQuery; +import com.influxdb.client.domain.Organization; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * solution operate influxdb bucket api and CRUD time series data api... + *

should be contains how to get organization, maybe without creation

+ * + * @author j.x + * @date 2024/4/14 23:52 + * @since 1.1.8 + */ +@Slf4j +public class InfluxdbCommandExecutorAdaptation { + + @Getter + final InfluxDBClient influxDBClient; + @Getter + final InfluxDBClientOptions clientOptions; + + final ReadWriteLock readWriteLock; + + Organization currentOrganization; + + public InfluxdbCommandExecutorAdaptation(InfluxDBClient influxDBClient, InfluxDBClientOptions clientOptions) { + this.influxDBClient = influxDBClient; + this.clientOptions = clientOptions; + this.readWriteLock = new ReentrantReadWriteLock(); + } + + + /** + * get {@link Organization}, if empty according {@link InfluxDBClientOptions#getOrg()} create new {@link Organization} + * + * @return organization + */ + public Organization getCurrentOrganization() { + Lock readLock = readWriteLock.readLock(); + readLock.lock(); + try { + if (currentOrganization != null) { + return currentOrganization; + } + } finally { + readLock.unlock(); + } + Lock writeLock = readWriteLock.writeLock(); + writeLock.lock(); + try { + OrganizationsApi organizationsApi = influxDBClient.getOrganizationsApi(); + OrganizationsQuery organizationsQuery = new OrganizationsQuery(); + organizationsQuery.setOrg(clientOptions.getOrg()); + List organizations = organizationsApi.findOrganizations(organizationsQuery); + if (organizations.isEmpty()) { + log.warn("organization [{}] not found any one through api, now creat new organization base on InfluxDBClientOptions org name", clientOptions.getOrg()); + String org = clientOptions.getOrg(); + if (StringUtils.isBlank(org)) { + throw Exceptions.unNull("influxdb client options org value is empty."); + } + currentOrganization = organizationsApi.createOrganization(org); + } else { + // take out first one + currentOrganization = organizations.getFirst(); + } + + return currentOrganization; + } finally { + writeLock.unlock(); + } + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbCreateBucketCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbCreateBucketCommandExecutor.java new file mode 100644 index 00000000..38076d34 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbCreateBucketCommandExecutor.java @@ -0,0 +1,72 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.util.Requires; +import cc.allio.uno.core.util.StringUtils; +import cc.allio.uno.core.util.Values; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.ddl.InfluxdbCreateBucketOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.CTOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.*; +import com.influxdb.client.domain.Bucket; +import com.influxdb.client.domain.Organization; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; + +/** + * influxdb create bucket command executor + * + * @author j.x + * @date 2024/4/13 18:25 + * @see InfluxdbCreateBucketOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(CTOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbCreateBucketCommandExecutor implements CTOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + final InfluxdbCommandExecutorAdaptation adaptation; + + public InfluxdbCreateBucketCommandExecutor(InfluxDBClient influxDBClient, InfluxdbCommandExecutorAdaptation adaptation) { + this.influxDBClient = influxDBClient; + this.adaptation = adaptation; + } + + @Override + public Boolean doExec(InfluxdbCreateBucketOperator operator, ResultSetHandler handler) throws Throwable { + Table fromBucket = operator.getFromBucket(); + Requires.isNotNull(fromBucket, "fromBucket"); + BucketsApi bucketsApi = influxDBClient.getBucketsApi(); + // create bucket + Bucket bucket = new Bucket(); + bucket.setName(fromBucket.getName().format()); + Values.mapping(operator::getDescription, bucket::setDescription); + Values.mapping(operator::getLabels, bucket::setLabels); + Values.mapping(operator::getRetentionRules, bucket::setRetentionRules); + Values.mapping(operator::getSchemaType, bucket::setSchemaType); + // set org + Organization currentOrganization = adaptation.getCurrentOrganization(); + Values.mapping(currentOrganization::getId, bucket::setOrgID); + ResultRow resultRow; + try { + Bucket createBucket = bucketsApi.createBucket(bucket); + String id = createBucket.getId(); + resultRow = ResultRow.buildUpdateRow(StringUtils.isNotBlank(id)); + } catch (Throwable ex) { + log.error("Failed to Create influxdb bucket {}", fromBucket, ex); + resultRow = ResultRow.buildUpdateRow(false); + } + ResultGroup resultGroup = new ResultGroup(); + resultGroup.addRow(resultRow); + return handler.apply(resultGroup); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbDeleteCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbDeleteCommandExecutor.java new file mode 100644 index 00000000..740d9c04 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbDeleteCommandExecutor.java @@ -0,0 +1,95 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.util.DateUtil; +import cc.allio.uno.core.util.StringUtils; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbDeleteOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.DOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.DeleteApi; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientOptions; +import com.influxdb.client.domain.DeletePredicateRequest; +import lombok.extern.slf4j.Slf4j; + +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.util.Date; +import java.util.Map; + +/** + * influxdb delete command + * + * @author j.x + * @date 2024/4/14 17:06 + * @see InfluxdbDeleteOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(DOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbDeleteCommandExecutor implements DOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + final InfluxDBClientOptions clientOptions; + + public InfluxdbDeleteCommandExecutor(InfluxDBClient influxDBClient, InfluxDBClientOptions clientOptions) { + this.influxDBClient = influxDBClient; + this.clientOptions = clientOptions; + } + + @Override + public Boolean doExec(InfluxdbDeleteOperator operator, ResultSetHandler handler) throws Throwable { + Table measurement = operator.getTable(); + if (measurement == null) { + throw Exceptions.unNull("measurement is empty"); + } + String bucket = operator.getBucket(); + if (StringUtils.isBlank(bucket)) { + bucket = clientOptions.getBucket(); + } + DeleteApi deleteApi = influxDBClient.getDeleteApi(); + String predicate = operator.getDSL(); + Date startTime = operator.getStartTime(); + if (startTime == null) { + // set to epoch time + startTime = DateUtil.getEpochTime(); + } + Date stopTime = operator.getStopTime(); + if (stopTime == null) { + // set to now + stopTime = DateUtil.now(); + } + DeletePredicateRequest deletePredicateRequest = new DeletePredicateRequest(); + deletePredicateRequest.setStart(toOffsetDateTime(startTime)); + deletePredicateRequest.setStop(toOffsetDateTime(stopTime)); + deletePredicateRequest.setPredicate(predicate); + ResultRow resultRow; + try { + deleteApi.delete(deletePredicateRequest, bucket, clientOptions.getOrg()); + resultRow = ResultRow.buildUpdateRow(true); + } catch (Throwable ex) { + log.error("Failed to delete predicate [{}], measurement [{}], bucket [{}]", deletePredicateRequest, measurement, bucket, ex); + resultRow = ResultRow.buildUpdateRow(false); + } + print(log, Map.of("result", resultRow, "measurement", measurement, "predicate", deletePredicateRequest)); + ResultGroup resultGroup = new ResultGroup(); + resultGroup.addRow(resultRow); + return handler.apply(resultGroup); + } + + OffsetDateTime toOffsetDateTime(Date date) { + if (date == null) { + return null; + } + Instant instant = date.toInstant(); + return OffsetDateTime.ofInstant(instant, ZoneId.systemDefault()); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbDropBucketCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbDropBucketCommandExecutor.java new file mode 100644 index 00000000..4cedc0c7 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbDropBucketCommandExecutor.java @@ -0,0 +1,59 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.util.Requires; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.ddl.InfluxdbDropBucketOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.exception.ExecuteException; +import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.DTOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.BucketsApi; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.domain.Bucket; +import lombok.extern.slf4j.Slf4j; + +/** + * influxdb drop bucket command executor + * + * @author j.x + * @date 2024/4/13 18:45 + * @see InfluxdbDropBucketOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(DTOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbDropBucketCommandExecutor implements DTOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + + public InfluxdbDropBucketCommandExecutor(InfluxDBClient influxDBClient) { + this.influxDBClient = influxDBClient; + } + + @Override + public Boolean doExec(InfluxdbDropBucketOperator operator, ResultSetHandler handler) throws Throwable { + Table fromBucket = operator.getTable(); + Requires.isNotNull(fromBucket, "fromBucket"); + BucketsApi bucketsApi = influxDBClient.getBucketsApi(); + Bucket removalBucket = bucketsApi.findBucketByName(fromBucket.getName().getName()); + if (removalBucket == null) { + throw new ExecuteException(String.format("From the bucket name %s not found anything bucket", fromBucket.getName().format())); + } + ResultRow resultRow; + try { + bucketsApi.deleteBucket(removalBucket.getId()); + resultRow = ResultRow.buildUpdateRow(true); + } catch (Throwable ex) { + log.error("Failed drop bucket, the bucket is {}", fromBucket, ex); + resultRow = ResultRow.buildUpdateRow(false); + } + ResultGroup resultGroup = new ResultGroup(); + resultGroup.addRow(resultRow); + return handler.apply(resultGroup); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbExistBucketCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbExistBucketCommandExecutor.java new file mode 100644 index 00000000..fc7210ba --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbExistBucketCommandExecutor.java @@ -0,0 +1,54 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.util.Requires; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.ddl.InfluxdbExistBucketOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.ETOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.BucketsApi; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.domain.Bucket; +import lombok.extern.slf4j.Slf4j; + +/** + * influxdb exist bucket command executor + * + * @author j.x + * @date 2024/4/13 19:38 + * @see InfluxdbExistBucketOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(ETOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbExistBucketCommandExecutor implements ETOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + + public InfluxdbExistBucketCommandExecutor(InfluxDBClient influxDBClient) { + this.influxDBClient = influxDBClient; + } + + @Override + public Boolean doExec(InfluxdbExistBucketOperator operator, ResultSetHandler handler) throws Throwable { + Table fromBucket = operator.getTable(); + Requires.isNotNull(fromBucket, "fromBucket"); + BucketsApi bucketsApi = influxDBClient.getBucketsApi(); + ResultRow resultRow; + try { + Bucket existBucket = bucketsApi.findBucketByName(fromBucket.getName().format()); + resultRow = ResultRow.buildUpdateRow(existBucket != null); + } catch (Throwable ex) { + log.error("Failed exist bucket {}", fromBucket, ex); + resultRow = ResultRow.buildUpdateRow(false); + } + ResultGroup resultGroup = new ResultGroup(); + resultGroup.addRow(resultRow); + return handler.apply(resultGroup); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbInsertCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbInsertCommandExecutor.java new file mode 100644 index 00000000..a799ddf5 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbInsertCommandExecutor.java @@ -0,0 +1,112 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.type.Types; +import cc.allio.uno.core.util.DateUtil; +import cc.allio.uno.data.orm.dsl.DSLName; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbInsertOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.IOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.WriteApiBlocking; +import com.influxdb.client.domain.WritePrecision; +import com.influxdb.client.write.Point; +import lombok.extern.slf4j.Slf4j; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * influxdb insert command executor + * + * @author j.x + * @date 2024/4/14 17:22 + * @see InfluxdbInsertOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(IOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbInsertCommandExecutor implements IOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + + public InfluxdbInsertCommandExecutor(InfluxDBClient influxDBClient) { + this.influxDBClient = influxDBClient; + } + + @Override + public Boolean doExec(InfluxdbInsertOperator operator, ResultSetHandler handler) throws Throwable { + Table measurement = operator.getTable(); + if (measurement == null) { + throw Exceptions.unNull("measurement is empty"); + } + WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking(); + // build to write point + List> fields = operator.getFields(); + List> tags = operator.getTags(); + int size = Math.max(fields.size(), tags.size()); + List points = new ArrayList<>(size); + for (int i = 0; i < size; i++) { + Point point = new Point(measurement.getName().format()); + // set to field, maybe field size less than var i + if (fields.size() - 1 >= i) { + Map field = fields.get(i); + for (Map.Entry fieldEntry : field.entrySet()) { + DSLName dslName = fieldEntry.getKey(); + String fieldName = dslName.format(); + Object value = fieldEntry.getValue(); + // set to specific type + switch (value) { + case Boolean b -> point.addField(fieldName, b); + case Integer n -> point.addField(fieldName, n); + case Long l -> point.addField(fieldName, l); + case Double d -> point.addField(fieldName, d); + case Short s -> point.addField(fieldName, s); + case Float f -> point.addField(fieldName, f.doubleValue()); + case BigDecimal f -> point.addField(fieldName, f.doubleValue()); + case null, default -> { + // set to string + String stringValue = Types.toString(value); + point.addField(fieldName, stringValue); + } + } + } + } + + // set to tags, maybe tag size less than var i + if (tags.size() - 1 >= i) { + Map tag = tags.get(i); + for (Map.Entry tagEntry : tag.entrySet()) { + DSLName dslName = tagEntry.getKey(); + String tagName = dslName.format(); + Object value = tagEntry.getValue(); + String stringValue = Types.toString(value); + point.addTag(tagName, stringValue); + } + } + // set current time as influx timestamp + point.time(DateUtil.now().getTime(), WritePrecision.MS); + points.add(point); + } + ResultRow resultRow; + try { + writeApiBlocking.writePoints(points); + resultRow = ResultRow.buildUpdateRow(true); + } catch (Throwable ex) { + log.error("Failed insert measurement [{}]", measurement, ex); + resultRow = ResultRow.buildUpdateRow(false); + } + ResultGroup resultGroup = new ResultGroup(); + resultGroup.addRow(resultRow); + return handler.apply(resultGroup); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbQueryCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbQueryCommandExecutor.java new file mode 100644 index 00000000..e543e1fe --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbQueryCommandExecutor.java @@ -0,0 +1,112 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.data.orm.dsl.DSLName; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbQueryOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.ResultSet; +import cc.allio.uno.data.orm.executor.handler.ListResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.QOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.google.common.collect.Lists; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientOptions; +import com.influxdb.client.InfluxQLQueryApi; +import com.influxdb.client.domain.InfluxQLQuery; +import com.influxdb.query.InfluxQLQueryResult; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * influxdb query command executor + * + * @author j.x + * @date 2024/4/14 17:53 + * @since 1.1.8 + */ +@Slf4j +@AutoService(QOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbQueryCommandExecutor implements QOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + final InfluxDBClientOptions clientOptions; + + public InfluxdbQueryCommandExecutor(InfluxDBClient influxDBClient, InfluxDBClientOptions clientOptions) { + this.influxDBClient = influxDBClient; + this.clientOptions = clientOptions; + } + + @Override + public List doExec(InfluxdbQueryOperator operator, ListResultSetHandler handler) throws Throwable { + Table measurement = operator.getTable(); + if (measurement == null) { + throw Exceptions.unNull("measurement is empty"); + } + InfluxQLQueryApi influxQLQueryApi = influxDBClient.getInfluxQLQueryApi(); + + // InfluxQL dsl (like SQL) + String command = operator.getDSL(); + InfluxQLQuery influxQlQuery = new InfluxQLQuery(command, clientOptions.getBucket()); + influxQlQuery.setPrecision(InfluxQLQuery.InfluxQLPrecision.SECONDS); + InfluxQLQueryResult result = influxQLQueryApi.query(influxQlQuery); + ResultSet resultSet = new ResultSet(); + if (result == null) { + return handler.apply(resultSet); + } + List results = result.getResults(); + // by count + if (operator.isCount()) { + long count = results.stream().flatMap(r -> r.getSeries().stream()).flatMap(s -> s.getValues().stream()).count(); + ResultRow resultRow = ResultRow.buildCountRow(count); + ResultGroup resultGroup = new ResultGroup(); + resultGroup.addRow(resultRow); + resultSet.setResultGroups(Lists.newArrayList(resultGroup)); + } else { + List resultGroups = + results.stream() + .flatMap(r -> { + List series = r.getSeries(); + return series.stream(); + }) + .flatMap(series -> { + List values = series.getValues(); + return values.stream() + .map(record -> { + ResultGroup resultGroup = new ResultGroup(); + // build row from columns + Map columns = series.getColumns(); + for (String col : columns.keySet()) { + ResultRow.ResultRowBuilder rowBuilder = ResultRow.builder(); + rowBuilder.column(DSLName.of(col)); + rowBuilder.value(Optional.ofNullable(record).map(r -> r.getValueByKey(col)).orElse(null)); + ResultRow row = rowBuilder.build(); + resultGroup.addRow(row); + } + // build row from tags + Map tags = series.getTags(); + for (Map.Entry tagEntry : tags.entrySet()) { + String tagCol = tagEntry.getKey(); + String tagValue = tagEntry.getValue(); + ResultRow.ResultRowBuilder rowBuilder = ResultRow.builder(); + rowBuilder.column(DSLName.of(tagCol)); + rowBuilder.value(tagValue); + ResultRow row = rowBuilder.build(); + resultGroup.addRow(row); + } + return resultGroup; + }); + }) + .toList(); + resultSet.setResultGroups(resultGroups); + } + return handler.apply(resultSet); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbShowBucketsCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbShowBucketsCommandExecutor.java new file mode 100644 index 00000000..61ba3b56 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbShowBucketsCommandExecutor.java @@ -0,0 +1,96 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.data.orm.dsl.DSLName; +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.ddl.InfluxdbShowBucketsOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultRow; +import cc.allio.uno.data.orm.executor.ResultSet; +import cc.allio.uno.data.orm.executor.handler.ListResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.STInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.*; +import com.influxdb.client.domain.Bucket; +import lombok.extern.slf4j.Slf4j; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static cc.allio.uno.data.orm.executor.handler.TableListResultSetHandler.*; + +/** + * influxdb show buckets command executor + * + * @author j.x + * @date 2024/4/13 20:00 + * @see InfluxdbShowBucketsOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(STInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbShowBucketsCommandExecutor implements STInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + + public InfluxdbShowBucketsCommandExecutor(InfluxDBClient influxDBClient) { + this.influxDBClient = influxDBClient; + } + + @Override + public List
doExec(InfluxdbShowBucketsOperator operator, ListResultSetHandler
handler) throws Throwable { + List
fromBuckets = operator.getFromBuckets(); + if (fromBuckets == null) { + fromBuckets = Collections.emptyList(); + } + List filteringBucketNames = fromBuckets.stream().map(Table::getName).map(DSLName::format).toList(); + BucketsApi bucketsApi = influxDBClient.getBucketsApi(); + List buckets = bucketsApi.findBuckets(); + List resultGroups = + buckets.stream() + .filter(bucket -> { + if (filteringBucketNames.isEmpty()) { + return true; + } + // filter exist name + String bucketName = bucket.getName(); + return filteringBucketNames.contains(bucketName); + }) + .map(bucket -> { + ResultGroup resultGroup = new ResultGroup(); + // set database name + ResultRow rowDb = + ResultRow.builder() + .column(TABLE_CATALOG_DSL_NAME) + .value(bucket.getName()) + .build(); + resultGroup.addRow(rowDb); + // set bucket name + String bucketName = bucket.getName(); + ResultRow rowName = + ResultRow.builder() + .column(TABLE_NAME_DSL_NAME) + .value(bucketName) + .build(); + resultGroup.addRow(rowName); + // set bucket type + String bucketType = bucket.getType().getValue(); + ResultRow rowType = + ResultRow.builder() + .column(TABLE_TYPE_DSL_NAME) + .value(bucketType) + .build(); + resultGroup.addRow(rowType); + return resultGroup; + }) + .toList(); + + print(log, Map.of("buckets", buckets)); + ResultSet resultSet = new ResultSet(); + resultSet.setResultGroups(resultGroups); + return handler.apply(resultSet); + } +} diff --git a/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbUpdateCommandExecutor.java b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbUpdateCommandExecutor.java new file mode 100644 index 00000000..7580fc60 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/main/java/cc/allio/uno/data/orm/executor/influxdb/internal/InfluxdbUpdateCommandExecutor.java @@ -0,0 +1,53 @@ +package cc.allio.uno.data.orm.executor.influxdb.internal; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.data.orm.dsl.DSLName; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbInsertOperator; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbUpdateOperator; +import cc.allio.uno.data.orm.executor.CommandExecutor; +import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; +import cc.allio.uno.data.orm.executor.internal.UOInnerCommandExecutor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import com.influxdb.client.InfluxDBClient; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; +import java.util.Map; + +/** + * influxdb update command (bridge to {@link InfluxdbInsertCommandExecutor}) + * + * @author j.x + * @date 2024/4/14 17:45 + * @see InfluxdbUpdateOperator + * @since 1.1.8 + */ +@Slf4j +@AutoService(UOInnerCommandExecutor.class) +@CommandExecutor.Group(ExecutorKey.INFLUXDB_LITERAL) +public class InfluxdbUpdateCommandExecutor implements UOInnerCommandExecutor { + + final InfluxDBClient influxDBClient; + + final InfluxdbInsertCommandExecutor bridge; + + public InfluxdbUpdateCommandExecutor(InfluxDBClient influxDBClient) { + this.influxDBClient = influxDBClient; + this.bridge = new InfluxdbInsertCommandExecutor(influxDBClient); + } + + @Override + public Boolean doExec(InfluxdbUpdateOperator operator, ResultSetHandler handler) throws Throwable { + InfluxdbInsertOperator influxdbInsertOperator = new InfluxdbInsertOperator(); + influxdbInsertOperator.from(operator.getTable()); + List> tags = influxdbInsertOperator.getTags(); + for (Map tag : tags) { + influxdbInsertOperator.tags(tag); + } + List> fields = influxdbInsertOperator.getFields(); + for (Map field : fields) { + influxdbInsertOperator.inserts(field); + } + return bridge.doExec(influxdbInsertOperator, handler); + } +} diff --git a/uno-data/uno-data-influxdb/src/test/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorTest.java b/uno-data/uno-data-influxdb/src/test/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorTest.java new file mode 100644 index 00000000..0750ed70 --- /dev/null +++ b/uno-data/uno-data-influxdb/src/test/java/cc/allio/uno/data/orm/executor/influxdb/InfluxdbCommandExecutorTest.java @@ -0,0 +1,109 @@ +package cc.allio.uno.data.orm.executor.influxdb; + +import cc.allio.uno.data.orm.dsl.Table; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbDeleteOperator; +import cc.allio.uno.data.orm.dsl.influxdb.dml.InfluxdbInsertOperator; +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; +import cc.allio.uno.data.test.executor.CommandExecutorSetter; +import cc.allio.uno.test.BaseTestCase; +import cc.allio.uno.test.RunTest; +import cc.allio.uno.test.testcontainers.ContainerType; +import cc.allio.uno.test.testcontainers.RunContainer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +@RunTest +@RunContainer(ContainerType.Influxdb) +public class InfluxdbCommandExecutorTest extends BaseTestCase implements CommandExecutorSetter { + + InfluxdbCommandExecutor executor; + + @BeforeEach + void init() { + boolean dual = executor.createTable(o -> o.from("dual")); + assertTrue(dual); + } + + @Test + void testShowBuckets() { + List
buckets = executor.showTables(); + // test-bucket + // _monitoring + // dual + // _tasks + assertEquals(4, buckets.size()); + } + + @Test + void testDropBucket() { + boolean dropped = executor.dropTable("dual"); + assertTrue(dropped); + List
buckets = executor.showTables(); + assertEquals(3, buckets.size()); + } + + @Test + void testExistBucket() { + boolean existed = executor.existTable("dual"); + assertTrue(existed); + } + + @Test + void testInsertTimeSeries() { + boolean insert = executor.insert( + o -> o.insert("a", 1, "b", 2) + .from("dual") + .castReality(InfluxdbInsertOperator.class) + .tags("t", "t")); + assertTrue(insert); + } + + @Test + void testQueryTimeSeries() { + prepareData(); + + List resultGroups = executor.queryList(o -> o.selectAll().from("dual").eq("a", 1)); + assertEquals(2, resultGroups.size()); + resultGroups = executor.queryList(o -> o.selectAll().from("dual").eq("a", 2)); + assertEquals(0, resultGroups.size()); + + // test count + ResultGroup r = executor.queryOne(o -> o.from("dual").count().selectAll()); + assertEquals(2, r.getLongValue(BoolResultHandler.GUESS_COUNT)); + } + + + @Test + void testDeleteTimeSeries() { + prepareData(); + + boolean deleted = executor.delete(o -> o.from("dual").castReality(InfluxdbDeleteOperator.class)); + assertTrue(deleted); + ResultGroup r = executor.queryOne(o -> o.from("dual").count().selectAll()); + assertEquals(0, r.getLongValue(BoolResultHandler.GUESS_COUNT)); + } + + void prepareData() { + executor.insert( + o -> o.insert("a", 1, "b", 2) + .from("dual") + .castReality(InfluxdbInsertOperator.class) + .tags("t", "t")); + + executor.insert( + o -> o.insert("a", 1, "b", 2) + .from("dual") + .castReality(InfluxdbInsertOperator.class) + .tags("t", "t")); + } + + @Test + + @Override + public void setCommandExecutor(InfluxdbCommandExecutor executor) { + this.executor = executor; + } +} diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/MongodbProperties.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/MongodbProperties.java new file mode 100644 index 00000000..1ecf26f7 --- /dev/null +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/MongodbProperties.java @@ -0,0 +1,46 @@ +package cc.allio.uno.data.orm.config.mongodb; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * describe mongodb properties + * + * @author j.x + * @date 2024/4/14 19:35 + * @since 1.1.8 + */ +@Data +@ConfigurationProperties(prefix = "allio.uno.data.mongodb") +public class MongodbProperties { + + /** + * enable influxdb + */ + private Boolean enabled = false; + + /** + * influxdb address, like as localhost:27017 + */ + private String address = "localhost:27017"; + + /** + * auth for username + */ + private String username; + + /** + * auth for password + */ + private String password; + + /** + * database + */ + private String database; + + /** + * is system default command executor + */ + private Boolean systemDefault = true; +} diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/UnoMongodbAutoConfiguration.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/UnoMongodbAutoConfiguration.java index c0aaa050..42a7e169 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/UnoMongodbAutoConfiguration.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/config/mongodb/UnoMongodbAutoConfiguration.java @@ -1,13 +1,34 @@ package cc.allio.uno.data.orm.config.mongodb; +import cc.allio.uno.data.orm.dsl.OperatorKey; +import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.executor.CommandExecutorRegistry; +import cc.allio.uno.data.orm.executor.ExecutorOptionsBuilder; +import cc.allio.uno.data.orm.executor.interceptor.Interceptor; +import cc.allio.uno.data.orm.executor.mongodb.MongodbCommandExecutor; import cc.allio.uno.data.orm.executor.mongodb.MongodbCommandExecutorLoader; import cc.allio.uno.data.orm.executor.mongodb.MongodbCommandExecutorProcessor; +import cc.allio.uno.data.orm.executor.options.ExecutorKey; +import cc.allio.uno.data.orm.executor.options.ExecutorOptions; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.List; + +/** + * configuration for mongodb + * + * @author j.x + * @date 2024/3/14 19:37 + * @since 1.1.7 + */ @Configuration +@EnableConfigurationProperties(MongodbProperties.class) public class UnoMongodbAutoConfiguration { @Bean @@ -21,4 +42,25 @@ public MongodbCommandExecutorLoader mongodbCommandExecutorLoader() { public MongodbCommandExecutorProcessor mongodbCommandExecutorProcessor() { return new MongodbCommandExecutorProcessor(); } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnBean(CommandExecutorRegistry.class) + @ConditionalOnProperty(prefix = "allio.uno.data.mongodb", name = "enabled", havingValue = "true") + public MongodbCommandExecutor defaultDbCommandExecutor(MongodbCommandExecutorLoader commandExecutorLoader, + MongodbProperties mongodbProperties, + CommandExecutorRegistry commandExecutorRegistry, + ObjectProvider> interceptorProvider) { + List interceptors = interceptorProvider.getIfAvailable(List::of); + ExecutorOptions executorOptions = + ExecutorOptionsBuilder.create(DBType.MONGODB) + .address(mongodbProperties.getAddress()) + .username(mongodbProperties.getUsername()) + .password(mongodbProperties.getPassword()) + .executorKey(ExecutorKey.MONGODB) + .operatorKey(OperatorKey.MONGODB) + .interceptors(interceptors) + .build(); + return commandExecutorRegistry.register(executorOptions, () -> commandExecutorLoader.load(executorOptions), false); + } } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbAlterCollectionOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbAlterCollectionOperator.java index 24255dcf..8ac63dc3 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbAlterCollectionOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbAlterCollectionOperator.java @@ -11,6 +11,7 @@ import org.bson.BsonString; import java.util.Collection; +import java.util.function.UnaryOperator; /** * mongodb alter collection(table) operator @@ -22,7 +23,7 @@ @Getter @AutoService(AlterTableOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbAlterCollectionOperator implements AlterTableOperator { +public class MongodbAlterCollectionOperator implements AlterTableOperator { private Table fromColl; private Table toColl; @@ -40,12 +41,17 @@ public String getDSL() { } @Override - public AlterTableOperator parse(String dsl) { + public MongodbAlterCollectionOperator parse(String dsl) { // nothing to do reset(); return self(); } + @Override + public MongodbAlterCollectionOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbAlterCollectionOperator()); + } + @Override public void reset() { this.fromColl = null; @@ -63,7 +69,7 @@ public DBType getDBType() { } @Override - public AlterTableOperator from(Table table) { + public MongodbAlterCollectionOperator from(Table table) { this.fromColl = table; return self(); } @@ -74,25 +80,25 @@ public Table getTable() { } @Override - public AlterTableOperator alertColumns(Collection columnDefs) { + public MongodbAlterCollectionOperator alertColumns(Collection columnDefs) { // nothing to do return self(); } @Override - public AlterTableOperator addColumns(Collection columnDefs) { + public MongodbAlterCollectionOperator addColumns(Collection columnDefs) { // nothing to do return self(); } @Override - public AlterTableOperator deleteColumns(Collection columns) { + public MongodbAlterCollectionOperator deleteColumns(Collection columns) { // nothing to do return self(); } @Override - public AlterTableOperator rename(Table to) { + public MongodbAlterCollectionOperator rename(Table to) { this.toColl = to; return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperator.java index 08cf40da..f88996bb 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperator.java @@ -12,6 +12,8 @@ import org.bson.BsonDocument; import org.bson.BsonString; +import java.util.function.UnaryOperator; + /** * mongodb create collection operator * @@ -22,7 +24,7 @@ @Getter @AutoService(CreateTableOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbCreateCollectionOperator implements CreateTableOperator { +public class MongodbCreateCollectionOperator implements CreateTableOperator { private Table fromColl; @@ -37,11 +39,16 @@ public String getDSL() { } @Override - public CreateTableOperator parse(String dsl) { + public MongodbCreateCollectionOperator parse(String dsl) { reset(); return self(); } + @Override + public MongodbCreateCollectionOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbCreateCollectionOperator()); + } + @Override public void reset() { this.fromColl = null; @@ -58,7 +65,7 @@ public DBType getDBType() { } @Override - public CreateTableOperator from(Table table) { + public MongodbCreateCollectionOperator from(Table table) { this.fromColl = table; return self(); } @@ -69,13 +76,13 @@ public Table getTable() { } @Override - public CreateTableOperator column(ColumnDef columnDef) { + public MongodbCreateCollectionOperator column(ColumnDef columnDef) { // nothing to do return self(); } @Override - public CreateTableOperator comment(String comment) { + public MongodbCreateCollectionOperator comment(String comment) { // nothing to do return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperator.java index fa5d7076..4c226c0b 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperator.java @@ -11,6 +11,8 @@ import org.bson.BsonDocument; import org.bson.BsonString; +import java.util.function.UnaryOperator; + /** * mongodb drop collection operator * @@ -20,7 +22,7 @@ */ @AutoService(DropTableOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbDropCollectionOperator implements DropTableOperator { +public class MongodbDropCollectionOperator implements DropTableOperator { private Table fromColl; @@ -34,11 +36,16 @@ public String getDSL() { } @Override - public DropTableOperator parse(String dsl) { + public MongodbDropCollectionOperator parse(String dsl) { reset(); return self(); } + @Override + public MongodbDropCollectionOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbDropCollectionOperator()); + } + @Override public void reset() { this.fromColl = null; @@ -55,7 +62,7 @@ public DBType getDBType() { } @Override - public DropTableOperator from(Table table) { + public MongodbDropCollectionOperator from(Table table) { this.fromColl = table; return self(); } @@ -66,7 +73,7 @@ public Table getTable() { } @Override - public DropTableOperator ifExist(Boolean ifExist) { + public MongodbDropCollectionOperator ifExist(Boolean ifExist) { // nothing to do return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperator.java index c935a509..7e579de8 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperator.java @@ -13,6 +13,7 @@ import org.bson.BsonString; import java.util.List; +import java.util.function.UnaryOperator; /** * mongodb exist collection operator @@ -23,7 +24,7 @@ */ @AutoService(ExistTableOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbExistCollectionOperator implements ExistTableOperator { +public class MongodbExistCollectionOperator implements ExistTableOperator { private Table fromColl; @@ -40,11 +41,16 @@ public String getDSL() { } @Override - public ExistTableOperator parse(String dsl) { + public MongodbExistCollectionOperator parse(String dsl) { reset(); return self(); } + @Override + public MongodbExistCollectionOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbExistCollectionOperator()); + } + @Override public void reset() { this.fromColl = null; @@ -62,16 +68,16 @@ public DBType getDBType() { @Override public String getPrepareDSL() { - throw Exceptions.unOperate("getDSL"); + return getDSL(); } @Override public List getPrepareValues() { - throw Exceptions.unOperate("getDSL"); + return List.of(); } @Override - public ExistTableOperator from(Table table) { + public MongodbExistCollectionOperator from(Table table) { this.fromColl = table; return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperator.java index 6e229101..2abcbf26 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperator.java @@ -13,7 +13,9 @@ import org.bson.BsonInt32; import org.bson.BsonString; +import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; /** * mongodb show collections operator @@ -25,14 +27,14 @@ @Getter @AutoService(ShowTablesOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbShowCollectionsOperator implements ShowTablesOperator { +public class MongodbShowCollectionsOperator implements ShowTablesOperator { private Database fromDb; private final List
tables = Lists.newArrayList(); @Override - public ShowTablesOperator database(Database database) { + public MongodbShowCollectionsOperator database(Database database) { this.fromDb = database; return self(); } @@ -44,7 +46,6 @@ public String getDSL() { throw Exceptions.unOperate("check collections is empty"); } BsonDocument bson = new BsonDocument("listCollections", new BsonInt32(1)); - BsonArray collections = new BsonArray(); for (Table table : tables) { collections.add(new BsonString(table.getName().format())); @@ -55,11 +56,16 @@ public String getDSL() { } @Override - public ShowTablesOperator parse(String dsl) { + public MongodbShowCollectionsOperator parse(String dsl) { reset(); return self(); } + @Override + public MongodbShowCollectionsOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbShowCollectionsOperator()); + } + @Override public void reset() { this.fromDb = null; @@ -82,7 +88,7 @@ public String getPrepareDSL() { @Override public List getPrepareValues() { - throw Exceptions.unOperate("getPrepareValues"); + return Collections.emptyList(); } @Override @@ -91,18 +97,18 @@ public Table getTable() { } @Override - public QueryOperator toQueryOperator() { + public QueryOperator toQueryOperator() { throw Exceptions.unOperate("toQueryOperator"); } @Override - public ShowTablesOperator schema(String schema) { + public MongodbShowCollectionsOperator schema(String schema) { // nothing to do return self(); } @Override - public ShowTablesOperator from(Table table) { + public MongodbShowCollectionsOperator from(Table table) { this.tables.add(table); return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperator.java index b7c0527f..52ebd30d 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperator.java @@ -1,17 +1,17 @@ package cc.allio.uno.data.orm.dsl.mongodb.dml; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.Operator; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.PrepareValue; import cc.allio.uno.data.orm.dsl.Table; import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; import cc.allio.uno.data.orm.dsl.type.DBType; -import com.mongodb.client.model.Filters; import org.bson.BsonDocument; +import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; /** * mongodb delete document operator @@ -22,7 +22,7 @@ */ @AutoService(DeleteOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbDeleteOperator extends MongodbWhereOperatorImpl implements DeleteOperator { +public class MongodbDeleteOperator extends MongodbWhereOperatorImpl implements DeleteOperator { private Table fromColl; @@ -36,11 +36,16 @@ public String getDSL() { } @Override - public DeleteOperator parse(String dsl) { + public MongodbDeleteOperator parse(String dsl) { this.filter = BsonDocument.parse(dsl); return self(); } + @Override + public MongodbDeleteOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbDeleteOperator()); + } + @Override public void reset() { clear(); @@ -64,11 +69,11 @@ public String getPrepareDSL() { @Override public List getPrepareValues() { - throw Exceptions.unOperate("getPrepareValues"); + return Collections.emptyList(); } @Override - public DeleteOperator from(Table table) { + public MongodbDeleteOperator from(Table table) { this.fromColl = table; return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperator.java index eb1b02bf..c884d52e 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperator.java @@ -1,7 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.dml; import cc.allio.uno.auto.service.AutoService; -import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.core.util.CollectionUtils; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.dml.InsertOperator; @@ -9,12 +8,12 @@ import cc.allio.uno.data.orm.dsl.type.DBType; import com.google.common.collect.Lists; import lombok.Getter; -import org.bson.BsonArray; import org.bson.Document; import java.util.Collection; import java.util.List; import java.util.function.Supplier; +import java.util.function.UnaryOperator; /** * mongodb insert document operator @@ -25,7 +24,7 @@ */ @AutoService(InsertOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbInsertOperator implements InsertOperator { +public class MongodbInsertOperator implements InsertOperator { @Getter private List docs; @@ -44,12 +43,17 @@ public String getDSL() { } @Override - public InsertOperator parse(String dsl) { + public MongodbInsertOperator parse(String dsl) { List documentsFromDsl = MongodbSupport.parse(dsl); this.docs.addAll(documentsFromDsl); return self(); } + @Override + public MongodbInsertOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbInsertOperator()); + } + @Override public void reset() { this.docs = Lists.newArrayList(); @@ -74,11 +78,11 @@ public String getPrepareDSL() { @Override public List getPrepareValues() { - throw Exceptions.unOperate("getPrepareValues"); + return List.of(); } @Override - public InsertOperator from(Table table) { + public MongodbInsertOperator from(Table table) { this.fromColl = table; return self(); } @@ -89,7 +93,7 @@ public Table getTable() { } @Override - public InsertOperator strictFill(String f, Supplier v) { + public MongodbInsertOperator strictFill(String f, Supplier v) { for (Document doc : docs) { doc.put(f, v.get()); } @@ -97,13 +101,13 @@ public InsertOperator strictFill(String f, Supplier v) { } @Override - public InsertOperator columns(Collection columns) { + public MongodbInsertOperator columns(Collection columns) { this.columns = Lists.newArrayList(columns); return self(); } @Override - public InsertOperator values(List values) { + public MongodbInsertOperator values(List values) { if (CollectionUtils.isEmpty(columns)) { return self(); } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java index 6aa7053b..efc38497 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbQueryOperator.java @@ -1,6 +1,7 @@ package cc.allio.uno.data.orm.dsl.mongodb.dml; import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.core.StringPool; import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; @@ -17,6 +18,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.function.UnaryOperator; /** * mongodb query document operator implementation @@ -28,7 +30,7 @@ @Slf4j @AutoService(QueryOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbQueryOperator extends MongodbWhereOperatorImpl implements QueryOperator { +public class MongodbQueryOperator extends MongodbWhereOperatorImpl implements QueryOperator { private Table fromColl; private List orders; @@ -49,12 +51,18 @@ public MongodbQueryOperator() { @Override public String getDSL() { - return null; + return StringPool.EMPTY; } @Override - public QueryOperator parse(String dsl) { - throw Exceptions.unOperate("parse"); + public MongodbQueryOperator parse(String dsl) { + // nothing to do + return self(); + } + + @Override + public MongodbQueryOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbQueryOperator()); } @Override @@ -84,11 +92,11 @@ public String getPrepareDSL() { @Override public List getPrepareValues() { - throw Exceptions.unOperate("getPrepareValues"); + return List.of(); } @Override - public QueryOperator from(Table table) { + public MongodbQueryOperator from(Table table) { this.fromColl = table; return self(); } @@ -99,19 +107,19 @@ public Table getTable() { } @Override - public QueryOperator select(DSLName dslName) { + public MongodbQueryOperator select(DSLName dslName) { // nothing to do return self(); } @Override - public QueryOperator select(DSLName dslName, String alias) { + public MongodbQueryOperator select(DSLName dslName, String alias) { // nothing to do return self(); } @Override - public QueryOperator selects(Collection dslNames) { + public MongodbQueryOperator selects(Collection dslNames) { // nothing to do return self(); } @@ -122,22 +130,22 @@ public List obtainSelectColumns() { } @Override - public QueryOperator distinct() { + public MongodbQueryOperator distinct() { // nothing to do return self(); } @Override - public QueryOperator distinctOn(DSLName sqlName, String alias) { - this.distinct = new Distinct(sqlName); + public MongodbQueryOperator distinctOn(DSLName dslName, String alias) { + this.distinct = new Distinct(dslName); return self(); } @Override - public QueryOperator aggregate(Func syntax, DSLName sqlName, String alias, Distinct distinct) { + public MongodbQueryOperator aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct) { // TODO improve aggregate function switch (syntax) { - case AVG_FUNCTION -> Accumulators.avg(sqlName.format(), null); + case AVG_FUNCTION -> Accumulators.avg(dslName.format(), null); case COUNT_FUNCTION -> count = true; } @@ -145,18 +153,18 @@ public QueryOperator aggregate(Func syntax, DSLName sqlName, String alias, Disti } @Override - public QueryOperator from(QueryOperator fromTable, String alias) { + public MongodbQueryOperator from(QueryOperator fromTable, String alias) { throw Exceptions.unOperate("from(QueryOperator fromTable, String alias)"); } @Override - public QueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { + public MongodbQueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { throw Exceptions.unOperate("join"); } @Override - public QueryOperator orderBy(DSLName sqlName, OrderCondition orderCondition) { - Bson order = toBsonOrder(sqlName, orderCondition); + public MongodbQueryOperator orderBy(DSLName dslName, OrderCondition orderCondition) { + Bson order = toBsonOrder(dslName, orderCondition); this.orders.add(order); this.bsonOrder = Sorts.orderBy(orders); return self(); @@ -171,13 +179,13 @@ Bson toBsonOrder(DSLName sqlName, OrderCondition orderCondition) { } @Override - public QueryOperator limit(Long limit, Long offset) { + public MongodbQueryOperator limit(Long limit, Long offset) { this.bsonWindow = Windows.range(offset, offset + limit); return self(); } @Override - public QueryOperator groupByOnes(Collection fieldNames) { + public MongodbQueryOperator groupByOnes(Collection fieldNames) { if (log.isDebugEnabled()) { log.debug("mongodb query operate 'groupByOnes' nothing to do. "); } @@ -185,7 +193,7 @@ public QueryOperator groupByOnes(Collection fieldNames) { } @Override - public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + public MongodbQueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { throw Exceptions.unOperate("tree"); } } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperator.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperator.java index bb0d04ff..702abad3 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperator.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperator.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; +import java.util.function.UnaryOperator; /** * mongodb update document operator implementation @@ -25,7 +26,7 @@ */ @AutoService(UpdateOperator.class) @Operator.Group(OperatorKey.MONGODB_LITERAL) -public class MongodbUpdateOperator extends MongodbWhereOperatorImpl implements UpdateOperator { +public class MongodbUpdateOperator extends MongodbWhereOperatorImpl implements UpdateOperator { private Table fromColl; @Getter @@ -40,14 +41,18 @@ public MongodbUpdateOperator() { @Override public String getDSL() { - - return update.toBsonDocument().toJson(); } @Override - public UpdateOperator parse(String dsl) { - throw Exceptions.unOperate("parse"); + public MongodbUpdateOperator parse(String dsl) { + // nothing todo + return self(); + } + + @Override + public MongodbUpdateOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new MongodbUpdateOperator()); } @Override @@ -75,11 +80,11 @@ public String getPrepareDSL() { @Override public List getPrepareValues() { - throw Exceptions.unOperate("getPrepareValues"); + return List.of(); } @Override - public UpdateOperator from(Table table) { + public MongodbUpdateOperator from(Table table) { this.fromColl = table; return self(); } @@ -90,14 +95,14 @@ public Table getTable() { } @Override - public UpdateOperator updates(Map values) { + public MongodbUpdateOperator updates(Map values) { this.trigger.setAll(values); this.update = this.trigger.doIt(); return self(); } @Override - public UpdateOperator strictFill(String f, Supplier v) { + public MongodbUpdateOperator strictFill(String f, Supplier v) { this.trigger.set(f, v.get()); this.update = this.trigger.doIt(); return self(); diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutor.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutor.java index 328a4bf0..fdca1064 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutor.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutor.java @@ -2,7 +2,7 @@ import cc.allio.uno.core.util.StringUtils; import cc.allio.uno.core.util.template.ExpressionTemplate; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.executor.AbstractCommandExecutor; import cc.allio.uno.data.orm.executor.AggregateCommandExecutor; @@ -49,7 +49,7 @@ public MongodbCommandExecutor(ExecutorOptions options) { String databaseName = options.getDatabase(); this.mongoClient = MongoClients.create(url); this.database = mongoClient.getDatabase(databaseName); - this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.MONGODB); + this.operatorGroup = OperatorGroup.getOperatorGroup(OperatorKey.MONGODB, options); SPIInnerCommandScanner scanner = options.getScanner(); this.manager = scanner.scan(database, mongoClient); } @@ -77,7 +77,7 @@ public void destroy() { } @Override - protected InnerCommandExecutorManager getManager() { + protected InnerCommandExecutorManager getInnerCommandExecutorManager() { return manager; } } diff --git a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbShowCollectionCommandExecutor.java b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbShowCollectionCommandExecutor.java index 9c196649..e75fec51 100644 --- a/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbShowCollectionCommandExecutor.java +++ b/uno-data/uno-data-mongodb/src/main/java/cc/allio/uno/data/orm/executor/mongodb/internal/MongodbShowCollectionCommandExecutor.java @@ -17,12 +17,9 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import lombok.extern.slf4j.Slf4j; -import org.bson.Document; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import static cc.allio.uno.data.orm.executor.handler.TableListResultSetHandler.*; @@ -49,21 +46,17 @@ public List
doExec(MongodbShowCollectionsOperator operator, ListResultSet Database fromDb = operator.getFromDb(); Requires.isNotNull(fromDb, "fromDb"); List
tables = operator.getTables(); - List filterTableNames = - tables.stream() - .map(Table::getName) - .map(DSLName::format) - .toList(); + List filteringCollectionNames = tables.stream().map(Table::getName).map(DSLName::format).toList(); MongoDatabase database = mongoClient.getDatabase(fromDb.getName().format()); List resultGroups = Lists.newArrayList(database.listCollections()) .stream() .filter(document -> { - if (filterTableNames.isEmpty()) { + if (filteringCollectionNames.isEmpty()) { return true; } // filter exist name String collectionName = document.getString("name"); - return filterTableNames.contains(collectionName); + return filteringCollectionNames.contains(collectionName); }) .map(document -> { ResultGroup resultGroup = new ResultGroup(); diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongoAlterCollectionOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongoAlterCollectionOperatorTest.java index 1967d62a..d06ce610 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongoAlterCollectionOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongoAlterCollectionOperatorTest.java @@ -1,10 +1,8 @@ package cc.allio.uno.data.orm.dsl.mongodb.ddl; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.Table; -import cc.allio.uno.data.orm.dsl.type.DBType; -import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperatorTest.java index 446ec8e5..10625b90 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbCreateCollectionOperatorTest.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.ddl; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.Table; import cc.allio.uno.test.BaseTestCase; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperatorTest.java index f4126b5f..4537206d 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbDropCollectionOperatorTest.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.ddl; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.Table; import cc.allio.uno.test.BaseTestCase; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperatorTest.java index 1b3703e1..4ee7ec7c 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbExistCollectionOperatorTest.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.ddl; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.Table; import cc.allio.uno.test.BaseTestCase; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperatorTest.java index f74b8d16..8c1fec44 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/ddl/MongodbShowCollectionsOperatorTest.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.ddl; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperatorTest.java index 136954a3..062c61bc 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbDeleteOperatorTest.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.dml; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperatorTest.java index 3bdc1b3c..55859be6 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperatorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbInsertOperatorTest.java @@ -1,6 +1,6 @@ package cc.allio.uno.data.orm.dsl.mongodb.dml; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperatorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperatorTest.java deleted file mode 100644 index 1f63d598..00000000 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbUpdateOperatorTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package cc.allio.uno.data.orm.dsl.mongodb.dml; - -import cc.allio.uno.test.BaseTestCase; - - class MongodbUpdateOperatorTest extends BaseTestCase { - - -} diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbWhereOperatorImplTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbWhereOperatorImplTest.java deleted file mode 100644 index 09880436..00000000 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/dsl/mongodb/dml/MongodbWhereOperatorImplTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package cc.allio.uno.data.orm.dsl.mongodb.dml; - -import cc.allio.uno.test.BaseTestCase; -import com.mongodb.client.model.Filters; -import org.bson.BsonDocument; -import org.junit.jupiter.api.Test; - - -public class MongodbWhereOperatorImplTest extends BaseTestCase { - - @Test - void testEq() { - var filterAnd = Filters.and(Filters.eq("a", "1")); - var filterOr = Filters.or(Filters.eq("b", "2")); - - - new BsonDocument(); - - - - String json = filterAnd.toBsonDocument().toJson(); - - System.out.println(json); - } -} diff --git a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorTest.java b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorTest.java index 63f67a66..303f4115 100644 --- a/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorTest.java +++ b/uno-data/uno-data-mongodb/src/test/java/cc/allio/uno/data/orm/executor/mongodb/MongodbCommandExecutorTest.java @@ -38,7 +38,7 @@ void testShowCollection() { @Test void testInsertDocument() { - InsertOperator insertOperator = executor.getOperatorGroup().insert(); + InsertOperator insertOperator = executor.getOperatorGroup().insert(); insertOperator.insert("a", "a", "b", "b").from("dual"); boolean success = executor.insert(insertOperator); @@ -49,7 +49,7 @@ void testInsertDocument() { void testUpdateDocument() { prepareValues(); - UpdateOperator updateOperator = executor.getOperatorGroup().update(); + UpdateOperator updateOperator = executor.getOperatorGroup().update(); updateOperator.from("dual").update("a", "c").eq("a", "a"); boolean success = executor.update(updateOperator); assertTrue(success); @@ -57,7 +57,7 @@ void testUpdateDocument() { @Test void testRenameAndDropCollection() { - AlterTableOperator alterTableOperator = executor.getOperatorGroup().alterTables(); + AlterTableOperator alterTableOperator = executor.getOperatorGroup().alterTables(); alterTableOperator.from("dual").rename("dual2"); boolean success = executor.alertTable(alterTableOperator); @@ -86,7 +86,7 @@ void testQuery() { } void prepareValues() { - InsertOperator insertOperator = executor.getOperatorGroup().insert(); + InsertOperator insertOperator = executor.getOperatorGroup().insert(); insertOperator.insert("a", "a", "b", "b").from("dual"); executor.insert(insertOperator); } diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLAlterTableOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLAlterTableOperator.java index 29d96d18..de0b11d4 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLAlterTableOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLAlterTableOperator.java @@ -15,6 +15,7 @@ import cc.allio.uno.data.orm.dsl.ddl.AlterTableOperator; import java.util.Collection; +import java.util.function.UnaryOperator; /** * druid for modify xxxx structure @@ -25,7 +26,7 @@ */ @AutoService(AlterTableOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLAlterTableOperator implements AlterTableOperator { +public class SQLAlterTableOperator implements AlterTableOperator { private SQLAlterTableStatement alterTableStatement; private DBType dbType; @@ -59,11 +60,16 @@ public String getDSL() { } @Override - public AlterTableOperator parse(String dsl) { + public SQLAlterTableOperator parse(String dsl) { this.alterTableStatement = (SQLAlterTableStatement) SQLUtils.parseSingleStatement(dsl, druidDbType); return self(); } + @Override + public SQLAlterTableOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLAlterTableOperator(dbType)); + } + @Override public void reset() { this.alterTableStatement = new SQLAlterTableStatement(druidDbType); @@ -83,7 +89,7 @@ public DBType getDBType() { } @Override - public AlterTableOperator alertColumns(Collection columnDefs) { + public SQLAlterTableOperator alertColumns(Collection columnDefs) { for (ColumnDef columnDef : columnDefs) { SQLColumnDefinition columnDefinition = new SQLColumnDefinition(); columnDefinition.setComment(columnDef.getComment()); @@ -114,7 +120,7 @@ public AlterTableOperator alertColumns(Collection columnDefs) { } @Override - public AlterTableOperator addColumns(Collection columnDefs) { + public SQLAlterTableOperator addColumns(Collection columnDefs) { for (ColumnDef columnDef : columnDefs) { SQLAlterTableAddColumn alterTableAddColumn = new SQLAlterTableAddColumn(); alterTableAddColumn.toString(); @@ -126,7 +132,7 @@ public AlterTableOperator addColumns(Collection columnDefs) { } @Override - public AlterTableOperator deleteColumns(Collection columns) { + public SQLAlterTableOperator deleteColumns(Collection columns) { SQLAlterTableDropColumnItem dropColumnItem = new SQLAlterTableDropColumnItem(); for (DSLName column : columns) { dropColumnItem.addColumn(new SQLIdentifierExpr(column.format())); @@ -136,7 +142,7 @@ public AlterTableOperator deleteColumns(Collection columns) { } @Override - public AlterTableOperator rename(Table to) { + public SQLAlterTableOperator rename(Table to) { SQLAlterTableRename alterTableRename = new SQLAlterTableRename(); alterTableRename.setTo(new SQLIdentifierExpr(to.getName().format())); this.alterTableStatement.addItem(alterTableRename); @@ -144,7 +150,7 @@ public AlterTableOperator rename(Table to) { } @Override - public AlterTableOperator from(Table table) { + public SQLAlterTableOperator from(Table table) { this.from = table; SQLExprTableSource tableSource = DDLSQLSupport.createTableSource(table, dbType); this.alterTableStatement.setTableSource(tableSource); diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLCreateTableOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLCreateTableOperator.java index d57e0cd5..372a3c8f 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLCreateTableOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLCreateTableOperator.java @@ -13,6 +13,8 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateTableStatement; import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; +import java.util.function.UnaryOperator; + /** * 基于Druid registry operator * @@ -22,7 +24,7 @@ */ @AutoService(CreateTableOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLCreateTableOperator implements CreateTableOperator { +public class SQLCreateTableOperator implements CreateTableOperator { private DBType dbType; private DbType druidType; @@ -50,11 +52,16 @@ public String getDSL() { } @Override - public CreateTableOperator parse(String dsl) { + public SQLCreateTableOperator parse(String dsl) { this.createTableStatement = (SQLCreateTableStatement) SQLUtils.parseSingleStatement(dsl, druidType); return self(); } + @Override + public SQLCreateTableOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLCreateTableOperator(dbType)); + } + @Override public void reset() { // eachReset @@ -74,12 +81,12 @@ public DBType getDBType() { } @Override - public CreateTableOperator from(String table) { + public SQLCreateTableOperator from(String table) { return from(Table.of(table)); } @Override - public CreateTableOperator from(Table table) { + public SQLCreateTableOperator from(Table table) { this.table = table; SQLExprTableSource tableSource = DDLSQLSupport.createTableSource(table, dbType); createTableStatement.setTableSource(tableSource); @@ -92,14 +99,14 @@ public Table getTable() { } @Override - public CreateTableOperator column(ColumnDef columnDef) { + public SQLCreateTableOperator column(ColumnDef columnDef) { SQLColumnDefinition columnDefinition = DDLSQLSupport.createColumnDefinition(columnDef, dbType); createTableStatement.addColumn(columnDefinition); return self(); } @Override - public CreateTableOperator comment(String comment) { + public SQLCreateTableOperator comment(String comment) { createTableStatement.setComment(new SQLIdentifierExpr(comment)); return self(); } diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLDropTableOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLDropTableOperator.java index 9e305b17..0bfc4ba9 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLDropTableOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLDropTableOperator.java @@ -11,6 +11,8 @@ import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; import cc.allio.uno.data.orm.dsl.ddl.DropTableOperator; +import java.util.function.UnaryOperator; + /** * DruidSQLDropTableOperator * @@ -20,7 +22,7 @@ */ @AutoService(DropTableOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLDropTableOperator implements DropTableOperator { +public class SQLDropTableOperator implements DropTableOperator { private DBType dbType; private DbType druidDbType; @@ -44,10 +46,15 @@ public String getDSL() { } @Override - public DropTableOperator parse(String dsl) { + public SQLDropTableOperator parse(String dsl) { return null; } + @Override + public SQLDropTableOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLDropTableOperator(dbType)); + } + @Override public void reset() { // eachReset @@ -68,7 +75,7 @@ public DBType getDBType() { } @Override - public DropTableOperator from(Table table) { + public SQLDropTableOperator from(Table table) { SQLExprTableSource tableSource = new UnoSQLExprTableSource(druidDbType); tableSource.setExpr(table.getName().getName()); tableSource.setSchema(table.getSchema()); @@ -84,7 +91,7 @@ public Table getTable() { } @Override - public DropTableOperator ifExist(Boolean ifExist) { + public SQLDropTableOperator ifExist(Boolean ifExist) { dropTableStatement.setIfExists(ifExist); return self(); } diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLExistTableOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLExistTableOperator.java index da3ba198..60323d9c 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLExistTableOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLExistTableOperator.java @@ -2,14 +2,16 @@ import cc.allio.uno.auto.service.AutoService; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.sql.SQLSupport; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLQueryOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import com.alibaba.druid.DbType; import cc.allio.uno.data.orm.dsl.ddl.ExistTableOperator; import java.util.List; +import java.util.function.UnaryOperator; /** * druid @@ -20,12 +22,12 @@ */ @AutoService(ExistTableOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLExistTableOperator extends PrepareOperatorImpl implements ExistTableOperator { +public class SQLExistTableOperator extends PrepareOperatorImpl implements ExistTableOperator { private DBType dbType; private DbType druidDbType; private Table table; - private final QueryOperator queryOperator; + private final SQLQueryOperator queryOperator; public SQLExistTableOperator() { this(DBType.getSystemDbType()); @@ -34,7 +36,7 @@ public SQLExistTableOperator() { public SQLExistTableOperator(DBType dbType) { this.dbType = dbType; this.druidDbType = SQLSupport.translateDb(dbType); - this.queryOperator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL, dbType); + this.queryOperator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL, dbType); } @Override @@ -43,17 +45,22 @@ public String getDSL() { } @Override - public ExistTableOperator parse(String dsl) { + public SQLExistTableOperator parse(String dsl) { throw SQLSupport.on(this).onNonsupport("parse").execute(); } + @Override + public SQLExistTableOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLExistTableOperator(dbType)); + } + @Override public String getPrepareDSL() { return queryOperator.getPrepareDSL(); } @Override - public ExistTableOperator from(Table table) { + public SQLExistTableOperator from(Table table) { Object obj = SQLSupport.on(this) .onDb(druidDbType) .then(() -> diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java index 1c6554ac..fedaebd1 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java @@ -4,12 +4,15 @@ import cc.allio.uno.data.orm.dsl.*; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.sql.SQLSupport; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLQueryOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import com.alibaba.druid.DbType; import cc.allio.uno.data.orm.dsl.ddl.ShowColumnsOperator; import java.util.List; +import java.util.function.UnaryOperator; /** * 表结构 @@ -20,13 +23,13 @@ */ @AutoService(ShowColumnsOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLShowColumnsOperator extends PrepareOperatorImpl implements ShowColumnsOperator { +public class SQLShowColumnsOperator extends PrepareOperatorImpl implements ShowColumnsOperator { private DBType dbType; private DbType druidDbType; private Table table; private Database database; - private final QueryOperator queryOperator; + private final SQLQueryOperator queryOperator; public SQLShowColumnsOperator() { this(DBType.getSystemDbType()); @@ -35,7 +38,7 @@ public SQLShowColumnsOperator() { public SQLShowColumnsOperator(DBType dbType) { this.dbType = dbType; this.druidDbType = SQLSupport.translateDb(dbType); - this.queryOperator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL, dbType); + this.queryOperator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL, dbType); } @Override @@ -47,10 +50,15 @@ public String getDSL() { } @Override - public ShowColumnsOperator parse(String dsl) { + public SQLShowColumnsOperator parse(String dsl) { throw SQLSupport.on(this).onNonsupport("parse").execute(); } + @Override + public SQLShowColumnsOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLShowColumnsOperator(dbType)); + } + @Override public String getPrepareDSL() { throw SQLSupport.on(this).onNonsupport("getPrepareDSL").execute(); @@ -80,7 +88,7 @@ public DBType getDBType() { } @Override - public ShowColumnsOperator from(Table table) { + public SQLShowColumnsOperator from(Table table) { Object result = SQLSupport.on(this) .onDb(druidDbType) .then(queryOperator::reset) @@ -136,7 +144,7 @@ public Table getTable() { } @Override - public QueryOperator toQueryOperator() { + public QueryOperator toQueryOperator() { return SQLSupport.on(this) .onDb(druidDbType) .then(() -> queryOperator) @@ -144,7 +152,7 @@ public QueryOperator toQueryOperator() { } @Override - public ShowColumnsOperator database(Database database) { + public SQLShowColumnsOperator database(Database database) { this.database = database; return self(); } diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java index 4bb866c8..bfda06d3 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java @@ -6,23 +6,26 @@ import cc.allio.uno.data.orm.dsl.ddl.ShowTablesOperator; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.sql.SQLSupport; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLQueryOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import com.alibaba.druid.DbType; import com.google.common.collect.Lists; import java.util.List; +import java.util.function.UnaryOperator; @AutoService(ShowTablesOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLShowTablesOperator extends PrepareOperatorImpl implements ShowTablesOperator { +public class SQLShowTablesOperator extends PrepareOperatorImpl implements ShowTablesOperator { private DBType dbType; private DbType druidDbType; private String schema; private Database database; private List
tables; - private QueryOperator queryOperator; + private SQLQueryOperator queryOperator; public SQLShowTablesOperator() { this(DBType.getSystemDbType()); @@ -31,7 +34,7 @@ public SQLShowTablesOperator() { public SQLShowTablesOperator(DBType dbType) { this.dbType = dbType; this.druidDbType = SQLSupport.translateDb(dbType); - this.queryOperator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL, dbType); + this.queryOperator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL, dbType); this.schema = "PUBLIC"; this.tables = Lists.newArrayList(); } @@ -43,11 +46,16 @@ public String getDSL() { } @Override - public ShowTablesOperator parse(String dsl) { + public SQLShowTablesOperator parse(String dsl) { this.queryOperator = queryOperator.parse(dsl); return self(); } + @Override + public SQLShowTablesOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLShowTablesOperator(dbType)); + } + @Override public void setDBType(DBType dbType) { this.dbType = dbType; @@ -66,19 +74,19 @@ public String getPrepareDSL() { } @Override - public QueryOperator toQueryOperator() { + public QueryOperator toQueryOperator() { trigger(); return queryOperator; } @Override - public ShowTablesOperator database(Database database) { + public SQLShowTablesOperator database(Database database) { this.database = database; return self(); } @Override - public ShowTablesOperator schema(String schema) { + public SQLShowTablesOperator schema(String schema) { this.schema = schema; return self(); } @@ -128,7 +136,7 @@ private void trigger() { } @Override - public ShowTablesOperator from(Table table) { + public SQLShowTablesOperator from(Table table) { this.tables.add(table); return self(); } diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLDeleteOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLDeleteOperator.java index 836e0a6b..6f56fbcb 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLDeleteOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLDeleteOperator.java @@ -17,6 +17,7 @@ import reactor.util.function.Tuple2; import java.util.function.Consumer; +import java.util.function.UnaryOperator; /** * DruidSQLDeleteQueryOperator @@ -27,7 +28,7 @@ */ @AutoService(DeleteOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLDeleteOperator extends SQLWhereOperatorImpl implements DeleteOperator { +public class SQLDeleteOperator extends SQLWhereOperatorImpl implements DeleteOperator { private DBType dbType; private DbType druidDbType; @@ -56,7 +57,7 @@ public String getDSL() { } @Override - public DeleteOperator parse(String dsl) { + public SQLDeleteOperator parse(String dsl) { this.deleteStatement = (SQLDeleteStatement) SQLUtils.parseSingleStatement(dsl, druidDbType); SQLExpr where = this.deleteStatement.getWhere(); if (SQLSupport.isBinaryExpr(where)) { @@ -74,6 +75,11 @@ public DeleteOperator parse(String dsl) { return self(); } + @Override + public SQLDeleteOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLDeleteOperator(dbType)); + } + @Override public void reset() { super.reset(); @@ -99,7 +105,7 @@ public String getPrepareDSL() { } @Override - public DeleteOperator from(Table table) { + public SQLDeleteOperator from(Table table) { SQLExprTableSource tableSource = new UnoSQLExprTableSource(druidDbType); tableSource.setExpr(new SQLIdentifierExpr(table.getName().format())); tableSource.setCatalog(table.getCatalog()); diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLInsertOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLInsertOperator.java index 180b83af..83f5a4f5 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLInsertOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLInsertOperator.java @@ -20,7 +20,7 @@ import java.util.*; import java.util.function.Supplier; -import java.util.stream.Collectors; +import java.util.function.UnaryOperator; /** * Druid INSERT @@ -31,7 +31,7 @@ */ @AutoService(InsertOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLInsertOperator extends PrepareOperatorImpl implements InsertOperator { +public class SQLInsertOperator extends PrepareOperatorImpl implements InsertOperator { private DBType dbType; private DbType druidDbType; @@ -58,11 +58,11 @@ public String getDSL() { return ParameterizedOutputVisitorUtils.restore( getPrepareDSL(), druidDbType, - getPrepareValues().stream().map(PrepareValue::getValue).collect(Collectors.toList())); + getPrepareValues().stream().map(PrepareValue::getValue).toList()); } @Override - public InsertOperator parse(String dsl) { + public SQLInsertOperator parse(String dsl) { this.insertStatement = (SQLInsertStatement) SQLUtils.parseSingleStatement(dsl, druidDbType); List columns = insertStatement.getColumns(); List valuesList = insertStatement.getValuesList(); @@ -86,6 +86,11 @@ public InsertOperator parse(String dsl) { return self(); } + @Override + public SQLInsertOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLInsertOperator(dbType)); + } + @Override public void reset() { super.reset(); @@ -107,7 +112,7 @@ public DBType getDBType() { } @Override - public InsertOperator from(Table table) { + public SQLInsertOperator from(Table table) { SQLExprTableSource tableSource = new UnoSQLExprTableSource(druidDbType); tableSource.setExpr(new SQLIdentifierExpr(table.getName().format())); tableSource.setCatalog(table.getCatalog()); @@ -124,12 +129,12 @@ public Table getTable() { @Override - public InsertOperator strictFill(String f, Supplier v) { + public SQLInsertOperator strictFill(String f, Supplier v) { return strictFill(f, v, true); } @Override - public InsertOperator columns(Collection columns) { + public SQLInsertOperator columns(Collection columns) { List valuesList = insertStatement.getValuesList(); if (valuesList.isEmpty()) { for (DSLName column : columns) { @@ -144,7 +149,7 @@ public InsertOperator columns(Collection columns) { } @Override - public InsertOperator values(List values) { + public SQLInsertOperator values(List values) { SQLInsertStatement.ValuesClause valuesClause = new SQLInsertStatement.ValuesClause(); for (int i = 0; i < columns.size(); i++) { String column = columns.get(i); @@ -178,7 +183,7 @@ public boolean isBatched() { * @param existAssign existAssign * @return SQLInsertOperator */ - private InsertOperator strictFill(String f, Supplier supplier, boolean existAssign) { + private SQLInsertOperator strictFill(String f, Supplier supplier, boolean existAssign) { // 1.在batch里面 // 2.如果相同column值覆盖 int index = columns.indexOf(f); diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java index 2b8d4077..72f5eec9 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLQueryOperator.java @@ -21,6 +21,7 @@ import cc.allio.uno.core.StringPool; import cc.allio.uno.core.util.CollectionUtils; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; import com.google.common.collect.Lists; import org.apache.commons.lang3.reflect.FieldUtils; @@ -28,6 +29,7 @@ import java.util.Collection; import java.util.List; import java.util.function.Consumer; +import java.util.function.UnaryOperator; /** * Druid Query Operator @@ -38,7 +40,7 @@ */ @AutoService(QueryOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLQueryOperator extends SQLWhereOperatorImpl implements QueryOperator { +public class SQLQueryOperator extends SQLWhereOperatorImpl implements QueryOperator { private DBType dbType; private DbType druidDbType; @@ -93,8 +95,23 @@ public String getDSL() { getPrepareValues().stream().map(PrepareValue::getValue).toList()); } + /** + * from DSL remain where DSL + * + * @return the where dsl + */ + public String getWhereDSL() { + List parameters = getPrepareValues().stream().map(PrepareValue::getValue).toList(); + StringBuilder out = new StringBuilder(); + SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, druidDbType); + visitor.setInputParameters(parameters); + SQLExpr where = selectQuery.getWhere(); + where.accept(visitor); + return out.toString(); + } + @Override - public QueryOperator parse(String dsl) { + public SQLQueryOperator parse(String dsl) { SQLStatement sqlStatement = SQLUtils.parseSingleStatement(dsl, druidDbType); Field selectField = FieldUtils.getField(sqlStatement.getClass(), "select", true); if (selectField == null) { @@ -124,6 +141,11 @@ public QueryOperator parse(String dsl) { return self(); } + @Override + public SQLQueryOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLQueryOperator(dbType)); + } + @Override public void reset() { super.reset(); @@ -150,19 +172,19 @@ public DBType getDBType() { } @Override - public QueryOperator select(DSLName dslName) { + public SQLQueryOperator select(DSLName dslName) { addColumns(dslName.format(), null, true); return self(); } @Override - public QueryOperator select(DSLName dslName, String alias) { + public SQLQueryOperator select(DSLName dslName, String alias) { addColumns(dslName.format(), alias, true); return self(); } @Override - public QueryOperator selects(Collection dslNames) { + public SQLQueryOperator selects(Collection dslNames) { for (DSLName sqlName : dslNames) { addColumns(sqlName.format(), null, true); } @@ -175,34 +197,34 @@ public List obtainSelectColumns() { } @Override - public QueryOperator distinct() { + public SQLQueryOperator distinct() { SQLSelectItem sqlSelectItem = new SQLSelectItem(new SQLAggregateExpr("", SQLAggregateOption.DISTINCT)); selectQuery.addSelectItem(sqlSelectItem); return self(); } @Override - public QueryOperator distinctOn(DSLName sqlName, String alias) { - SQLSelectItem sqlSelectItem = new SQLSelectItem(new SQLAggregateExpr(sqlName.format(), SQLAggregateOption.DISTINCT)); + public SQLQueryOperator distinctOn(DSLName dslName, String alias) { + SQLSelectItem sqlSelectItem = new SQLSelectItem(new SQLAggregateExpr(dslName.format(), SQLAggregateOption.DISTINCT)); selectQuery.addSelectItem(sqlSelectItem); return self(); } @Override - public QueryOperator aggregate(Func syntax, DSLName sqlName, String alias, Distinct distinct) { + public SQLQueryOperator aggregate(Func syntax, DSLName dslName, String alias, Distinct distinct) { SQLSelectItem sqlSelectItem = new SQLSelectItem(); SQLAggregateExpr sqlAggregateExpr = new SQLAggregateExpr(syntax.getName()); if (distinct != null) { sqlAggregateExpr.setOption(SQLAggregateOption.DISTINCT); } - sqlAggregateExpr.addArgument(new SQLIdentifierExpr(sqlName.format())); + sqlAggregateExpr.addArgument(new SQLIdentifierExpr(dslName.format())); sqlSelectItem.setExpr(sqlAggregateExpr); selectQuery.addSelectItem(sqlSelectItem); return self(); } @Override - public QueryOperator from(Table table) { + public SQLQueryOperator from(Table table) { SQLExprTableSource tableSource = new UnoSQLExprTableSource(druidDbType); tableSource.setExpr(new SQLIdentifierExpr(table.getName().format())); tableSource.setCatalog(table.getCatalog()); @@ -218,7 +240,7 @@ public Table getTable() { } @Override - public QueryOperator from(QueryOperator fromTable, String alias) { + public SQLQueryOperator from(QueryOperator fromTable, String alias) { String fromSQL = fromTable.getDSL(); SQLStatement sqlStatement = SQLUtils.parseSingleStatement(fromSQL, druidDbType, true); SQLSubqueryTableSource from = new SQLSubqueryTableSource((SQLSelect) sqlStatement); @@ -228,7 +250,7 @@ public QueryOperator from(QueryOperator fromTable, String alias) { } @Override - public QueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { + public SQLQueryOperator join(Table left, JoinType joinType, Table right, BinaryCondition condition) { // 构建连接关系 SQLExprTableSource rightSource = new UnoSQLExprTableSource(druidDbType); rightSource.setExpr(new SQLIdentifierExpr(right.getName().format())); @@ -258,26 +280,26 @@ public QueryOperator join(Table left, JoinType joinType, Table right, BinaryCond } @Override - public QueryOperator orderBy(DSLName sqlName, OrderCondition orderCondition) { + public SQLQueryOperator orderBy(DSLName dslName, OrderCondition orderCondition) { SQLOrderingSpecification druidOrder; if (orderCondition == OrderCondition.ASC) { druidOrder = SQLOrderingSpecification.ASC; } else { druidOrder = SQLOrderingSpecification.DESC; } - getOrderBy().addItem(new SQLIdentifierExpr(sqlName.format()), druidOrder); + getOrderBy().addItem(new SQLIdentifierExpr(dslName.format()), druidOrder); return self(); } @Override - public QueryOperator limit(Long limit, Long offset) { + public SQLQueryOperator limit(Long limit, Long offset) { getLimit().setRowCount(Math.toIntExact(limit)); getLimit().setOffset(Math.toIntExact(offset)); return self(); } @Override - public QueryOperator groupByOnes(Collection fieldNames) { + public SQLQueryOperator groupByOnes(Collection fieldNames) { fieldNames.stream() .map(sqlName -> new SQLIdentifierExpr(sqlName.format())) .forEach(getGroupBy()::addItem); @@ -285,7 +307,7 @@ public QueryOperator groupByOnes(Collection fieldNames) { } @Override - public QueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { + public SQLQueryOperator tree(QueryOperator baseQuery, QueryOperator subQuery) { String baseQueryDsl = baseQuery.getDSL(); String subQueryDsl = subQuery.getDSL(); String treeQuery = diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLUpdateOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLUpdateOperator.java index 65f8a11d..d82854b2 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLUpdateOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLUpdateOperator.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.function.Consumer; import java.util.function.Supplier; +import java.util.function.UnaryOperator; /** * DruidSQLUpdateOperator @@ -35,7 +36,7 @@ */ @AutoService(UpdateOperator.class) @Operator.Group(OperatorKey.SQL_LITERAL) -public class SQLUpdateOperator extends SQLWhereOperatorImpl implements UpdateOperator { +public class SQLUpdateOperator extends SQLWhereOperatorImpl implements UpdateOperator { private DBType dbType; private DbType druidDbType; @@ -73,7 +74,7 @@ public String getDSL() { } @Override - public UpdateOperator parse(String dsl) { + public SQLUpdateOperator parse(String dsl) { this.updateStatement = (SQLUpdateStatement) SQLUtils.parseSingleStatement(dsl, druidDbType); // 重构建update使之成为prepare dsl List items = this.updateStatement.getItems(); @@ -108,6 +109,11 @@ public UpdateOperator parse(String dsl) { return self(); } + @Override + public SQLUpdateOperator customize(UnaryOperator operatorFunc) { + return operatorFunc.apply(new SQLUpdateOperator(dbType)); + } + @Override public void reset() { super.reset(); @@ -130,7 +136,7 @@ public DBType getDBType() { } @Override - public UpdateOperator from(Table table) { + public SQLUpdateOperator from(Table table) { SQLExprTableSource tableSource = new UnoSQLExprTableSource(druidDbType); tableSource.setExpr(new SQLIdentifierExpr(table.getName().format())); tableSource.setCatalog(table.getCatalog()); @@ -162,7 +168,7 @@ protected Consumer getSetWhere() { } @Override - public UpdateOperator updates(Map values) { + public SQLUpdateOperator updates(Map values) { if (CollectionUtils.isEmpty(values)) { throw new DSLException("update needs not empty values"); } @@ -179,7 +185,7 @@ public UpdateOperator updates(Map values) { } @Override - public UpdateOperator strictFill(String f, Supplier v) { + public SQLUpdateOperator strictFill(String f, Supplier v) { // set语句 List columns = updateStatement.getItems() diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLWhereOperatorImpl.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLWhereOperatorImpl.java index a6b2c4b2..4365f737 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLWhereOperatorImpl.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dml/SQLWhereOperatorImpl.java @@ -22,8 +22,7 @@ * @since 1.1.7 */ public abstract class SQLWhereOperatorImpl & PrepareOperator> - extends PrepareOperatorImpl - implements WhereOperator { + extends PrepareOperatorImpl implements WhereOperator { private SQLExpr where = null; private LogicMode mode = LogicMode.AND; @@ -34,76 +33,99 @@ protected SQLWhereOperatorImpl() { @Override public T gt(DSLName sqlName, Object value) { + // set placeholder value + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.GreaterThan, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), getDruidType()); + placeholder, + getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T gte(DSLName sqlName, Object value) { + // set placeholder value + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), SQLBinaryOperator.GreaterThanOrEqual, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + identifierExpr, + SQLBinaryOperator.GreaterThanOrEqual, + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T lt(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.LessThan, new SQLVariantRefExpr(StringPool.QUESTION_MARK), getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T lte(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), SQLBinaryOperator.LessThanOrEqual, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + identifierExpr, + SQLBinaryOperator.LessThanOrEqual, + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T eq(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.Equality, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T neq(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.NotEqual, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @@ -111,7 +133,8 @@ public T neq(DSLName sqlName, Object value) { public T notNull(DSLName sqlName) { SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), SQLBinaryOperator.IsNot, + new SQLIdentifierExpr(sqlName.format()), + SQLBinaryOperator.IsNot, new SQLNullExpr(), getDruidType()); appendAndSetWhere(expr); @@ -135,7 +158,9 @@ public T in(DSLName sqlName, V... values) { SQLInListExpr sqlInListExpr = new SQLInListExpr(new SQLIdentifierExpr(sqlName.format())); Collection vs = Values.collectionExpand(values); for (V v : vs) { - sqlInListExpr.addTarget(new SQLVariantRefExpr(StringPool.QUESTION_MARK)); + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + sqlInListExpr.addTarget(placeholder); addPrepareValue(sqlName.getName(), v); } appendAndSetWhere(sqlInListExpr); @@ -148,7 +173,9 @@ public T notIn(DSLName sqlName, V... values) { sqlInListExpr.setNot(true); Collection vs = Values.collectionExpand(values); for (V v : vs) { - sqlInListExpr.addTarget(new SQLVariantRefExpr(StringPool.QUESTION_MARK)); + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + sqlInListExpr.addTarget(placeholder); addPrepareValue(sqlName.getName(), v); } appendAndSetWhere(sqlInListExpr); @@ -157,132 +184,158 @@ public T notIn(DSLName sqlName, V... values) { @Override public T between(DSLName sqlName, Object withValue, Object endValue) { - SQLBetweenExpr sqlBetweenExpr = - new SQLBetweenExpr( - new SQLIdentifierExpr(sqlName.format()), - new SQLVariantRefExpr(StringPool.QUESTION_MARK), - new SQLVariantRefExpr(StringPool.QUESTION_MARK)); + SQLVariantRefExpr withPlaceholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + withPlaceholder.setIndex(prepareIndex); addPrepareValue(sqlName.getName(), withValue); + SQLVariantRefExpr endPlaceholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + endPlaceholder.setIndex(prepareIndex); addPrepareValue(sqlName.getName(), endValue); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); + SQLBetweenExpr sqlBetweenExpr = new SQLBetweenExpr(identifierExpr, withPlaceholder, endPlaceholder); appendAndSetWhere(sqlBetweenExpr); return self(); } @Override public T notBetween(DSLName sqlName, Object withValue, Object endValue) { - SQLBetweenExpr sqlBetweenExpr = - new SQLBetweenExpr( - new SQLIdentifierExpr(sqlName.format()), - new SQLVariantRefExpr(StringPool.QUESTION_MARK), - new SQLVariantRefExpr(StringPool.QUESTION_MARK)); - sqlBetweenExpr.setNot(true); + SQLVariantRefExpr withPlaceholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + withPlaceholder.setIndex(prepareIndex); addPrepareValue(sqlName.getName(), withValue); + SQLVariantRefExpr endPlaceholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + endPlaceholder.setIndex(prepareIndex); addPrepareValue(sqlName.getName(), endValue); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); + SQLBetweenExpr sqlBetweenExpr = new SQLBetweenExpr(identifierExpr, withPlaceholder, endPlaceholder); + sqlBetweenExpr.setNot(true); appendAndSetWhere(sqlBetweenExpr); return self(); } @Override public T like(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.Like, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T $like(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value)); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.Like, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value)); return self(); } @Override public T like$(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), Types.toString(value) + SQLBinaryOperator.Modulus.name); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.Like, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), Types.toString(value) + SQLBinaryOperator.Modulus.name); return self(); } @Override public T $like$(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value) + SQLBinaryOperator.Modulus.name); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.Like, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value) + SQLBinaryOperator.Modulus.name); return self(); } @Override public T notLike(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), value); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.NotLike, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), value); return self(); } @Override public T $notLike(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value)); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.NotLike, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value)); return self(); } @Override public T notLike$(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), Types.toString(value) + SQLBinaryOperator.Modulus.name); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.NotLike, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), Types.toString(value) + SQLBinaryOperator.Modulus.name); return self(); } @Override public T $notLike$(DSLName sqlName, Object value) { + SQLVariantRefExpr placeholder = new SQLVariantRefExpr(StringPool.QUESTION_MARK); + placeholder.setIndex(prepareIndex); + addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value) + SQLBinaryOperator.Modulus.name); + SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(sqlName.format()); SQLBinaryOpExpr expr = new SQLBinaryOpExpr( - new SQLIdentifierExpr(sqlName.format()), + identifierExpr, SQLBinaryOperator.NotLike, - new SQLVariantRefExpr(StringPool.QUESTION_MARK), + placeholder, getDruidType()); appendAndSetWhere(expr); - addPrepareValue(sqlName.getName(), SQLBinaryOperator.Modulus.name + Types.toString(value) + SQLBinaryOperator.Modulus.name); return self(); } diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLAlterTableOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLAlterTableOperatorTest.java index 8dbe280c..9b755c5a 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLAlterTableOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLAlterTableOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.ddl.sql; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; -import cc.allio.uno.data.orm.dsl.ddl.AlterTableOperator; +import cc.allio.uno.data.orm.dsl.sql.ddl.SQLAlterTableOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.data.test.model.Operators; import cc.allio.uno.test.BaseTestCase; @@ -13,11 +13,11 @@ public class SQLAlterTableOperatorTest extends BaseTestCase { - Operators.OperatorFeature feature; + Operators.OperatorFeature feature; @BeforeEach void init() { - AlterTableOperator alterTableOperator = OperatorGroup.getOperator(AlterTableOperator.class, OperatorKey.SQL); + SQLAlterTableOperator alterTableOperator = OperatorGroup.getOperator(SQLAlterTableOperator.class, OperatorKey.SQL); feature = Operators.feature(alterTableOperator); } diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLCreateOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLCreateOperatorTest.java index ef693f7f..b4e756af 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLCreateOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLCreateOperatorTest.java @@ -2,7 +2,7 @@ import cc.allio.uno.data.orm.dsl.ColumnDef; import cc.allio.uno.data.orm.dsl.DSLName; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.ddl.CreateTableOperator; import cc.allio.uno.data.orm.dsl.type.DBType; @@ -16,7 +16,7 @@ public class SQLCreateOperatorTest extends BaseTestCase { @Test void testCreateTable() { - CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL); + CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL); String sql = createTableOperator.from("dual") .column( ColumnDef.builder() @@ -34,7 +34,7 @@ void testCreateTable() { @Test void testParseCreateTableSQL() { - CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL); + CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL); String sql = "CREATE TABLE dual (\n" + "\tname char(9) PRIMARY KEY NOT NULL UNIQUE\n" + ")"; @@ -46,7 +46,7 @@ void testParseCreateTableSQL() { @Test void testPgCreateComplexTypeTable() { - CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); + CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); createTableOperator.from("dual"); ColumnDef d1 = ColumnDef.builder() .dslName(DSLName.of("d1")) @@ -78,7 +78,7 @@ void testPgCreateComplexTypeTable() { @Test void testPgParsePojoCreateTableSQL() { - CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); + CreateTableOperator createTableOperator = OperatorGroup.getOperator(CreateTableOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); String sql = createTableOperator.fromPojo(User.class).getDSL(); assertEquals("CREATE TABLE t_users (\n" + "\tid int8 PRIMARY KEY,\n" + diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowColumnsOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowColumnsOperatorTest.java index 13a6cb41..82cb1228 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowColumnsOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowColumnsOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.ddl.sql; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; -import cc.allio.uno.data.orm.dsl.ddl.ShowColumnsOperator; +import cc.allio.uno.data.orm.dsl.sql.ddl.SQLShowColumnsOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; @@ -11,7 +11,7 @@ public class SQLShowColumnsOperatorTest extends BaseTestCase { @Test void testMySQLShoWColumns() { - ShowColumnsOperator showColumnsOperator = OperatorGroup.getOperator(ShowColumnsOperator.class, OperatorKey.SQL, DBType.MYSQL); + SQLShowColumnsOperator showColumnsOperator = OperatorGroup.getOperator(SQLShowColumnsOperator.class, OperatorKey.SQL, DBType.MYSQL); String dsl = showColumnsOperator.from("dual").getDSL(); assertEquals("SELECT *, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME\n" + "\t, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH\n" + diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowTablesOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowTablesOperatorTest.java index b572cde6..33b898d9 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowTablesOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/ddl/sql/SQLShowTablesOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.ddl.sql; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; -import cc.allio.uno.data.orm.dsl.ddl.ShowTablesOperator; +import cc.allio.uno.data.orm.dsl.sql.ddl.SQLShowTablesOperator; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; @@ -11,7 +11,7 @@ public class SQLShowTablesOperatorTest extends BaseTestCase { @Test void testSQLLByH2() { - ShowTablesOperator operator = OperatorGroup.getOperator(ShowTablesOperator.class, OperatorKey.SQL); + SQLShowTablesOperator operator = OperatorGroup.getOperator(SQLShowTablesOperator.class, OperatorKey.SQL); String sql = operator.getDSL(); assertEquals("SELECT TABLE_CATALOG AS CATALOG, TABLE_SCHEMA AS SCHEMA, TABLE_NAME AS NAME, TABLE_TYPE AS TYPE\n" + "FROM INFORMATION_SCHEMA.TABLES\n" + @@ -21,7 +21,7 @@ void testSQLLByH2() { @Test void testSQLLByMySQL() { - ShowTablesOperator operator = OperatorGroup.getOperator(ShowTablesOperator.class, OperatorKey.SQL, DBType.MYSQL); + SQLShowTablesOperator operator = OperatorGroup.getOperator(SQLShowTablesOperator.class, OperatorKey.SQL, DBType.MYSQL); String sql = operator.database("da").getDSL(); assertEquals("SELECT TABLE_CATALOG AS CATALOG, TABLE_SCHEMA AS SCHEMA, TABLE_NAME AS NAME, TABLE_TYPE AS TYPE\n" + "FROM INFORMATION_SCHEMA.TABLES\n" + @@ -32,7 +32,7 @@ void testSQLLByMySQL() { @Test void testSQLLByPostgreSQL() { - ShowTablesOperator operator = OperatorGroup.getOperator(ShowTablesOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); + SQLShowTablesOperator operator = OperatorGroup.getOperator(SQLShowTablesOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); String sql = operator.getDSL(); assertEquals("SELECT TABLE_CATALOG AS CATALOG, TABLE_SCHEMA AS SCHEMA, TABLE_NAME AS NAME, TABLE_TYPE AS TYPE\n" + "FROM INFORMATION_SCHEMA.TABLES\n" + @@ -42,7 +42,7 @@ void testSQLLByPostgreSQL() { @Test void testDualShowTable() { - ShowTablesOperator operator = OperatorGroup.getOperator(ShowTablesOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); + SQLShowTablesOperator operator = OperatorGroup.getOperator(SQLShowTablesOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); String sql = operator.from("dual").getDSL(); assertEquals("SELECT TABLE_CATALOG AS CATALOG, TABLE_SCHEMA AS SCHEMA, TABLE_NAME AS NAME, TABLE_TYPE AS TYPE\n" + "FROM INFORMATION_SCHEMA.TABLES\n" + @@ -53,7 +53,7 @@ void testDualShowTable() { @Test void testCompositeShowTable() { - ShowTablesOperator operator = OperatorGroup.getOperator(ShowTablesOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); + SQLShowTablesOperator operator = OperatorGroup.getOperator(SQLShowTablesOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); String sql = operator.from("dualA").from("dualB").database("da").schema("db").getDSL(); assertEquals("SELECT TABLE_CATALOG AS CATALOG, TABLE_SCHEMA AS SCHEMA, TABLE_NAME AS NAME, TABLE_TYPE AS TYPE\n" + "FROM INFORMATION_SCHEMA.TABLES\n" + diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLDeleteOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLDeleteOperatorTest.java index 24558c21..cf2e6ef1 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLDeleteOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLDeleteOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.dml.sql; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; -import cc.allio.uno.data.orm.dsl.dml.DeleteOperator; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLDeleteOperator; import cc.allio.uno.data.test.model.Operators; import cc.allio.uno.data.test.model.User; import cc.allio.uno.test.BaseTestCase; @@ -12,14 +12,14 @@ public class SQLDeleteOperatorTest extends BaseTestCase { @Test void testSimplePojoInsert() { - DeleteOperator deleteOperator = OperatorGroup.getOperator(DeleteOperator.class, OperatorKey.SQL); + SQLDeleteOperator deleteOperator = OperatorGroup.getOperator(SQLDeleteOperator.class, OperatorKey.SQL); String sql = deleteOperator.from(User.class).from(User.class).getDSL(); assertEquals("DELETE FROM PUBLIC.t_users", sql); } @Test void testComplexCondition() { - DeleteOperator deleteOperator = OperatorGroup.getOperator(DeleteOperator.class, OperatorKey.SQL); + SQLDeleteOperator deleteOperator = OperatorGroup.getOperator(SQLDeleteOperator.class, OperatorKey.SQL); String sql = deleteOperator.from("dual") .eq("a", "a") .or() @@ -37,7 +37,7 @@ void testComplexCondition() { @Test void testParseSQL() { - DeleteOperator deleteOperator = OperatorGroup.getOperator(DeleteOperator.class, OperatorKey.SQL); + SQLDeleteOperator deleteOperator = OperatorGroup.getOperator(SQLDeleteOperator.class, OperatorKey.SQL); Operators.thenRest(() -> { String sql = "DELETE FROM PUBLIC.dual\n" + "WHERE (a = 'a'\n" + diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLInsertOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLInsertOperatorTest.java index 0f686cfe..fdaf6bc4 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLInsertOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLInsertOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.dml.sql; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; -import cc.allio.uno.data.orm.dsl.dml.InsertOperator; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLInsertOperator; import cc.allio.uno.data.test.model.Operators; import cc.allio.uno.test.BaseTestCase; import com.google.common.collect.Lists; @@ -15,7 +15,7 @@ public class SQLInsertOperatorTest extends BaseTestCase { @Test void testInsert() { - InsertOperator insertOperator = OperatorGroup.getOperator(InsertOperator.class, OperatorKey.SQL); + SQLInsertOperator insertOperator = OperatorGroup.getOperator(SQLInsertOperator.class, OperatorKey.SQL); String sql = insertOperator.from("test") .insert("x", "2") .getDSL(); @@ -32,7 +32,7 @@ void testInsert() { @Test void testNotEqualityInsertColumn() { - InsertOperator insertOperator = OperatorGroup.getOperator(InsertOperator.class, OperatorKey.SQL); + SQLInsertOperator insertOperator = OperatorGroup.getOperator(SQLInsertOperator.class, OperatorKey.SQL); String sql = insertOperator.from("dual") .insert("a1", null, "a2", "2") .insert("a1", null) @@ -43,7 +43,7 @@ void testNotEqualityInsertColumn() { @Test void testNullValueInsert() { - InsertOperator insertOperator = OperatorGroup.getOperator(InsertOperator.class, OperatorKey.SQL); + SQLInsertOperator insertOperator = OperatorGroup.getOperator(SQLInsertOperator.class, OperatorKey.SQL); Operators.thenRest(() -> { String sql = insertOperator.from("dual") @@ -67,7 +67,7 @@ void testNullValueInsert() { @Test void testStrictSingleValueClause() { - InsertOperator insertOperator = OperatorGroup.getOperator(InsertOperator.class, OperatorKey.SQL); + SQLInsertOperator insertOperator = OperatorGroup.getOperator(SQLInsertOperator.class, OperatorKey.SQL); new Operators(insertOperator) .then(() -> { // column不存在 @@ -93,7 +93,7 @@ void testStrictSingleValueClause() { @Test void testStrictMultiValueClause() { - InsertOperator insertOperator = OperatorGroup.getOperator(InsertOperator.class, OperatorKey.SQL); + SQLInsertOperator insertOperator = OperatorGroup.getOperator(SQLInsertOperator.class, OperatorKey.SQL); new Operators(insertOperator) .then(() -> { // column不存在 @@ -120,7 +120,7 @@ void testStrictMultiValueClause() { @Test void testParse() { - InsertOperator insertOperator = OperatorGroup.getOperator(InsertOperator.class, OperatorKey.SQL); + SQLInsertOperator insertOperator = OperatorGroup.getOperator(SQLInsertOperator.class, OperatorKey.SQL); String oriSQL = "INSERT INTO PUBLIC.t_user (name, age)\n" + "VALUES ('21', 2), ('xc', 2)"; insertOperator.parse(oriSQL); diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java index ded2422a..fe9d4bc0 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLQueryOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.dml.sql; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.dml.QueryOperator; -import cc.allio.uno.data.orm.dsl.type.DBType; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLQueryOperator; import cc.allio.uno.data.test.model.Operators; import cc.allio.uno.test.BaseTestCase; import org.junit.jupiter.api.Test; @@ -11,14 +11,14 @@ public class SQLQueryOperatorTest extends BaseTestCase { @Test void testSelect() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.selectAll().getDSL(); assertEquals("SELECT *", sql); } @Test void testFunc() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z") .min("z") .getDSL(); @@ -27,7 +27,7 @@ void testFunc() { @Test void testSimpleWhere() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z").from("dual").like("x", "zxc").getDSL(); assertEquals("SELECT z\n" + "FROM PUBLIC.dual\n" + @@ -36,7 +36,7 @@ void testSimpleWhere() { @Test void testMultiWhere() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z").from("dual").like("a", "a").eq("b", "b").getDSL(); assertEquals("SELECT z\n" + "FROM PUBLIC.dual\n" + @@ -46,7 +46,7 @@ void testMultiWhere() { @Test void testLogicPredicateOr() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z").from("dual").or().eq("a", "a").eq("b", "b").getDSL(); assertEquals("SELECT z\n" + "FROM PUBLIC.dual\n" + @@ -56,7 +56,7 @@ void testLogicPredicateOr() { @Test void testMultiLogicPredicate() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z").from("dual").or().eq("a", "a").eq("b", "b").and().eq("d", "d").getDSL(); assertEquals("SELECT z\n" + "FROM PUBLIC.dual\n" + @@ -67,7 +67,7 @@ void testMultiLogicPredicate() { @Test void testSubTable() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.leftJoin(Table.of("dual1"), Table.of("dual2"), BinaryCondition.of("dual1.cc", "dual2.aa", TokenOperator.EQUALITY)) .leftJoinThen("dual", "dual3", BinaryCondition.of("dual.xx", "dual3.xx", TokenOperator.EQUALITY)) .getDSL(); @@ -79,7 +79,7 @@ void testSubTable() { @Test void testOrder() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("a") .from("dual") .orderBy("a", OrderCondition.DESC) @@ -91,7 +91,7 @@ void testOrder() { @Test void testGroup() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z") .from("dual") .groupByOne("z") @@ -104,7 +104,7 @@ void testGroup() { @Test void testLimit() { Operators.thenRest(() -> { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z") .from("dual") .page(1L, 10L) @@ -117,14 +117,14 @@ void testLimit() { // test pg Operators.thenRest(() -> { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL, DBType.POSTGRESQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z") .from("dual") .page(1L, 10L) .getDSL(); assertEquals("SELECT z\n" + "FROM PUBLIC.dual\n" + - "LIMIT 10 OFFSET 0", sql); + "LIMIT 0, 10", sql); return operator; }); } @@ -132,7 +132,7 @@ void testLimit() { @Test void testIn() { Operators.thenRest(() -> { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String sql = operator.select("z") .from("dual") .in("t1", "2", "2") @@ -147,7 +147,7 @@ void testIn() { @Test void testParse() { - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); Operators.thenRest(() -> { String s1 = "SELECT *\n" + "FROM dual"; @@ -196,17 +196,6 @@ void testParse() { return operator; }); - // druid failed -// Operators.thenRest(() -> { -// String s6 = "SELECT \n" + -// "FROM (dual1\n" + -// "\tLEFT JOIN PUBLIC.dual2 ON dual1.cc = dual2.aa) AS dual\n" + -// "\tLEFT JOIN PUBLIC.dual3 ON dual.xx = dual3.xx"; -// String s6p = operator.parse(s6).getDSL(); -// assertEquals(s6, s6p); -// return operator; -// }); - Operators.thenRest(() -> { String s7 = "SELECT a\n" + "FROM PUBLIC.dual\n" + @@ -242,20 +231,28 @@ void testRecursive() { " UNION (SELECT sub.* FROM ((SELECT *\n" + "FROM org\n" + ") sub INNER JOIN biz_tree P ON P.ID = sub.parent_id))) SELECT * FROM biz_tree"; - QueryOperator operator = OperatorGroup.getOperator(QueryOperator.class, OperatorKey.SQL, DBType.MYSQL); + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); String dsl = operator.parse(sql).getDSL(); assertEquals("WITH RECURSIVE biz_tree AS (\n" + "\t\tSELECT *\n" + "\t\tFROM org\n" + "\t\tUNION\n" + - "\t\t(SELECT sub.*\n" + + "\t\tSELECT sub.*\n" + "\t\tFROM (\n" + "\t\t\tSELECT *\n" + "\t\t\tFROM org\n" + "\t\t) sub\n" + - "\t\t\tINNER JOIN biz_tree P ON P.ID = sub.parent_id)\n" + + "\t\t\tINNER JOIN biz_tree P ON P.ID = sub.parent_id\n" + "\t)\n" + "SELECT *\n" + "FROM biz_tree", dsl); } + + @Test + void testPeelWhere() { + SQLQueryOperator operator = OperatorGroup.getOperator(SQLQueryOperator.class, OperatorKey.SQL); + + String whereDSL = operator.eq("a", "a").getWhereDSL(); + assertEquals("a = 'a'", whereDSL); + } } diff --git a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLUpdateOperatorTest.java b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLUpdateOperatorTest.java index 337a7287..266ad0cb 100644 --- a/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLUpdateOperatorTest.java +++ b/uno-data/uno-data-sql/src/test/java/cc/allio/uno/data/orm/dsl/dml/sql/SQLUpdateOperatorTest.java @@ -1,8 +1,8 @@ package cc.allio.uno.data.orm.dsl.dml.sql; -import cc.allio.uno.data.orm.dsl.OperatorGroup; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.dsl.OperatorKey; -import cc.allio.uno.data.orm.dsl.dml.UpdateOperator; +import cc.allio.uno.data.orm.dsl.sql.dml.SQLUpdateOperator; import cc.allio.uno.data.test.model.Operators; import cc.allio.uno.data.test.model.User; import cc.allio.uno.test.BaseTestCase; @@ -12,7 +12,7 @@ public class SQLUpdateOperatorTest extends BaseTestCase { @Test void testSimplePojoInsert() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); User user = new User(); user.setName("a"); String sql = updateOperator.from(User.class).updatePojo(user).getDSL(); @@ -22,7 +22,7 @@ void testSimplePojoInsert() { @Test void testUpdateWherePrepareValue() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); updateOperator.eq("eq1", "eq1"); updateOperator.update("t11", "t11"); @@ -50,7 +50,7 @@ void testUpdateWherePrepareValue() { @Test void testSimpleUpdate() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); User user = new User(); user.setName("test"); String sql = updateOperator.from(User.class).updatePojo(user).getDSL(); @@ -60,7 +60,7 @@ void testSimpleUpdate() { @Test void testWhereUpdate() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); String sql = updateOperator.from("dual").update("a", "a").eq("a", "a").getDSL(); assertEquals("UPDATE PUBLIC.dual\n" + "SET a = 'a'\n" + @@ -69,7 +69,7 @@ void testWhereUpdate() { @Test void testComplexWhereUpdate() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); String sql = updateOperator.from("dual").update("a", "a") .eq("a", "a") .eq("b", "b") @@ -88,7 +88,7 @@ void testComplexWhereUpdate() { @Test void testStrictFill() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); new Operators(updateOperator) .then(() -> { @@ -121,7 +121,7 @@ void testStrictFill() { @Test void testParse() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); Operators.thenRest(() -> { String sql = "UPDATE PUBLIC.dual\n" + "SET a = 'a'\n" + @@ -148,7 +148,7 @@ void testParse() { @Test void testStrictModelUpdate() { - UpdateOperator updateOperator = OperatorGroup.getOperator(UpdateOperator.class, OperatorKey.SQL); + SQLUpdateOperator updateOperator = OperatorGroup.getOperator(SQLUpdateOperator.class, OperatorKey.SQL); User user = new User(); user.setId(1L); String sql = updateOperator.updatePojo(user).getDSL(); diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java index 0deaa1ed..6db3b763 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/CommandExecutorInjectRunner.java @@ -101,12 +101,13 @@ E loadCommandExecutor(ExecutorOptions execu * @param container container * @return ExecutorOptions */ - private ExecutorOptions drain(Container container) { + ExecutorOptions drain(Container container) { ContainerType containerType = container.getContainerType(); ContainerExecutorOptionsTranslator translator = translatorMap.get(containerType); if (translator == null) { return null; } + // with xxx DBType dbType = translator.withDBType(container); ExecutorKey executorKey = translator.withExecutorKey(container); OperatorKey operatorKey = translator.withOperatorKey(container); @@ -121,6 +122,8 @@ private ExecutorOptions drain(Container container) { executorOptions.setPassword(password); boolean isDefault = translator.withDefault(); executorOptions.setSystemDefault(isDefault); + Map property = translator.withOthers(container); + executorOptions.putAll(property); return executorOptions; } } \ No newline at end of file diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/FakeCommandExecutor.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/FakeCommandExecutor.java index 36caa2a2..69c112ea 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/FakeCommandExecutor.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/FakeCommandExecutor.java @@ -2,8 +2,7 @@ import cc.allio.uno.core.exception.Exceptions; import cc.allio.uno.data.orm.dsl.Operator; -import cc.allio.uno.data.orm.dsl.OperatorGroup; -import cc.allio.uno.data.orm.dsl.dml.QueryOperator; +import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; import cc.allio.uno.data.orm.executor.AggregateCommandExecutor; import cc.allio.uno.data.orm.executor.CommandType; import cc.allio.uno.data.orm.executor.handler.ListResultSetHandler; diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/testcontainers/InfluxdbPrelude.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/testcontainers/InfluxdbPrelude.java new file mode 100644 index 00000000..2540d7c7 --- /dev/null +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/testcontainers/InfluxdbPrelude.java @@ -0,0 +1,33 @@ +package cc.allio.uno.data.test.executor.testcontainers; + +import cc.allio.uno.auto.service.AutoService; +import cc.allio.uno.test.testcontainers.Container; +import cc.allio.uno.test.testcontainers.ContainerType; +import cc.allio.uno.test.testcontainers.Prelude; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.InfluxDBContainer; + +/** + * prepare influxdb in start before. + * + * @author j.x + * @date 2024/4/16 14:13 + * @see cc.allio.uno.data.test.executor.translator.InfluxdbTranslator + * @since 1.1.8 + */ +@AutoService(Prelude.class) +public class InfluxdbPrelude implements Prelude { + + @Override + public void onPrepare(Container container) { + GenericContainer internal = container.getInternal(); + if (internal instanceof InfluxDBContainer influxDBContainer) { + influxDBContainer.withAdminToken("admin"); + } + } + + @Override + public boolean match(ContainerType containerType) { + return ContainerType.Influxdb == containerType; + } +} diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java index 7f4d5b71..f4857091 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/ContainerExecutorOptionsTranslator.java @@ -7,6 +7,8 @@ import cc.allio.uno.test.testcontainers.Container; import org.testcontainers.containers.GenericContainer; +import java.util.Map; + /** * translate {@link Container} to {@link ExecutorOptions} * @@ -75,6 +77,16 @@ default String withAddress(Container testContainer) { */ String withPassword(Container testContainer); + /** + * translate to other arguments + * + * @param testContainer the not null test container + * @return the map for arguments + */ + default Map withOthers(Container testContainer) { + return Map.of(); + } + /** * return is default database, the default is false */ diff --git a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/InfluxdbTranslator.java b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/InfluxdbTranslator.java index 259db855..3a40b718 100644 --- a/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/InfluxdbTranslator.java +++ b/uno-data/uno-data-test/src/main/java/cc/allio/uno/data/test/executor/translator/InfluxdbTranslator.java @@ -1,22 +1,30 @@ package cc.allio.uno.data.test.executor.translator; +import cc.allio.uno.core.StringPool; import cc.allio.uno.data.orm.dsl.OperatorKey; import cc.allio.uno.data.orm.dsl.type.DBType; import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.test.testcontainers.Container; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.InfluxDBContainer; + +import java.util.Map; /** * influxdb describe executor options * * @author j.x * @date 2024/4/1 16:47 + * @see org.testcontainers.containers.InfluxDBContainer * @since 1.1.8 */ public class InfluxdbTranslator implements ContainerExecutorOptionsTranslator { - private static final String DOCKER_INFLUXDB_INIT_USERNAME = "DOCKER_INFLUXDB_INIT_USERNAME"; - private static final String DOCKER_INFLUXDB_INIT_PASSWORD = "DOCKER_INFLUXDB_INIT_PASSWORD"; - private static final String DOCKER_INFLUXDB_INIT_ORG = "DOCKER_INFLUXDB_INIT_ORG"; + static final String DOCKER_INFLUXDB_INIT_USERNAME = "DOCKER_INFLUXDB_INIT_USERNAME"; + static final String DOCKER_INFLUXDB_INIT_PASSWORD = "DOCKER_INFLUXDB_INIT_PASSWORD"; + static final String DOCKER_INFLUXDB_INIT_BUCKET = "DOCKER_INFLUXDB_INIT_BUCKET"; + static final String DOCKER_INFLUXDB_INIT_ORG = "DOCKER_INFLUXDB_INIT_ORG"; + static final String DOCKER_INFLUXDB_INIT_ADMIN_TOKEN = "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"; @Override public DBType withDBType(Container testContainer) { @@ -33,9 +41,15 @@ public OperatorKey withOperatorKey(Container testContainer) { return OperatorKey.INFLUXDB; } + @Override + public String withAddress(Container testContainer) { + GenericContainer internal = testContainer.getInternal(); + return StringPool.HTTP + internal.getHost() + ":" + internal.getMappedPort(InfluxDBContainer.INFLUXDB_PORT); + } + @Override public String withDatabase(Container testContainer) { - return testContainer.getEnv(DOCKER_INFLUXDB_INIT_ORG); + return testContainer.getEnv(DOCKER_INFLUXDB_INIT_BUCKET); } @Override @@ -47,4 +61,9 @@ public String withUsername(Container testContainer) { public String withPassword(Container testContainer) { return testContainer.getEnv(DOCKER_INFLUXDB_INIT_PASSWORD); } + + @Override + public Map withOthers(Container testContainer) { + return Map.of("organization", testContainer.getEnv(DOCKER_INFLUXDB_INIT_ORG), "token", testContainer.getEnv(DOCKER_INFLUXDB_INIT_ADMIN_TOKEN)); + } } diff --git a/uno-sequential/src/test/java/cc/allio/uno/sequential/wash/WasherAssemblerTest.java b/uno-sequential/src/test/java/cc/allio/uno/sequential/wash/WasherAssemblerTest.java index f9f2a7d2..576968d6 100644 --- a/uno-sequential/src/test/java/cc/allio/uno/sequential/wash/WasherAssemblerTest.java +++ b/uno-sequential/src/test/java/cc/allio/uno/sequential/wash/WasherAssemblerTest.java @@ -1,7 +1,7 @@ package cc.allio.uno.sequential.wash; import cc.allio.uno.core.type.MemoryTypeManager; -import cc.allio.uno.data.orm.config.elasticsearch.UDElasticSearchAutoConfiguration; +import cc.allio.uno.data.orm.config.elasticsearch.UnoElasticSearchAutoConfiguration; import cc.allio.uno.sequential.TypeSequential; import cc.allio.uno.sequential.UnCodeSequential; import cc.allio.uno.sequnetial.context.DefaultSequentialContext; @@ -22,7 +22,7 @@ * @date 2022/5/19 16:17 * @since 1.0 */ -@RunTest(components = UDElasticSearchAutoConfiguration.class) +@RunTest(components = UnoElasticSearchAutoConfiguration.class) @EsEnv @EsProperties(uris = "http://43.143.195.208:9200") class WasherAssemblerTest extends BaseTestCase { diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Container.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Container.java index 61b2c59f..9eefc55d 100644 --- a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Container.java +++ b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Container.java @@ -5,6 +5,8 @@ import lombok.Data; import org.testcontainers.containers.GenericContainer; +import java.util.List; + /** * describe {@link org.testcontainers.containers.Container} * @@ -18,6 +20,7 @@ public final class Container { private final ContainerType containerType; private final GenericContainer internal; + private final List containerPrelude; /** * get container env by key @@ -32,6 +35,15 @@ public String getEnv(String key) { return StringPool.EMPTY; } + /** + * prepare container + */ + public void prelude() { + containerPrelude.stream() + .filter(prelude -> prelude.match(containerType)) + .forEach(p -> p.onPrepare(this)); + } + /** * start internal container * diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/ContainerType.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/ContainerType.java index 18367533..32a594e5 100644 --- a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/ContainerType.java +++ b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/ContainerType.java @@ -19,13 +19,13 @@ public enum ContainerType { MySQL("mysql:8.0.36", "org.testcontainers.containers.MySQLContainer"), MSSQL("", "org.testcontainers.containers.MSSQLServerContainer"), Mongodb("mongo:7.0.6", "org.testcontainers.containers.MongoDBContainer"), - Influxdb("2.7.5", "org.testcontainers.containers.InfluxDBContainer"), + Influxdb("influxdb:2.7", "org.testcontainers.containers.InfluxDBContainer"), // marked test Test("", "cc.allio.uno.test.testcontainers.TestContainer"); final String version; - final String className; + final String testContainerClassName; public DockerImageName getDockerImageName() { DockerImageName imageName = DockerImageName.parse(version); diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/LogPrelude.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/LogPrelude.java new file mode 100644 index 00000000..0bade684 --- /dev/null +++ b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/LogPrelude.java @@ -0,0 +1,26 @@ +package cc.allio.uno.test.testcontainers; + +import lombok.extern.slf4j.Slf4j; + +/** + * log for {@link Container} information + * + * @author j.x + * @date 2024/4/16 14:37 + * @since 1.1.8 + */ +@Slf4j +public class LogPrelude implements Prelude { + + @Override + public void onPrepare(Container container) { + if (log.isDebugEnabled()) { + log.debug("container is [{}]", container.getContainerType()); + } + } + + @Override + public boolean match(ContainerType containerType) { + return true; + } +} diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Prelude.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Prelude.java new file mode 100644 index 00000000..a68377f1 --- /dev/null +++ b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/Prelude.java @@ -0,0 +1,27 @@ +package cc.allio.uno.test.testcontainers; + +/** + * prepare action {@link Container} start up before + *

sub-class should be through SPI mechanism.

+ * + * @author j.x + * @date 2024/4/16 13:58 + * @since 1.1.8 + */ +public interface Prelude { + + /** + * on prepare in {@link Container#start()} before + * + * @param container the container instance + */ + void onPrepare(Container container); + + /** + * match specifies {@link ContainerType}. if match then invoke {@link #onPrepare(Container)} + * + * @param containerType the container type + * @return true if match + */ + boolean match(ContainerType containerType); +} diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/RunContainer.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/RunContainer.java index 8e8258c8..75d886fd 100644 --- a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/RunContainer.java +++ b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/RunContainer.java @@ -25,4 +25,11 @@ * @see ContainerType */ ContainerType value(); + + /** + * through give the class {@link Prelude}, build and trigger {@link Prelude#onPrepare(Container)} + * + * @return the class of {@link Prelude} + */ + Class prelude() default LogPrelude.class; } diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/SetupContainer.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/SetupContainer.java index 1210f91d..0dfac3eb 100644 --- a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/SetupContainer.java +++ b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/SetupContainer.java @@ -1,16 +1,20 @@ package cc.allio.uno.test.testcontainers; import cc.allio.uno.core.exception.Exceptions; +import cc.allio.uno.core.spi.ClassPathServiceLoader; import cc.allio.uno.core.util.ClassUtils; import cc.allio.uno.test.CoreTest; import cc.allio.uno.test.runner.RefreshCompleteRunner; +import com.google.common.collect.Lists; import jakarta.annotation.Priority; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.AnnotationUtils; import org.testcontainers.containers.GenericContainer; import org.testcontainers.utility.DockerImageName; +import java.util.List; import java.util.Optional; +import java.util.ServiceLoader; /** * setup test-container if exist {@link RunContainer} @@ -23,6 +27,18 @@ @Priority(Integer.MIN_VALUE) public class SetupContainer implements RefreshCompleteRunner { + private final List preludes; + + public SetupContainer() { + this.preludes = Lists.newArrayList(); + // load prelude instance by spi + var preludeOnSPI = Lists.newArrayList(ClassPathServiceLoader.load(Prelude.class)) + .stream() + .map(ServiceLoader.Provider::get) + .toList(); + this.preludes.addAll(preludeOnSPI); + } + @Override public void onRefreshComplete(CoreTest coreTest) throws Throwable { Class testClass = coreTest.getTestClass(); @@ -30,22 +46,36 @@ public void onRefreshComplete(CoreTest coreTest) throws Throwable { Optional.ofNullable(runTestContainer) .map(RunContainer::value) .flatMap(containerType -> { - String className = containerType.getClassName(); + String className = containerType.getTestContainerClassName(); if (log.isDebugEnabled()) { log.debug("load test-container {} class", className); } + + Class> containerClass; try { - Class> containerClass = (Class>) Class.forName(className); - DockerImageName dockerImageName = containerType.getDockerImageName(); - GenericContainer c = ClassUtils.newInstance(containerClass, dockerImageName); - Container testContainer = new Container(containerType, c); - // start container - testContainer.start(); - return Optional.of(testContainer); + containerClass = (Class>) Class.forName(className); } catch (Throwable ex) { log.error("load class {} for test-container failed", className, ex); throw Exceptions.unchecked(ex); } + DockerImageName dockerImageName = containerType.getDockerImageName(); + GenericContainer c = ClassUtils.newInstance(containerClass, dockerImageName); + // load RunContainer prelude + Class preludeClass = runTestContainer.prelude(); + if (preludeClass != null) { + Prelude prelude = ClassUtils.newInstance(preludeClass); + preludes.add(prelude); + } + Container testContainer = new Container(containerType, c, preludes); + try { + // on prepare container + testContainer.prelude(); + } catch (Throwable ex) { + log.error("Field to prelude Container [{}]. ", containerType, ex); + } + // start container, if error then throwing + testContainer.start(); + return Optional.of(testContainer); }) .ifPresent(coreTest::setContainer); } diff --git a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/TestContainer.java b/uno-test/src/main/java/cc/allio/uno/test/testcontainers/TestContainer.java deleted file mode 100644 index 6319fad2..00000000 --- a/uno-test/src/main/java/cc/allio/uno/test/testcontainers/TestContainer.java +++ /dev/null @@ -1,25 +0,0 @@ -package cc.allio.uno.test.testcontainers; - -import lombok.extern.slf4j.Slf4j; -import org.testcontainers.containers.GenericContainer; - -/** - * mark test container - * - * @author j.x - * @date 2024/3/21 00:28 - * @since 1.1.7 - */ -@Slf4j -public class TestContainer extends GenericContainer { - - @Override - public void start() { - log.debug("start test container... then nothing todo."); - } - - @Override - public void stop() { - log.debug("end test container..."); - } -}