|
@@ -232,19 +232,12 @@ public class IllegalSQLInnerInterceptor extends JsqlParserSupport implements Inn
|
|
|
//是否使用索引
|
|
|
boolean useIndexFlag = false;
|
|
|
if (StringUtils.isNotBlank(columnName)) {
|
|
|
- String tableInfo = table.getName();
|
|
|
+ String tableName = table.getName();
|
|
|
//表存在的索引
|
|
|
- String dbName = null;
|
|
|
- String tableName;
|
|
|
- String[] tableArray = tableInfo.split("\\.");
|
|
|
- if (tableArray.length == 1) {
|
|
|
- tableName = tableArray[0];
|
|
|
- } else {
|
|
|
- dbName = tableArray[0];
|
|
|
- tableName = tableArray[1];
|
|
|
- }
|
|
|
+ String dbName = getPartItemValue(table, 1);
|
|
|
+ String catalogName = getPartItemValue(table, 2);
|
|
|
columnName = SqlParserUtils.removeWrapperSymbol(columnName);
|
|
|
- List<IndexInfo> indexInfos = getIndexInfos(dbName, tableName, connection);
|
|
|
+ List<IndexInfo> indexInfos = getIndexInfos(catalogName, dbName, tableName, connection);
|
|
|
for (IndexInfo indexInfo : indexInfos) {
|
|
|
if (indexInfo.getColumnName().equalsIgnoreCase(columnName)) {
|
|
|
useIndexFlag = true;
|
|
@@ -253,10 +246,14 @@ public class IllegalSQLInnerInterceptor extends JsqlParserSupport implements Inn
|
|
|
}
|
|
|
}
|
|
|
if (!useIndexFlag) {
|
|
|
- throw new MybatisPlusException("非法SQL,SQL未使用到索引, table:" + table + ", columnName:" + columnName);
|
|
|
+ throw new MybatisPlusException("非法SQL,SQL未使用到索引, table:" + table.getName() + ", columnName:" + columnName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private String getPartItemValue(Table table, int index) {
|
|
|
+ return index < table.getNameParts().size() ? table.getNameParts().get(index) : null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证where条件的字段,是否有not、or等等,并且where的第一个字段,必须使用索引
|
|
|
*
|
|
@@ -315,10 +312,12 @@ public class IllegalSQLInnerInterceptor extends JsqlParserSupport implements Inn
|
|
|
/**
|
|
|
* 得到表的索引信息
|
|
|
*
|
|
|
- * @param dbName ignore
|
|
|
- * @param tableName ignore
|
|
|
- * @param conn ignore
|
|
|
- * @return ignore
|
|
|
+ * @param dbName 数据库名
|
|
|
+ * @param tableName 表名
|
|
|
+ * @param conn 数据库连接
|
|
|
+ * @return 索引信息
|
|
|
+ * @see #getIndexInfos(String, String, String, String, Connection)
|
|
|
+ * @deprecated 3.5.11
|
|
|
*/
|
|
|
public List<IndexInfo> getIndexInfos(String dbName, String tableName, Connection conn) {
|
|
|
return getIndexInfos(null, dbName, tableName, conn);
|
|
@@ -327,13 +326,31 @@ public class IllegalSQLInnerInterceptor extends JsqlParserSupport implements Inn
|
|
|
/**
|
|
|
* 得到表的索引信息
|
|
|
*
|
|
|
- * @param key ignore
|
|
|
- * @param dbName ignore
|
|
|
- * @param tableName ignore
|
|
|
- * @param conn ignore
|
|
|
- * @return ignore
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param dbName 数据库名
|
|
|
+ * @param tableName 表名
|
|
|
+ * @param conn 数据库连接
|
|
|
+ * @return 索引信息
|
|
|
+ * @see #getIndexInfos(String, String, String, String, Connection)
|
|
|
+ * @deprecated 3.5.11
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
public List<IndexInfo> getIndexInfos(String key, String dbName, String tableName, Connection conn) {
|
|
|
+ return getIndexInfos(key, null, dbName, tableName, conn);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到表的索引信息
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param catalogName catalogName
|
|
|
+ * @param dbName 数据库名
|
|
|
+ * @param tableName 表名
|
|
|
+ * @param conn 数据库连接
|
|
|
+ * @return 索引信息
|
|
|
+ * @since 3.5.11
|
|
|
+ */
|
|
|
+ public List<IndexInfo> getIndexInfos(String key, String catalogName, String dbName, String tableName, Connection conn) {
|
|
|
List<IndexInfo> indexInfos = null;
|
|
|
if (StringUtils.isNotBlank(key)) {
|
|
|
indexInfos = indexInfoMap.get(key);
|
|
@@ -342,7 +359,7 @@ public class IllegalSQLInnerInterceptor extends JsqlParserSupport implements Inn
|
|
|
ResultSet rs;
|
|
|
try {
|
|
|
DatabaseMetaData metadata = conn.getMetaData();
|
|
|
- String catalog = StringUtils.isBlank(dbName) ? conn.getCatalog() : dbName;
|
|
|
+ String catalog = StringUtils.isBlank(catalogName) ? conn.getCatalog() : catalogName;
|
|
|
String schema = StringUtils.isBlank(dbName) ? conn.getSchema() : dbName;
|
|
|
rs = metadata.getIndexInfo(catalog, schema, SqlParserUtils.removeWrapperSymbol(tableName), false, true);
|
|
|
indexInfos = new ArrayList<>();
|