mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add for update; close #I702QL
This commit is contained in:
parent
f64ff59aab
commit
98975d2507
@ -297,6 +297,13 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
sqlBuilder = buildLimitOffsetSql(sqlBuilder, queryWrapper, limitRows, limitOffset);
|
sqlBuilder = buildLimitOffsetSql(sqlBuilder, queryWrapper, limitRows, limitOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||||
|
for (String endFragment : endFragments) {
|
||||||
|
sqlBuilder.append(" ").append(endFragment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sqlBuilder.toString();
|
return sqlBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,6 +346,13 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
//buildOrderBySql(sqlBuilder, queryWrapper);
|
//buildOrderBySql(sqlBuilder, queryWrapper);
|
||||||
//buildLimitSql(sqlBuilder, queryWrapper);
|
//buildLimitSql(sqlBuilder, queryWrapper);
|
||||||
|
|
||||||
|
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||||
|
for (String endFragment : endFragments) {
|
||||||
|
sqlBuilder.append(" ").append(endFragment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sqlBuilder.toString();
|
return sqlBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -642,6 +656,14 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql.append(" WHERE ").append(whereConditionSql);
|
sql.append(" WHERE ").append(whereConditionSql);
|
||||||
|
|
||||||
|
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||||
|
for (String endFragment : endFragments) {
|
||||||
|
sql.append(" ").append(endFragment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -38,6 +38,8 @@ public class BaseQueryWrapper<T> implements Serializable {
|
|||||||
protected Integer limitOffset;
|
protected Integer limitOffset;
|
||||||
protected Integer limitRows;
|
protected Integer limitRows;
|
||||||
|
|
||||||
|
protected List<String> endFragments;
|
||||||
|
|
||||||
protected Map<String, Object> context;
|
protected Map<String, Object> context;
|
||||||
|
|
||||||
// protected boolean ignoreBlankStrings = false;
|
// protected boolean ignoreBlankStrings = false;
|
||||||
@ -120,6 +122,13 @@ public class BaseQueryWrapper<T> implements Serializable {
|
|||||||
joinTables.add(queryTable);
|
joinTables.add(queryTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void addEndFragment(String fragment){
|
||||||
|
if (endFragments == null){
|
||||||
|
endFragments = new ArrayList<>();
|
||||||
|
}
|
||||||
|
endFragments.add(fragment);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected List<QueryTable> getQueryTables() {
|
protected List<QueryTable> getQueryTables() {
|
||||||
return queryTables;
|
return queryTables;
|
||||||
@ -221,6 +230,14 @@ public class BaseQueryWrapper<T> implements Serializable {
|
|||||||
this.limitRows = limitRows;
|
this.limitRows = limitRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected List<String> getEndFragments() {
|
||||||
|
return endFragments;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setEndFragments(List<String> endFragments) {
|
||||||
|
this.endFragments = endFragments;
|
||||||
|
}
|
||||||
|
|
||||||
protected Map<String, Object> getContext() {
|
protected Map<String, Object> getContext() {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -158,6 +158,15 @@ public class CPI {
|
|||||||
queryWrapper.setLimitRows(limitRows);
|
queryWrapper.setLimitRows(limitRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<String> getEndFragments(QueryWrapper queryWrapper) {
|
||||||
|
return queryWrapper.getEndFragments();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setEndFragments(QueryWrapper queryWrapper,List<String> endFragments) {
|
||||||
|
queryWrapper.setEndFragments(endFragments);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Map<String, Object> getContext(QueryWrapper queryWrapper) {
|
public static Map<String, Object> getContext(QueryWrapper queryWrapper) {
|
||||||
return queryWrapper.getContext();
|
return queryWrapper.getContext();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -155,8 +155,8 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Joiner<QueryWrapper> leftJoinIf(String table, boolean condition) {
|
public Joiner<QueryWrapper> leftJoinIf(String table, boolean when) {
|
||||||
return joining(Join.TYPE_LEFT, table, condition);
|
return joining(Join.TYPE_LEFT, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> leftJoin(TableDef table) {
|
public Joiner<QueryWrapper> leftJoin(TableDef table) {
|
||||||
@ -164,104 +164,104 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Joiner<QueryWrapper> leftJoinIf(TableDef table, boolean condition) {
|
public Joiner<QueryWrapper> leftJoinIf(TableDef table, boolean when) {
|
||||||
return joining(Join.TYPE_LEFT, table.getTableName(), condition);
|
return joining(Join.TYPE_LEFT, table.getTableName(), when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> leftJoin(QueryWrapper table) {
|
public Joiner<QueryWrapper> leftJoin(QueryWrapper table) {
|
||||||
return joining(Join.TYPE_LEFT, table, true);
|
return joining(Join.TYPE_LEFT, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> leftJoinIf(QueryWrapper table, boolean condition) {
|
public Joiner<QueryWrapper> leftJoinIf(QueryWrapper table, boolean when) {
|
||||||
return joining(Join.TYPE_LEFT, table, condition);
|
return joining(Join.TYPE_LEFT, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> rightJoin(String table) {
|
public Joiner<QueryWrapper> rightJoin(String table) {
|
||||||
return joining(Join.TYPE_RIGHT, table, true);
|
return joining(Join.TYPE_RIGHT, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> rightJoinIf(String table, boolean condition) {
|
public Joiner<QueryWrapper> rightJoinIf(String table, boolean when) {
|
||||||
return joining(Join.TYPE_RIGHT, table, condition);
|
return joining(Join.TYPE_RIGHT, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> rightJoin(QueryWrapper table) {
|
public Joiner<QueryWrapper> rightJoin(QueryWrapper table) {
|
||||||
return joining(Join.TYPE_RIGHT, table, true);
|
return joining(Join.TYPE_RIGHT, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> rightJoinIf(QueryWrapper table, boolean condition) {
|
public Joiner<QueryWrapper> rightJoinIf(QueryWrapper table, boolean when) {
|
||||||
return joining(Join.TYPE_RIGHT, table, condition);
|
return joining(Join.TYPE_RIGHT, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> innerJoin(String table) {
|
public Joiner<QueryWrapper> innerJoin(String table) {
|
||||||
return joining(Join.TYPE_INNER, table, true);
|
return joining(Join.TYPE_INNER, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> innerJoinIf(String table, boolean condition) {
|
public Joiner<QueryWrapper> innerJoinIf(String table, boolean when) {
|
||||||
return joining(Join.TYPE_INNER, table, condition);
|
return joining(Join.TYPE_INNER, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> innerJoin(TableDef table) {
|
public Joiner<QueryWrapper> innerJoin(TableDef table) {
|
||||||
return innerJoinIf(table, true);
|
return innerJoinIf(table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> innerJoinIf(TableDef table, boolean condition) {
|
public Joiner<QueryWrapper> innerJoinIf(TableDef table, boolean when) {
|
||||||
return joining(Join.TYPE_INNER, table.getTableName(), condition);
|
return joining(Join.TYPE_INNER, table.getTableName(), when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> innerJoin(QueryWrapper table) {
|
public Joiner<QueryWrapper> innerJoin(QueryWrapper table) {
|
||||||
return joining(Join.TYPE_INNER, table, true);
|
return joining(Join.TYPE_INNER, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> innerJoinIf(QueryWrapper table, boolean condition) {
|
public Joiner<QueryWrapper> innerJoinIf(QueryWrapper table, boolean when) {
|
||||||
return joining(Join.TYPE_INNER, table, condition);
|
return joining(Join.TYPE_INNER, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> fullJoin(String table) {
|
public Joiner<QueryWrapper> fullJoin(String table) {
|
||||||
return joining(Join.TYPE_FULL, table, true);
|
return joining(Join.TYPE_FULL, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> fullJoinIf(String table, boolean condition) {
|
public Joiner<QueryWrapper> fullJoinIf(String table, boolean when) {
|
||||||
return joining(Join.TYPE_FULL, table, condition);
|
return joining(Join.TYPE_FULL, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> fullJoin(QueryWrapper table) {
|
public Joiner<QueryWrapper> fullJoin(QueryWrapper table) {
|
||||||
return joining(Join.TYPE_FULL, table, true);
|
return joining(Join.TYPE_FULL, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> fullJoinIf(QueryWrapper table, boolean condition) {
|
public Joiner<QueryWrapper> fullJoinIf(QueryWrapper table, boolean when) {
|
||||||
return joining(Join.TYPE_FULL, table, condition);
|
return joining(Join.TYPE_FULL, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> crossJoin(String table) {
|
public Joiner<QueryWrapper> crossJoin(String table) {
|
||||||
return joining(Join.TYPE_CROSS, table, true);
|
return joining(Join.TYPE_CROSS, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> crossJoinIf(String table, boolean condition) {
|
public Joiner<QueryWrapper> crossJoinIf(String table, boolean when) {
|
||||||
return joining(Join.TYPE_CROSS, table, condition);
|
return joining(Join.TYPE_CROSS, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> crossJoin(QueryWrapper table) {
|
public Joiner<QueryWrapper> crossJoin(QueryWrapper table) {
|
||||||
return joining(Join.TYPE_CROSS, table, true);
|
return joining(Join.TYPE_CROSS, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> crossJoinIf(QueryWrapper table, boolean condition) {
|
public Joiner<QueryWrapper> crossJoinIf(QueryWrapper table, boolean when) {
|
||||||
return joining(Join.TYPE_CROSS, table, condition);
|
return joining(Join.TYPE_CROSS, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> join(String table) {
|
public Joiner<QueryWrapper> join(String table) {
|
||||||
return joining(Join.TYPE_JOIN, table, true);
|
return joining(Join.TYPE_JOIN, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> join(String table, boolean condition) {
|
public Joiner<QueryWrapper> join(String table, boolean when) {
|
||||||
return joining(Join.TYPE_JOIN, table, condition);
|
return joining(Join.TYPE_JOIN, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> join(QueryWrapper table) {
|
public Joiner<QueryWrapper> join(QueryWrapper table) {
|
||||||
return joining(Join.TYPE_JOIN, table, true);
|
return joining(Join.TYPE_JOIN, table, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Joiner<QueryWrapper> join(QueryWrapper table, boolean condition) {
|
public Joiner<QueryWrapper> join(QueryWrapper table, boolean when) {
|
||||||
return joining(Join.TYPE_JOIN, table, condition);
|
return joining(Join.TYPE_JOIN, table, when);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper union(QueryWrapper unionQuery) {
|
public QueryWrapper union(QueryWrapper unionQuery) {
|
||||||
@ -280,6 +280,12 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public QueryWrapper forUpdate(){
|
||||||
|
addEndFragment("FOR UPDATE");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected Joiner<QueryWrapper> joining(String type, String table, boolean condition) {
|
protected Joiner<QueryWrapper> joining(String type, String table, boolean condition) {
|
||||||
Join join = new Join(type, table, condition);
|
Join join = new Join(type, table, condition);
|
||||||
addJoinTable(join.getQueryTable());
|
addJoinTable(join.getQueryTable());
|
||||||
|
|||||||
@ -292,6 +292,20 @@ public class AccountSqlTester {
|
|||||||
System.out.println(sql);
|
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
|
@Test
|
||||||
public void testLimitOffset() {
|
public void testLimitOffset() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user