Skip to content

Commit

Permalink
feat: sqlserver增加for update支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 20, 2023
1 parent e2805d4 commit 3b97c38
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.hswebframework.ezorm.rdb.supports.mssql;

import org.hswebframework.ezorm.rdb.executor.SqlRequest;
import org.hswebframework.ezorm.rdb.metadata.RDBFeatureType;
import org.hswebframework.ezorm.rdb.metadata.RDBSchemaMetadata;
import org.hswebframework.ezorm.rdb.metadata.TableOrViewMetadata;
import org.hswebframework.ezorm.rdb.operator.builder.DefaultQuerySqlBuilder;
import org.hswebframework.ezorm.rdb.operator.builder.FragmentBlock;
import org.hswebframework.ezorm.rdb.operator.builder.Paginator;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.BlockSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.SqlFragments;
import org.hswebframework.ezorm.rdb.operator.dml.query.QueryOperatorParameter;

class SqlServerQuerySqlBuilder extends DefaultQuerySqlBuilder {
public SqlServerQuerySqlBuilder(RDBSchemaMetadata schema) {
super(schema);
}

protected SqlRequest build(TableOrViewMetadata metadata, QueryOperatorParameter parameter) {
BlockSqlFragments fragments = BlockSqlFragments.of();

fragments.addBlock(FragmentBlock.before, "select");

fragments.addBlock(FragmentBlock.selectColumn, select(parameter, metadata)
.orElseGet(() -> PrepareSqlFragments.of().addSql("*")));

fragments.addBlock(FragmentBlock.selectFrom, from(metadata, parameter));


if (Boolean.TRUE.equals(parameter.getForUpdate())) {
fragments.addBlock(FragmentBlock.selectFrom, PrepareSqlFragments.of().addSql("with(updlock)"));
}

join(parameter, metadata)
.ifPresent(join -> fragments.addBlock(FragmentBlock.join, join));
//where

where(parameter, metadata)
.ifPresent(where ->
fragments.addBlock(FragmentBlock.where, "where")
.addBlock(FragmentBlock.where, where));

//group by

//having

//order by
orderBy(parameter, metadata)
.ifPresent(order -> fragments.addBlock(FragmentBlock.orderBy, "order by")
.addBlock(FragmentBlock.orderBy, order));

//分页
if (!Boolean.TRUE.equals(parameter.getForUpdate()) && parameter.getPageIndex() != null && parameter.getPageSize() != null) {
return metadata.<Paginator>findFeature(RDBFeatureType.paginator.getId())
.map(paginator -> paginator.doPaging(fragments, parameter.getPageIndex(), parameter.getPageSize()))
.map(SqlFragments::toRequest)
.orElseGet(fragments::toRequest);

}
return fragments.toRequest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public SqlServerSchemaMetadata(String name) {
addFeature(new SqlServer2012TableMetadataParser(this));
addFeature(new SqlServerIndexMetadataParser(this));
addFeature(Dialect.MSSQL);
addFeature(new SqlServerQuerySqlBuilder(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ public void testReactivePager() {

}

@Test
public void testForUpdate(){
repository
.createQuery()
.where(BasicTestEntity::getId, "forupdate")
.select("id", "name")
.forUpdate()
.fetch()
.then()
.as(StepVerifier::create)
.verifyComplete();
}

@Test
public void testInsertMerge() {

Expand Down

0 comments on commit 3b97c38

Please sign in to comment.