From 98975d25079d505ab20e95f94abc59a1cc5e0e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 25 May 2023 10:31:56 +0800 Subject: [PATCH] feat: add for update; close #I702QL --- .../core/dialect/impl/CommonsDialectImpl.java | 22 +++++++ .../core/query/BaseQueryWrapper.java | 17 +++++ .../java/com/mybatisflex/core/query/CPI.java | 9 +++ .../mybatisflex/core/query/QueryWrapper.java | 62 ++++++++++--------- .../coretest/AccountSqlTester.java | 14 +++++ 5 files changed, 96 insertions(+), 28 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 04bf26d8..3f6811ea 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -297,6 +297,13 @@ public class CommonsDialectImpl implements IDialect { sqlBuilder = buildLimitOffsetSql(sqlBuilder, queryWrapper, limitRows, limitOffset); } + List endFragments = CPI.getEndFragments(queryWrapper); + if (CollectionUtil.isNotEmpty(endFragments)) { + for (String endFragment : endFragments) { + sqlBuilder.append(" ").append(endFragment); + } + } + return sqlBuilder.toString(); } @@ -339,6 +346,13 @@ public class CommonsDialectImpl implements IDialect { //buildOrderBySql(sqlBuilder, queryWrapper); //buildLimitSql(sqlBuilder, queryWrapper); + List endFragments = CPI.getEndFragments(queryWrapper); + if (CollectionUtil.isNotEmpty(endFragments)) { + for (String endFragment : endFragments) { + sqlBuilder.append(" ").append(endFragment); + } + } + return sqlBuilder.toString(); } @@ -642,6 +656,14 @@ public class CommonsDialectImpl implements IDialect { } sql.append(" WHERE ").append(whereConditionSql); + + List endFragments = CPI.getEndFragments(queryWrapper); + if (CollectionUtil.isNotEmpty(endFragments)) { + for (String endFragment : endFragments) { + sql.append(" ").append(endFragment); + } + } + return sql.toString(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java index b657e768..bffc02cb 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java @@ -38,6 +38,8 @@ public class BaseQueryWrapper implements Serializable { protected Integer limitOffset; protected Integer limitRows; + protected List endFragments; + protected Map context; // protected boolean ignoreBlankStrings = false; @@ -120,6 +122,13 @@ public class BaseQueryWrapper implements Serializable { joinTables.add(queryTable); } + protected void addEndFragment(String fragment){ + if (endFragments == null){ + endFragments = new ArrayList<>(); + } + endFragments.add(fragment); + } + protected List getQueryTables() { return queryTables; @@ -221,6 +230,14 @@ public class BaseQueryWrapper implements Serializable { this.limitRows = limitRows; } + protected List getEndFragments() { + return endFragments; + } + + protected void setEndFragments(List endFragments) { + this.endFragments = endFragments; + } + protected Map getContext() { return context; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java index 2a1fc02d..e61e0198 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java @@ -158,6 +158,15 @@ public class CPI { queryWrapper.setLimitRows(limitRows); } + public static List getEndFragments(QueryWrapper queryWrapper) { + return queryWrapper.getEndFragments(); + } + + public static void setEndFragments(QueryWrapper queryWrapper,List endFragments) { + queryWrapper.setEndFragments(endFragments); + } + + public static Map getContext(QueryWrapper queryWrapper) { return queryWrapper.getContext(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index d82d12f2..7400d680 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -155,8 +155,8 @@ public class QueryWrapper extends BaseQueryWrapper { } - public Joiner leftJoinIf(String table, boolean condition) { - return joining(Join.TYPE_LEFT, table, condition); + public Joiner leftJoinIf(String table, boolean when) { + return joining(Join.TYPE_LEFT, table, when); } public Joiner leftJoin(TableDef table) { @@ -164,104 +164,104 @@ public class QueryWrapper extends BaseQueryWrapper { } - public Joiner leftJoinIf(TableDef table, boolean condition) { - return joining(Join.TYPE_LEFT, table.getTableName(), condition); + public Joiner leftJoinIf(TableDef table, boolean when) { + return joining(Join.TYPE_LEFT, table.getTableName(), when); } public Joiner leftJoin(QueryWrapper table) { return joining(Join.TYPE_LEFT, table, true); } - public Joiner leftJoinIf(QueryWrapper table, boolean condition) { - return joining(Join.TYPE_LEFT, table, condition); + public Joiner leftJoinIf(QueryWrapper table, boolean when) { + return joining(Join.TYPE_LEFT, table, when); } public Joiner rightJoin(String table) { return joining(Join.TYPE_RIGHT, table, true); } - public Joiner rightJoinIf(String table, boolean condition) { - return joining(Join.TYPE_RIGHT, table, condition); + public Joiner rightJoinIf(String table, boolean when) { + return joining(Join.TYPE_RIGHT, table, when); } public Joiner rightJoin(QueryWrapper table) { return joining(Join.TYPE_RIGHT, table, true); } - public Joiner rightJoinIf(QueryWrapper table, boolean condition) { - return joining(Join.TYPE_RIGHT, table, condition); + public Joiner rightJoinIf(QueryWrapper table, boolean when) { + return joining(Join.TYPE_RIGHT, table, when); } public Joiner innerJoin(String table) { return joining(Join.TYPE_INNER, table, true); } - public Joiner innerJoinIf(String table, boolean condition) { - return joining(Join.TYPE_INNER, table, condition); + public Joiner innerJoinIf(String table, boolean when) { + return joining(Join.TYPE_INNER, table, when); } public Joiner innerJoin(TableDef table) { return innerJoinIf(table, true); } - public Joiner innerJoinIf(TableDef table, boolean condition) { - return joining(Join.TYPE_INNER, table.getTableName(), condition); + public Joiner innerJoinIf(TableDef table, boolean when) { + return joining(Join.TYPE_INNER, table.getTableName(), when); } public Joiner innerJoin(QueryWrapper table) { return joining(Join.TYPE_INNER, table, true); } - public Joiner innerJoinIf(QueryWrapper table, boolean condition) { - return joining(Join.TYPE_INNER, table, condition); + public Joiner innerJoinIf(QueryWrapper table, boolean when) { + return joining(Join.TYPE_INNER, table, when); } public Joiner fullJoin(String table) { return joining(Join.TYPE_FULL, table, true); } - public Joiner fullJoinIf(String table, boolean condition) { - return joining(Join.TYPE_FULL, table, condition); + public Joiner fullJoinIf(String table, boolean when) { + return joining(Join.TYPE_FULL, table, when); } public Joiner fullJoin(QueryWrapper table) { return joining(Join.TYPE_FULL, table, true); } - public Joiner fullJoinIf(QueryWrapper table, boolean condition) { - return joining(Join.TYPE_FULL, table, condition); + public Joiner fullJoinIf(QueryWrapper table, boolean when) { + return joining(Join.TYPE_FULL, table, when); } public Joiner crossJoin(String table) { return joining(Join.TYPE_CROSS, table, true); } - public Joiner crossJoinIf(String table, boolean condition) { - return joining(Join.TYPE_CROSS, table, condition); + public Joiner crossJoinIf(String table, boolean when) { + return joining(Join.TYPE_CROSS, table, when); } public Joiner crossJoin(QueryWrapper table) { return joining(Join.TYPE_CROSS, table, true); } - public Joiner crossJoinIf(QueryWrapper table, boolean condition) { - return joining(Join.TYPE_CROSS, table, condition); + public Joiner crossJoinIf(QueryWrapper table, boolean when) { + return joining(Join.TYPE_CROSS, table, when); } public Joiner join(String table) { return joining(Join.TYPE_JOIN, table, true); } - public Joiner join(String table, boolean condition) { - return joining(Join.TYPE_JOIN, table, condition); + public Joiner join(String table, boolean when) { + return joining(Join.TYPE_JOIN, table, when); } public Joiner join(QueryWrapper table) { return joining(Join.TYPE_JOIN, table, true); } - public Joiner join(QueryWrapper table, boolean condition) { - return joining(Join.TYPE_JOIN, table, condition); + public Joiner join(QueryWrapper table, boolean when) { + return joining(Join.TYPE_JOIN, table, when); } public QueryWrapper union(QueryWrapper unionQuery) { @@ -280,6 +280,12 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } + public QueryWrapper forUpdate(){ + addEndFragment("FOR UPDATE"); + return this; + } + + protected Joiner joining(String type, String table, boolean condition) { Join join = new Join(type, table, condition); addJoinTable(join.getQueryTable()); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index 2babf40e..4f6a5256 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -292,6 +292,20 @@ public class AccountSqlTester { System.out.println(sql); } + @Test + public void testForUpdate() { + IDialect dialect = new CommonsDialectImpl(); + + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(ACCOUNT) + .and(ACCOUNT.USER_NAME.like("michael")) + .forUpdate(); + + String sql = dialect.forSelectByQuery(queryWrapper); + System.out.println(sql); + } + @Test public void testLimitOffset() {