mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: add schema support; close #I6VD6U
This commit is contained in:
parent
40d0513259
commit
832c161cfb
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
|
||||
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<QueryWrapper> {
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> leftJoin(String table) {
|
||||
return joining(Join.TYPE_LEFT, table, true);
|
||||
return joining(Join.TYPE_LEFT, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> leftJoinIf(String table, boolean when) {
|
||||
return joining(Join.TYPE_LEFT, table, when);
|
||||
return joining(Join.TYPE_LEFT, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> leftJoin(TableDef table) {
|
||||
return joining(Join.TYPE_LEFT, table.getTableName(), true);
|
||||
return joining(Join.TYPE_LEFT, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> leftJoinIf(TableDef table, boolean when) {
|
||||
return joining(Join.TYPE_LEFT, table.getTableName(), when);
|
||||
return joining(Join.TYPE_LEFT, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> leftJoin(QueryWrapper table) {
|
||||
@ -176,12 +175,21 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(Join.TYPE_LEFT, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> rightJoin(String table) {
|
||||
return joining(Join.TYPE_RIGHT, table, true);
|
||||
return joining(Join.TYPE_RIGHT, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> rightJoinIf(String table, boolean when) {
|
||||
return joining(Join.TYPE_RIGHT, table, when);
|
||||
return joining(Join.TYPE_RIGHT, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> rightJoinIf(TableDef table) {
|
||||
return joining(Join.TYPE_RIGHT, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> rightJoinIf(TableDef table, boolean when) {
|
||||
return joining(Join.TYPE_RIGHT, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> rightJoin(QueryWrapper table) {
|
||||
@ -192,12 +200,13 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(Join.TYPE_RIGHT, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> innerJoin(String table) {
|
||||
return joining(Join.TYPE_INNER, table, true);
|
||||
return joining(Join.TYPE_INNER, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> innerJoinIf(String table, boolean when) {
|
||||
return joining(Join.TYPE_INNER, table, when);
|
||||
return joining(Join.TYPE_INNER, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> innerJoin(TableDef table) {
|
||||
@ -205,7 +214,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> innerJoinIf(TableDef table, boolean when) {
|
||||
return joining(Join.TYPE_INNER, table.getTableName(), when);
|
||||
return joining(Join.TYPE_INNER, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> innerJoin(QueryWrapper table) {
|
||||
@ -216,12 +225,21 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(Join.TYPE_INNER, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> fullJoin(String table) {
|
||||
return joining(Join.TYPE_FULL, table, true);
|
||||
return joining(Join.TYPE_FULL, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> fullJoinIf(String table, boolean when) {
|
||||
return joining(Join.TYPE_FULL, table, when);
|
||||
return joining(Join.TYPE_FULL, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> fullJoinIf(TableDef table) {
|
||||
return joining(Join.TYPE_FULL, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> fullJoinIf(TableDef table, boolean when) {
|
||||
return joining(Join.TYPE_FULL, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> fullJoin(QueryWrapper table) {
|
||||
@ -232,12 +250,21 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(Join.TYPE_FULL, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> crossJoin(String table) {
|
||||
return joining(Join.TYPE_CROSS, table, true);
|
||||
return joining(Join.TYPE_CROSS, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> crossJoinIf(String table, boolean when) {
|
||||
return joining(Join.TYPE_CROSS, table, when);
|
||||
return joining(Join.TYPE_CROSS, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> crossJoinIf(TableDef table) {
|
||||
return joining(Join.TYPE_CROSS, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> crossJoinIf(TableDef table, boolean when) {
|
||||
return joining(Join.TYPE_CROSS, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> crossJoin(QueryWrapper table) {
|
||||
@ -248,12 +275,21 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(Join.TYPE_CROSS, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> join(String table) {
|
||||
return joining(Join.TYPE_JOIN, table, true);
|
||||
return joining(Join.TYPE_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> join(String table, boolean when) {
|
||||
return joining(Join.TYPE_JOIN, table, when);
|
||||
return joining(Join.TYPE_JOIN, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> join(TableDef table) {
|
||||
return joining(Join.TYPE_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> join(TableDef table, boolean when) {
|
||||
return joining(Join.TYPE_JOIN, new QueryTable(table), when);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> join(QueryWrapper table) {
|
||||
@ -264,6 +300,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
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<QueryWrapper> {
|
||||
// }
|
||||
|
||||
|
||||
protected Joiner<QueryWrapper> joining(String type, String table, boolean condition) {
|
||||
protected Joiner<QueryWrapper> joining(String type, QueryTable table, boolean condition) {
|
||||
Join join = new Join(type, table, condition);
|
||||
addJoinTable(join.getQueryTable());
|
||||
return new Joiner<>(addJoin(join), join);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
|
||||
@ -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<String, String> propertyAndColumns
|
||||
private String buildTablesClass(String entityClass, String schema, String tableName, Map<String, String> propertyAndColumns
|
||||
, List<String> 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);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user