#1749 - turn table name to lowercase when backend mysql is case sensitive. (#1754) (#2081)

Co-authored-by: Collapsar <838800176@qq.com>
This commit is contained in:
zhengfang.sun
2020-09-01 10:51:17 +08:00
committed by GitHub
parent 6f4622eb32
commit 3bf92291aa
2 changed files with 19 additions and 20 deletions
@@ -489,7 +489,7 @@ public class ServerSchemaStatVisitor extends MySqlSchemaStatVisitor {
tableName = getOwnerTableName(betweenExpr, column);
}
if (tableName != null && !"".equals(tableName)) {
checkAliasInColumn(tableName);
checkAliasInColumn(tableName, false);
return new Column(tableName, column);
}
}
@@ -509,42 +509,43 @@ public class ServerSchemaStatVisitor extends MySqlSchemaStatVisitor {
private Column getColumnByExpr(SQLPropertyExpr expr) {
SQLExpr owner = expr.getOwner();
String column = expr.getName();
boolean containSchema = false;
if (owner instanceof SQLIdentifierExpr || owner instanceof SQLPropertyExpr) {
String tableName;
if (owner instanceof SQLPropertyExpr) {
tableName = ((SQLPropertyExpr) owner).getName();
if (((SQLPropertyExpr) owner).getOwner() instanceof SQLIdentifierExpr) {
containSchema = true;
tableName = ((SQLIdentifierExpr) ((SQLPropertyExpr) owner).getOwner()).getName() + "." + tableName;
}
} else {
tableName = ((SQLIdentifierExpr) owner).getName();
}
checkAliasInColumn(tableName);
checkAliasInColumn(tableName, containSchema);
return new Column(tableName, column);
}
return null;
}
private void checkAliasInColumn(String tableName) {
private void checkAliasInColumn(String tableName, boolean containSchema) {
if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
tableName = tableName.toLowerCase();
}
if (aliasMap.containsKey(tableName)) {
return;
}
if (containSchema) {
putAliasToMap(tableName, tableName.replace("`", ""));
return;
}
String tempStr;
if (StringUtil.isAlias(tableName)) {
if (StringUtil.containsApostrophe(tableName)) {
tempStr = tableName.replace("`", "");
} else {
tempStr = "`" + tableName + "`";
}
if (aliasMap.containsKey(tempStr)) {
putAliasToMap(tableName, aliasMap.get(tempStr));
} else {
putAliasToMap(tableName, tableName.replace("`", ""));
}
putAliasToMap(tableName, aliasMap.getOrDefault(tempStr, tempStr));
}
/**
@@ -926,6 +927,7 @@ public class ServerSchemaStatVisitor extends MySqlSchemaStatVisitor {
if (name != null) {
if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
name = name.toLowerCase();
value = value.toLowerCase();
}
aliasMap.put(name, value);
}
@@ -1,8 +1,8 @@
/*
* Copyright (C) 2016-2020 ActionTech.
* based on code by MyCATCopyrightHolder Copyright (c) 2013, OpenCloudDB/MyCAT.
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.
*/
* Copyright (C) 2016-2020 ActionTech.
* based on code by MyCATCopyrightHolder Copyright (c) 2013, OpenCloudDB/MyCAT.
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.
*/
package com.actiontech.dble.util;
import com.actiontech.dble.backend.mysql.CharsetUtil;
@@ -493,12 +493,9 @@ public final class StringUtil {
return str;
}
public static boolean isAlias(String aliasName) {
if (aliasName.contains("`.") || aliasName.contains(".`") || aliasName.contains(".")) {
return false;
}
char firstValue = aliasName.charAt(0);
return (firstValue == '`') && (firstValue == aliasName.charAt(aliasName.length() - 1));
public static boolean containsApostrophe(String tableName) {
char firstValue = tableName.charAt(0);
return (firstValue == '`') && (firstValue == tableName.charAt(tableName.length() - 1));
}
public static String removeAllApostrophe(String str) {