Skip to content

Commit

Permalink
refactor: 优化BatchSqlFragments初始化指定容量
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed May 24, 2024
1 parent ca2bb19 commit b733679
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
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;
import org.hswebframework.ezorm.rdb.operator.dml.update.UpdateOperatorParameter;

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<UpdateOperatorParameter> 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<RDBColumnMetadata> 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<RDBColumnMetadata> distinctColumns = new HashSet<>();

for (UpdateColumn column : parameter.getColumns()) {
SqlFragments columnFragments = EmptySqlFragments.INSTANCE;
Expand All @@ -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
Expand Down Expand Up @@ -90,7 +99,7 @@ else if (value instanceof NativeSql) {

if (columnFragments.isNotEmpty()) {
if (index++ != 0) {
fragments.addSql(",");
fragments.add(SqlFragments.COMMA);
}
fragments.add(columnFragments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b733679

Please sign in to comment.