From 3b97c383d15c1fc398bfbc02ae1e10d3c9c56e7f Mon Sep 17 00:00:00 2001 From: zhouhao Date: Wed, 20 Dec 2023 15:06:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20sqlserver=E5=A2=9E=E5=8A=A0for=20update?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mssql/SqlServerQuerySqlBuilder.java | 63 +++++++++++++++++++ .../mssql/SqlServerSchemaMetadata.java | 1 + .../rdb/supports/BasicReactiveTests.java | 13 ++++ 3 files changed, 77 insertions(+) create mode 100644 hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerQuerySqlBuilder.java diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerQuerySqlBuilder.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerQuerySqlBuilder.java new file mode 100644 index 00000000..5867fc6c --- /dev/null +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerQuerySqlBuilder.java @@ -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.findFeature(RDBFeatureType.paginator.getId()) + .map(paginator -> paginator.doPaging(fragments, parameter.getPageIndex(), parameter.getPageSize())) + .map(SqlFragments::toRequest) + .orElseGet(fragments::toRequest); + + } + return fragments.toRequest(); + } +} diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerSchemaMetadata.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerSchemaMetadata.java index 837257d8..47b0d494 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerSchemaMetadata.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/mssql/SqlServerSchemaMetadata.java @@ -15,6 +15,7 @@ public SqlServerSchemaMetadata(String name) { addFeature(new SqlServer2012TableMetadataParser(this)); addFeature(new SqlServerIndexMetadataParser(this)); addFeature(Dialect.MSSQL); + addFeature(new SqlServerQuerySqlBuilder(this)); } @Override diff --git a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java index cede7ae9..e9f1df2a 100644 --- a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java +++ b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java @@ -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() {