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

View File

@ -41,6 +41,19 @@ public class AccountSqlTester {
System.out.println(sql); 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 @Test
public void testSelectColumnsAndFunctionsSql() { public void testSelectColumnsAndFunctionsSql() {
QueryWrapper query = new QueryWrapper() QueryWrapper query = new QueryWrapper()