mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
feat: add dynamic table support; close #I6VRMF #I6WS3E #I72X21
This commit is contained in:
parent
d35155ea22
commit
47f41d4e05
@ -18,6 +18,7 @@ package com.mybatisflex.core.dialect;
|
|||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
|
import com.mybatisflex.core.table.TableManager;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -25,6 +26,14 @@ public interface IDialect {
|
|||||||
|
|
||||||
String wrap(String keyword);
|
String wrap(String keyword);
|
||||||
|
|
||||||
|
default String getRealTable(String table) {
|
||||||
|
return TableManager.getRealTable(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
default String getRealSchema(String schema) {
|
||||||
|
return TableManager.getRealSchema(schema);
|
||||||
|
}
|
||||||
|
|
||||||
String forHint(String hintString);
|
String forHint(String hintString);
|
||||||
|
|
||||||
String forInsertRow(String tableName, Row row);
|
String forInsertRow(String tableName, Row row);
|
||||||
@ -54,7 +63,6 @@ public interface IDialect {
|
|||||||
String buildWhereConditionSql(QueryWrapper queryWrapper);
|
String buildWhereConditionSql(QueryWrapper queryWrapper);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//////for entity /////
|
//////for entity /////
|
||||||
String forInsertEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls);
|
String forInsertEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls);
|
||||||
|
|
||||||
|
|||||||
@ -59,6 +59,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
return "*".equals(keyword) ? keyword : keywordWrap.wrap(keyword);
|
return "*".equals(keyword) ? keyword : keywordWrap.wrap(keyword);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String forHint(String hintString) {
|
public String forHint(String hintString) {
|
||||||
return StringUtil.isNotBlank(hintString) ? "/*+ " + hintString + " */ " : "";
|
return StringUtil.isNotBlank(hintString) ? "/*+ " + hintString + " */ " : "";
|
||||||
@ -81,7 +82,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
String sql = "INSERT INTO " + wrap(tableName) +
|
String sql = "INSERT INTO " + wrap(getRealTable(tableName)) +
|
||||||
"(" + fields + ") VALUES " +
|
"(" + fields + ") VALUES " +
|
||||||
"(" + questions + ")";
|
"(" + questions + ")";
|
||||||
return sql;
|
return sql;
|
||||||
@ -111,7 +112,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String sql = "INSERT INTO " + wrap(tableName) +
|
String sql = "INSERT INTO " + wrap(getRealTable(tableName)) +
|
||||||
"(" + fields + ") VALUES " + questions;
|
"(" + fields + ") VALUES " + questions;
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
@ -121,7 +122,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forDeleteById(String tableName, String[] primaryKeys) {
|
public String forDeleteById(String tableName, String[] primaryKeys) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append("DELETE FROM ");
|
sql.append("DELETE FROM ");
|
||||||
sql.append(wrap(tableName));
|
sql.append(wrap(getRealTable(tableName)));
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
for (int i = 0; i < primaryKeys.length; i++) {
|
for (int i = 0; i < primaryKeys.length; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
@ -137,7 +138,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forDeleteBatchByIds(String tableName, String[] primaryKeys, Object[] ids) {
|
public String forDeleteBatchByIds(String tableName, String[] primaryKeys, Object[] ids) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append("DELETE FROM ");
|
sql.append("DELETE FROM ");
|
||||||
sql.append(wrap(tableName));
|
sql.append(wrap(getRealTable(tableName)));
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
|
|
||||||
//多主键的场景
|
//多主键的场景
|
||||||
@ -180,7 +181,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
Set<String> modifyAttrs = row.obtainModifyAttrs();
|
Set<String> modifyAttrs = row.obtainModifyAttrs();
|
||||||
String[] primaryKeys = RowCPI.obtainsPrimaryKeyStrings(row);
|
String[] primaryKeys = RowCPI.obtainsPrimaryKeyStrings(row);
|
||||||
|
|
||||||
sql.append("UPDATE ").append(wrap(tableName)).append(" SET ");
|
sql.append("UPDATE ").append(wrap(getRealTable(tableName))).append(" SET ");
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (Map.Entry<String, Object> e : row.entrySet()) {
|
for (Map.Entry<String, Object> e : row.entrySet()) {
|
||||||
String colName = e.getKey();
|
String colName = e.getKey();
|
||||||
@ -215,7 +216,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String tableName = queryTables.get(0).getName();
|
String tableName = queryTables.get(0).getName();
|
||||||
sql.append("UPDATE ").append(wrap(tableName)).append(" SET ");
|
sql.append("UPDATE ").append(wrap(getRealTable(tableName))).append(" SET ");
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (String modifyAttr : modifyAttrs) {
|
for (String modifyAttr : modifyAttrs) {
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
@ -249,7 +250,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
@Override
|
@Override
|
||||||
public String forSelectOneById(String tableName, String[] primaryKeys, Object[] primaryValues) {
|
public String forSelectOneById(String tableName, String[] primaryKeys, Object[] primaryValues) {
|
||||||
StringBuilder sql = new StringBuilder("SELECT * FROM ");
|
StringBuilder sql = new StringBuilder("SELECT * FROM ");
|
||||||
sql.append(wrap(tableName)).append(" WHERE ");
|
sql.append(wrap(getRealTable(tableName))).append(" WHERE ");
|
||||||
for (int i = 0; i < primaryKeys.length; i++) {
|
for (int i = 0; i < primaryKeys.length; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
sql.append(" AND ");
|
sql.append(" AND ");
|
||||||
@ -875,7 +876,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|| normalValueOfLogicDelete instanceof Boolean) {
|
|| normalValueOfLogicDelete instanceof Boolean) {
|
||||||
return normalValueOfLogicDelete;
|
return normalValueOfLogicDelete;
|
||||||
}
|
}
|
||||||
return "'" + normalValueOfLogicDelete.toString() + "'";
|
return "'" + normalValueOfLogicDelete + "'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -885,7 +886,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|| deletedValueOfLogicDelete instanceof Boolean) {
|
|| deletedValueOfLogicDelete instanceof Boolean) {
|
||||||
return deletedValueOfLogicDelete;
|
return deletedValueOfLogicDelete;
|
||||||
}
|
}
|
||||||
return "'" + deletedValueOfLogicDelete.toString() + "'";
|
return "'" + deletedValueOfLogicDelete + "'";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,10 +18,7 @@ package com.mybatisflex.core.query;
|
|||||||
|
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.table.TableDef;
|
import com.mybatisflex.core.table.TableDef;
|
||||||
import com.mybatisflex.core.util.LambdaGetter;
|
import com.mybatisflex.core.util.*;
|
||||||
import com.mybatisflex.core.util.LambdaUtil;
|
|
||||||
import com.mybatisflex.core.util.SqlUtil;
|
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -45,9 +42,9 @@ public class QueryColumn implements Serializable {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryColumn(String tableName, String name) {
|
public QueryColumn(String schema, String tableName, String name) {
|
||||||
SqlUtil.keepColumnSafely(name);
|
SqlUtil.keepColumnSafely(name);
|
||||||
this.table = new QueryTable(tableName);
|
this.table = new QueryTable(schema, tableName);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,25 +355,56 @@ public class QueryColumn implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String wrap(IDialect dialect, String table, String column) {
|
|
||||||
if (StringUtil.isNotBlank(table)) {
|
|
||||||
return dialect.wrap(table) + "." + dialect.wrap(column);
|
|
||||||
} else {
|
|
||||||
return dialect.wrap(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
|
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
|
||||||
String tableName = WrapperUtil.getColumnTableName(queryTables, table);
|
QueryTable selectTable = getSelectTable(queryTables, table);
|
||||||
return wrap(dialect, tableName, name);
|
if (selectTable == null) {
|
||||||
|
return dialect.wrap(name);
|
||||||
|
} else {
|
||||||
|
if (StringUtil.isNotBlank(selectTable.alias)) {
|
||||||
|
return dialect.wrap(selectTable.alias) + "." + dialect.wrap(name);
|
||||||
|
} else if (StringUtil.isNotBlank(selectTable.getSchema()) && StringUtil.isNotBlank(selectTable.getName())) {
|
||||||
|
return dialect.wrap(dialect.getRealSchema(selectTable.schema)) + "." + dialect.wrap(dialect.getRealTable(selectTable.getName())) + "." + dialect.wrap(name);
|
||||||
|
} else if (StringUtil.isNotBlank(selectTable.getName())) {
|
||||||
|
return dialect.wrap(dialect.getRealTable(selectTable.getName())) + "." + dialect.wrap(name);
|
||||||
|
} else {
|
||||||
|
return dialect.wrap(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
|
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
|
||||||
String tableName = WrapperUtil.getColumnTableName(queryTables, table);
|
return toConditionSql(queryTables, dialect) + WrapperUtil.buildAsAlias(alias, dialect);
|
||||||
return wrap(dialect, tableName, name) + WrapperUtil.buildAsAlias(alias, dialect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QueryTable getSelectTable(List<QueryTable> queryTables, QueryTable columnTable) {
|
||||||
|
if (queryTables == null || queryTables.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queryTables.size() == 1 && queryTables.get(0).isSameTable(columnTable)) {
|
||||||
|
//ignore table
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CollectionUtil.isEmpty(queryTables)) {
|
||||||
|
return columnTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (columnTable == null && queryTables.size() == 1) {
|
||||||
|
return queryTables.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (QueryTable table : queryTables) {
|
||||||
|
if (table.isSameTable(columnTable)) {
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return columnTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "QueryColumn{" +
|
return "QueryColumn{" +
|
||||||
|
|||||||
@ -62,9 +62,9 @@ public class QueryCondition implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static QueryCondition create(String table, String column, String logic, Object value) {
|
public static QueryCondition create(String schema, String table, String column, String logic, Object value) {
|
||||||
QueryCondition condition = new QueryCondition();
|
QueryCondition condition = new QueryCondition();
|
||||||
condition.setColumn(new QueryColumn(table, column));
|
condition.setColumn(new QueryColumn(schema, table, column));
|
||||||
condition.setLogic(logic);
|
condition.setLogic(logic);
|
||||||
condition.setValue(value);
|
condition.setValue(value);
|
||||||
return condition;
|
return condition;
|
||||||
|
|||||||
@ -43,11 +43,25 @@ public class QueryTable implements Serializable {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryTable(String table, String alias) {
|
public QueryTable(String schema, String name) {
|
||||||
|
this.schema = schema;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryTable(String schema, String table, String alias) {
|
||||||
|
this.schema = schema;
|
||||||
this.name = table;
|
this.name = table;
|
||||||
this.alias = alias;
|
this.alias = alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSchema() {
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchema(String schema) {
|
||||||
|
this.schema = schema;
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -82,9 +96,9 @@ public class QueryTable implements Serializable {
|
|||||||
public String toSql(IDialect dialect) {
|
public String toSql(IDialect dialect) {
|
||||||
String sql;
|
String sql;
|
||||||
if (StringUtil.isNotBlank(schema)) {
|
if (StringUtil.isNotBlank(schema)) {
|
||||||
sql = dialect.wrap(schema) + "." + dialect.wrap(name) + WrapperUtil.buildAsAlias(alias, dialect);
|
sql = dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(name)) + WrapperUtil.buildAsAlias(alias, dialect);
|
||||||
} else {
|
} else {
|
||||||
sql = dialect.wrap(name) + WrapperUtil.buildAsAlias(alias, dialect);
|
sql = dialect.wrap(dialect.getRealTable(name)) + WrapperUtil.buildAsAlias(alias, dialect);
|
||||||
}
|
}
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,6 @@ package com.mybatisflex.core.query;
|
|||||||
|
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
|
||||||
import com.mybatisflex.core.util.EnumWrapper;
|
import com.mybatisflex.core.util.EnumWrapper;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
@ -132,39 +131,7 @@ class WrapperUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getColumnTableName(List<QueryTable> queryTables, QueryTable queryTable) {
|
|
||||||
if (queryTables == null) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (queryTables.size() == 1 && queryTables.get(0).isSameTable(queryTable)) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
QueryTable realTable = getRealTable(queryTables, queryTable);
|
|
||||||
if (realTable == null) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return StringUtil.isNotBlank(realTable.alias) ? realTable.alias : realTable.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static QueryTable getRealTable(List<QueryTable> queryTables, QueryTable queryTable) {
|
|
||||||
if (CollectionUtil.isEmpty(queryTables)) {
|
|
||||||
return queryTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (queryTable == null && queryTables.size() == 1) {
|
|
||||||
return queryTables.get(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (QueryTable table : queryTables) {
|
|
||||||
if (table.isSameTable(queryTable)) {
|
|
||||||
return table;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return queryTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.table;
|
||||||
|
|
||||||
|
public interface DynamicSchemaProcessor {
|
||||||
|
String process(String schema);
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.table;
|
||||||
|
|
||||||
|
public interface DynamicTableProcessor {
|
||||||
|
String process(String tableName);
|
||||||
|
}
|
||||||
@ -42,7 +42,7 @@ public class TableDef implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public QueryTable as(String alias) {
|
public QueryTable as(String alias) {
|
||||||
return new QueryTable(tableName, alias);
|
return new QueryTable(schema, tableName, alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -577,12 +577,12 @@ public class TableInfo {
|
|||||||
if (versionValue == null) {
|
if (versionValue == null) {
|
||||||
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
||||||
}
|
}
|
||||||
queryWrapper.and(QueryCondition.create(tableName, versionColumn, QueryCondition.LOGIC_EQUALS, versionValue));
|
queryWrapper.and(QueryCondition.create(schema, tableName, versionColumn, QueryCondition.LOGIC_EQUALS, versionValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
//逻辑删除
|
//逻辑删除
|
||||||
if (StringUtil.isNotBlank(logicDeleteColumn)) {
|
if (StringUtil.isNotBlank(logicDeleteColumn)) {
|
||||||
queryWrapper.and(QueryCondition.create(tableName, logicDeleteColumn, QueryCondition.LOGIC_EQUALS
|
queryWrapper.and(QueryCondition.create(schema, tableName, logicDeleteColumn, QueryCondition.LOGIC_EQUALS
|
||||||
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
, FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -590,9 +590,9 @@ public class TableInfo {
|
|||||||
Object[] tenantIdArgs = buildTenantIdArgs();
|
Object[] tenantIdArgs = buildTenantIdArgs();
|
||||||
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
if (ArrayUtil.isNotEmpty(tenantIdArgs)) {
|
||||||
if (tenantIdArgs.length == 1) {
|
if (tenantIdArgs.length == 1) {
|
||||||
queryWrapper.and(QueryCondition.create(tableName, tenantIdColumn, QueryCondition.LOGIC_EQUALS, tenantIdArgs[0]));
|
queryWrapper.and(QueryCondition.create(schema, tableName, tenantIdColumn, QueryCondition.LOGIC_EQUALS, tenantIdArgs[0]));
|
||||||
} else {
|
} else {
|
||||||
queryWrapper.and(QueryCondition.create(tableName, tenantIdColumn, QueryCondition.LOGIC_IN, tenantIdArgs));
|
queryWrapper.and(QueryCondition.create(schema, tableName, tenantIdColumn, QueryCondition.LOGIC_IN, tenantIdArgs));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -647,7 +647,7 @@ public class TableInfo {
|
|||||||
|
|
||||||
public List<QueryColumn> getDefaultQueryColumn() {
|
public List<QueryColumn> getDefaultQueryColumn() {
|
||||||
return Arrays.stream(defaultColumns)
|
return Arrays.stream(defaultColumns)
|
||||||
.map(name -> new QueryColumn(getTableName(), name))
|
.map(name -> new QueryColumn(schema, getTableName(), name))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,110 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.table;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TableManager {
|
||||||
|
|
||||||
|
private static DynamicSchemaProcessor dynamicSchemaProcessor;
|
||||||
|
private static DynamicTableProcessor dynamicTableProcessor;
|
||||||
|
|
||||||
|
private static final ThreadLocal<Map<String, String>> tableNameMappingTL = ThreadLocal.withInitial(HashMap::new);
|
||||||
|
private static final ThreadLocal<Map<String, String>> schemaMappingTL = ThreadLocal.withInitial(HashMap::new);
|
||||||
|
|
||||||
|
|
||||||
|
public static DynamicSchemaProcessor getDynamicSchemaProcessor() {
|
||||||
|
return dynamicSchemaProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setDynamicSchemaProcessor(DynamicSchemaProcessor dynamicSchemaProcessor) {
|
||||||
|
TableManager.dynamicSchemaProcessor = dynamicSchemaProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DynamicTableProcessor getDynamicTableProcessor() {
|
||||||
|
return dynamicTableProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setDynamicTableProcessor(DynamicTableProcessor dynamicTableProcessor) {
|
||||||
|
TableManager.dynamicTableProcessor = dynamicTableProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void setHintTableMapping(String tableName, String mappingTable) {
|
||||||
|
tableNameMappingTL.get().put(tableName, mappingTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getHintTableMapping(String tableName) {
|
||||||
|
return tableNameMappingTL.get().get(tableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setHintSchemaMapping(String schema, String mappingSchema) {
|
||||||
|
schemaMappingTL.get().put(schema, mappingSchema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getHintSchemaMapping(String schema) {
|
||||||
|
return schemaMappingTL.get().get(schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getRealTable(String tableName) {
|
||||||
|
if (dynamicTableProcessor == null) {
|
||||||
|
return tableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> mapping = tableNameMappingTL.get();
|
||||||
|
|
||||||
|
String dynamicTableName = mapping.get(tableName);
|
||||||
|
if (StringUtil.isNotBlank(dynamicTableName)) {
|
||||||
|
return dynamicTableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamicTableName = dynamicTableProcessor.process(tableName);
|
||||||
|
mapping.put(tableName, dynamicTableName);
|
||||||
|
return dynamicTableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getRealSchema(String schema) {
|
||||||
|
if (dynamicSchemaProcessor == null) {
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> mapping = schemaMappingTL.get();
|
||||||
|
String dynamiSchema = mapping.get(schema);
|
||||||
|
if (StringUtil.isNotBlank(dynamiSchema)) {
|
||||||
|
return dynamiSchema;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamiSchema = dynamicSchemaProcessor.process(schema);
|
||||||
|
mapping.put(schema, dynamiSchema);
|
||||||
|
return dynamiSchema;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// public static void clear() {
|
||||||
|
// if (dynamicTableProcessor != null) {
|
||||||
|
// tableNameMappingTL.remove();
|
||||||
|
// }
|
||||||
|
// if (dynamicSchemaProcessor != null) {
|
||||||
|
// schemaMappingTL.remove();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@ import com.mybatisflex.annotation.Table;
|
|||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@Table(value = "tb_account",schema = "flex")
|
@Table(value = "tb_a01",schema = "flex")
|
||||||
public class Account01 {
|
public class Account01 {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
|||||||
@ -1,13 +1,14 @@
|
|||||||
package com.mybatisflex.coretest;
|
package com.mybatisflex.coretest;
|
||||||
|
|
||||||
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
|
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.dialect.KeywordWrap;
|
import com.mybatisflex.core.dialect.KeywordWrap;
|
||||||
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
|
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
|
||||||
|
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
|
||||||
import com.mybatisflex.core.query.CPI;
|
import com.mybatisflex.core.query.CPI;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
import com.mybatisflex.core.table.TableInfoFactory;
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
|
import com.mybatisflex.core.table.TableManager;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -42,6 +43,36 @@ public class AccountSqlTester {
|
|||||||
System.out.println(sql);
|
System.out.println(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSelectWithSchemaSql01() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT01).leftJoin(ACCOUNT).on(ACCOUNT01.ID.eq(ACCOUNT.ID))
|
||||||
|
.where(ACCOUNT01.ID.ge(100))
|
||||||
|
.and(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
TableManager.setDynamicTableProcessor(original -> original+"_01");
|
||||||
|
TableManager.setDynamicTableProcessor(original -> original+"_01");
|
||||||
|
|
||||||
|
System.out.println(query.toDebugSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSelectWithSchemaSql02() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT01).as("a1").leftJoin(ACCOUNT).on(ACCOUNT01.ID.eq(ACCOUNT.ID))
|
||||||
|
.where(ACCOUNT01.ID.ge(100))
|
||||||
|
.and(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
TableManager.setDynamicTableProcessor(original -> original+"_01");
|
||||||
|
TableManager.setDynamicTableProcessor(original -> original+"_01");
|
||||||
|
|
||||||
|
System.out.println(query.toDebugSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectColumnsSql() {
|
public void testSelectColumnsSql() {
|
||||||
QueryWrapper query = new QueryWrapper()
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
|||||||
@ -21,15 +21,11 @@ import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
|||||||
import com.mybatisflex.core.audit.MessageCollector;
|
import com.mybatisflex.core.audit.MessageCollector;
|
||||||
import com.mybatisflex.core.paginate.Page;
|
import com.mybatisflex.core.paginate.Page;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.row.Db;
|
|
||||||
import com.mybatisflex.core.row.RowUtil;
|
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.select;
|
|
||||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||||
import static com.mybatisflex.test.table.ArticleTableDef.ARTICLE;
|
import static com.mybatisflex.test.table.ArticleTableDef.ARTICLE;
|
||||||
|
|
||||||
@ -138,7 +134,7 @@ public class EntityTestStarter {
|
|||||||
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||||
.where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
.where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
||||||
|
|
||||||
RowUtil.printPretty(Db.selectListByQuery(asWrapper));
|
// RowUtil.printPretty(Db.selectListByQuery(asWrapper));
|
||||||
//
|
//
|
||||||
// List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
// List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
||||||
// System.out.println(articleDTOS);
|
// System.out.println(articleDTOS);
|
||||||
@ -195,15 +191,15 @@ public class EntityTestStarter {
|
|||||||
// .or(SYS_CONFIG.TYPE.like(word).when(StrChecker.isNotBlank(word)))
|
// .or(SYS_CONFIG.TYPE.like(word).when(StrChecker.isNotBlank(word)))
|
||||||
// );
|
// );
|
||||||
|
|
||||||
List<Account> accounts = accountMapper.selectListByQuery(
|
// List<Account> accounts = accountMapper.selectListByQuery(
|
||||||
select().where(ACCOUNT.AGE.ge(18).when(false))
|
// select().where(ACCOUNT.AGE.ge(18).when(false))
|
||||||
.and(ACCOUNT.USER_NAME.like("aaaa").when(false)
|
// .and(ACCOUNT.USER_NAME.like("aaaa").when(false)
|
||||||
.or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
// .or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
||||||
.or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
// .or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
||||||
.or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
// .or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
||||||
)
|
// )
|
||||||
);
|
// );
|
||||||
System.out.println(accounts);
|
// System.out.println(accounts);
|
||||||
|
|
||||||
|
|
||||||
// Page<Account> paginate = accountMapper.paginate(1, 10, QueryWrapper.create());
|
// Page<Account> paginate = accountMapper.paginate(1, 10, QueryWrapper.create());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user