diff --git a/README.md b/README.md index 6e76300c1..be638d95a 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ QQ群:170029046 ```xml - 0.9.36 + 0.9.37 com.easy-query @@ -65,7 +65,7 @@ QQ群:170029046 ```xml - 0.9.36 + 0.9.37 diff --git a/pom.xml b/pom.xml index b34b1acd6..f19c00fa2 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.easy-query easy-query-all pom - 0.9.36 + 0.9.37 easy-query java object query distributed connector https://github.com/xuejmnet/easy-query @@ -33,8 +33,8 @@ sql-db-support/sql-pgsql sql-db-support/sql-sqlite - - + sql-test + samples diff --git a/samples/pom.xml b/samples/pom.xml index a270fb69c..715381428 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -5,7 +5,7 @@ easy-query-all com.easy-query - 0.9.36 + 0.9.37 4.0.0 diff --git a/samples/springbootdemo/pom.xml b/samples/springbootdemo/pom.xml index 1de54daa8..cf5c1a2da 100644 --- a/samples/springbootdemo/pom.xml +++ b/samples/springbootdemo/pom.xml @@ -55,7 +55,7 @@ com.easy-query sql-springboot-starter - 0.9.36 + 0.9.37 compile diff --git a/sql-core/pom.xml b/sql-core/pom.xml index ac8bd0af2..beb9223c1 100644 --- a/sql-core/pom.xml +++ b/sql-core/pom.xml @@ -5,7 +5,7 @@ easy-query-all com.easy-query - 0.9.36 + 0.9.37 4.0.0 diff --git a/sql-core/src/main/java/com/easy/query/core/api/client/DefaultEasyQueryClient.java b/sql-core/src/main/java/com/easy/query/core/api/client/DefaultEasyQueryClient.java index 88a726b34..4ab9685b4 100644 --- a/sql-core/src/main/java/com/easy/query/core/api/client/DefaultEasyQueryClient.java +++ b/sql-core/src/main/java/com/easy/query/core/api/client/DefaultEasyQueryClient.java @@ -57,6 +57,11 @@ public ClientQueryable queryable(Class clazz) { return easySQLApiFactory.createQueryable(clazz, runtimeContext); } + @Override + public ClientQueryable queryable(ClientQueryable queryable) { + return null; + } + @Override public ClientQueryable queryable(String sql, Class clazz) { return easySQLApiFactory.createQueryable(sql, clazz, runtimeContext); diff --git a/sql-core/src/main/java/com/easy/query/core/api/client/EasyQueryClient.java b/sql-core/src/main/java/com/easy/query/core/api/client/EasyQueryClient.java index baaebfcc8..c913f1ab4 100644 --- a/sql-core/src/main/java/com/easy/query/core/api/client/EasyQueryClient.java +++ b/sql-core/src/main/java/com/easy/query/core/api/client/EasyQueryClient.java @@ -51,6 +51,8 @@ default long sqlExecute(String sql) { ClientQueryable queryable(Class clazz); + ClientQueryable queryable(ClientQueryable queryable); + ClientQueryable queryable(String sql, Class clazz); default Transaction beginTransaction() { diff --git a/sql-core/src/main/java/com/easy/query/core/api/def/DefaultSQLClientApiFactory.java b/sql-core/src/main/java/com/easy/query/core/api/def/DefaultSQLClientApiFactory.java index ec1836bc0..96d442353 100644 --- a/sql-core/src/main/java/com/easy/query/core/api/def/DefaultSQLClientApiFactory.java +++ b/sql-core/src/main/java/com/easy/query/core/api/def/DefaultSQLClientApiFactory.java @@ -68,7 +68,6 @@ public ClientQueryable createQueryable(Class clazz, QueryRuntimeContex EntityQueryExpressionBuilder entityQueryExpressionBuilder = expressionBuilderFactory.createEntityQueryExpressionBuilder(queryExpressionContext); EntityMetadata entityMetadata = runtimeContext.getEntityMetadataManager().getEntityMetadata(clazz); int tableIndex = EasyUtil.getNextTableIndex(entityQueryExpressionBuilder); - ; EntityTableExpressionBuilder sqlTable = expressionBuilderFactory.createEntityTableExpressionBuilder(entityMetadata, tableIndex, MultiTableTypeEnum.FROM, runtimeContext); entityQueryExpressionBuilder.addSQLEntityTableExpression(sqlTable); return new EasyClientQueryable<>(clazz, entityQueryExpressionBuilder); diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable.java index 343e2f1c6..d1dfcb750 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable.java @@ -64,16 +64,6 @@ default BigDecimal sumBigDecimalOrNull(String property) { return sumBigDecimalOrDefault(property, null); } - /** - * 防止溢出 - * - * @param property - * @return - */ - default BigDecimal sumBigDecimalNotNull(String property) { - return sumBigDecimalOrDefault(property, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(String property, BigDecimal def); default TMember sumOrNull(String property) { @@ -120,13 +110,6 @@ default Float avgOrDefault(String property, Float def) TResult avgOrDefault(String property, TResult def, Class resultClass); - default Integer lenOrNull(String property) { - return lenOrDefault(property, null); - } - - Integer lenOrDefault(String property, Integer def); - - /** * 对当前表达式返回自定义select列 * diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable2.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable2.java index 3e8d95c02..ed03dc798 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable2.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable2.java @@ -6,11 +6,7 @@ import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression2; import com.easy.query.core.expression.lambda.SQLExpression3; -import com.easy.query.core.expression.parser.core.base.ColumnAsSelector; -import com.easy.query.core.expression.parser.core.base.ColumnResultSelector; -import com.easy.query.core.expression.parser.core.base.ColumnSelector; -import com.easy.query.core.expression.parser.core.base.GroupBySelector; -import com.easy.query.core.expression.parser.core.base.WherePredicate; +import com.easy.query.core.expression.parser.core.base.*; import java.math.BigDecimal; import java.util.function.Function; @@ -75,17 +71,6 @@ default BigDecimal sumBigDecimalOrNull(SQLExpression2 - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, BigDecimal def); default TMember sumOrNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { @@ -106,17 +91,31 @@ default TMember minOrNull(SQLExpression2, Col TMember minOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TMember def); - default TMember avgOrNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, Double.class); + } + + default BigDecimal avgBigDecimalOrNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, BigDecimal.class); + } + + default Float avgFloatOrNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, Float.class); + } + + default Double avgOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, Double def) { + return avgOrDefault(columnSelectorExpression, def, Double.class); } - TMember avgOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return avgOrDefault(columnSelectorExpression, def, BigDecimal.class); + } - default Integer lenOrNull(SQLExpression2, ColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Float avgFloatOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, Float def) { + return avgOrDefault(columnSelectorExpression, def, Float.class); } - Integer lenOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, Integer def); + TResult avgOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass); //endregion //region group @@ -134,6 +133,20 @@ default ClientQueryable2 groupBy(SQLExpression2, Gro ClientQueryable2 groupBy(boolean condition, SQLExpression2, GroupBySelector> selectExpression); + @Override + default ClientQueryable2 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + ClientQueryable2 having(boolean condition, SQLExpression1> predicateExpression); + + default ClientQueryable2 having(SQLExpression2, WhereAggregatePredicate> predicateExpression) { + return having(true, predicateExpression); + } + + ClientQueryable2 having(boolean condition, SQLExpression2, WhereAggregatePredicate> predicateExpression); + //endregion //region order @Override diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable3.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable3.java index 6318e67cf..1131657ab 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable3.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable3.java @@ -6,11 +6,7 @@ import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression3; import com.easy.query.core.expression.lambda.SQLExpression4; -import com.easy.query.core.expression.parser.core.base.ColumnAsSelector; -import com.easy.query.core.expression.parser.core.base.ColumnResultSelector; -import com.easy.query.core.expression.parser.core.base.ColumnSelector; -import com.easy.query.core.expression.parser.core.base.GroupBySelector; -import com.easy.query.core.expression.parser.core.base.WherePredicate; +import com.easy.query.core.expression.parser.core.base.*; import java.math.BigDecimal; import java.util.function.Function; @@ -76,17 +72,6 @@ default BigDecimal sumBigDecimalOrNull(SQLExpression3 - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, BigDecimal def); default TMember sumOrNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { @@ -107,17 +92,32 @@ default TMember minOrNull(SQLExpression3, Col TMember minOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def); - default TMember avgOrNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, Double.class); + } + + default BigDecimal avgBigDecimalOrNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, BigDecimal.class); + } + + default Float avgFloatOrNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, Float.class); } - TMember avgOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def); + default Double avgOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Double def) { + return avgOrDefault(columnSelectorExpression, def, Double.class); + } + + default BigDecimal avgBigDecimalOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return avgOrDefault(columnSelectorExpression, def, BigDecimal.class); + } - default Integer lenOrNull(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Float avgFloatOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Float def) { + return avgOrDefault(columnSelectorExpression, def, Float.class); } - Integer lenOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Integer def); + TResult avgOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass); + //endregion //region group @@ -135,6 +135,21 @@ default ClientQueryable3 groupBy(SQLExpression3, ClientQueryable3 groupBy(boolean condition, SQLExpression3, GroupBySelector, GroupBySelector> selectExpression); + + @Override + default ClientQueryable3 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + ClientQueryable3 having(boolean condition, SQLExpression1> predicateExpression); + + default ClientQueryable3 having(SQLExpression3, WhereAggregatePredicate, WhereAggregatePredicate> predicateExpression) { + return having(true, predicateExpression); + } + + ClientQueryable3 having(boolean condition, SQLExpression3, WhereAggregatePredicate, WhereAggregatePredicate> predicateExpression); + //endregion //region order @Override diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable4.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable4.java index 2efa46bba..1b3e441df 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable4.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/ClientQueryable4.java @@ -5,11 +5,7 @@ import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression4; -import com.easy.query.core.expression.parser.core.base.ColumnAsSelector; -import com.easy.query.core.expression.parser.core.base.ColumnResultSelector; -import com.easy.query.core.expression.parser.core.base.ColumnSelector; -import com.easy.query.core.expression.parser.core.base.GroupBySelector; -import com.easy.query.core.expression.parser.core.base.WherePredicate; +import com.easy.query.core.expression.parser.core.base.*; import java.math.BigDecimal; import java.util.function.Function; @@ -60,17 +56,6 @@ default BigDecimal sumBigDecimalOrNull(SQLExpression4 - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, BigDecimal def); default TMember sumOrNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { @@ -91,17 +76,32 @@ default TMember minOrNull(SQLExpression4, Col TMember minOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def); - default TMember avgOrNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, Double.class); + } + + default BigDecimal avgBigDecimalOrNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, BigDecimal.class); } - TMember avgOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def); + default Float avgFloatOrNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { + return avgOrDefault(columnSelectorExpression, null, Float.class); + } - default Integer lenOrNull(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Double avgOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Double def) { + return avgOrDefault(columnSelectorExpression, def, Double.class); } - Integer lenOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Integer def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return avgOrDefault(columnSelectorExpression, def, BigDecimal.class); + } + + default Float avgFloatOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Float def) { + return avgOrDefault(columnSelectorExpression, def, Float.class); + } + + TResult avgOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass); + //endregion @@ -120,6 +120,20 @@ default ClientQueryable4 groupBy(SQLExpression4 groupBy(boolean condition, SQLExpression4, GroupBySelector, GroupBySelector, GroupBySelector> selectExpression); + @Override + default ClientQueryable4 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + ClientQueryable4 having(boolean condition, SQLExpression1> predicateExpression); + + default ClientQueryable4 having(SQLExpression4, WhereAggregatePredicate, WhereAggregatePredicate, WhereAggregatePredicate> predicateExpression) { + return having(true, predicateExpression); + } + + ClientQueryable4 having(boolean condition, SQLExpression4, WhereAggregatePredicate, WhereAggregatePredicate, WhereAggregatePredicate> predicateExpression); + //endregion //region order @Override diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable.java index f465762b8..d4d18e39e 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable.java @@ -199,18 +199,6 @@ public TResult avgOrDefault(Str return EasyCollectionUtil.firstOrDefault(result, def); } - @Override - public Integer lenOrDefault(String property, Integer def) { - setExecuteMethod(ExecuteMethodEnum.LEN); - EntityTableExpressionBuilder table = entityQueryExpressionBuilder.getTable(0); - - ColumnFunction lenFunction = runtimeContext.getColumnFunctionFactory().createLenFunction(); - FuncColumnSegment funcColumnSegment = sqlSegmentFactory.createFuncColumnSegment(table.getEntityTable(), property, entityQueryExpressionBuilder.getRuntimeContext(), lenFunction, null); - - List result = cloneQueryable().select(funcColumnSegment, true).toList(Integer.class); - return EasyCollectionUtil.firstOrDefault(result, def); - } - private List selectAggregateList(String property, ColumnFunction columnFunction, Class resultClass) { EntityTableExpressionBuilder table = entityQueryExpressionBuilder.getTable(0); Class tMemberClass = resultClass == null ? (Class) table.getEntityMetadata().getColumnNotNull(property).getPropertyType() : resultClass; diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable2.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable2.java index 508de8ab1..3a5e20fb7 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable2.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable2.java @@ -12,16 +12,11 @@ import com.easy.query.core.expression.lambda.SQLExpression2; import com.easy.query.core.expression.lambda.SQLExpression3; import com.easy.query.core.expression.parser.core.available.TableAvailable; -import com.easy.query.core.expression.parser.core.base.ColumnAsSelector; -import com.easy.query.core.expression.parser.core.base.ColumnResultSelector; -import com.easy.query.core.expression.parser.core.base.ColumnSelector; -import com.easy.query.core.expression.parser.core.base.GroupBySelector; -import com.easy.query.core.expression.parser.core.base.WherePredicate; +import com.easy.query.core.expression.parser.core.base.*; import com.easy.query.core.expression.segment.FuncColumnSegment; import com.easy.query.core.expression.segment.SQLEntitySegment; import com.easy.query.core.expression.segment.builder.ProjectSQLBuilderSegmentImpl; import com.easy.query.core.expression.sql.builder.EntityQueryExpressionBuilder; -import com.easy.query.core.metadata.ColumnMetadata; import com.easy.query.core.util.EasyCollectionUtil; import com.easy.query.core.util.EasySQLExpressionUtil; @@ -155,7 +150,7 @@ public ClientQueryable select(Class resultClass, SQLExpression2 List selectAggregateList(SQLExpression2, ColumnResultSelector> columnSelectorExpression, ColumnFunction columnFunction) { + private List selectAggregateList(SQLExpression2, ColumnResultSelector> columnSelectorExpression, ColumnFunction columnFunction, Class resultClass) { ProjectSQLBuilderSegmentImpl projectSQLBuilderSegment = new ProjectSQLBuilderSegmentImpl(); @@ -169,17 +164,16 @@ private List selectAggregateList(SQLExpression2 tMemberClass = resultClass == null ? (Class) table.getEntityMetadata().getColumnNotNull(propertyName).getPropertyType() : resultClass; FuncColumnSegment funcColumnSegment = sqlSegmentFactory.createFuncColumnSegment(table, propertyName, entityQueryExpressionBuilder.getRuntimeContext(), columnFunction, null); - return cloneQueryable().select(funcColumnSegment, true).toList((Class) columnMetadata.getPropertyType()); + return cloneQueryable().select(funcColumnSegment, true).toList(tMemberClass); } @Override public BigDecimal sumBigDecimalOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, BigDecimal def) { ColumnFunction sumFunction = runtimeContext.getColumnFunctionFactory().createSumFunction(false); - List result = selectAggregateList(columnSelectorExpression, sumFunction); + List result = selectAggregateList(columnSelectorExpression, sumFunction, null); TMember resultMember = EasyCollectionUtil.firstOrNull(result); if (resultMember == null) { return def; @@ -190,44 +184,28 @@ public BigDecimal sumBigDecimalOrDefault(SQLExpression2 @Override public TMember sumOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction sumFunction = runtimeContext.getColumnFunctionFactory().createSumFunction(false); - List result = selectAggregateList(columnSelectorExpression, sumFunction); + List result = selectAggregateList(columnSelectorExpression, sumFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override public TMember maxOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction maxFunction = runtimeContext.getColumnFunctionFactory().createMaxFunction(); - List result = selectAggregateList(columnSelectorExpression, maxFunction); + List result = selectAggregateList(columnSelectorExpression, maxFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override public TMember minOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction minFunction = runtimeContext.getColumnFunctionFactory().createMinFunction(); - List result = selectAggregateList(columnSelectorExpression, minFunction); + List result = selectAggregateList(columnSelectorExpression, minFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override - public TMember avgOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TMember def) { + public TResult avgOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { ColumnFunction avgFunction = runtimeContext.getColumnFunctionFactory().createAvgFunction(false); - List result = selectAggregateList(columnSelectorExpression, avgFunction); - return EasyCollectionUtil.firstOrDefault(result, def); - } - - @Override - public Integer lenOrDefault(SQLExpression2, ColumnResultSelector> columnSelectorExpression, Integer def) { - - ProjectSQLBuilderSegmentImpl projectSQLBuilderSegment = new ProjectSQLBuilderSegmentImpl(); - - ColumnResultSelector sqlColumnResultSelector1 = getSQLExpressionProvider1().getColumnResultSelector(projectSQLBuilderSegment); - ColumnResultSelector sqlColumnResultSelector2 = getSQLExpressionProvider2().getColumnResultSelector(projectSQLBuilderSegment); - columnSelectorExpression.apply(sqlColumnResultSelector1, sqlColumnResultSelector2); - if (projectSQLBuilderSegment.isEmpty()) { - throw new EasyQueryException("aggreagate query not found column"); - } - ColumnFunction lenFunction = runtimeContext.getColumnFunctionFactory().createLenFunction(); - List result = cloneQueryable().select(lenFunction.getFuncColumn(projectSQLBuilderSegment.toSQL(null))).toList(Integer.class); + List result = selectAggregateList(columnSelectorExpression, avgFunction, resultClass); return EasyCollectionUtil.firstOrDefault(result, def); } @@ -279,6 +257,22 @@ public ClientQueryable2 groupBy(boolean condition, SQLExpression2 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); + return this; + } + + @Override + public ClientQueryable2 having(boolean condition, SQLExpression2, WhereAggregatePredicate> predicateExpression) { + if (condition) { + WhereAggregatePredicate sqlGroupSelector1 = getSQLExpressionProvider1().getAggregatePredicate(); + WhereAggregatePredicate sqlGroupSelector2 = getSQLExpressionProvider2().getAggregatePredicate(); + predicateExpression.apply(sqlGroupSelector1, sqlGroupSelector2); + } + return this; + } + @Override public ClientQueryable2 limit(boolean condition, long offset, long rows) { super.limit(condition, offset, rows); diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable3.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable3.java index 33fe0775a..a161efb5c 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable3.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable3.java @@ -12,16 +12,11 @@ import com.easy.query.core.expression.lambda.SQLExpression3; import com.easy.query.core.expression.lambda.SQLExpression4; import com.easy.query.core.expression.parser.core.available.TableAvailable; -import com.easy.query.core.expression.parser.core.base.ColumnAsSelector; -import com.easy.query.core.expression.parser.core.base.ColumnResultSelector; -import com.easy.query.core.expression.parser.core.base.ColumnSelector; -import com.easy.query.core.expression.parser.core.base.GroupBySelector; -import com.easy.query.core.expression.parser.core.base.WherePredicate; +import com.easy.query.core.expression.parser.core.base.*; import com.easy.query.core.expression.segment.FuncColumnSegment; import com.easy.query.core.expression.segment.SQLEntitySegment; import com.easy.query.core.expression.segment.builder.ProjectSQLBuilderSegmentImpl; import com.easy.query.core.expression.sql.builder.EntityQueryExpressionBuilder; -import com.easy.query.core.metadata.ColumnMetadata; import com.easy.query.core.util.EasyCollectionUtil; import com.easy.query.core.util.EasySQLExpressionUtil; @@ -171,7 +166,7 @@ public ClientQueryable select(Class resultClass, SQLExpression3 List selectAggregateList(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, ColumnFunction columnFunction) { + private List selectAggregateList(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, ColumnFunction columnFunction, Class resultClass) { ProjectSQLBuilderSegmentImpl projectSQLBuilderSegment = new ProjectSQLBuilderSegmentImpl(); @@ -186,17 +181,16 @@ private List selectAggregateList(SQLExpression3 tMemberClass = resultClass == null ? (Class) table.getEntityMetadata().getColumnNotNull(propertyName).getPropertyType() : resultClass; FuncColumnSegment funcColumnSegment = sqlSegmentFactory.createFuncColumnSegment(table, propertyName, entityQueryExpressionBuilder.getRuntimeContext(), columnFunction, null); - return cloneQueryable().select(funcColumnSegment, true).toList((Class) columnMetadata.getPropertyType()); + return cloneQueryable().select(funcColumnSegment, true).toList(tMemberClass); } @Override public BigDecimal sumBigDecimalOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, BigDecimal def) { ColumnFunction sumFunction = runtimeContext.getColumnFunctionFactory().createSumFunction(false); - List result = selectAggregateList(columnSelectorExpression, sumFunction); + List result = selectAggregateList(columnSelectorExpression, sumFunction, null); TMember resultMember = EasyCollectionUtil.firstOrNull(result); if (resultMember == null) { return def; @@ -208,7 +202,7 @@ public BigDecimal sumBigDecimalOrDefault(SQLExpression3 public TMember sumOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction sumFunction = runtimeContext.getColumnFunctionFactory().createSumFunction(false); - List result = selectAggregateList(columnSelectorExpression, sumFunction); + List result = selectAggregateList(columnSelectorExpression, sumFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @@ -216,7 +210,7 @@ public TMember sumOrDefault(SQLExpression3 TMember maxOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction maxFunction = runtimeContext.getColumnFunctionFactory().createMaxFunction(); - List result = selectAggregateList(columnSelectorExpression, maxFunction); + List result = selectAggregateList(columnSelectorExpression, maxFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @@ -224,35 +218,18 @@ public TMember maxOrDefault(SQLExpression3, C public TMember minOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction minFunction = runtimeContext.getColumnFunctionFactory().createMinFunction(); - List result = selectAggregateList(columnSelectorExpression, minFunction); + List result = selectAggregateList(columnSelectorExpression, minFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override - public TMember avgOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { + public TResult avgOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { ColumnFunction avgFunction = runtimeContext.getColumnFunctionFactory().createAvgFunction(false); - List result = selectAggregateList(columnSelectorExpression, avgFunction); + List result = selectAggregateList(columnSelectorExpression, avgFunction, resultClass); return EasyCollectionUtil.firstOrDefault(result, def); } - @Override - public Integer lenOrDefault(SQLExpression3, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Integer def) { - - ProjectSQLBuilderSegmentImpl projectSQLBuilderSegment = new ProjectSQLBuilderSegmentImpl(); - - ColumnResultSelector sqlColumnResultSelector1 = getSQLExpressionProvider1().getColumnResultSelector(projectSQLBuilderSegment); - ColumnResultSelector sqlColumnResultSelector2 = getSQLExpressionProvider2().getColumnResultSelector(projectSQLBuilderSegment); - ColumnResultSelector sqlColumnResultSelector3 = getSQLExpressionProvider3().getColumnResultSelector(projectSQLBuilderSegment); - columnSelectorExpression.apply(sqlColumnResultSelector1, sqlColumnResultSelector2, sqlColumnResultSelector3); - if (projectSQLBuilderSegment.isEmpty()) { - throw new EasyQueryException("aggreagate query not found column"); - } - - ColumnFunction lenFunction = runtimeContext.getColumnFunctionFactory().createLenFunction(); - List result = cloneQueryable().select(lenFunction.getFuncColumn(projectSQLBuilderSegment.toSQL(null))).toList(Integer.class); - return EasyCollectionUtil.firstOrDefault(result, def); - } @Override public ClientQueryable3 groupBy(boolean condition, SQLExpression1> selectExpression) { @@ -271,6 +248,23 @@ public ClientQueryable3 groupBy(boolean condition, SQLExpression3 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(predicateExpression); + return this; + } + + @Override + public ClientQueryable3 having(boolean condition, SQLExpression3, WhereAggregatePredicate, WhereAggregatePredicate> predicateExpression) { + if (condition) { + WhereAggregatePredicate sqlGroupSelector1 = getSQLExpressionProvider1().getAggregatePredicate(); + WhereAggregatePredicate sqlGroupSelector2 = getSQLExpressionProvider2().getAggregatePredicate(); + WhereAggregatePredicate sqlGroupSelector3 = getSQLExpressionProvider3().getAggregatePredicate(); + predicateExpression.apply(sqlGroupSelector1, sqlGroupSelector2, sqlGroupSelector3); + } + return this; + } + @Override public ClientQueryable3 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); diff --git a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable4.java b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable4.java index 4a68525d0..7e36cfc6f 100644 --- a/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable4.java +++ b/sql-core/src/main/java/com/easy/query/core/basic/api/select/abstraction/AbstractClientQueryable4.java @@ -9,16 +9,11 @@ import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression4; import com.easy.query.core.expression.parser.core.available.TableAvailable; -import com.easy.query.core.expression.parser.core.base.ColumnAsSelector; -import com.easy.query.core.expression.parser.core.base.ColumnResultSelector; -import com.easy.query.core.expression.parser.core.base.ColumnSelector; -import com.easy.query.core.expression.parser.core.base.GroupBySelector; -import com.easy.query.core.expression.parser.core.base.WherePredicate; +import com.easy.query.core.expression.parser.core.base.*; import com.easy.query.core.expression.segment.FuncColumnSegment; import com.easy.query.core.expression.segment.SQLEntitySegment; import com.easy.query.core.expression.segment.builder.ProjectSQLBuilderSegmentImpl; import com.easy.query.core.expression.sql.builder.EntityQueryExpressionBuilder; -import com.easy.query.core.metadata.ColumnMetadata; import com.easy.query.core.util.EasyCollectionUtil; import java.math.BigDecimal; @@ -138,7 +133,7 @@ public ClientQueryable select(Class resultClass, SQLExpression4 List selectAggregateList(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, ColumnFunction columnFunction) { + private List selectAggregateList(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, ColumnFunction columnFunction, Class resultClass) { ProjectSQLBuilderSegmentImpl projectSQLBuilderSegment = new ProjectSQLBuilderSegmentImpl(); @@ -154,17 +149,17 @@ private List selectAggregateList(SQLExpression4 tMemberClass = resultClass == null ? (Class) table.getEntityMetadata().getColumnNotNull(propertyName).getPropertyType() : resultClass; FuncColumnSegment funcColumnSegment = sqlSegmentFactory.createFuncColumnSegment(table, propertyName, entityQueryExpressionBuilder.getRuntimeContext(), columnFunction, null); - return cloneQueryable().select(funcColumnSegment, true).toList((Class) columnMetadata.getPropertyType()); + return cloneQueryable().select(funcColumnSegment, true).toList(tMemberClass); } @Override public BigDecimal sumBigDecimalOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, BigDecimal def) { ColumnFunction sumFunction = runtimeContext.getColumnFunctionFactory().createSumFunction(false); - List result = selectAggregateList(columnSelectorExpression, sumFunction); + List result = selectAggregateList(columnSelectorExpression, sumFunction, null); TMember resultMember = EasyCollectionUtil.firstOrNull(result); if (resultMember == null) { return def; @@ -175,46 +170,28 @@ public BigDecimal sumBigDecimalOrDefault(SQLExpression4 @Override public TMember sumOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction sumFunction = runtimeContext.getColumnFunctionFactory().createSumFunction(false); - List result = selectAggregateList(columnSelectorExpression, sumFunction); + List result = selectAggregateList(columnSelectorExpression, sumFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override public TMember maxOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction maxFunction = runtimeContext.getColumnFunctionFactory().createMaxFunction(); - List result = selectAggregateList(columnSelectorExpression, maxFunction); + List result = selectAggregateList(columnSelectorExpression, maxFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override public TMember minOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { ColumnFunction minFunction = runtimeContext.getColumnFunctionFactory().createMinFunction(); - List result = selectAggregateList(columnSelectorExpression, minFunction); + List result = selectAggregateList(columnSelectorExpression, minFunction, null); return EasyCollectionUtil.firstOrDefault(result, def); } @Override - public TMember avgOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TMember def) { + public TResult avgOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { ColumnFunction avgFunction = runtimeContext.getColumnFunctionFactory().createAvgFunction(false); - List result = selectAggregateList(columnSelectorExpression, avgFunction); - return EasyCollectionUtil.firstOrDefault(result, def); - } - - @Override - public Integer lenOrDefault(SQLExpression4, ColumnResultSelector, ColumnResultSelector, ColumnResultSelector> columnSelectorExpression, Integer def) { - - ProjectSQLBuilderSegmentImpl projectSQLBuilderSegment = new ProjectSQLBuilderSegmentImpl(); - - ColumnResultSelector sqlColumnResultSelector1 = getSQLExpressionProvider1().getColumnResultSelector(projectSQLBuilderSegment); - ColumnResultSelector sqlColumnResultSelector2 = getSQLExpressionProvider2().getColumnResultSelector(projectSQLBuilderSegment); - ColumnResultSelector sqlColumnResultSelector3 = getSQLExpressionProvider3().getColumnResultSelector(projectSQLBuilderSegment); - ColumnResultSelector sqlColumnResultSelector4 = getSQLExpressionProvider4().getColumnResultSelector(projectSQLBuilderSegment); - columnSelectorExpression.apply(sqlColumnResultSelector1, sqlColumnResultSelector2, sqlColumnResultSelector3, sqlColumnResultSelector4); - if (projectSQLBuilderSegment.isEmpty()) { - throw new EasyQueryException("aggreagate query not found column"); - } - ColumnFunction lenFunction = runtimeContext.getColumnFunctionFactory().createLenFunction(); - List result = cloneQueryable().select(lenFunction.getFuncColumn(projectSQLBuilderSegment.toSQL(null))).toList(Integer.class); + List result = selectAggregateList(columnSelectorExpression, avgFunction, resultClass); return EasyCollectionUtil.firstOrDefault(result, def); } @@ -236,6 +213,24 @@ public ClientQueryable4 groupBy(boolean condition, SQLExpression return this; } + @Override + public ClientQueryable4 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(predicateExpression); + return this; + } + + @Override + public ClientQueryable4 having(boolean condition, SQLExpression4, WhereAggregatePredicate, WhereAggregatePredicate, WhereAggregatePredicate> predicateExpression) { + if (condition) { + WhereAggregatePredicate sqlGroupSelector1 = getSQLExpressionProvider1().getAggregatePredicate(); + WhereAggregatePredicate sqlGroupSelector2 = getSQLExpressionProvider2().getAggregatePredicate(); + WhereAggregatePredicate sqlGroupSelector3 = getSQLExpressionProvider3().getAggregatePredicate(); + WhereAggregatePredicate sqlGroupSelector4 = getSQLExpressionProvider4().getAggregatePredicate(); + predicateExpression.apply(sqlGroupSelector1, sqlGroupSelector2, sqlGroupSelector3, sqlGroupSelector4); + } + return this; + } + @Override public ClientQueryable4 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); diff --git a/sql-db-support/pom.xml b/sql-db-support/pom.xml index 24e7a5b08..88a60972b 100644 --- a/sql-db-support/pom.xml +++ b/sql-db-support/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 sql-db-support diff --git a/sql-db-support/sql-h2/pom.xml b/sql-db-support/sql-h2/pom.xml index 72faba3cb..638b5a65f 100644 --- a/sql-db-support/sql-h2/pom.xml +++ b/sql-db-support/sql-h2/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-db-support/sql-mssql/pom.xml b/sql-db-support/sql-mssql/pom.xml index 05d058b3c..03c706ba8 100644 --- a/sql-db-support/sql-mssql/pom.xml +++ b/sql-db-support/sql-mssql/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-db-support/sql-mysql/pom.xml b/sql-db-support/sql-mysql/pom.xml index b3b893cee..c12f39920 100644 --- a/sql-db-support/sql-mysql/pom.xml +++ b/sql-db-support/sql-mysql/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-db-support/sql-pgsql/pom.xml b/sql-db-support/sql-pgsql/pom.xml index e2eab9f79..90359be82 100644 --- a/sql-db-support/sql-pgsql/pom.xml +++ b/sql-db-support/sql-pgsql/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-db-support/sql-sqlite/pom.xml b/sql-db-support/sql-sqlite/pom.xml index 41295eaa6..a48e1efd2 100644 --- a/sql-db-support/sql-sqlite/pom.xml +++ b/sql-db-support/sql-sqlite/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-extension/pom.xml b/sql-extension/pom.xml index 346f0ccfb..a4aace9d1 100644 --- a/sql-extension/pom.xml +++ b/sql-extension/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 sql-extension diff --git a/sql-extension/sql-kt-springboot-starter/pom.xml b/sql-extension/sql-kt-springboot-starter/pom.xml index 322677266..f668c504d 100644 --- a/sql-extension/sql-kt-springboot-starter/pom.xml +++ b/sql-extension/sql-kt-springboot-starter/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-extension/sql-springboot-starter/pom.xml b/sql-extension/sql-springboot-starter/pom.xml index 26272c22d..81a07e64a 100644 --- a/sql-extension/sql-springboot-starter/pom.xml +++ b/sql-extension/sql-springboot-starter/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-platform/pom.xml b/sql-platform/pom.xml index 013657f59..088df116c 100644 --- a/sql-platform/pom.xml +++ b/sql-platform/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 sql-platform diff --git a/sql-platform/sql-api4j/pom.xml b/sql-platform/sql-api4j/pom.xml index ac8703d87..b4f376525 100644 --- a/sql-platform/sql-api4j/pom.xml +++ b/sql-platform/sql-api4j/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable.java index d4ba2cc4f..c6a424e1c 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable.java @@ -75,17 +75,6 @@ default BigDecimal sumBigDecimalOrNull(Property - * @return - */ - default BigDecimal sumBigDecimalNotNull(Property column) { - return getEntityQueryable().sumBigDecimalNotNull(EasyLambdaUtil.getPropertyName(column)); - } - default BigDecimal sumBigDecimalOrDefault(Property column, BigDecimal def) { return getEntityQueryable().sumBigDecimalOrDefault(EasyLambdaUtil.getPropertyName(column), def); } @@ -142,13 +131,6 @@ default TResult avgOrDefault(Pr return getEntityQueryable().avgOrDefault(EasyLambdaUtil.getPropertyName(column), def, resultClass); } - - default Integer lenOrNull(Property column) { - return lenOrDefault(column, null); - } - - Integer lenOrDefault(Property column, Integer def); - /** * 对当前表达式返回自定义select列 * @@ -171,6 +153,7 @@ default Integer lenOrNull(Property column) { /** * 设置返回对象,返回对象会根据selectExpression映射相同列名 + * 多次select会在前一次基础上进行对上次结果进行匿名表处理 * * @param resultClass * @param selectExpression diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable2.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable2.java index d7c463d0f..0bb76bbcd 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable2.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable2.java @@ -1,11 +1,9 @@ package com.easy.query.api4j.select; -import com.easy.query.api4j.sql.SQLColumnAsSelector; -import com.easy.query.api4j.sql.SQLColumnResultSelector; -import com.easy.query.api4j.sql.SQLColumnSelector; -import com.easy.query.api4j.sql.SQLGroupBySelector; -import com.easy.query.api4j.sql.SQLWherePredicate; +import com.easy.query.api4j.sql.*; +import com.easy.query.api4j.sql.impl.*; import com.easy.query.core.api.client.EasyQueryClient; +import com.easy.query.core.basic.api.select.ClientQueryable2; import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression2; @@ -22,6 +20,8 @@ * @Date: 2023/2/6 22:42 */ public interface Queryable2 extends Queryable { + ClientQueryable2 getClientQueryable2(); + Queryable3 leftJoin(Class joinClass, SQLExpression3, SQLWherePredicate, SQLWherePredicate> on); Queryable3 leftJoin(Queryable joinQueryable, SQLExpression3, SQLWherePredicate, SQLWherePredicate> on); @@ -51,10 +51,18 @@ default Queryable2 where(SQLExpression1> whereExpr Queryable2 where(boolean condition, SQLExpression1> whereExpression); default Queryable2 where(SQLExpression2, SQLWherePredicate> whereExpression) { - return where(true, whereExpression); + getClientQueryable2().where((wherePredicate1, wherePredicate2) -> { + whereExpression.apply(new SQLWherePredicateImpl<>(wherePredicate1), new SQLWherePredicateImpl<>(wherePredicate2)); + }); + return this; } - Queryable2 where(boolean condition, SQLExpression2, SQLWherePredicate> whereExpression); + default Queryable2 where(boolean condition, SQLExpression2, SQLWherePredicate> whereExpression) { + getClientQueryable2().where(condition, (wherePredicate1, wherePredicate2) -> { + whereExpression.apply(new SQLWherePredicateImpl<>(wherePredicate1), new SQLWherePredicateImpl<>(wherePredicate2)); + }); + return this; + } //endregion @@ -71,67 +79,142 @@ default Queryable2 where(SQLExpression2, SQLWhereP * @return */ default BigDecimal sumBigDecimalOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, null); + return getClientQueryable2().sumBigDecimalOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); } - /** - * 防止溢出 - * - * @param columnSelectorExpression - * @param - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def); + default BigDecimal sumBigDecimalOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable2().sumBigDecimalOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); + } default TMember sumOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return sumOrDefault(columnSelectorExpression, null); + return getClientQueryable2().sumOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); } - TMember sumOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default TMember sumOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable2().sumOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); + } default TMember maxOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return maxOrDefault(columnSelectorExpression, null); + return getClientQueryable2().maxOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); } - TMember maxOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default TMember maxOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable2().maxOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); + } default TMember minOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return minOrDefault(columnSelectorExpression, null); + return getClientQueryable2().minOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); + } + + default TMember minOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable2().minOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); } - TMember minOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default Double avgOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable2().avgOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); + } - default TMember avgOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default BigDecimal avgBigDecimalOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable2().avgBigDecimalOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); } - TMember avgOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default Float avgFloatOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable2().avgFloatOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }); + } + + default Double avgOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, Double def) { + return getClientQueryable2().avgOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); + } - default Integer lenOrNull(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default BigDecimal avgBigDecimalOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable2().avgBigDecimalOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); + } + + default Float avgFloatOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, Float def) { + return getClientQueryable2().avgFloatOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def); + } + + default TResult avgOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { + return getClientQueryable2().avgOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); + }, def, resultClass); } - Integer lenOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, Integer def); //endregion //region group @Override - default Queryable2 groupBy(SQLExpression1> selectExpression) { + default Queryable2 groupBy(SQLExpression1> selectExpression) { return groupBy(true, selectExpression); } @Override - Queryable2 groupBy(boolean condition, SQLExpression1> selectExpression); + Queryable2 groupBy(boolean condition, SQLExpression1> selectExpression); default Queryable2 groupBy(SQLExpression2, SQLGroupBySelector> selectExpression) { - return groupBy(true, selectExpression); + getClientQueryable2().groupBy((selector1, selector2) -> { + selectExpression.apply(new SQLGroupBySelectorImpl<>(selector1), new SQLGroupBySelectorImpl<>(selector2)); + }); + return this; } - Queryable2 groupBy(boolean condition, SQLExpression2, SQLGroupBySelector> selectExpression); + default Queryable2 groupBy(boolean condition, SQLExpression2, SQLGroupBySelector> selectExpression) { + getClientQueryable2().groupBy(condition, (selector1, selector2) -> { + selectExpression.apply(new SQLGroupBySelectorImpl<>(selector1), new SQLGroupBySelectorImpl<>(selector2)); + }); + return this; + } + + @Override + default Queryable2 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + Queryable2 having(boolean condition, SQLExpression1> predicateExpression); + + default Queryable2 having(SQLExpression2, SQLWhereAggregatePredicate> predicateExpression) { + getClientQueryable2().having((predicate1, predicate2) -> { + predicateExpression.apply(new SQLWhereAggregatePredicateImpl<>(predicate1), new SQLWhereAggregatePredicateImpl<>(predicate2)); + }); + return this; + } + + default Queryable2 having(boolean condition, SQLExpression2, SQLWhereAggregatePredicate> predicateExpression) { + getClientQueryable2().having(condition, (predicate1, predicate2) -> { + predicateExpression.apply(new SQLWhereAggregatePredicateImpl<>(predicate1), new SQLWhereAggregatePredicateImpl<>(predicate2)); + }); + return this; + } //endregion //region order @@ -144,10 +227,18 @@ default Queryable2 orderByAsc(SQLExpression1> sele Queryable2 orderByAsc(boolean condition, SQLExpression1> selectExpression); default Queryable2 orderByAsc(SQLExpression2, SQLColumnSelector> selectExpression) { - return orderByAsc(true, selectExpression); + getClientQueryable2().orderByAsc((selector1, selector2) -> { + selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2)); + }); + return this; } - Queryable2 orderByAsc(boolean condition, SQLExpression2, SQLColumnSelector> selectExpression); + default Queryable2 orderByAsc(boolean condition, SQLExpression2, SQLColumnSelector> selectExpression) { + getClientQueryable2().orderByAsc(condition, (selector1, selector2) -> { + selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2)); + }); + return this; + } @Override default Queryable2 orderByDesc(SQLExpression1> selectExpression) { @@ -158,10 +249,18 @@ default Queryable2 orderByDesc(SQLExpression1> sel Queryable2 orderByDesc(boolean condition, SQLExpression1> selectExpression); default Queryable2 orderByDesc(SQLExpression2, SQLColumnSelector> selectExpression) { - return orderByDesc(true, selectExpression); + getClientQueryable2().orderByDesc((selector1, selector2) -> { + selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2)); + }); + return this; } - Queryable2 orderByDesc(boolean condition, SQLExpression2, SQLColumnSelector> selectExpression); + default Queryable2 orderByDesc(boolean condition, SQLExpression2, SQLColumnSelector> selectExpression) { + getClientQueryable2().orderByDesc(condition, (selector1, selector2) -> { + selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2)); + }); + return this; + } //endregion //region limit diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable3.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable3.java index 5937fab2f..691fe518e 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable3.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable3.java @@ -1,11 +1,10 @@ package com.easy.query.api4j.select; -import com.easy.query.api4j.sql.SQLColumnAsSelector; -import com.easy.query.api4j.sql.SQLColumnResultSelector; -import com.easy.query.api4j.sql.SQLColumnSelector; -import com.easy.query.api4j.sql.SQLGroupBySelector; -import com.easy.query.api4j.sql.SQLWherePredicate; +import com.easy.query.api4j.sql.*; +import com.easy.query.api4j.sql.impl.SQLColumnResultSelectorImpl; +import com.easy.query.api4j.sql.impl.SQLWhereAggregatePredicateImpl; import com.easy.query.core.api.client.EasyQueryClient; +import com.easy.query.core.basic.api.select.ClientQueryable3; import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression3; @@ -23,7 +22,9 @@ */ public interface Queryable3 extends Queryable { - Queryable4 leftJoin(Class joinClass, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> on); + ClientQueryable3 getClientQueryable3(); + + Queryable4 leftJoin(Class joinClass, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> on); Queryable4 leftJoin(Queryable joinQueryable, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> on); Queryable4 rightJoin(Class joinClass, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> on); Queryable4 rightJoin(Queryable joinQueryable, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> on); @@ -65,51 +66,95 @@ default Queryable3 where(SQLExpression3, SQLWh * @return */ default BigDecimal sumBigDecimalOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, null); + return getClientQueryable3().sumBigDecimalOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); } - /** - * 防止溢出 - * - * @param columnSelectorExpression - * @param - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def); + default BigDecimal sumBigDecimalOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable3().sumBigDecimalOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); + } default TMember sumOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return sumOrDefault(columnSelectorExpression, null); + return getClientQueryable3().sumOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); } - TMember sumOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default TMember sumOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable3().sumOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); + } default TMember maxOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return maxOrDefault(columnSelectorExpression, null); + return getClientQueryable3().maxOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); } - TMember maxOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default TMember maxOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable3().maxOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); + } default TMember minOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return minOrDefault(columnSelectorExpression, null); + return getClientQueryable3().minOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); + } + + default TMember minOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable3().minOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); } - TMember minOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default Double avgOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable3().avgOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); + } - default TMember avgOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default BigDecimal avgBigDecimalOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable3().avgBigDecimalOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); } - TMember avgOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default Float avgFloatOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable3().avgFloatOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }); + } - default Integer lenOrNull(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Double avgOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Double def) { + return getClientQueryable3().avgOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); } - Integer lenOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Integer def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable3().avgBigDecimalOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); + } + + default Float avgFloatOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Float def) { + return getClientQueryable3().avgFloatOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def); + } + + default TResult avgOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { + return getClientQueryable3().avgOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); + }, def, resultClass); + } //endregion //region group @@ -120,12 +165,36 @@ default Queryable3 groupBy(SQLExpression1> se @Override Queryable3 groupBy(boolean condition, SQLExpression1> selectExpression); + default Queryable3 groupBy(SQLExpression3, SQLGroupBySelector, SQLGroupBySelector> selectExpression) { return groupBy(true, selectExpression); } Queryable3 groupBy(boolean condition, SQLExpression3, SQLGroupBySelector, SQLGroupBySelector> selectExpression); + + @Override + default Queryable3 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + Queryable3 having(boolean condition, SQLExpression1> predicateExpression); + + default Queryable3 having(SQLExpression3, SQLWhereAggregatePredicate, SQLWhereAggregatePredicate> predicateExpression) { + getClientQueryable3().having((predicate1, predicate2, predicate3) -> { + predicateExpression.apply(new SQLWhereAggregatePredicateImpl<>(predicate1), new SQLWhereAggregatePredicateImpl<>(predicate2), new SQLWhereAggregatePredicateImpl<>(predicate3)); + }); + return this; + } + + default Queryable3 having(boolean condition, SQLExpression3, SQLWhereAggregatePredicate, SQLWhereAggregatePredicate> predicateExpression) { + getClientQueryable3().having(condition, (predicate1, predicate2, predicate3) -> { + predicateExpression.apply(new SQLWhereAggregatePredicateImpl<>(predicate1), new SQLWhereAggregatePredicateImpl<>(predicate2), new SQLWhereAggregatePredicateImpl<>(predicate3)); + }); + return this; + } + //endregion //region order @Override @@ -135,6 +204,7 @@ default Queryable3 orderByAsc(SQLExpression1> @Override Queryable3 orderByAsc(boolean condition, SQLExpression1> selectExpression); + default Queryable3 orderByAsc(SQLExpression3, SQLColumnSelector, SQLColumnSelector> selectExpression) { return orderByAsc(true, selectExpression); } diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable4.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable4.java index b72f2c4e2..8dd41bfe8 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable4.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/Queryable4.java @@ -1,11 +1,10 @@ package com.easy.query.api4j.select; -import com.easy.query.api4j.sql.SQLColumnAsSelector; -import com.easy.query.api4j.sql.SQLColumnResultSelector; -import com.easy.query.api4j.sql.SQLColumnSelector; -import com.easy.query.api4j.sql.SQLGroupBySelector; -import com.easy.query.api4j.sql.SQLWherePredicate; +import com.easy.query.api4j.sql.*; +import com.easy.query.api4j.sql.impl.SQLColumnResultSelectorImpl; +import com.easy.query.api4j.sql.impl.SQLWhereAggregatePredicateImpl; import com.easy.query.core.api.client.EasyQueryClient; +import com.easy.query.core.basic.api.select.ClientQueryable4; import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression4; @@ -14,29 +13,34 @@ import java.util.function.Function; /** + * @author xuejiaming * @FileName: Queryable4.java * @Description: 文件说明 * @Date: 2023/3/9 12:10 - * @author xuejiaming */ -public interface Queryable4 extends Queryable { +public interface Queryable4 extends Queryable { + ClientQueryable4 getClientQueryable4(); + //region where - default Queryable4 whereObject(Object object){ - return whereObject(true,object); + default Queryable4 whereObject(Object object) { + return whereObject(true, object); } - Queryable4 whereObject(boolean condition, Object object); + + Queryable4 whereObject(boolean condition, Object object); + @Override - default Queryable4 where(SQLExpression1> whereExpression) { + default Queryable4 where(SQLExpression1> whereExpression) { return where(true, whereExpression); } @Override - Queryable4 where(boolean condition, SQLExpression1> whereExpression); - default Queryable4 where(SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> whereExpression) { + Queryable4 where(boolean condition, SQLExpression1> whereExpression); + + default Queryable4 where(SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> whereExpression) { return where(true, whereExpression); } - Queryable4 where(boolean condition, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> whereExpression); + Queryable4 where(boolean condition, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> whereExpression); //endregion //region select @@ -53,135 +57,207 @@ default Queryable4 where(SQLExpression4, SQLW * @return */ default BigDecimal sumBigDecimalOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, null); + return getClientQueryable4().sumBigDecimalOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); } - /** - * 防止溢出 - * - * @param columnSelectorExpression - * @param - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def); + default BigDecimal sumBigDecimalOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable4().sumBigDecimalOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); + } default TMember sumOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return sumOrDefault(columnSelectorExpression, null); + return getClientQueryable4().sumOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); } - TMember sumOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default TMember sumOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable4().sumOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); + } default TMember maxOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return maxOrDefault(columnSelectorExpression, null); + return getClientQueryable4().maxOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); } - TMember maxOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default TMember maxOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable4().maxOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); + } default TMember minOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return minOrDefault(columnSelectorExpression, null); + return getClientQueryable4().minOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); + } + + default TMember minOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable4().minOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); + } + + default Double avgOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable4().avgOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); } - TMember minOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable4().avgBigDecimalOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); + } + + default Float avgFloatOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { + return getClientQueryable4().avgFloatOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }); + } - default TMember avgOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Double def) { + return getClientQueryable4().avgOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); } - TMember avgOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable4().avgBigDecimalOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); + } - default Integer lenOrNull(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Float avgFloatOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Float def) { + return getClientQueryable4().avgFloatOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def); } - Integer lenOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Integer def); + default TResult avgOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { + return getClientQueryable4().avgOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); + }, def, resultClass); + } //endregion //region group @Override - default Queryable4 groupBy(SQLExpression1> selectExpression) { + default Queryable4 groupBy(SQLExpression1> selectExpression) { return groupBy(true, selectExpression); } @Override - Queryable4 groupBy(boolean condition, SQLExpression1> selectExpression); - default Queryable4 groupBy(SQLExpression4, SQLGroupBySelector, SQLGroupBySelector, SQLGroupBySelector> selectExpression) { + Queryable4 groupBy(boolean condition, SQLExpression1> selectExpression); + + default Queryable4 groupBy(SQLExpression4, SQLGroupBySelector, SQLGroupBySelector, SQLGroupBySelector> selectExpression) { return groupBy(true, selectExpression); } - Queryable4 groupBy(boolean condition, SQLExpression4, SQLGroupBySelector, SQLGroupBySelector, SQLGroupBySelector> selectExpression); + Queryable4 groupBy(boolean condition, SQLExpression4, SQLGroupBySelector, SQLGroupBySelector, SQLGroupBySelector> selectExpression); + + + @Override + default Queryable4 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + Queryable4 having(boolean condition, SQLExpression1> predicateExpression); + + default Queryable4 having(SQLExpression4, SQLWhereAggregatePredicate, SQLWhereAggregatePredicate, SQLWhereAggregatePredicate> predicateExpression) { + getClientQueryable4().having((predicate1, predicate2, predicate3, predicate4) -> { + predicateExpression.apply(new SQLWhereAggregatePredicateImpl<>(predicate1), new SQLWhereAggregatePredicateImpl<>(predicate2), new SQLWhereAggregatePredicateImpl<>(predicate3), new SQLWhereAggregatePredicateImpl<>(predicate4)); + }); + return this; + } + + default Queryable4 having(boolean condition, SQLExpression4, SQLWhereAggregatePredicate, SQLWhereAggregatePredicate, SQLWhereAggregatePredicate> predicateExpression) { + getClientQueryable4().having(condition, (predicate1, predicate2, predicate3, predicate4) -> { + predicateExpression.apply(new SQLWhereAggregatePredicateImpl<>(predicate1), new SQLWhereAggregatePredicateImpl<>(predicate2), new SQLWhereAggregatePredicateImpl<>(predicate3), new SQLWhereAggregatePredicateImpl<>(predicate4)); + }); + return this; + } //endregion //region order @Override - default Queryable4 orderByAsc(SQLExpression1> selectExpression) { + default Queryable4 orderByAsc(SQLExpression1> selectExpression) { return orderByAsc(true, selectExpression); } @Override - Queryable4 orderByAsc(boolean condition, SQLExpression1> selectExpression); - default Queryable4 orderByAsc(SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression) { + Queryable4 orderByAsc(boolean condition, SQLExpression1> selectExpression); + + default Queryable4 orderByAsc(SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression) { return orderByAsc(true, selectExpression); } - Queryable4 orderByAsc(boolean condition, SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression); + Queryable4 orderByAsc(boolean condition, SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression); @Override - default Queryable4 orderByDesc(SQLExpression1> selectExpression) { + default Queryable4 orderByDesc(SQLExpression1> selectExpression) { return orderByDesc(true, selectExpression); } @Override - Queryable4 orderByDesc(boolean condition, SQLExpression1> selectExpression); - default Queryable4 orderByDesc(SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression) { + Queryable4 orderByDesc(boolean condition, SQLExpression1> selectExpression); + + default Queryable4 orderByDesc(SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression) { return orderByDesc(true, selectExpression); } - Queryable4 orderByDesc(boolean condition, SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression); + Queryable4 orderByDesc(boolean condition, SQLExpression4, SQLColumnSelector, SQLColumnSelector, SQLColumnSelector> selectExpression); //endregion //region limit @Override - default Queryable4 limit(long rows) { + default Queryable4 limit(long rows) { return limit(true, rows); } @Override - default Queryable4 limit(boolean condition, long rows) { + default Queryable4 limit(boolean condition, long rows) { return limit(condition, 0, rows); } @Override - default Queryable4 limit(long offset, long rows) { + default Queryable4 limit(long offset, long rows) { return limit(true, offset, rows); } @Override - Queryable4 limit(boolean condition, long offset, long rows); + Queryable4 limit(boolean condition, long offset, long rows); - default Queryable4 distinct() { + default Queryable4 distinct() { return distinct(true); } - Queryable4 distinct(boolean condition); + Queryable4 distinct(boolean condition); //endregion @Override - Queryable4 disableLogicDelete(); + Queryable4 disableLogicDelete(); @Override - Queryable4 enableLogicDelete(); + Queryable4 enableLogicDelete(); + @Override - Queryable4 useLogicDelete(boolean enable); + Queryable4 useLogicDelete(boolean enable); @Override - Queryable4 noInterceptor(); + Queryable4 noInterceptor(); + @Override - Queryable4 useInterceptor(); + Queryable4 useInterceptor(); /** * 自动将查询结果集合全部添加到当前上下文追踪中,如果当前查询结果十分庞大,并且更新数据只有个别条数,建议不要使用 @@ -190,27 +266,34 @@ default Queryable4 distinct() { * @return */ @Override - Queryable4 asTracking(); + Queryable4 asTracking(); + @Override - Queryable4 asNoTracking(); + Queryable4 asNoTracking(); + @Override - Queryable4 queryLargeColumn(boolean queryLarge); + Queryable4 queryLargeColumn(boolean queryLarge); + @Override - Queryable4 useShardingConfigure(int maxShardingQueryLimit, ConnectionModeEnum connectionMode); + Queryable4 useShardingConfigure(int maxShardingQueryLimit, ConnectionModeEnum connectionMode); + @Override - Queryable4 useMaxShardingQueryLimit(int maxShardingQueryLimit); + Queryable4 useMaxShardingQueryLimit(int maxShardingQueryLimit); + @Override - Queryable4 useConnectionMode(ConnectionModeEnum connectionMode); + Queryable4 useConnectionMode(ConnectionModeEnum connectionMode); + /** * 将当前表达式最近的一张表的表名修改成 {@param tableName} * 如果当前最近的表是正常的数据库表名,那么直接将表名改写 * 如果当前最近的表是匿名表比如嵌套queryable的表那么将alias改成对应的表名 + * * @param tableName * @return */ @Override - default Queryable4 asTable(String tableName){ - return asTable(old->tableName); + default Queryable4 asTable(String tableName) { + return asTable(old -> tableName); } /** diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable.java index cf0c4cc0c..ab0156dfa 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable.java @@ -12,13 +12,11 @@ import com.easy.query.core.basic.api.select.ClientQueryable2; import com.easy.query.core.basic.jdbc.parameter.ToSQLContext; import com.easy.query.core.enums.sharding.ConnectionModeEnum; -import com.easy.query.core.expression.lambda.Property; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression2; import com.easy.query.core.expression.segment.ColumnSegment; import com.easy.query.core.expression.sql.builder.EntityQueryExpressionBuilder; import com.easy.query.core.util.EasyCollectionUtil; -import com.easy.query.core.util.EasyLambdaUtil; import java.util.Collection; import java.util.List; @@ -73,11 +71,6 @@ public boolean all(SQLExpression1> whereExpression) { }); } - @Override - public Integer lenOrDefault(Property column, Integer def) { - return entityQueryable.lenOrDefault(EasyLambdaUtil.getPropertyName(column), def); - } - @Override public TR firstOrNull(Class resultClass) { diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable2.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable2.java index dcec2880a..82f61b199 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable2.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable2.java @@ -5,16 +5,8 @@ import com.easy.query.api4j.select.Queryable3; import com.easy.query.api4j.select.impl.EasyQueryable; import com.easy.query.api4j.select.impl.EasyQueryable3; -import com.easy.query.api4j.sql.SQLColumnAsSelector; -import com.easy.query.api4j.sql.SQLColumnResultSelector; -import com.easy.query.api4j.sql.SQLColumnSelector; -import com.easy.query.api4j.sql.SQLGroupBySelector; -import com.easy.query.api4j.sql.SQLWherePredicate; -import com.easy.query.api4j.sql.impl.SQLColumnAsSelectorImpl; -import com.easy.query.api4j.sql.impl.SQLColumnResultSelectorImpl; -import com.easy.query.api4j.sql.impl.SQLColumnSelectorImpl; -import com.easy.query.api4j.sql.impl.SQLGroupBySelectorImpl; -import com.easy.query.api4j.sql.impl.SQLWherePredicateImpl; +import com.easy.query.api4j.sql.*; +import com.easy.query.api4j.sql.impl.*; import com.easy.query.core.basic.api.select.ClientQueryable; import com.easy.query.core.basic.api.select.ClientQueryable2; import com.easy.query.core.basic.api.select.ClientQueryable3; @@ -23,7 +15,6 @@ import com.easy.query.core.expression.lambda.SQLExpression2; import com.easy.query.core.expression.lambda.SQLExpression3; -import java.math.BigDecimal; import java.util.function.Function; /** @@ -42,6 +33,11 @@ public AbstractQueryable2(ClientQueryable2 entityQueryable2) { this.entityQueryable2 = entityQueryable2; } + @Override + public ClientQueryable2 getClientQueryable2() { + return entityQueryable2; + } + @Override public Queryable3 leftJoin(Class joinClass, SQLExpression3, SQLWherePredicate, SQLWherePredicate> on) { ClientQueryable3 entityQueryable3 = entityQueryable2.leftJoin(joinClass, (wherePredicate1, wherePredicate2, wherePredicate3) -> { @@ -102,16 +98,6 @@ public Queryable2 where(boolean condition, SQLExpression1 where(boolean condition, SQLExpression2, SQLWherePredicate> whereExpression) { - if (condition) { - entityQueryable2.where((wherePredicate1, wherePredicate2) -> { - whereExpression.apply(new SQLWherePredicateImpl<>(wherePredicate1), new SQLWherePredicateImpl<>(wherePredicate2)); - }); - } - return this; - } - @Override public Queryable select(Class resultClass, SQLExpression2, SQLColumnAsSelector> selectExpression) { @@ -121,78 +107,14 @@ public Queryable select(Class resultClass, SQLExpression2(select); } - @Override - public BigDecimal sumBigDecimalOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { - - return entityQueryable2.sumBigDecimalOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember sumOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.sumOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember maxOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.maxOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember minOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.minOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember avgOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.avgOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public Integer lenOrDefault(SQLExpression2, SQLColumnResultSelector> columnSelectorExpression, Integer def) { - return entityQueryable2.lenOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2)); - }, def); - } - @Override public Queryable2 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); return this; } - - @Override - public Queryable2 orderByAsc(boolean condition, SQLExpression2, SQLColumnSelector> selectExpression) { - if (condition) { - entityQueryable2.orderByAsc((selector1, selector2) -> { - selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2)); - }); - } - return this; - } - @Override public Queryable2 orderByDesc(boolean condition, SQLExpression1> selectExpression) { - super.orderByAsc(condition, selectExpression); - return this; - } - - @Override - public Queryable2 orderByDesc(boolean condition, SQLExpression2, SQLColumnSelector> selectExpression) { - if (condition) { - entityQueryable2.orderByDesc((selector1, selector2) -> { - selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2)); - }); - } + super.orderByDesc(condition, selectExpression); return this; } @@ -203,12 +125,8 @@ public Queryable2 groupBy(boolean condition, SQLExpression1 groupBy(boolean condition, SQLExpression2, SQLGroupBySelector> selectExpression) { - if (condition) { - entityQueryable2.groupBy((selector1, selector2) -> { - selectExpression.apply(new SQLGroupBySelectorImpl<>(selector1), new SQLGroupBySelectorImpl<>(selector2)); - }); - } + public Queryable2 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); return this; } diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable3.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable3.java index f8e82da4b..7c759c0f7 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable3.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable3.java @@ -5,13 +5,8 @@ import com.easy.query.api4j.select.Queryable4; import com.easy.query.api4j.select.impl.EasyQueryable; import com.easy.query.api4j.select.impl.EasyQueryable4; -import com.easy.query.api4j.sql.SQLColumnAsSelector; -import com.easy.query.api4j.sql.SQLColumnResultSelector; -import com.easy.query.api4j.sql.SQLColumnSelector; -import com.easy.query.api4j.sql.SQLGroupBySelector; -import com.easy.query.api4j.sql.SQLWherePredicate; +import com.easy.query.api4j.sql.*; import com.easy.query.api4j.sql.impl.SQLColumnAsSelectorImpl; -import com.easy.query.api4j.sql.impl.SQLColumnResultSelectorImpl; import com.easy.query.api4j.sql.impl.SQLColumnSelectorImpl; import com.easy.query.api4j.sql.impl.SQLGroupBySelectorImpl; import com.easy.query.api4j.sql.impl.SQLWherePredicateImpl; @@ -23,7 +18,6 @@ import com.easy.query.core.expression.lambda.SQLExpression3; import com.easy.query.core.expression.lambda.SQLExpression4; -import java.math.BigDecimal; import java.util.function.Function; /** @@ -42,6 +36,11 @@ public AbstractQueryable3(ClientQueryable3 entityQueryable3) { this.entityQueryable3 = entityQueryable3; } + @Override + public ClientQueryable3 getClientQueryable3() { + return entityQueryable3; + } + @Override public Queryable4 leftJoin(Class joinClass, SQLExpression4, SQLWherePredicate, SQLWherePredicate, SQLWherePredicate> on) { ClientQueryable4 entityQueryable4 = entityQueryable3.leftJoin(joinClass, (where1, where2, where3, where4) -> { @@ -120,52 +119,6 @@ public Queryable select(Class resultClass, SQLExpression3(select); } - @Override - public BigDecimal sumBigDecimalOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { - return entityQueryable3.sumBigDecimalOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember sumOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.sumOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember maxOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.maxOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember minOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.minOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember avgOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.avgOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public Integer lenOrDefault(SQLExpression3, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Integer def) { - return entityQueryable3.lenOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3)); - }, def); - } - @Override public Queryable3 groupBy(boolean condition, SQLExpression1> selectExpression) { super.groupBy(condition, selectExpression); @@ -182,6 +135,12 @@ public Queryable3 groupBy(boolean condition, SQLExpression3 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); + return this; + } + @Override public Queryable3 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); @@ -200,14 +159,14 @@ public Queryable3 orderByAsc(boolean condition, SQLExpression3 orderByDesc(boolean condition, SQLExpression1> selectExpression) { - super.orderByAsc(condition, selectExpression); + super.orderByDesc(condition, selectExpression); return this; } @Override public Queryable3 orderByDesc(boolean condition, SQLExpression3, SQLColumnSelector, SQLColumnSelector> selectExpression) { if (condition) { - entityQueryable3.orderByAsc((selector1, selector2, selector3) -> { + entityQueryable3.orderByDesc((selector1, selector2, selector3) -> { selectExpression.apply(new SQLColumnSelectorImpl<>(selector1), new SQLColumnSelectorImpl<>(selector2), new SQLColumnSelectorImpl<>(selector3)); }); } diff --git a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable4.java b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable4.java index 562916b9d..7c2fbc902 100644 --- a/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable4.java +++ b/sql-platform/sql-api4j/src/main/java/com/easy/query/api4j/select/abstraction/AbstractQueryable4.java @@ -3,13 +3,8 @@ import com.easy.query.api4j.select.Queryable; import com.easy.query.api4j.select.Queryable4; import com.easy.query.api4j.select.impl.EasyQueryable; -import com.easy.query.api4j.sql.SQLColumnAsSelector; -import com.easy.query.api4j.sql.SQLColumnResultSelector; -import com.easy.query.api4j.sql.SQLColumnSelector; -import com.easy.query.api4j.sql.SQLGroupBySelector; -import com.easy.query.api4j.sql.SQLWherePredicate; +import com.easy.query.api4j.sql.*; import com.easy.query.api4j.sql.impl.SQLColumnAsSelectorImpl; -import com.easy.query.api4j.sql.impl.SQLColumnResultSelectorImpl; import com.easy.query.api4j.sql.impl.SQLColumnSelectorImpl; import com.easy.query.api4j.sql.impl.SQLGroupBySelectorImpl; import com.easy.query.api4j.sql.impl.SQLWherePredicateImpl; @@ -19,7 +14,6 @@ import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression4; -import java.math.BigDecimal; import java.util.function.Function; /** @@ -37,6 +31,11 @@ public AbstractQueryable4(ClientQueryable4 entityQueryable4) { this.entityQueryable4 = entityQueryable4; } + @Override + public ClientQueryable4 getClientQueryable4() { + return entityQueryable4; + } + @Override public Queryable4 whereObject(boolean condition, Object object) { super.whereObject(condition, object); @@ -67,48 +66,6 @@ public Queryable select(Class resultClass, SQLExpression4(select); } - @Override - public BigDecimal sumBigDecimalOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, BigDecimal def) { - return entityQueryable4.sumBigDecimalOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember sumOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.sumOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember maxOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.maxOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember minOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.minOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember avgOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.avgOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public Integer lenOrDefault(SQLExpression4, SQLColumnResultSelector, SQLColumnResultSelector, SQLColumnResultSelector> columnSelectorExpression, Integer def) { - return entityQueryable4.lenOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLColumnResultSelectorImpl<>(selector1), new SQLColumnResultSelectorImpl<>(selector2), new SQLColumnResultSelectorImpl<>(selector3), new SQLColumnResultSelectorImpl<>(selector4)); - }, def); - } - @Override public Queryable4 groupBy(boolean condition, SQLExpression1> selectExpression) { super.groupBy(condition, selectExpression); @@ -125,6 +82,12 @@ public Queryable4 groupBy(boolean condition, SQLExpression4 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); + return this; + } + @Override public Queryable4 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); @@ -143,7 +106,7 @@ public Queryable4 orderByAsc(boolean condition, SQLExpression4 orderByDesc(boolean condition, SQLExpression1> selectExpression) { - super.orderByAsc(condition, selectExpression); + super.orderByDesc(condition, selectExpression); return this; } diff --git a/sql-platform/sql-api4kt/pom.xml b/sql-platform/sql-api4kt/pom.xml index f9ea9f67d..d0a08ceca 100644 --- a/sql-platform/sql-api4kt/pom.xml +++ b/sql-platform/sql-api4kt/pom.xml @@ -6,7 +6,7 @@ com.easy-query easy-query-all - 0.9.36 + 0.9.37 ../../pom.xml diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable.java index 0a776e191..ffe41a359 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable.java @@ -70,17 +70,6 @@ default BigDecimal sumBigDecimalOrNull(KProperty1 - * @return - */ - default BigDecimal sumBigDecimalNotNull(KProperty1 column) { - return getEntityQueryable().sumBigDecimalNotNull(EasyKtLambdaUtil.getPropertyName(column)); - } - default BigDecimal sumBigDecimalOrDefault(KProperty1 column, BigDecimal def) { return getEntityQueryable().sumBigDecimalOrDefault(EasyKtLambdaUtil.getPropertyName(column), def); } @@ -137,13 +126,6 @@ default TResult avgOrDefault(KP return getEntityQueryable().avgOrDefault(EasyKtLambdaUtil.getPropertyName(column), def, resultClass); } - - default Integer lenOrNull(KProperty1 column) { - return lenOrDefault(column, null); - } - - Integer lenOrDefault(KProperty1 column, Integer def); - /** * 对当前表达式返回自定义select列 * diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable2.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable2.java index 00d0a7fea..9ea89c450 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable2.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable2.java @@ -1,11 +1,10 @@ package com.easy.query.api4kt.select; -import com.easy.query.api4kt.sql.SQLKtColumnAsSelector; -import com.easy.query.api4kt.sql.SQLKtColumnResultSelector; -import com.easy.query.api4kt.sql.SQLKtColumnSelector; -import com.easy.query.api4kt.sql.SQLKtGroupBySelector; -import com.easy.query.api4kt.sql.SQLKtWherePredicate; +import com.easy.query.api4kt.sql.*; +import com.easy.query.api4kt.sql.impl.SQLKtColumnResultSelectorImpl; +import com.easy.query.api4kt.sql.impl.SQLKtWhereAggregatePredicateImpl; import com.easy.query.core.api.client.EasyQueryClient; +import com.easy.query.core.basic.api.select.ClientQueryable2; import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression2; @@ -22,6 +21,8 @@ * @Date: 2023/2/6 22:42 */ public interface KtQueryable2 extends KtQueryable { + ClientQueryable2 getClientQueryable2(); + KtQueryable3 leftJoin(Class joinClass, SQLExpression3, SQLKtWherePredicate, SQLKtWherePredicate> on); KtQueryable3 leftJoin(KtQueryable joinQueryable, SQLExpression3, SQLKtWherePredicate, SQLKtWherePredicate> on); @@ -71,51 +72,95 @@ default KtQueryable2 where(SQLExpression2, SQLKt * @return */ default BigDecimal sumBigDecimalOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, null); + return getClientQueryable2().sumBigDecimalOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); } - /** - * 防止溢出 - * - * @param columnSelectorExpression - * @param - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def); + default BigDecimal sumBigDecimalOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable2().sumBigDecimalOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); + } default TMember sumOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumOrDefault(columnSelectorExpression, null); + return getClientQueryable2().sumOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); } - TMember sumOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default TMember sumOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable2().sumOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); + } default TMember maxOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return maxOrDefault(columnSelectorExpression, null); + return getClientQueryable2().maxOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); } - TMember maxOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default TMember maxOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable2().maxOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); + } default TMember minOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return minOrDefault(columnSelectorExpression, null); + return getClientQueryable2().minOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); + } + + default TMember minOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable2().minOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); + } + + default Double avgOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable2().avgOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); + } + + default BigDecimal avgBigDecimalOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable2().avgBigDecimalOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); } - TMember minOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default Float avgFloatOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable2().avgFloatOrNull((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }); + } - default TMember avgOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, Double def) { + return getClientQueryable2().avgOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); } - TMember avgOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable2().avgBigDecimalOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); + } - default Integer lenOrNull(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Float avgFloatOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, Float def) { + return getClientQueryable2().avgFloatOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def); } - Integer lenOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, Integer def); + default TResult avgOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { + return getClientQueryable2().avgOrDefault((selector1, selector2) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); + }, def, resultClass); + } //endregion //region group @@ -133,6 +178,28 @@ default KtQueryable2 groupBy(SQLExpression2, SQ KtQueryable2 groupBy(boolean condition, SQLExpression2, SQLKtGroupBySelector> selectExpression); + @Override + default KtQueryable2 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + KtQueryable2 having(boolean condition, SQLExpression1> predicateExpression); + + default KtQueryable2 having(SQLExpression2, SQLKtWhereAggregatePredicate> predicateExpression) { + getClientQueryable2().having((predicate1, predicate2) -> { + predicateExpression.apply(new SQLKtWhereAggregatePredicateImpl<>(predicate1), new SQLKtWhereAggregatePredicateImpl<>(predicate2)); + }); + return this; + } + + default KtQueryable2 having(boolean condition, SQLExpression2, SQLKtWhereAggregatePredicate> predicateExpression) { + getClientQueryable2().having(condition, (predicate1, predicate2) -> { + predicateExpression.apply(new SQLKtWhereAggregatePredicateImpl<>(predicate1), new SQLKtWhereAggregatePredicateImpl<>(predicate2)); + }); + return this; + } + //endregion //region order @Override diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable3.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable3.java index d634f79c6..13cb6db6b 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable3.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable3.java @@ -1,11 +1,10 @@ package com.easy.query.api4kt.select; -import com.easy.query.api4kt.sql.SQLKtColumnAsSelector; -import com.easy.query.api4kt.sql.SQLKtColumnResultSelector; -import com.easy.query.api4kt.sql.SQLKtColumnSelector; -import com.easy.query.api4kt.sql.SQLKtGroupBySelector; -import com.easy.query.api4kt.sql.SQLKtWherePredicate; +import com.easy.query.api4kt.sql.*; +import com.easy.query.api4kt.sql.impl.SQLKtColumnResultSelectorImpl; +import com.easy.query.api4kt.sql.impl.SQLKtWhereAggregatePredicateImpl; import com.easy.query.core.api.client.EasyQueryClient; +import com.easy.query.core.basic.api.select.ClientQueryable3; import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression3; @@ -22,6 +21,7 @@ * @Date: 2023/2/6 22:44 */ public interface KtQueryable3 extends KtQueryable { + ClientQueryable3 getClientQueryable3(); KtQueryable4 leftJoin(Class joinClass, SQLExpression4, SQLKtWherePredicate, SQLKtWherePredicate, SQLKtWherePredicate> on); @@ -72,51 +72,95 @@ default KtQueryable3 where(SQLExpression3, S * @return */ default BigDecimal sumBigDecimalOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, null); + return getClientQueryable3().sumBigDecimalOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); } - /** - * 防止溢出 - * - * @param columnSelectorExpression - * @param - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def); + default BigDecimal sumBigDecimalOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable3().sumBigDecimalOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); + } default TMember sumOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumOrDefault(columnSelectorExpression, null); + return getClientQueryable3().sumOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); } - TMember sumOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default TMember sumOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable3().sumOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); + } default TMember maxOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return maxOrDefault(columnSelectorExpression, null); + return getClientQueryable3().maxOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); } - TMember maxOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default TMember maxOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable3().maxOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); + } default TMember minOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return minOrDefault(columnSelectorExpression, null); + return getClientQueryable3().minOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); + } + + default TMember minOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable3().minOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); + } + + default Double avgOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable3().avgOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); } - TMember minOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable3().avgBigDecimalOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); + } + + default Float avgFloatOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable3().avgFloatOrNull((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }); + } - default TMember avgOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Double def) { + return getClientQueryable3().avgOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); } - TMember avgOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable3().avgBigDecimalOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); + } - default Integer lenOrNull(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Float avgFloatOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Float def) { + return getClientQueryable3().avgFloatOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def); } - Integer lenOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Integer def); + default TResult avgOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { + return getClientQueryable3().avgOrDefault((selector1, selector2, selector3) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); + }, def, resultClass); + } //endregion //region group @@ -134,6 +178,29 @@ default KtQueryable3 groupBy(SQLExpression3 KtQueryable3 groupBy(boolean condition, SQLExpression3, SQLKtGroupBySelector, SQLKtGroupBySelector> selectExpression); + + @Override + default KtQueryable3 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + KtQueryable3 having(boolean condition, SQLExpression1> predicateExpression); + + default KtQueryable3 having(SQLExpression3, SQLKtWhereAggregatePredicate, SQLKtWhereAggregatePredicate> predicateExpression) { + getClientQueryable3().having((predicate1, predicate2, predicate3) -> { + predicateExpression.apply(new SQLKtWhereAggregatePredicateImpl<>(predicate1), new SQLKtWhereAggregatePredicateImpl<>(predicate2), new SQLKtWhereAggregatePredicateImpl<>(predicate3)); + }); + return this; + } + + default KtQueryable3 having(boolean condition, SQLExpression3, SQLKtWhereAggregatePredicate, SQLKtWhereAggregatePredicate> predicateExpression) { + getClientQueryable3().having(condition, (predicate1, predicate2, predicate3) -> { + predicateExpression.apply(new SQLKtWhereAggregatePredicateImpl<>(predicate1), new SQLKtWhereAggregatePredicateImpl<>(predicate2), new SQLKtWhereAggregatePredicateImpl<>(predicate3)); + }); + return this; + } + //endregion //region order @Override diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable4.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable4.java index 54c7039fb..28466e199 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable4.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/KtQueryable4.java @@ -1,11 +1,10 @@ package com.easy.query.api4kt.select; -import com.easy.query.api4kt.sql.SQLKtColumnAsSelector; -import com.easy.query.api4kt.sql.SQLKtColumnResultSelector; -import com.easy.query.api4kt.sql.SQLKtColumnSelector; -import com.easy.query.api4kt.sql.SQLKtGroupBySelector; -import com.easy.query.api4kt.sql.SQLKtWherePredicate; +import com.easy.query.api4kt.sql.*; +import com.easy.query.api4kt.sql.impl.SQLKtColumnResultSelectorImpl; +import com.easy.query.api4kt.sql.impl.SQLKtWhereAggregatePredicateImpl; import com.easy.query.core.api.client.EasyQueryClient; +import com.easy.query.core.basic.api.select.ClientQueryable4; import com.easy.query.core.enums.sharding.ConnectionModeEnum; import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression4; @@ -20,6 +19,8 @@ * @Date: 2023/3/9 12:10 */ public interface KtQueryable4 extends KtQueryable { + ClientQueryable4 getClientQueryable4(); + //region where default KtQueryable4 whereObject(Object object) { return whereObject(true, object); @@ -56,51 +57,95 @@ default KtQueryable4 where(SQLExpression4 BigDecimal sumBigDecimalOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, null); + return getClientQueryable4().sumBigDecimalOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); } - /** - * 防止溢出 - * - * @param columnSelectorExpression - * @param - * @return - */ - default BigDecimal sumBigDecimalNotNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumBigDecimalOrDefault(columnSelectorExpression, BigDecimal.ZERO); - } - BigDecimal sumBigDecimalOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def); + default BigDecimal sumBigDecimalOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable4().sumBigDecimalOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); + } default TMember sumOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return sumOrDefault(columnSelectorExpression, null); + return getClientQueryable4().sumOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); } - TMember sumOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default TMember sumOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable4().sumOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); + } default TMember maxOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return maxOrDefault(columnSelectorExpression, null); + return getClientQueryable4().maxOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); } - TMember maxOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default TMember maxOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable4().maxOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); + } default TMember minOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return minOrDefault(columnSelectorExpression, null); + return getClientQueryable4().minOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); + } + + default TMember minOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { + return getClientQueryable4().minOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); + } + + default Double avgOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable4().avgOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); + } + + default BigDecimal avgBigDecimalOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable4().avgBigDecimalOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); } - TMember minOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default Float avgFloatOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { + return getClientQueryable4().avgFloatOrNull((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }); + } - default TMember avgOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return avgOrDefault(columnSelectorExpression, null); + default Double avgOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Double def) { + return getClientQueryable4().avgOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); } - TMember avgOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def); + default BigDecimal avgBigDecimalOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { + return getClientQueryable4().avgBigDecimalOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); + } - default Integer lenOrNull(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression) { - return lenOrDefault(columnSelectorExpression, null); + default Float avgFloatOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Float def) { + return getClientQueryable4().avgFloatOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def); } - Integer lenOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Integer def); + default TResult avgOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TResult def, Class resultClass) { + return getClientQueryable4().avgOrDefault((selector1, selector2, selector3, selector4) -> { + columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); + }, def, resultClass); + } //endregion @@ -119,6 +164,28 @@ default KtQueryable4 groupBy(SQLExpression4 groupBy(boolean condition, SQLExpression4, SQLKtGroupBySelector, SQLKtGroupBySelector, SQLKtGroupBySelector> selectExpression); + @Override + default KtQueryable4 having(SQLExpression1> predicateExpression) { + return having(true, predicateExpression); + } + + @Override + KtQueryable4 having(boolean condition, SQLExpression1> predicateExpression); + + default KtQueryable4 having(SQLExpression4, SQLKtWhereAggregatePredicate, SQLKtWhereAggregatePredicate, SQLKtWhereAggregatePredicate> predicateExpression) { + getClientQueryable4().having((predicate1, predicate2, predicate3, predicate4) -> { + predicateExpression.apply(new SQLKtWhereAggregatePredicateImpl<>(predicate1), new SQLKtWhereAggregatePredicateImpl<>(predicate2), new SQLKtWhereAggregatePredicateImpl<>(predicate3), new SQLKtWhereAggregatePredicateImpl<>(predicate4)); + }); + return this; + } + + default KtQueryable4 having(boolean condition, SQLExpression4, SQLKtWhereAggregatePredicate, SQLKtWhereAggregatePredicate, SQLKtWhereAggregatePredicate> predicateExpression) { + getClientQueryable4().having(condition, (predicate1, predicate2, predicate3, predicate4) -> { + predicateExpression.apply(new SQLKtWhereAggregatePredicateImpl<>(predicate1), new SQLKtWhereAggregatePredicateImpl<>(predicate2), new SQLKtWhereAggregatePredicateImpl<>(predicate3), new SQLKtWhereAggregatePredicateImpl<>(predicate4)); + }); + return this; + } + //endregion //region order @Override diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable.java index 8b4882b54..6882d35be 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable.java @@ -72,10 +72,6 @@ public boolean all(SQLExpression1> whereExpression) { whereExpression.apply(new SQLKtWherePredicateImpl<>(wherePredicate)); }); } - @Override - public Integer lenOrDefault(KProperty1 column, Integer def) { - return entityQueryable.lenOrDefault(EasyKtLambdaUtil.getPropertyName(column), def); - } @Override diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable2.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable2.java index 2a28899a4..4e5fbe123 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable2.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable2.java @@ -5,16 +5,8 @@ import com.easy.query.api4kt.select.KtQueryable3; import com.easy.query.api4kt.select.impl.EasyKtQueryable; import com.easy.query.api4kt.select.impl.EasyKtQueryable3; -import com.easy.query.api4kt.sql.SQLKtColumnAsSelector; -import com.easy.query.api4kt.sql.SQLKtColumnResultSelector; -import com.easy.query.api4kt.sql.SQLKtColumnSelector; -import com.easy.query.api4kt.sql.SQLKtGroupBySelector; -import com.easy.query.api4kt.sql.SQLKtWherePredicate; -import com.easy.query.api4kt.sql.impl.SQLKtColumnAsSelectorImpl; -import com.easy.query.api4kt.sql.impl.SQLKtColumnResultSelectorImpl; -import com.easy.query.api4kt.sql.impl.SQLKtColumnSelectorImpl; -import com.easy.query.api4kt.sql.impl.SQLKtGroupBySelectorImpl; -import com.easy.query.api4kt.sql.impl.SQLKtWherePredicateImpl; +import com.easy.query.api4kt.sql.*; +import com.easy.query.api4kt.sql.impl.*; import com.easy.query.core.basic.api.select.ClientQueryable; import com.easy.query.core.basic.api.select.ClientQueryable2; import com.easy.query.core.basic.api.select.ClientQueryable3; @@ -23,7 +15,6 @@ import com.easy.query.core.expression.lambda.SQLExpression2; import com.easy.query.core.expression.lambda.SQLExpression3; -import java.math.BigDecimal; import java.util.function.Function; /** @@ -42,6 +33,11 @@ public AbstractKtQueryable2(ClientQueryable2 entityQueryable2) { this.entityQueryable2 = entityQueryable2; } + @Override + public ClientQueryable2 getClientQueryable2() { + return entityQueryable2; + } + @Override public KtQueryable3 leftJoin(Class joinClass, SQLExpression3, SQLKtWherePredicate, SQLKtWherePredicate> on) { ClientQueryable3 entityQueryable3 = entityQueryable2.leftJoin(joinClass, (wherePredicate1, wherePredicate2, wherePredicate3) -> { @@ -121,49 +117,6 @@ public KtQueryable select(Class resultClass, SQLExpression2(select); } - @Override - public BigDecimal sumBigDecimalOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { - - return entityQueryable2.sumBigDecimalOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember sumOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.sumOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember maxOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.maxOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember minOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.minOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public TMember avgOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable2.avgOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); - }, def); - } - - @Override - public Integer lenOrDefault(SQLExpression2, SQLKtColumnResultSelector> columnSelectorExpression, Integer def) { - return entityQueryable2.lenOrDefault((selector1, selector2) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2)); - }, def); - } - @Override public KtQueryable2 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); @@ -182,7 +135,7 @@ public KtQueryable2 orderByAsc(boolean condition, SQLExpression2 orderByDesc(boolean condition, SQLExpression1> selectExpression) { - super.orderByAsc(condition, selectExpression); + super.orderByDesc(condition, selectExpression); return this; } @@ -212,6 +165,12 @@ public KtQueryable2 groupBy(boolean condition, SQLExpression2 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); + return this; + } + @Override public KtQueryable2 limit(boolean condition, long offset, long rows) { super.limit(condition, offset, rows); diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable3.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable3.java index 852cd5f46..e99ffa637 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable3.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable3.java @@ -5,13 +5,8 @@ import com.easy.query.api4kt.select.KtQueryable4; import com.easy.query.api4kt.select.impl.EasyKtQueryable; import com.easy.query.api4kt.select.impl.EasyKtQueryable4; -import com.easy.query.api4kt.sql.SQLKtColumnAsSelector; -import com.easy.query.api4kt.sql.SQLKtColumnResultSelector; -import com.easy.query.api4kt.sql.SQLKtColumnSelector; -import com.easy.query.api4kt.sql.SQLKtGroupBySelector; -import com.easy.query.api4kt.sql.SQLKtWherePredicate; +import com.easy.query.api4kt.sql.*; import com.easy.query.api4kt.sql.impl.SQLKtColumnAsSelectorImpl; -import com.easy.query.api4kt.sql.impl.SQLKtColumnResultSelectorImpl; import com.easy.query.api4kt.sql.impl.SQLKtColumnSelectorImpl; import com.easy.query.api4kt.sql.impl.SQLKtGroupBySelectorImpl; import com.easy.query.api4kt.sql.impl.SQLKtWherePredicateImpl; @@ -23,7 +18,6 @@ import com.easy.query.core.expression.lambda.SQLExpression3; import com.easy.query.core.expression.lambda.SQLExpression4; -import java.math.BigDecimal; import java.util.function.Function; /** @@ -42,6 +36,11 @@ public AbstractKtQueryable3(ClientQueryable3 entityQueryable3) { this.entityQueryable3 = entityQueryable3; } + @Override + public ClientQueryable3 getClientQueryable3() { + return entityQueryable3; + } + @Override public KtQueryable4 leftJoin(Class joinClass, SQLExpression4, SQLKtWherePredicate, SQLKtWherePredicate, SQLKtWherePredicate> on) { ClientQueryable4 entityQueryable4 = entityQueryable3.leftJoin(joinClass, (where1, where2, where3, where4) -> { @@ -119,53 +118,6 @@ public KtQueryable select(Class resultClass, SQLExpression3(select); } - - @Override - public BigDecimal sumBigDecimalOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { - return entityQueryable3.sumBigDecimalOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember sumOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.sumOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember maxOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.maxOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember minOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.minOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public TMember avgOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - - return entityQueryable3.avgOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); - }, def); - } - - @Override - public Integer lenOrDefault(SQLExpression3, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Integer def) { - return entityQueryable3.lenOrDefault((selector1, selector2, selector3) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3)); - }, def); - } - @Override public KtQueryable3 groupBy(boolean condition, SQLExpression1> selectExpression) { super.groupBy(condition, selectExpression); @@ -182,6 +134,12 @@ public KtQueryable3 groupBy(boolean condition, SQLExpression3 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); + return this; + } + @Override public KtQueryable3 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); @@ -200,14 +158,14 @@ public KtQueryable3 orderByAsc(boolean condition, SQLExpression3 orderByDesc(boolean condition, SQLExpression1> selectExpression) { - super.orderByAsc(condition, selectExpression); + super.orderByDesc(condition, selectExpression); return this; } @Override public KtQueryable3 orderByDesc(boolean condition, SQLExpression3, SQLKtColumnSelector, SQLKtColumnSelector> selectExpression) { if (condition) { - entityQueryable3.orderByAsc((selector1, selector2, selector3) -> { + entityQueryable3.orderByDesc((selector1, selector2, selector3) -> { selectExpression.apply(new SQLKtColumnSelectorImpl<>(selector1), new SQLKtColumnSelectorImpl<>(selector2), new SQLKtColumnSelectorImpl<>(selector3)); }); } diff --git a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable4.java b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable4.java index 56a969b65..199b04ddf 100644 --- a/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable4.java +++ b/sql-platform/sql-api4kt/src/main/java/com/easy/query/api4kt/select/abstraction/AbstractKtQueryable4.java @@ -3,13 +3,8 @@ import com.easy.query.api4kt.select.KtQueryable; import com.easy.query.api4kt.select.KtQueryable4; import com.easy.query.api4kt.select.impl.EasyKtQueryable; -import com.easy.query.api4kt.sql.SQLKtColumnAsSelector; -import com.easy.query.api4kt.sql.SQLKtColumnResultSelector; -import com.easy.query.api4kt.sql.SQLKtColumnSelector; -import com.easy.query.api4kt.sql.SQLKtGroupBySelector; -import com.easy.query.api4kt.sql.SQLKtWherePredicate; +import com.easy.query.api4kt.sql.*; import com.easy.query.api4kt.sql.impl.SQLKtColumnAsSelectorImpl; -import com.easy.query.api4kt.sql.impl.SQLKtColumnResultSelectorImpl; import com.easy.query.api4kt.sql.impl.SQLKtColumnSelectorImpl; import com.easy.query.api4kt.sql.impl.SQLKtGroupBySelectorImpl; import com.easy.query.api4kt.sql.impl.SQLKtWherePredicateImpl; @@ -19,7 +14,6 @@ import com.easy.query.core.expression.lambda.SQLExpression1; import com.easy.query.core.expression.lambda.SQLExpression4; -import java.math.BigDecimal; import java.util.function.Function; /** @@ -37,6 +31,11 @@ public AbstractKtQueryable4(ClientQueryable4 entityQueryable4) { this.entityQueryable4 = entityQueryable4; } + @Override + public ClientQueryable4 getClientQueryable4() { + return entityQueryable4; + } + @Override public KtQueryable4 whereObject(boolean condition, Object object) { super.whereObject(condition, object); @@ -67,48 +66,6 @@ public KtQueryable select(Class resultClass, SQLExpression4(select); } - @Override - public BigDecimal sumBigDecimalOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, BigDecimal def) { - return entityQueryable4.sumBigDecimalOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember sumOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.sumOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember maxOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.maxOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember minOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.minOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public TMember avgOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, TMember def) { - return entityQueryable4.avgOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); - }, def); - } - - @Override - public Integer lenOrDefault(SQLExpression4, SQLKtColumnResultSelector, SQLKtColumnResultSelector, SQLKtColumnResultSelector> columnSelectorExpression, Integer def) { - return entityQueryable4.lenOrDefault((selector1, selector2, selector3, selector4) -> { - columnSelectorExpression.apply(new SQLKtColumnResultSelectorImpl<>(selector1), new SQLKtColumnResultSelectorImpl<>(selector2), new SQLKtColumnResultSelectorImpl<>(selector3), new SQLKtColumnResultSelectorImpl<>(selector4)); - }, def); - } - @Override public KtQueryable4 groupBy(boolean condition, SQLExpression1> selectExpression) { super.groupBy(condition, selectExpression); @@ -125,6 +82,12 @@ public KtQueryable4 groupBy(boolean condition, SQLExpression4 having(boolean condition, SQLExpression1> predicateExpression) { + super.having(condition, predicateExpression); + return this; + } + @Override public KtQueryable4 orderByAsc(boolean condition, SQLExpression1> selectExpression) { super.orderByAsc(condition, selectExpression); @@ -143,7 +106,7 @@ public KtQueryable4 orderByAsc(boolean condition, SQLExpression4 @Override public KtQueryable4 orderByDesc(boolean condition, SQLExpression1> selectExpression) { - super.orderByAsc(condition, selectExpression); + super.orderByDesc(condition, selectExpression); return this; } diff --git a/sql-test/pom.xml b/sql-test/pom.xml index d4e19e0ac..57d6e2090 100644 --- a/sql-test/pom.xml +++ b/sql-test/pom.xml @@ -5,7 +5,7 @@ easy-query-all com.easy-query - 0.9.36 + 0.9.37 4.0.0 @@ -35,13 +35,13 @@ com.easy-query sql-h2 - 0.9.36 + 0.9.37 compile com.easy-query sql-api4j - 0.9.36 + 0.9.37 compile diff --git a/sql-test/src/main/java/com/easy/query/test/QueryTest.java b/sql-test/src/main/java/com/easy/query/test/QueryTest.java index 4d2fa6dc5..b15a68ed8 100644 --- a/sql-test/src/main/java/com/easy/query/test/QueryTest.java +++ b/sql-test/src/main/java/com/easy/query/test/QueryTest.java @@ -4,6 +4,7 @@ import com.easy.query.core.api.pagination.EasyPageResult; import com.easy.query.core.basic.api.select.ClientQueryable; import com.easy.query.core.basic.jdbc.parameter.DefaultToSQLContext; +import com.easy.query.core.enums.AggregatePredicateCompare; import com.easy.query.core.enums.SQLPredicateCompareEnum; import com.easy.query.core.exception.EasyQueryOrderByInvalidOperationException; import com.easy.query.core.exception.EasyQuerySQLCommandException; @@ -1674,7 +1675,7 @@ public void query90_1() { public void query91() { BigDecimal bigDecimal = easyQuery.queryable(BlogEntity.class) .where(o -> o.in(BlogEntity::getId, Arrays.asList("1", "2"))) - .sumBigDecimalNotNull(BlogEntity::getScore); + .sumBigDecimalOrDefault(BlogEntity::getScore, BigDecimal.ZERO); Assert.assertTrue(new BigDecimal("2.40").compareTo(bigDecimal) == 0); Assert.assertTrue(new BigDecimal("2.4").compareTo(bigDecimal) == 0); @@ -1825,33 +1826,6 @@ public void query104() { Assert.assertTrue(number.compareTo(new BigDecimal("1.5")) == 0); } - @Test - public void query105() { - Integer integer = easyQuery.queryable(BlogEntity.class) - .where(o -> o.eq(BlogEntity::getId, "1")) - .lenOrNull(BlogEntity::getId); - - Assert.assertTrue(integer == 1); - } - - @Test - public void query106() { - Integer integer = easyQuery.queryable(BlogEntity.class) - .where(o -> o.eq(BlogEntity::getId, "11")) - .lenOrNull(BlogEntity::getId); - - Assert.assertTrue(integer == 2); - } - - @Test - public void query107() { - Integer integer = easyQuery.queryable(BlogEntity.class) - .where(o -> o.eq(BlogEntity::getId, "11x")) - .lenOrDefault(BlogEntity::getId, null); - - Assert.assertTrue(integer == null); - } - @Test public void query108() { Integer integer = easyQuery.queryable(BlogEntity.class) @@ -1871,24 +1845,6 @@ public void query108() { Assert.assertTrue(integer2 == 35); } - @Test - public void query109() { - Integer integer = easyQuery.queryable(BlogEntity.class) - .where(o -> o.eq(BlogEntity::getId, "11x")) - .lenOrNull(BlogEntity::getId); - - Assert.assertTrue(integer == null); - } - - @Test - public void query110() { - Integer integer = easyQuery.queryable(BlogEntity.class) - .where(o -> o.eq(BlogEntity::getId, "11")) - .lenOrNull(BlogEntity::getId); - - Assert.assertTrue(integer == 2); - } - @Test public void query111() { String toSql = easyQuery @@ -1919,4 +1875,350 @@ public void query113() { .sumOrNull((t, t1) -> t1.column(BlogEntity::getScore)); Assert.assertTrue(bigDecimal == null); } + + @Test + public void query114() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .sumBigDecimalOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query115() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .sumBigDecimalOrDefault((t, t1) -> t1.column(BlogEntity::getScore), null); + Assert.assertTrue(bigDecimal == null); + BigDecimal bigDecimal1 = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .sumBigDecimalOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal1) == 0); + } + + @Test + public void query116() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .maxOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + BigDecimal bigDecimal1 = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .maxOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal1) == 0); + } + + @Test + public void query117() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "3"))) + .maxOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query118() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .minOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + BigDecimal bigDecimal1 = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .minOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal1) == 0); + } + + @Test + public void query119() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "3"))) + .minOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query120() { + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .avgBigDecimalOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5x"))) + .avgBigDecimalOrNull((t, t1) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgBigDecimalOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5x"))) + .avgBigDecimalOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal) == 0); + } + { + Double bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .avgOrNull((t, t1) -> t1.column(BlogEntity::getStar)); + Assert.assertTrue(3.3333d == bigDecimal); + } + { + Double bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5x"))) + .avgOrDefault((t, t1) -> t1.column(BlogEntity::getStar), null); + Assert.assertTrue(null == bigDecimal); + } + { + Double bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgOrDefault((t, t1) -> t1.column(BlogEntity::getStar), null); + Assert.assertTrue(5d == bigDecimal); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgOrDefault((t, t1) -> t1.column(BlogEntity::getStar), null, BigDecimal.class); + Assert.assertTrue(new BigDecimal("5").compareTo(bigDecimal) == 0); + } + { + Float bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgOrDefault((t, t1) -> t1.column(BlogEntity::getStar), null, Float.class); + Assert.assertTrue(5f == bigDecimal); + } + } + + @Test + public void query121() { + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByAsc(false, (t, t1) -> t1.column(BlogEntity::getOrder)) + .orderByAsc(true, (t, t1) -> t1.column(BlogEntity::getId)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) ORDER BY t1.`id` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByDesc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) ORDER BY t1.`order` DESC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByDesc(false, (t, t1) -> t1.column(BlogEntity::getOrder)) + .orderByDesc(true, (t, t1) -> t1.column(BlogEntity::getId).column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) ORDER BY t1.`id` DESC,t1.`order` DESC", sql); + } + } + + @Test + public void query122() { + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy((t, t1) -> t.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(false, (t, t1) -> t.column(Topic::getStars)) + .groupBy(true, (t, t1) -> t.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .innerJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(false, (t, t1) -> t.column(Topic::getStars)) + .groupBy(true, (t, t1) -> t.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t INNER JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC", sql); + } + { + List list = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy((t, t1) -> t.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)) + .toList(); + Assert.assertEquals(3, list.size()); + for (Topic topic : list) { + Assert.assertNotNull(topic.getId()); + Assert.assertNull(topic.getStars()); + Assert.assertNull(topic.getTitle()); + } + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .having(o -> o.count(Topic::getId, AggregatePredicateCompare.GE, 1)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` HAVING COUNT(t.`id`) >= ? ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .having(false, (t, t1) -> t1.count(BlogEntity::getId, AggregatePredicateCompare.GE, 1)) + .having(true, (t, t1) -> t.count(Topic::getId, AggregatePredicateCompare.GE, 1)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` HAVING COUNT(t.`id`) >= ? ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .having(false, (t, t1) -> t1.count(BlogEntity::getId, AggregatePredicateCompare.GE, 1)) + .having((t, t1) -> t.count(Topic::getId, AggregatePredicateCompare.GE, 1)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` HAVING COUNT(t.`id`) >= ? ORDER BY t1.`order` ASC", sql); + } + } + + @Test + public void query123() { + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .distinct() + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)) + .limit(10).toSQL(); + Assert.assertEquals("SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC LIMIT 10", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select(Long.class, o -> o.columnCount(Topic::getId)).toSQL(); + Assert.assertEquals("SELECT COUNT(t2.`id`) AS `id` FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC) t2", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t, t1) -> t1.column(BlogEntity::getOrder)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select("count(1)").toSQL(); + Assert.assertEquals("SELECT count(1) FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC) t2", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByDesc((t) -> t.column(Topic::getStars)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select("count(1)").toSQL(); + Assert.assertEquals("SELECT count(1) FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t.`stars` DESC) t2", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t) -> t.column(Topic::getStars)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select("count(1)").toSQL(); + Assert.assertEquals("SELECT count(1) FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t.`stars` ASC) t2", sql); + } + } + + } diff --git a/sql-test/src/main/java/com/easy/query/test/QueryTest2.java b/sql-test/src/main/java/com/easy/query/test/QueryTest2.java new file mode 100644 index 000000000..937f5e1fd --- /dev/null +++ b/sql-test/src/main/java/com/easy/query/test/QueryTest2.java @@ -0,0 +1,430 @@ +package com.easy.query.test; + +import com.easy.query.test.entity.BlogEntity; +import com.easy.query.test.entity.Topic; +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; + +/** + * create time 2023/6/8 21:38 + * 文件说明 + * + * @author xuejiaming + */ +public class QueryTest2 extends BaseTest { + + @Test + public void query124() { + String toSql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .limit(1, 2) + .toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` = ? LIMIT 2 OFFSET 1", toSql); + } + + @Test + public void query125() { + String toSql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .select(BlogEntity.class, (t, t1, t2) -> t.column(Topic::getId) + .then(t1) + .column(BlogEntity::getTitle) + .then(t2) + .column(BlogEntity::getStar)) + .limit(1, 2) + .toSQL(); + Assert.assertEquals("SELECT t.`id`,t1.`title`,t2.`star` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` INNER JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` = ? LIMIT 2 OFFSET 1", toSql); + } + + @Test + public void query126() { + { + + List list = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .select(BlogEntity.class, (t, t1, t2) -> t.column(Topic::getId) + .then(t1) + .column(BlogEntity::getTitle) + .then(t2) + .column(BlogEntity::getStar)) + .limit(1, 2) + .toList(); + Assert.assertEquals(0, list.size()); + } + { + + List list = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .select(BlogEntity.class, (t, t1, t2) -> t.column(Topic::getId) + .then(t1) + .column(BlogEntity::getTitle) + .then(t2) + .column(BlogEntity::getStar)) + .limit(1) + .toList(); + Assert.assertEquals(1, list.size()); + } + } + + @Test + public void query112() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .sumOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query113() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .sumOrNull((t, t1, t2) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + } + + @Test + public void query114() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3")) + .sumBigDecimalOrNull((t, t1, t2) -> t2.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query115() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .sumBigDecimalOrDefault((t, t1, t2) -> t2.column(BlogEntity::getScore), null); + Assert.assertTrue(bigDecimal == null); + BigDecimal bigDecimal1 = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .sumBigDecimalOrDefault((t, t1, t2) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal1) == 0); + } + + @Test + public void query116() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .maxOrNull((t, t1, t2) -> t2.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + BigDecimal bigDecimal1 = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .maxOrDefault((t, t1, t2) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal1) == 0); + } + + @Test + public void query117() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "3"))) + .maxOrNull((t, t1, t2) -> t2.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query118() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .minOrNull((t, t1, t2) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + BigDecimal bigDecimal1 = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.eq(Topic::getId, "3x")) + .minOrDefault((t, t1, t2) -> t2.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal1) == 0); + } + + @Test + public void query119() { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "3"))) + .minOrNull((t, t1, t2) -> t2.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + + @Test + public void query120() { + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .avgBigDecimalOrNull((t, t1, t2) -> t1.column(BlogEntity::getScore)); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5x"))) + .avgBigDecimalOrNull((t, t1, t2) -> t2.column(BlogEntity::getScore)); + Assert.assertTrue(bigDecimal == null); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgBigDecimalOrDefault((t, t1, t2) -> t2.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(new BigDecimal("1.2").compareTo(bigDecimal) == 0); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5x"))) + .avgBigDecimalOrDefault((t, t1) -> t1.column(BlogEntity::getScore), BigDecimal.ZERO); + Assert.assertTrue(BigDecimal.ZERO.compareTo(bigDecimal) == 0); + } + { + Double bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .avgOrNull((t, t1) -> t1.column(BlogEntity::getStar)); + Assert.assertTrue(3.3333d == bigDecimal); + } + { + Double bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5x"))) + .avgOrDefault((t, t1) -> t1.column(BlogEntity::getStar), null); + Assert.assertTrue(null == bigDecimal); + } + { + Double bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgOrDefault((t, t1, t2) -> t2.column(BlogEntity::getStar), null); + Assert.assertTrue(5d == bigDecimal); + } + { + BigDecimal bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgOrDefault((t, t1, t2) -> t2.column(BlogEntity::getStar), null, BigDecimal.class); + Assert.assertTrue(new BigDecimal("5").compareTo(bigDecimal) == 0); + } + { + Float bigDecimal = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3x", "2x", "5"))) + .avgOrDefault((t, t1, t2) -> t1.column(BlogEntity::getStar), null, Float.class); + Assert.assertTrue(5f == bigDecimal); + } + } + + @Test + public void query121() { + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByAsc((t, t1, t2) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByAsc(false, (t, t1, t2) -> t1.column(BlogEntity::getOrder)) + .orderByAsc(true, (t, t1, t2) -> t2.column(BlogEntity::getId)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) ORDER BY t2.`id` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByDesc((t, t1, t2) -> t2.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) ORDER BY t2.`order` DESC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .orderByDesc(false, (t, t1, t2) -> t2.column(BlogEntity::getOrder)) + .orderByDesc(true, (t, t1, t2) -> t2.column(BlogEntity::getId).column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) ORDER BY t2.`id` DESC,t2.`order` DESC", sql); + } + } + + @Test + public void query122() { + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t, t1, t2) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy((t, t1, t2) -> t2.column(BlogEntity::getId)) + .orderByAsc((t, t1, t2) -> t2.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t2.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t2.`id` ORDER BY t2.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(false, (t, t1, t2) -> t.column(Topic::getStars)) + .groupBy(true, (t, t1, t2) -> t.column(Topic::getId)) + .orderByAsc((t, t1, t2) -> t1.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` INNER JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .innerJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(false, (t, t1, t2) -> t.column(Topic::getStars)) + .groupBy(true, (t, t1, t2) -> t.column(Topic::getId)) + .orderByAsc((t, t1, t2) -> t2.column(BlogEntity::getOrder)).toSQL(); + Assert.assertEquals("SELECT t.`id` FROM `t_topic` t INNER JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` INNER JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t2.`order` ASC", sql); + } + } + + @Test + public void query123() { + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .distinct() + .orderByAsc((t, t1, t2) -> t2.column(BlogEntity::getOrder)) + .limit(10).toSQL(); + Assert.assertEquals("SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` INNER JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t2.`order` ASC LIMIT 10", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .innerJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t, t1, t2) -> t2.column(BlogEntity::getOrder)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select(Long.class, o -> o.columnCount(Topic::getId)).toSQL(); + Assert.assertEquals("SELECT COUNT(t3.`id`) AS `id` FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` INNER JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t2.`order` ASC) t3", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t, t1, t2) -> t1.column(BlogEntity::getOrder)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select("count(1)").toSQL(); + Assert.assertEquals("SELECT count(1) FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t1.`order` ASC) t3", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByDesc((t) -> t.column(Topic::getStars)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select("count(1)").toSQL(); + Assert.assertEquals("SELECT count(1) FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t.`stars` DESC) t3", sql); + } + { + String sql = easyQuery + .queryable(Topic.class) + .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId)) + .leftJoin(BlogEntity.class, (t, t1, t2) -> t.eq(t2, Topic::getId, BlogEntity::getId)) + .where(o -> o.in(Topic::getId, Arrays.asList("3", "2", "5"))) + .groupBy(o -> o.column(Topic::getId)) + .orderByAsc((t) -> t.column(Topic::getStars)) + .distinct() + .select(Topic.class, o -> o.column(Topic::getId)) + .select("count(1)").toSQL(); + Assert.assertEquals("SELECT count(1) FROM (SELECT DISTINCT t.`id` FROM `t_topic` t LEFT JOIN `t_blog` t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` LEFT JOIN `t_blog` t2 ON t2.`deleted` = ? AND t.`id` = t2.`id` WHERE t.`id` IN (?,?,?) GROUP BY t.`id` ORDER BY t.`stars` ASC) t3", sql); + } + } +}