|
@@ -13,7 +13,6 @@ import org.example.join.model.QueryColumnOrder;
|
|
|
import org.example.join.model.QueryCondition;
|
|
|
import org.example.join.model.Table;
|
|
|
import org.example.join.model.TableJoin;
|
|
|
-import org.example.join.model.TableSublist;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -24,7 +23,8 @@ import java.util.stream.Collectors;
|
|
|
public class Sql implements IFormat {
|
|
|
|
|
|
protected final List<IQueryColumn> queryFieldsList = new ArrayList<>();
|
|
|
- protected final List<ITable> queryTableList = new ArrayList<>();
|
|
|
+ protected final List<ITable> tableList = new ArrayList<>();
|
|
|
+ protected final List<TableJoin> tableJoinList = new ArrayList<>();
|
|
|
protected final List<QueryCondition> queryConditionList = new ArrayList<>();
|
|
|
protected final List<QueryColumnField> groupByList = new ArrayList<>();
|
|
|
protected final List<QueryColumnOrder> orderByList = new ArrayList<>();
|
|
@@ -81,23 +81,27 @@ public class Sql implements IFormat {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- List<ITable> queryTableList = sql.queryTableList;
|
|
|
- for (int i = queryTableList.size() - 1; i >= 0; i--) {
|
|
|
- ITable iTable = queryTableList.get(i);
|
|
|
+ for (int i = sql.tableList.size() - 1; i >= 0; i--) {
|
|
|
+ ITable iTable = sql.tableList.get(i);
|
|
|
if (iTable instanceof Table table) {
|
|
|
String tableDelFlag = SqlContext.getTableDelFlag(table);
|
|
|
if (ObjectUtil.isNotEmpty(tableDelFlag)) {
|
|
|
- QueryColumnField queryColumnField = new QueryColumnField(table.getAlias(), tableDelFlag);
|
|
|
- sql.queryConditionList.add(0, new QueryCondition(queryColumnField, SqlConstant.EQ, sql.logic ? 1 : 0));
|
|
|
+ QueryColumnField field = table.field(tableDelFlag);
|
|
|
+ sql.queryConditionList.add(0, field.eq(sql.logic ? 1 : 0));
|
|
|
}
|
|
|
- } else if (iTable instanceof TableJoin tableJoin && tableJoin.getTable() instanceof Table table) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (TableJoin tableJoin : sql.tableJoinList) {
|
|
|
+ if (tableJoin.getTable() instanceof Table table) {
|
|
|
String tableDelFlag = SqlContext.getTableDelFlag(table);
|
|
|
if (ObjectUtil.isNotEmpty(tableDelFlag)) {
|
|
|
- QueryColumnField queryColumnField = new QueryColumnField(table.getAlias(), tableDelFlag);
|
|
|
- tableJoin.getQueryConditionList().add(new QueryCondition(queryColumnField, SqlConstant.EQ, sql.logic ? 1 : 0));
|
|
|
+ QueryColumnField field = table.field(tableDelFlag);
|
|
|
+ tableJoin.getQueryConditionList().add(field.eq(sql.logic ? 1 : 0));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -105,8 +109,16 @@ public class Sql implements IFormat {
|
|
|
*/
|
|
|
private String createSql(Sql sql) {
|
|
|
|
|
|
+ // queryTable
|
|
|
+ String queryTable = listFormatSql(sql.tableList, StringPool.COMMA);
|
|
|
+ if (StrUtil.isBlank(queryTable)) {
|
|
|
+ throw new IllegalArgumentException("未关联主表");
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder sqlBuilder = new StringBuilder();
|
|
|
+
|
|
|
// select
|
|
|
- StringBuilder sqlBuilder = new StringBuilder().append(SqlConstant.SELECT);
|
|
|
+ sqlBuilder.append(SqlConstant.SELECT);
|
|
|
|
|
|
// distinct
|
|
|
if (sql.distinct) {
|
|
@@ -121,8 +133,14 @@ public class Sql implements IFormat {
|
|
|
sqlBuilder.append(queryFields);
|
|
|
}
|
|
|
|
|
|
- // from table join table on
|
|
|
- sqlBuilder.append(SqlConstant.FROM).append(getQueryTablesSql(sql.queryTableList));
|
|
|
+ // from table
|
|
|
+ sqlBuilder.append(SqlConstant.FROM).append(queryTable);
|
|
|
+
|
|
|
+ // joinTable
|
|
|
+ String joinTable = listFormatSql(sql.tableJoinList, StringPool.EMPTY);
|
|
|
+ if (StrUtil.isNotBlank(joinTable)) {
|
|
|
+ sqlBuilder.append(joinTable);
|
|
|
+ }
|
|
|
|
|
|
// where 条件
|
|
|
String where = listFormatSql(sql.queryConditionList, SqlConstant.AND);
|
|
@@ -149,24 +167,6 @@ public class Sql implements IFormat {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取查询表sql
|
|
|
- */
|
|
|
- private String getQueryTablesSql(List<ITable> queryTableList) {
|
|
|
-
|
|
|
- String from = queryTableList.stream()
|
|
|
- .filter(item -> item instanceof Table || item instanceof TableSublist)
|
|
|
- .map(IFormat::toSql)
|
|
|
- .collect(Collectors.joining(StringPool.COMMA));
|
|
|
-
|
|
|
- String join = queryTableList.stream()
|
|
|
- .filter(item -> item instanceof TableJoin)
|
|
|
- .map(IFormat::toSql)
|
|
|
- .collect(Collectors.joining(StringPool.EMPTY));
|
|
|
-
|
|
|
- return from + join;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 拼接sql片段
|
|
|
*/
|
|
|
private String listFormatSql(List<? extends IFormat> list, String joinStr) {
|