QueryWrapper.from support multi table

This commit is contained in:
开源海哥 2023-03-17 09:02:29 +08:00
parent b9a152c582
commit 35384a02c1
2 changed files with 37 additions and 14 deletions

View File

@ -21,6 +21,7 @@ import com.mybatisflex.core.util.ArrayUtil;
import com.mybatisflex.core.util.CollectionUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
@ -40,21 +41,28 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
public QueryWrapper from(TableDef tableDef) {
return from(tableDef.getTableName());
public QueryWrapper from(TableDef... tableDefs) {
for (TableDef tableDef : tableDefs) {
from(new QueryTable(tableDef.getTableName()));
}
return this;
}
public QueryWrapper from(String table) {
return from(new QueryTable(table));
public QueryWrapper from(String... tables) {
for (String table : tables) {
from(new QueryTable(table));
}
return this;
}
public QueryWrapper from(QueryTable table) {
public QueryWrapper from(QueryTable... tables) {
if (CollectionUtil.isEmpty(queryTables)) {
queryTables = new ArrayList<>();
queryTables.add(table);
queryTables.addAll(Arrays.asList(tables));
} else {
for (QueryTable table : tables) {
boolean contains = false;
for (QueryTable queryTable : queryTables) {
if (queryTable.isSameTable(table)) {
@ -65,6 +73,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
queryTables.add(table);
}
}
}
return this;
}
@ -73,6 +82,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
return from(new SelectQueryTable(queryWrapper));
}
public QueryWrapper as(String alias) {
if (CollectionUtil.isEmpty(queryTables)) {
throw new IllegalArgumentException("query table must not be empty.");
@ -212,6 +222,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
public Joiner<QueryWrapper> fullJoinIf(QueryWrapper table, boolean condition) {
return joining(Join.TYPE_FULL, table, condition);
}
public Joiner<QueryWrapper> crossJoin(String table) {
return joining(Join.TYPE_CROSS, table, true);
}
@ -229,7 +240,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
protected Joiner<QueryWrapper> joining(String type, String table, boolean condition) {
Join join = new Join(type, table, condition);
addJoinTable(join.getQueryTable());

View File

@ -41,6 +41,19 @@ public class AccountSqlTester {
System.out.println(sql);
}
@Test
public void testSelect1ColumnsSql() {
QueryWrapper query = new QueryWrapper()
.select(ACCOUNT.ID, ACCOUNT.USER_NAME,
ARTICLE.ID.as("articleId"), ARTICLE.TITLE)
.from(ACCOUNT.as("a"), ARTICLE.as("b"))
.where(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID));
IDialect dialect = new CommonsDialectImpl(KeywordWrap.NONE,LimitOffsetProcesser.MYSQL);
String sql = dialect.forSelectListByQuery(query);
System.out.println(sql);
}
@Test
public void testSelectColumnsAndFunctionsSql() {
QueryWrapper query = new QueryWrapper()