From 85d85e90ee5a0b2442c08752888c3bd493cc4b49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?=
<15040126243@163.com>
Date: Sun, 28 Jul 2024 00:02:40 +0800
Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E7=94=9F=E6=88=90=20=E6=95=B0=E6=8D=AE=E6=BA=90=E5=88=87?=
=?UTF-8?q?=E6=8D=A2=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 2 +-
.../java/org/anyline/proxy/CacheProxy.java | 349 ++++++++++++++++++
.../gen/config/MyBatisDataSourceMonitor.java | 7 +
.../gen/service/GenTableServiceImpl.java | 11 +-
.../dromara/gen/service/IGenTableService.java | 9 +
5 files changed, 371 insertions(+), 7 deletions(-)
create mode 100644 ruoyi-modules/ruoyi-gen/src/main/java/org/anyline/proxy/CacheProxy.java
diff --git a/pom.xml b/pom.xml
index ed6e9597..6a02d1f8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,7 +58,7 @@
3.2.1
- 8.7.2-20240726
+ 8.7.2-20240727
7.0.1
diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/org/anyline/proxy/CacheProxy.java b/ruoyi-modules/ruoyi-gen/src/main/java/org/anyline/proxy/CacheProxy.java
new file mode 100644
index 00000000..b264fc95
--- /dev/null
+++ b/ruoyi-modules/ruoyi-gen/src/main/java/org/anyline/proxy/CacheProxy.java
@@ -0,0 +1,349 @@
+/*
+ * Copyright 2006-2023 www.anyline.org
+ *
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.anyline.proxy;
+
+import org.anyline.cache.CacheProvider;
+import org.anyline.data.param.ConfigStore;
+import org.anyline.data.runtime.DataRuntime;
+import org.anyline.entity.OriginRow;
+import org.anyline.metadata.*;
+import org.anyline.metadata.graph.EdgeTable;
+import org.anyline.metadata.graph.VertexTable;
+import org.anyline.util.ConfigTable;
+import org.anyline.util.encrypt.MD5Util;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+
+public class CacheProxy {
+
+ private static final ThreadLocal thread_caches = new ThreadLocal<>();
+ private static final ThreadLocal thread_names = new ThreadLocal<>();
+ private static OriginRow application_caches = new OriginRow();
+ private static OriginRow application_names = new OriginRow();
+ public static CacheProvider provider;
+ public CacheProxy() {}
+ public static void init(CacheProvider provider) {
+ CacheProxy.provider = provider;
+ }
+ private static OriginRow caches(){
+ OriginRow result = new OriginRow();
+ if(ConfigTable.METADATA_CACHE_SCOPE == 1){
+ result = thread_caches.get();
+ if(null == result){
+ result = new OriginRow();
+ thread_caches.set(result);
+ }
+ }else if(ConfigTable.METADATA_CACHE_SCOPE == 9){
+ if(application_caches.isExpire(ConfigTable.METADATA_CACHE_SECOND*1000)){
+ application_caches = new OriginRow();
+ }
+ result = application_caches;
+ }
+ return result;
+ }
+ private static OriginRow names(){
+ OriginRow result = new OriginRow();
+ if(ConfigTable.METADATA_CACHE_SCOPE == 1){
+ result = thread_names.get();
+ if(null == result){
+ result = new OriginRow();
+ thread_names.set(result);
+ }
+ }else if(ConfigTable.METADATA_CACHE_SCOPE == 9){
+ if(application_names.isExpire(ConfigTable.METADATA_CACHE_SECOND*1000)){
+ application_names = new OriginRow();
+ }
+ result = application_names;
+ }
+ return result;
+ }
+/*
+
+ private static Map cache_columns = new HashMap<>();
+ private static Map> cache_names = new HashMap<>();
+ private static Map cache_table_maps = new HashMap<>();
+ private static Map cache_view_maps = new HashMap<>();
+*/
+
+
+ public static String key(DataRuntime runtime, String flag, boolean greedy, Catalog catalog, Schema schema, String pattern, int types, ConfigStore configs){
+ StringBuilder key = new StringBuilder();
+ key.append(runtime.datasource()).append("_").append(flag).append("_").append(greedy).append("_");
+ if(null != catalog){
+ key.append(catalog.getName());
+ }
+ key.append("_");
+ if(null != schema){
+ key.append(schema.getName());
+ }
+ key.append("_").append(pattern).append("_").append(types);
+ if(null != configs){
+ key.append(MD5Util.crypto(configs.json()));;
+ }
+ return key.toString().toUpperCase();
+ }
+ public static String key(DataRuntime runtime, String flag, boolean greedy, Table table){
+ StringBuilder key = new StringBuilder();
+ key.append(runtime.datasource()).append("_").append(flag).append("_").append(greedy).append("_");
+ String name = table.getName();
+ String catalog = table.getCatalogName();
+ String schema = table.getSchemaName();
+ if(null != catalog){
+ key.append(catalog);
+ }
+ key.append("_");
+ if(null != schema){
+ key.append(schema);
+ }
+ key.append("_").append(name);
+ return key.toString().toUpperCase();
+ }
+ public static String key(DataRuntime runtime, String flag, boolean greedy, Catalog catalog, Schema schema, String pattern){
+ StringBuilder key = new StringBuilder();
+ key.append(runtime.datasource()).append("_").append(flag).append("_").append(greedy).append("_");
+ if(null != catalog){
+ key.append(catalog.getName());
+ }
+ key.append("_");
+ if(null != schema){
+ key.append(schema.getName());
+ }
+ key.append("_").append(pattern);
+ return key.toString().toUpperCase();
+ }
+ public static String name(String key) {
+ return names().getString(key.toUpperCase());
+ }
+ public static void name(String key, String origin) {
+ names().put(key.toUpperCase(), origin);
+ }
+ public static List tables(String cache){
+ List tables = (List)caches().get(cache);
+ return tables;
+ }
+ public static List masterTables(String cache){
+ List tables = (List)caches().get(cache);
+ return tables;
+ }
+ public static List edgeTables(String cache){
+ List tables = (List)caches().get(cache);
+ return tables;
+ }
+ public static List vertexTables(String cache){
+ List tables = (List)caches().get(cache);
+ return tables;
+ }
+ public static void tables(String cache, List tables){
+ caches().put(cache, tables);
+ }
+
+ public static List views(String cache){
+ List view = (List)caches().get(cache);
+ return view;
+ }
+ public static void views(String cache, List view){
+ caches().put(cache, view);
+ }
+
+ public static void cache(String cache, Object value){
+ caches().put(cache, value);
+ }
+
+/*
+ public static void name(DriverAdapter adapter, List tables) {
+ if(null != tables) {
+ for (Table table : tables) {
+ name(adapter, table.getCatalog(), table.getSchema(), table.getName(), table.getName());
+ }
+ }
+ }*/
+/*
+ private static String key(DriverAdapter adapter, Catalog catalog, Schema schema) {
+ String key = null;
+ String catalog_name = null;
+ String schema_name = null;
+ if(null != catalog && adapter.supportCatalog()) {
+ catalog_name = catalog.getName();
+ }
+ if(null != schema && adapter.supportSchema()) {
+ schema_name = schema.getName();
+ }
+ if(null != catalog_name) {
+ key = catalog_name;
+ }
+ if(null != schema_name) {
+ if(null != key) {
+ key += "_" + schema_name;
+ }else{
+ key = schema_name;
+ }
+ }
+ if(null != key) {
+ key = key.toUpperCase();
+ }else{
+ key = "ALL";
+ }
+ return key;
+ }
+ private static String key(DriverAdapter adapter, Catalog catalog, Schema schema, Table table) {
+ String table_name = null;
+ if(null != table) {
+ table_name = table.getName();
+ }
+ String key = key(adapter, catalog, schema);
+ if(null != table_name) {
+ if(null != key) {
+ key += ":" + table_name;
+ }else{
+ key = table_name;
+ }
+ }
+ if(null != key) {
+ key = key.toUpperCase();
+ }
+ return key;
+ }*//*
+ public static void name(DriverAdapter adapter, Catalog catalog, Schema schema, String name, String origin) {
+ String group_key = key(adapter, catalog, schema);
+ Map maps = cache_names.get(group_key);
+ if(null == maps) {
+ maps = new HashMap<>();
+ cache_names.put(group_key, maps);
+ }
+ String name_key = (group_key + ":" + name).toUpperCase();
+ maps.put(name_key, origin);
+ }
+ public static Map names(DriverAdapter adapter, Catalog catalog, Schema schema) {
+ return cache_names.get(key(adapter, catalog, schema));
+ }
+ public static String name(DriverAdapter adapter, boolean greedy, Catalog catalog, Schema schema, String name) {
+ if(null == name) {
+ return null;
+ }
+ String group_key = key(adapter, catalog, schema);
+ Map maps = cache_names.get(group_key);
+ if(null != maps) {
+ String name_key = (group_key + ":" + name).toUpperCase();
+ String origin = maps.get(name_key);
+ if(null != origin) {
+ return origin;
+ }
+ }
+ if(greedy) {
+ for (Map names : cache_names.values()) {
+ for(String item:names.keySet()) {
+ if(item.endsWith((":"+name).toUpperCase())) {
+ return names.get(item);
+ }
+ }
+ }
+ }
+ return null;
+ }*/
+ /*public static String datasource(String datasource) {
+ if(null == datasource || "common".equalsIgnoreCase(datasource)) {
+ //datasource = DataSourceHolder.curDataSource();
+ }
+ if(null == datasource) {
+ datasource = "default";
+ }
+ return datasource.toUpperCase();
+ }
+*//*
+ public static String tableName(String datasource, String name) {
+ DataRow row = cache_table_maps.get(datasource(datasource));
+ if(null != row) {
+ return row.getString(name);
+ }
+ return name;
+ }
+ public static String viewName(String datasource, String name) {
+ DataRow row = cache_view_maps.get(datasource(datasource));
+ if(null != row) {
+ return row.getString(name);
+ }
+ return name;
+ }
+ public static void setTableMaps(String datasource, DataRow maps) {
+ cache_table_maps.put(datasource(datasource), maps);
+ }
+ public static void setViewMaps(String datasource, DataRow maps) {
+ cache_view_maps.put(datasource(datasource), maps);
+ }*/
+
+
+ /* public static DataRow getTableMaps(String datasource) {
+ DataRow row = cache_table_maps.get(datasource(datasource));
+ if(null == row) {
+ row = new DataRow();
+ cache_table_maps.put(datasource(datasource), row);
+ }
+ return row;
+ }*/
+
+ /* public static DataRow getViewMaps(String datasource) {
+ DataRow row = cache_view_maps.get(datasource(datasource));
+ if(null == row) {
+ row = new DataRow();
+ cache_view_maps.put(datasource(datasource), row);
+ }
+ return row;
+ }*/
+
+
+ public static LinkedHashMap columns(String key) {
+ return (LinkedHashMap) caches().get(key);
+ }
+ public static LinkedHashMap tags(String key) {
+ return (LinkedHashMap) caches().get(key);
+ }
+
+
+ /* public static void columns(DriverAdapter adapter, String datasource, Table table, LinkedHashMap columns) {
+ if(null == table) {
+ return;
+ }
+ String cache = ConfigTable.getString("METADATA_CACHE_KEY");
+ String key = datasource(datasource) + "_COLUMNS_" + key(adapter, table.getCatalog(), table.getSchema(), table);
+ key = key.toUpperCase();
+ if(null != provider && BasicUtil.isNotEmpty(cache) && !ConfigTable.IS_CACHE_DISABLED) {
+ provider.put(cache, key, columns);
+ }else{
+ DataRow static_cache = new DataRow();
+ static_cache.put("keys", columns);
+ cache_columns.put(key, static_cache);
+ }
+ } */
+
+ public static void clear() {/*
+ if(null != provider && !ConfigTable.IS_CACHE_DISABLED) {
+ String cache = ConfigTable.METADATA_CACHE_KEY;
+ if(BasicUtil.isNotEmpty(cache)) {
+ provider.clear(cache);
+ }
+ }else{
+ cache_columns.clear();
+ }*/
+
+ caches().clear();
+ names().clear();
+ }
+
+}
diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/config/MyBatisDataSourceMonitor.java b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/config/MyBatisDataSourceMonitor.java
index 4ce71f6e..aa7e72a5 100644
--- a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/config/MyBatisDataSourceMonitor.java
+++ b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/config/MyBatisDataSourceMonitor.java
@@ -15,6 +15,11 @@ import java.sql.DatabaseMetaData;
import java.util.HashMap;
import java.util.Map;
+/**
+ * anyline 适配 动态数据源改造
+ *
+ * @author Lion Li
+ */
@Slf4j
@Component
public class MyBatisDataSourceMonitor implements DataSourceMonitor {
@@ -22,6 +27,8 @@ public class MyBatisDataSourceMonitor implements DataSourceMonitor {
public MyBatisDataSourceMonitor() {
// 调整执行模式为自定义
ConfigTable.KEEP_ADAPTER = 2;
+ // 禁用缓存
+ ConfigTable.METADATA_CACHE_SCOPE = 0;
}
private final Map features = new HashMap<>();
diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java
index 85d0aad6..0f8ebf47 100644
--- a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java
+++ b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/GenTableServiceImpl.java
@@ -17,13 +17,13 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.anyline.metadata.Column;
import org.anyline.metadata.Table;
-import org.anyline.proxy.CacheProxy;
import org.anyline.proxy.ServiceProxy;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.exception.ServiceException;
+import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.FileUtils;
@@ -269,7 +269,7 @@ public class GenTableServiceImpl implements IGenTableService {
int row = baseMapper.insert(table);
if (row > 0) {
// 保存列信息
- List genTableColumns = selectDbTableColumnsByName(tableName, dataName);
+ List genTableColumns = SpringUtils.getAopProxy(this).selectDbTableColumnsByName(tableName, dataName);
List saveColumns = new ArrayList<>();
for (GenTableColumn column : genTableColumns) {
GenUtils.initColumnField(column, table);
@@ -293,9 +293,8 @@ public class GenTableServiceImpl implements IGenTableService {
* @return 列信息
*/
@DS("#dataName")
- private List selectDbTableColumnsByName(String tableName, String dataName) {
- // 清理anyline缓存
- CacheProxy.clear();
+ @Override
+ public List selectDbTableColumnsByName(String tableName, String dataName) {
LinkedHashMap columns = ServiceProxy.metadata().columns(tableName);
List tableColumns = new ArrayList<>();
columns.forEach((columnName, column) -> {
@@ -407,7 +406,7 @@ public class GenTableServiceImpl implements IGenTableService {
List tableColumns = table.getColumns();
Map tableColumnMap = StreamUtils.toIdentityMap(tableColumns, GenTableColumn::getColumnName);
- List dbTableColumns = selectDbTableColumnsByName(table.getTableName(), table.getDataName());
+ List dbTableColumns = SpringUtils.getAopProxy(this).selectDbTableColumnsByName(table.getTableName(), table.getDataName());
if (CollUtil.isEmpty(dbTableColumns)) {
throw new ServiceException("同步数据失败,原表结构不存在");
}
diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/IGenTableService.java b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/IGenTableService.java
index ea23c53d..4389f129 100644
--- a/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/IGenTableService.java
+++ b/ruoyi-modules/ruoyi-gen/src/main/java/org/dromara/gen/service/IGenTableService.java
@@ -85,6 +85,15 @@ public interface IGenTableService {
*/
void importGenTable(List tableList, String dataName);
+ /**
+ * 根据表名称查询列信息
+ *
+ * @param tableName 表名称
+ * @param dataName 数据源名称
+ * @return 列信息
+ */
+ List selectDbTableColumnsByName(String tableName, String dataName);
+
/**
* 预览代码
*