Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sqlserver增加for update支持 #64

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading