From b733679e20a51e158860d3cbd60dfc63603a9678 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Fri, 24 May 2024 18:47:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96BatchSqlFragments?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8C=87=E5=AE=9A=E5=AE=B9=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../query/SelectColumnFragmentBuilder.java | 2 +- .../update/DefaultUpdateSqlBuilder.java | 33 ++++++++++++------- .../mssql/SqlServer2012Paginator.java | 2 +- .../rdb/supports/mysql/MysqlPaginator.java | 2 +- .../rdb/supports/oracle/OraclePaginator.java | 2 +- .../postgres/PostgresqlPaginator.java | 2 +- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/query/SelectColumnFragmentBuilder.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/query/SelectColumnFragmentBuilder.java index d79f7ec6..f420d60e 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/query/SelectColumnFragmentBuilder.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/query/SelectColumnFragmentBuilder.java @@ -274,7 +274,7 @@ public SqlFragments createFragments(QueryOperatorParameter parameter, SelectColu return this .createFragments(columnFullName, columnMetadata, selectColumn) - .map(fragments -> new BatchSqlFragments() + .map(fragments -> new BatchSqlFragments(2,0) .add(fragments) .add(SqlFragments.of("as", getAlias(null, finalColumnMetadata, selectColumn)))) .orElse(null); diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/update/DefaultUpdateSqlBuilder.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/update/DefaultUpdateSqlBuilder.java index 9e5ac863..ebdcb2a2 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/update/DefaultUpdateSqlBuilder.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/update/DefaultUpdateSqlBuilder.java @@ -1,15 +1,13 @@ package org.hswebframework.ezorm.rdb.operator.builder.fragments.update; -import lombok.AllArgsConstructor; import lombok.Getter; import org.apache.commons.collections.CollectionUtils; import org.hswebframework.ezorm.core.param.Term; import org.hswebframework.ezorm.rdb.executor.EmptySqlRequest; -import org.hswebframework.ezorm.rdb.executor.NullValue; import org.hswebframework.ezorm.rdb.executor.SqlRequest; -import org.hswebframework.ezorm.rdb.metadata.key.ForeignKeyMetadata; import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata; import org.hswebframework.ezorm.rdb.metadata.RDBTableMetadata; +import org.hswebframework.ezorm.rdb.metadata.key.ForeignKeyMetadata; import org.hswebframework.ezorm.rdb.operator.builder.fragments.*; import org.hswebframework.ezorm.rdb.operator.builder.fragments.term.ForeignKeyTermFragmentBuilder; import org.hswebframework.ezorm.rdb.operator.dml.update.UpdateColumn; @@ -17,31 +15,42 @@ import java.util.*; -import static java.util.Optional.*; -import static org.hswebframework.ezorm.rdb.operator.builder.fragments.function.FunctionFragmentBuilder.*; +import static org.hswebframework.ezorm.rdb.operator.builder.fragments.function.FunctionFragmentBuilder.createFeatureId; -@AllArgsConstructor(staticName = "of") @SuppressWarnings("all") public class DefaultUpdateSqlBuilder extends AbstractTermsFragmentBuilder implements UpdateSqlBuilder { @Getter private RDBTableMetadata table; + private SqlFragments PREFIX = null; + + private DefaultUpdateSqlBuilder(RDBTableMetadata table) { + this.table = table; + } + + public static DefaultUpdateSqlBuilder of(RDBTableMetadata table) { + return new DefaultUpdateSqlBuilder(table); + } + + @Override public SqlRequest build(UpdateOperatorParameter parameter) { - + if (PREFIX == null) { + PREFIX = SqlFragments.of("update", table.getFullName(), "set"); + } if (CollectionUtils.isEmpty(parameter.getColumns())) { return EmptySqlRequest.INSTANCE; } if (CollectionUtils.isEmpty(parameter.getWhere())) { throw new UnsupportedOperationException("unsupported no conditions update"); } + Set distinctColumns = new HashSet<>(); - BatchSqlFragments fragments = new BatchSqlFragments(); + BatchSqlFragments fragments = new BatchSqlFragments(3 + distinctColumns.size(), distinctColumns.size()); - fragments.addSql("update", table.getFullName(), "set"); + fragments.add(PREFIX); int index = 0; - Set distinctColumns = new HashSet<>(); for (UpdateColumn column : parameter.getColumns()) { SqlFragments columnFragments = EmptySqlFragments.INSTANCE; @@ -62,7 +71,7 @@ public SqlRequest build(UpdateOperatorParameter parameter) { else if (distinctColumns.add(columnMetadata)) { //函数 if (column.getFunction() != null) { - columnFragments = new BatchSqlFragments() + columnFragments = new BatchSqlFragments(2,1) .addSql(columnMetadata.getQuoteName(), "=") .add( columnMetadata @@ -90,7 +99,7 @@ else if (value instanceof NativeSql) { if (columnFragments.isNotEmpty()) { if (index++ != 0) { - fragments.addSql(","); + fragments.add(SqlFragments.COMMA); } fragments.add(columnFragments); } diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServer2012Paginator.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServer2012Paginator.java index eb63adf8..43228e3e 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServer2012Paginator.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServer2012Paginator.java @@ -32,7 +32,7 @@ public SqlFragments doPaging(SqlFragments fragments, int pageIndex, int pageSize && !fragments.getSql().contains("ORDER BY"); if (!(fragments instanceof AppendableSqlFragments)) { - fragments = new BatchSqlFragments().add(fragments); + fragments = new BatchSqlFragments(3,2).add(fragments); } AppendableSqlFragments sqlFragments = ((AppendableSqlFragments) fragments); diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mysql/MysqlPaginator.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mysql/MysqlPaginator.java index 662eb4cd..2474f5cd 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mysql/MysqlPaginator.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mysql/MysqlPaginator.java @@ -23,7 +23,7 @@ public SqlFragments doPaging(SqlFragments fragments, int pageIndex, int pageSize } if (!(fragments instanceof AppendableSqlFragments)) { - fragments = new BatchSqlFragments().add(fragments); + fragments = new BatchSqlFragments(2,2).add(fragments); } return ((AppendableSqlFragments) fragments) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/oracle/OraclePaginator.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/oracle/OraclePaginator.java index 68bef0ad..ae41b459 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/oracle/OraclePaginator.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/oracle/OraclePaginator.java @@ -23,7 +23,7 @@ public SqlFragments doPaging(SqlFragments fragments, int pageIndex, int pageSize return fragments; } - BatchSqlFragments paging = new BatchSqlFragments(); + BatchSqlFragments paging = new BatchSqlFragments(3,2); paging.add(PREFIX) .add(fragments) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlPaginator.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlPaginator.java index 182644c7..0e08136c 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlPaginator.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlPaginator.java @@ -18,7 +18,7 @@ public SqlFragments doPaging(SqlFragments fragments, int pageIndex, int pageSize } if (!(fragments instanceof AppendableSqlFragments)) { - fragments = new BatchSqlFragments().add(fragments); + fragments = new BatchSqlFragments(2,2).add(fragments); } return ((AppendableSqlFragments) fragments)