From 832c161cfb0eae973a84dc79d3beca4a5ca3371d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 30 May 2023 13:09:22 +0800 Subject: [PATCH] feat: add schema support; close #I6VD6U --- .../java/com/mybatisflex/core/query/Join.java | 6 +- .../mybatisflex/core/query/QueryColumn.java | 2 +- .../mybatisflex/core/query/QueryTable.java | 31 ++++++- .../mybatisflex/core/query/QueryWrapper.java | 73 ++++++++++++---- .../com/mybatisflex/core/table/TableDef.java | 10 +++ .../com/mybatisflex/coretest/Account01.java | 84 +++++++++++++++++++ .../coretest/AccountSqlTester.java | 12 +++ .../processor/QueryEntityProcessor.java | 21 +++-- 8 files changed, 206 insertions(+), 33 deletions(-) create mode 100644 mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Account01.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java index 8f7c30de..848fe0d5 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java @@ -43,9 +43,9 @@ public class Join implements Serializable { protected QueryCondition on; protected boolean effective; - public Join(String type, String table, boolean when) { + public Join(String type, QueryTable table, boolean when) { this.type = type; - this.queryTable = new QueryTable(table); + this.queryTable = table; this.effective = when; } @@ -65,7 +65,7 @@ public class Join implements Serializable { this.on = condition; } - QueryCondition getOnCondition(){ + QueryCondition getOnCondition() { return on; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java index 9bf82fa0..eac99e3c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java @@ -53,7 +53,7 @@ public class QueryColumn implements Serializable { public QueryColumn(TableDef tableDef, String name) { SqlUtil.keepColumnSafely(name); - this.table = new QueryTable(tableDef.getTableName()); + this.table = new QueryTable(tableDef); this.name = name; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java index 2268fcb1..e0d37121 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java @@ -16,6 +16,8 @@ package com.mybatisflex.core.query; import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.table.TableDef; +import com.mybatisflex.core.util.StringUtil; import java.io.Serializable; import java.util.Objects; @@ -25,12 +27,18 @@ import java.util.Objects; */ public class QueryTable implements Serializable { + protected String schema; protected String name; protected String alias; public QueryTable() { } + public QueryTable(TableDef tableDef) { + this.name = tableDef.getTableName(); + this.schema = tableDef.getSchema(); + } + public QueryTable(String name) { this.name = name; } @@ -55,21 +63,38 @@ public class QueryTable implements Serializable { } boolean isSameTable(QueryTable table) { - return table != null && Objects.equals(name, table.name); + if (table == null) { + return false; + } + if (StringUtil.isNotBlank(alias) && StringUtil.isNotBlank(table.alias)) { + if (Objects.equals(alias, table.alias)) { + return false; + } + } + return Objects.equals(name, table.name); } + Object[] getValueArray() { return WrapperUtil.NULL_PARA_ARRAY; } public String toSql(IDialect dialect) { - return dialect.wrap(name) + WrapperUtil.buildAsAlias(alias, dialect); + String sql; + if (StringUtil.isNotBlank(schema)) { + sql = dialect.wrap(schema) + "." + dialect.wrap(name) + WrapperUtil.buildAsAlias(alias, dialect); + } else { + sql = dialect.wrap(name) + WrapperUtil.buildAsAlias(alias, dialect); + } + return sql; } + @Override public String toString() { return "QueryTable{" + - "name='" + name + '\'' + + "schema='" + schema + '\'' + + ", name='" + name + '\'' + ", alias='" + alias + '\'' + '}'; } 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 4db61cd0..4905172a 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 @@ -42,7 +42,7 @@ public class QueryWrapper extends BaseQueryWrapper { public QueryWrapper from(TableDef... tableDefs) { for (TableDef tableDef : tableDefs) { - from(new QueryTable(tableDef.getTableName())); + from(new QueryTable(tableDef)); } return this; } @@ -151,21 +151,20 @@ public class QueryWrapper extends BaseQueryWrapper { public Joiner leftJoin(String table) { - return joining(Join.TYPE_LEFT, table, true); + return joining(Join.TYPE_LEFT, new QueryTable(table), true); } public Joiner leftJoinIf(String table, boolean when) { - return joining(Join.TYPE_LEFT, table, when); + return joining(Join.TYPE_LEFT, new QueryTable(table), when); } public Joiner leftJoin(TableDef table) { - return joining(Join.TYPE_LEFT, table.getTableName(), true); + return joining(Join.TYPE_LEFT, new QueryTable(table), true); } - public Joiner leftJoinIf(TableDef table, boolean when) { - return joining(Join.TYPE_LEFT, table.getTableName(), when); + return joining(Join.TYPE_LEFT, new QueryTable(table), when); } public Joiner leftJoin(QueryWrapper table) { @@ -176,12 +175,21 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(Join.TYPE_LEFT, table, when); } + public Joiner rightJoin(String table) { - return joining(Join.TYPE_RIGHT, table, true); + return joining(Join.TYPE_RIGHT, new QueryTable(table), true); } public Joiner rightJoinIf(String table, boolean when) { - return joining(Join.TYPE_RIGHT, table, when); + return joining(Join.TYPE_RIGHT, new QueryTable(table), when); + } + + public Joiner rightJoinIf(TableDef table) { + return joining(Join.TYPE_RIGHT, new QueryTable(table), true); + } + + public Joiner rightJoinIf(TableDef table, boolean when) { + return joining(Join.TYPE_RIGHT, new QueryTable(table), when); } public Joiner rightJoin(QueryWrapper table) { @@ -192,12 +200,13 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(Join.TYPE_RIGHT, table, when); } + public Joiner innerJoin(String table) { - return joining(Join.TYPE_INNER, table, true); + return joining(Join.TYPE_INNER, new QueryTable(table), true); } public Joiner innerJoinIf(String table, boolean when) { - return joining(Join.TYPE_INNER, table, when); + return joining(Join.TYPE_INNER, new QueryTable(table), when); } public Joiner innerJoin(TableDef table) { @@ -205,7 +214,7 @@ public class QueryWrapper extends BaseQueryWrapper { } public Joiner innerJoinIf(TableDef table, boolean when) { - return joining(Join.TYPE_INNER, table.getTableName(), when); + return joining(Join.TYPE_INNER, new QueryTable(table), when); } public Joiner innerJoin(QueryWrapper table) { @@ -216,12 +225,21 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(Join.TYPE_INNER, table, when); } + public Joiner fullJoin(String table) { - return joining(Join.TYPE_FULL, table, true); + return joining(Join.TYPE_FULL, new QueryTable(table), true); } public Joiner fullJoinIf(String table, boolean when) { - return joining(Join.TYPE_FULL, table, when); + return joining(Join.TYPE_FULL, new QueryTable(table), when); + } + + public Joiner fullJoinIf(TableDef table) { + return joining(Join.TYPE_FULL, new QueryTable(table), true); + } + + public Joiner fullJoinIf(TableDef table, boolean when) { + return joining(Join.TYPE_FULL, new QueryTable(table), when); } public Joiner fullJoin(QueryWrapper table) { @@ -232,12 +250,21 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(Join.TYPE_FULL, table, when); } + public Joiner crossJoin(String table) { - return joining(Join.TYPE_CROSS, table, true); + return joining(Join.TYPE_CROSS, new QueryTable(table), true); } public Joiner crossJoinIf(String table, boolean when) { - return joining(Join.TYPE_CROSS, table, when); + return joining(Join.TYPE_CROSS, new QueryTable(table), when); + } + + public Joiner crossJoinIf(TableDef table) { + return joining(Join.TYPE_CROSS, new QueryTable(table), true); + } + + public Joiner crossJoinIf(TableDef table, boolean when) { + return joining(Join.TYPE_CROSS, new QueryTable(table), when); } public Joiner crossJoin(QueryWrapper table) { @@ -248,12 +275,21 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(Join.TYPE_CROSS, table, when); } + public Joiner join(String table) { - return joining(Join.TYPE_JOIN, table, true); + return joining(Join.TYPE_JOIN, new QueryTable(table), true); } public Joiner join(String table, boolean when) { - return joining(Join.TYPE_JOIN, table, when); + return joining(Join.TYPE_JOIN, new QueryTable(table), when); + } + + public Joiner join(TableDef table) { + return joining(Join.TYPE_JOIN, new QueryTable(table), true); + } + + public Joiner join(TableDef table, boolean when) { + return joining(Join.TYPE_JOIN, new QueryTable(table), when); } public Joiner join(QueryWrapper table) { @@ -264,6 +300,7 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(Join.TYPE_JOIN, table, when); } + public QueryWrapper union(QueryWrapper unionQuery) { if (unions == null) { unions = new ArrayList<>(); @@ -297,7 +334,7 @@ public class QueryWrapper extends BaseQueryWrapper { // } - protected Joiner joining(String type, String table, boolean condition) { + protected Joiner joining(String type, QueryTable table, boolean condition) { Join join = new Join(type, table, condition); addJoinTable(join.getQueryTable()); return new Joiner<>(addJoin(join), join); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableDef.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableDef.java index 1abe0b8e..e70f79d2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableDef.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableDef.java @@ -21,8 +21,14 @@ import java.io.Serializable; public class TableDef implements Serializable { + private String schema; private final String tableName; + public TableDef(String schema, String tableName) { + this.schema = schema; + this.tableName = tableName; + } + public TableDef(String tableName) { this.tableName = tableName; } @@ -31,6 +37,10 @@ public class TableDef implements Serializable { return tableName; } + public String getSchema() { + return schema; + } + public QueryTable as(String alias) { return new QueryTable(tableName, alias); } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Account01.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Account01.java new file mode 100644 index 00000000..7242448f --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Account01.java @@ -0,0 +1,84 @@ +package com.mybatisflex.coretest; + +import com.mybatisflex.annotation.Column; +import com.mybatisflex.annotation.Id; +import com.mybatisflex.annotation.Table; + +import java.util.Date; + +@Table(value = "tb_account",schema = "flex") +public class Account01 { + + @Id + private Long id; + + private String userName; + + private Date birthday; + + private int sex; + + private Integer age; + + private boolean isNormal; + + @Column(isLogicDelete = true) + private Boolean isDelete; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public int getSex() { + return sex; + } + + public void setSex(int sex) { + this.sex = sex; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public boolean isNormal() { + return isNormal; + } + + public void setNormal(boolean normal) { + isNormal = normal; + } + + public Boolean getDelete() { + return isDelete; + } + + public void setDelete(Boolean delete) { + isDelete = delete; + } +} 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 6e67d2eb..02ea99d2 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 @@ -13,6 +13,7 @@ import org.junit.Test; import java.util.Arrays; import static com.mybatisflex.core.query.QueryMethods.*; +import static com.mybatisflex.coretest.table.Account01TableDef.ACCOUNT01; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE; @@ -30,6 +31,17 @@ public class AccountSqlTester { System.out.println(sql); } + @Test + public void testSelectWithSchemaSql() { + QueryWrapper query = new QueryWrapper() + .select() + .from(ACCOUNT01); + + IDialect dialect = new CommonsDialectImpl(); + String sql = dialect.forSelectByQuery(query); + System.out.println(sql); + } + @Test public void testSelectColumnsSql() { QueryWrapper query = new QueryWrapper() diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/QueryEntityProcessor.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/QueryEntityProcessor.java index 07e9381b..1f667646 100644 --- a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/QueryEntityProcessor.java +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/QueryEntityProcessor.java @@ -81,7 +81,7 @@ public class QueryEntityProcessor extends AbstractProcessor { "@classesInfo" + "}\n"; - private static final String tableDefTemplate = "\n\n public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@tableName\");\n"; + private static final String tableDefTemplate = "\n\n public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@schema\", \"@tableName\");\n"; private static final String singleEntityClassTemplate = "package @package;\n" + "\n" + @@ -96,8 +96,8 @@ public class QueryEntityProcessor extends AbstractProcessor { "@defaultColumns" + "@allColumns" + "\n" + - " public @entityClassTableDef(String tableName) {\n" + - " super(tableName);\n" + + " public @entityClassTableDef(String schema, String tableName) {\n" + + " super(schema, tableName);\n" + " }\n" + "}\n"; @@ -109,8 +109,8 @@ public class QueryEntityProcessor extends AbstractProcessor { "@defaultColumns" + "@allColumns" + "\n" + - " public @entityClassTableDef(String tableName) {\n" + - " super(tableName);\n" + + " public @entityClassTableDef(String schema, String tableName) {\n" + + " super(schema, tableName);\n" + " }\n" + " }\n"; @@ -177,6 +177,10 @@ public class QueryEntityProcessor extends AbstractProcessor { } } + String schema = table != null && table.value().trim().length() != 0 + ? table.schema() + : ""; + String tableName = table != null && table.value().trim().length() != 0 ? table.value() : firstCharToLowerCase(entityClassElement.getSimpleName().toString()); @@ -203,13 +207,13 @@ public class QueryEntityProcessor extends AbstractProcessor { } if (allInTables) { - String content = buildTablesClass(entitySimpleName, tableName, propertyAndColumns, defaultColumns, tablesNameStyle, null, allInTables); + String content = buildTablesClass(entitySimpleName, schema, tableName, propertyAndColumns, defaultColumns, tablesNameStyle, null, allInTables); tablesContent.append(content); } //每一个 entity 生成一个独立的文件 else { String realGenPackage = genTablesPackage == null || genTablesPackage.trim().length() == 0 ? guessPackage.toString() : genTablesPackage; - String content = buildTablesClass(entitySimpleName, tableName, propertyAndColumns, defaultColumns, tablesNameStyle, realGenPackage, allInTables); + String content = buildTablesClass(entitySimpleName, schema, tableName, propertyAndColumns, defaultColumns, tablesNameStyle, realGenPackage, allInTables); genClass(genPath, realGenPackage, entitySimpleName + "TableDef", content); } @@ -327,12 +331,13 @@ public class QueryEntityProcessor extends AbstractProcessor { } - private String buildTablesClass(String entityClass, String tableName, Map propertyAndColumns + private String buildTablesClass(String entityClass, String schema, String tableName, Map propertyAndColumns , List defaultColumns, String tablesNameStyle, String realGenPackage, boolean allInTables) { // tableDefTemplate = " public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@tableName\");\n"; String tableDef = tableDefTemplate.replace("@entityClass", entityClass) + .replace("@schema", schema) .replace("@tableField", buildName(entityClass, tablesNameStyle)) .replace("@tableName", tableName);