mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-09-22 21:15:24 +08:00
Add CacheManage
This commit is contained in:
@ -1,11 +1,7 @@
|
|||||||
package ai.chat2db.server.domain.api.param;
|
package ai.chat2db.server.domain.api.param;
|
||||||
|
|
||||||
import java.io.Serial;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
import ai.chat2db.server.tools.base.wrapper.param.PageQueryParam;
|
import ai.chat2db.server.tools.base.wrapper.param.PageQueryParam;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@ -44,4 +40,11 @@ public class TablePageQueryParam extends PageQueryParam {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private String schemaName;
|
private String schemaName;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if true, refresh the cache
|
||||||
|
*/
|
||||||
|
private boolean refresh;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package ai.chat2db.server.domain.core.cache;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
public class CacheKey {
|
||||||
|
|
||||||
|
public static String getDataSourceKey(Long dataSourceId) {
|
||||||
|
return "schemas_datasourceId_" + dataSourceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getTableKey(Long dataSourceId, String databaseName, String schemaName) {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer("tables_dataSourceId" + dataSourceId);
|
||||||
|
if (!StringUtils.isEmpty(databaseName)) {
|
||||||
|
stringBuffer.append("_databaseName" + databaseName);
|
||||||
|
}
|
||||||
|
if (!StringUtils.isEmpty(schemaName)) {
|
||||||
|
stringBuffer.append("_schemaName" + schemaName);
|
||||||
|
}
|
||||||
|
return stringBuffer.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package ai.chat2db.server.domain.core.cache;
|
package ai.chat2db.server.domain.core.cache;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
|
||||||
@ -22,32 +24,73 @@ public class CacheManage {
|
|||||||
private static CacheManager cacheManager;
|
private static CacheManager cacheManager;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
|
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
|
||||||
.with(CacheManagerBuilder.persistence(PATH)) // 确保这个路径是存在且有写权限的
|
.with(CacheManagerBuilder.persistence(PATH)) // 确保这个路径是存在且有写权限的
|
||||||
.withCache(CACHE, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
|
.withCache(CACHE, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
|
||||||
ResourcePoolsBuilder.newResourcePoolsBuilder()
|
ResourcePoolsBuilder.newResourcePoolsBuilder()
|
||||||
.heap(1, EntryUnit.ENTRIES)
|
.heap(1000, EntryUnit.ENTRIES)
|
||||||
.disk(20, MemoryUnit.GB, true))) // 磁盘持久化设置为true
|
.disk(20, MemoryUnit.GB, true))) // 磁盘持久化设置为true
|
||||||
.build(true);
|
.build(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T get(String key,Class<T> clazz) {
|
public static <T> T get(String key, Class<T> clazz) {
|
||||||
Cache<String, String> myCache = cacheManager.getCache(CACHE, String.class, String.class);
|
Cache<String, String> myCache = cacheManager.getCache(CACHE, String.class, String.class);
|
||||||
String value = myCache.get(key);
|
String value = myCache.get(key);
|
||||||
if(!StringUtils.isEmpty(value)){
|
if (!StringUtils.isEmpty(value)) {
|
||||||
return JSON.parseObject(value,clazz);
|
return JSON.parseObject(value, clazz);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> getList(String key, Class<T> clazz) {
|
||||||
|
Cache<String, String> myCache = cacheManager.getCache(CACHE, String.class, String.class);
|
||||||
|
String value = myCache.get(key);
|
||||||
|
if (!StringUtils.isEmpty(value)) {
|
||||||
|
return JSON.parseArray(value, clazz);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <T> T get(String key, Class<T> clazz, Function<Object, Boolean> refresh,
|
||||||
|
Function<Object, T> function) {
|
||||||
|
T t;
|
||||||
|
if (refresh.apply(key)) {
|
||||||
|
t = function.apply(key);
|
||||||
|
put(key, t);
|
||||||
|
} else {
|
||||||
|
t = get(key, clazz);
|
||||||
|
if (t == null) {
|
||||||
|
t = function.apply(key);
|
||||||
|
put(key, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <T> List<T> getList(String key, Class<T> clazz, Function<Object, Boolean> refresh,
|
||||||
|
Function<Object, List<T>> function) {
|
||||||
|
List<T> t;
|
||||||
|
if (refresh.apply(key)) {
|
||||||
|
t = function.apply(key);
|
||||||
|
put(key, t);
|
||||||
|
} else {
|
||||||
|
t = getList(key, clazz);
|
||||||
|
if (t == null) {
|
||||||
|
t = function.apply(key);
|
||||||
|
put(key, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
public static void put(String s, Object value) {
|
public static void put(String s, Object value) {
|
||||||
Cache<String, String> myCache = cacheManager.getCache(CACHE, String.class, String.class);
|
Cache<String, String> myCache = cacheManager.getCache(CACHE, String.class, String.class);
|
||||||
myCache.put(s, JSON.toJSONString(value));
|
myCache.put(s, JSON.toJSONString(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void close() {
|
public static void close() {
|
||||||
cacheManager.close();
|
cacheManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,6 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
|
||||||
|
|
||||||
import ai.chat2db.server.domain.api.param.DatabaseOperationParam;
|
import ai.chat2db.server.domain.api.param.DatabaseOperationParam;
|
||||||
import ai.chat2db.server.domain.api.param.DatabaseQueryAllParam;
|
import ai.chat2db.server.domain.api.param.DatabaseQueryAllParam;
|
||||||
import ai.chat2db.server.domain.api.param.MetaDataQueryParam;
|
import ai.chat2db.server.domain.api.param.MetaDataQueryParam;
|
||||||
@ -20,10 +18,11 @@ import ai.chat2db.spi.model.Database;
|
|||||||
import ai.chat2db.spi.model.MetaSchema;
|
import ai.chat2db.spi.model.MetaSchema;
|
||||||
import ai.chat2db.spi.model.Schema;
|
import ai.chat2db.spi.model.Schema;
|
||||||
import ai.chat2db.spi.sql.Chat2DBContext;
|
import ai.chat2db.spi.sql.Chat2DBContext;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import static ai.chat2db.server.domain.core.cache.CacheKey.getDataSourceKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author moji
|
* @author moji
|
||||||
* @version DataSourceCoreServiceImpl.java, v 0.1 2022年09月23日 15:51 moji Exp $
|
* @version DataSourceCoreServiceImpl.java, v 0.1 2022年09月23日 15:51 moji Exp $
|
||||||
@ -33,7 +32,6 @@ import org.springframework.util.CollectionUtils;
|
|||||||
public class DatabaseServiceImpl implements DatabaseService {
|
public class DatabaseServiceImpl implements DatabaseService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(value = "database", key = "'data_source_' + #param.dataSourceId", condition = "#param.refresh == false")
|
|
||||||
public ListResult<Database> queryAll(DatabaseQueryAllParam param) {
|
public ListResult<Database> queryAll(DatabaseQueryAllParam param) {
|
||||||
return ListResult.of(Chat2DBContext.getMetaData().databases());
|
return ListResult.of(Chat2DBContext.getMetaData().databases());
|
||||||
}
|
}
|
||||||
@ -45,57 +43,34 @@ public class DatabaseServiceImpl implements DatabaseService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataResult<MetaSchema> queryDatabaseSchema(MetaDataQueryParam param) {
|
public DataResult<MetaSchema> queryDatabaseSchema(MetaDataQueryParam param) {
|
||||||
DataResult<MetaSchema> result = CacheManage.get("data_source_" + param.getDataSourceId(), DataResult.class);
|
MetaSchema ms = CacheManage.get(getDataSourceKey(param.getDataSourceId()), MetaSchema.class,
|
||||||
if (result == null || param.isRefresh()) {
|
(key) -> param.isRefresh(), (key) -> {
|
||||||
result = new DataResult<>();
|
MetaSchema metaSchema = new MetaSchema();
|
||||||
MetaSchema metaSchema = new MetaSchema();
|
List<Database> databases = Chat2DBContext.getMetaData().databases();
|
||||||
List<Database> databases = Chat2DBContext.getMetaData().databases();
|
if (!CollectionUtils.isEmpty(databases)) {
|
||||||
if (!CollectionUtils.isEmpty(databases)) {
|
List<Schema> schemaList = Chat2DBContext.getMetaData().schemas(null);
|
||||||
List<Schema> schemaList = Chat2DBContext.getMetaData().schemas(null);
|
if (databases.size() == 1) {
|
||||||
if (databases.size() == 1) {
|
databases.get(0).setSchemas(schemaList);
|
||||||
databases.get(0).setSchemas(schemaList);
|
metaSchema.setDatabases(databases);
|
||||||
metaSchema.setDatabases(databases);
|
} else {
|
||||||
} else {
|
Map<String, List<Schema>> schemaMap = schemaList.stream().collect(
|
||||||
Map<String, List<Schema>> schemaMap = schemaList.stream().collect(
|
Collectors.groupingBy(
|
||||||
Collectors.groupingBy(
|
schema -> schema.getDatabaseName() != null ? schema.getDatabaseName() : ""));
|
||||||
schema -> schema.getDatabaseName() != null ? schema.getDatabaseName() : ""));
|
for (Database dataBase : databases) {
|
||||||
for (Database dataBase : databases) {
|
dataBase.setSchemas(schemaMap.get(dataBase.getName()));
|
||||||
dataBase.setSchemas(schemaMap.get(dataBase.getName()));
|
}
|
||||||
|
metaSchema.setDatabases(databases);
|
||||||
}
|
}
|
||||||
metaSchema.setDatabases(databases);
|
} else {
|
||||||
|
List<Schema> schemas = Chat2DBContext.getMetaData().schemas(null);
|
||||||
|
metaSchema.setSchemas(schemas);
|
||||||
}
|
}
|
||||||
} else {
|
return metaSchema;
|
||||||
List<Schema> schemas = Chat2DBContext.getMetaData().schemas(null);
|
});
|
||||||
metaSchema.setSchemas(schemas);
|
|
||||||
}
|
return DataResult.of(ms);
|
||||||
result.setData(metaSchema);
|
|
||||||
CacheManage.put("data_source_" + param.getDataSourceId(), result);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String queryDatabaseSchemaCache(MetaDataQueryParam param) {
|
|
||||||
MetaSchema metaSchema = new MetaSchema();
|
|
||||||
List<Database> databases = Chat2DBContext.getMetaData().databases();
|
|
||||||
if (!CollectionUtils.isEmpty(databases)) {
|
|
||||||
List<Schema> schemaList = Chat2DBContext.getMetaData().schemas(null);
|
|
||||||
if (databases.size() == 1) {
|
|
||||||
databases.get(0).setSchemas(schemaList);
|
|
||||||
metaSchema.setDatabases(databases);
|
|
||||||
} else {
|
|
||||||
Map<String, List<Schema>> schemaMap = schemaList.stream().collect(
|
|
||||||
Collectors.groupingBy(schema -> schema.getDatabaseName() != null ? schema.getDatabaseName() : ""));
|
|
||||||
for (Database dataBase : databases) {
|
|
||||||
dataBase.setSchemas(schemaMap.get(dataBase.getName()));
|
|
||||||
}
|
|
||||||
metaSchema.setDatabases(databases);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
List<Schema> schemas = Chat2DBContext.getMetaData().schemas(null);
|
|
||||||
metaSchema.setSchemas(schemas);
|
|
||||||
}
|
|
||||||
return JSON.toJSONString(metaSchema);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult deleteDatabase(DatabaseOperationParam param) {
|
public ActionResult deleteDatabase(DatabaseOperationParam param) {
|
||||||
|
@ -6,10 +6,20 @@ import java.util.Map;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import ai.chat2db.server.domain.api.param.*;
|
import ai.chat2db.server.domain.api.param.DropParam;
|
||||||
|
import ai.chat2db.server.domain.api.param.PinTableParam;
|
||||||
|
import ai.chat2db.server.domain.api.param.ShowCreateTableParam;
|
||||||
|
import ai.chat2db.server.domain.api.param.TablePageQueryParam;
|
||||||
|
import ai.chat2db.server.domain.api.param.TableQueryParam;
|
||||||
|
import ai.chat2db.server.domain.api.param.TableSelector;
|
||||||
import ai.chat2db.server.domain.api.service.PinService;
|
import ai.chat2db.server.domain.api.service.PinService;
|
||||||
import ai.chat2db.server.domain.api.service.TableService;
|
import ai.chat2db.server.domain.api.service.TableService;
|
||||||
|
import ai.chat2db.server.domain.core.cache.CacheManage;
|
||||||
import ai.chat2db.server.domain.core.converter.PinTableConverter;
|
import ai.chat2db.server.domain.core.converter.PinTableConverter;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.PageResult;
|
||||||
import ai.chat2db.server.tools.common.util.ContextUtils;
|
import ai.chat2db.server.tools.common.util.ContextUtils;
|
||||||
import ai.chat2db.spi.DBManage;
|
import ai.chat2db.spi.DBManage;
|
||||||
import ai.chat2db.spi.MetaData;
|
import ai.chat2db.spi.MetaData;
|
||||||
@ -17,12 +27,6 @@ import ai.chat2db.spi.model.Sql;
|
|||||||
import ai.chat2db.spi.model.Table;
|
import ai.chat2db.spi.model.Table;
|
||||||
import ai.chat2db.spi.model.TableColumn;
|
import ai.chat2db.spi.model.TableColumn;
|
||||||
import ai.chat2db.spi.model.TableIndex;
|
import ai.chat2db.spi.model.TableIndex;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
|
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.PageResult;
|
|
||||||
import ai.chat2db.server.tools.common.util.EasyEnumUtils;
|
|
||||||
|
|
||||||
import ai.chat2db.spi.sql.Chat2DBContext;
|
import ai.chat2db.spi.sql.Chat2DBContext;
|
||||||
import ai.chat2db.spi.util.SqlUtils;
|
import ai.chat2db.spi.util.SqlUtils;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
@ -30,6 +34,8 @@ import org.apache.commons.collections4.CollectionUtils;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import static ai.chat2db.server.domain.core.cache.CacheKey.getTableKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author moji
|
* @author moji
|
||||||
* @version DataSourceCoreServiceImpl.java, v 0.1 2022年09月23日 15:51 moji Exp $
|
* @version DataSourceCoreServiceImpl.java, v 0.1 2022年09月23日 15:51 moji Exp $
|
||||||
@ -77,9 +83,9 @@ public class TableServiceImpl implements TableService {
|
|||||||
if (!CollectionUtils.isEmpty(tables)) {
|
if (!CollectionUtils.isEmpty(tables)) {
|
||||||
Table table = tables.get(0);
|
Table table = tables.get(0);
|
||||||
table.setIndexList(
|
table.setIndexList(
|
||||||
metaSchema.indexes(param.getDatabaseName(), param.getSchemaName(), param.getTableName()));
|
metaSchema.indexes(param.getDatabaseName(), param.getSchemaName(), param.getTableName()));
|
||||||
table.setColumnList(
|
table.setColumnList(
|
||||||
metaSchema.columns(param.getDatabaseName(), param.getSchemaName(), param.getTableName()));
|
metaSchema.columns(param.getDatabaseName(), param.getSchemaName(), param.getTableName()));
|
||||||
return DataResult.of(table);
|
return DataResult.of(table);
|
||||||
}
|
}
|
||||||
return DataResult.of(null);
|
return DataResult.of(null);
|
||||||
@ -93,8 +99,14 @@ public class TableServiceImpl implements TableService {
|
|||||||
@Override
|
@Override
|
||||||
public PageResult<Table> pageQuery(TablePageQueryParam param, TableSelector selector) {
|
public PageResult<Table> pageQuery(TablePageQueryParam param, TableSelector selector) {
|
||||||
MetaData metaSchema = Chat2DBContext.getMetaData();
|
MetaData metaSchema = Chat2DBContext.getMetaData();
|
||||||
List<Table> list = metaSchema.tables(param.getDatabaseName(), param.getSchemaName(), param.getTableName());
|
|
||||||
list = pinTable(list,param);
|
String tableKey = getTableKey(param.getDataSourceId(),param.getDatabaseName(), param.getSchemaName());
|
||||||
|
|
||||||
|
List<Table> list = CacheManage.getList(tableKey, Table.class,
|
||||||
|
(key) -> param.isRefresh(), (key) ->
|
||||||
|
metaSchema.tables(param.getDatabaseName(), param.getSchemaName(), param.getTableName()));
|
||||||
|
|
||||||
|
list = pinTable(list, param);
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
return PageResult.of(list, 0L, param);
|
return PageResult.of(list, 0L, param);
|
||||||
}
|
}
|
||||||
@ -122,14 +134,13 @@ public class TableServiceImpl implements TableService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Table table : list) {
|
for (Table table : list) {
|
||||||
if (table!=null && !tables.contains(table)) {
|
if (table != null && !tables.contains(table)) {
|
||||||
tables.add(table);
|
tables.add(table);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TableColumn> queryColumns(TableQueryParam param) {
|
public List<TableColumn> queryColumns(TableQueryParam param) {
|
||||||
MetaData metaSchema = Chat2DBContext.getMetaData();
|
MetaData metaSchema = Chat2DBContext.getMetaData();
|
||||||
|
@ -27,4 +27,10 @@ public class DataSourceBaseRequest implements DataSourceBaseRequestInfo{
|
|||||||
* 表所在空间
|
* 表所在空间
|
||||||
*/
|
*/
|
||||||
private String schemaName;
|
private String schemaName;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if true, refresh the cache
|
||||||
|
*/
|
||||||
|
private boolean refresh;
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,21 @@ package ai.chat2db.server.web.api.controller.driver;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import ai.chat2db.server.domain.api.service.JdbcDriverService;
|
import ai.chat2db.server.domain.api.service.JdbcDriverService;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
|
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||||
|
import ai.chat2db.server.web.api.controller.driver.request.JdbcDriverRequest;
|
||||||
import ai.chat2db.spi.config.DBConfig;
|
import ai.chat2db.spi.config.DBConfig;
|
||||||
import ai.chat2db.spi.util.JdbcJarUtils;
|
import ai.chat2db.spi.util.JdbcJarUtils;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -44,6 +49,7 @@ public class JdbcDriverController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载驱动
|
* 下载驱动
|
||||||
|
*
|
||||||
* @param dbType
|
* @param dbType
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -57,17 +63,13 @@ public class JdbcDriverController {
|
|||||||
* 上传驱动
|
* 上传驱动
|
||||||
*
|
*
|
||||||
* @param multipartFiles
|
* @param multipartFiles
|
||||||
* @param jdbcDriverClass
|
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload")
|
||||||
public ActionResult upload(@RequestParam MultipartFile[] multipartFiles, @RequestParam String jdbcDriverClass,
|
public ListResult<String> upload(@RequestParam MultipartFile[] multipartFiles) {
|
||||||
@RequestParam String dbType) {
|
List<String> list = new ArrayList<>();
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
|
||||||
for (int i = 0; i < multipartFiles.length; i++) {
|
for (int i = 0; i < multipartFiles.length; i++) {
|
||||||
if (i > 0) {
|
|
||||||
stringBuilder.append(",");
|
|
||||||
}
|
|
||||||
MultipartFile multipartFile = multipartFiles[i];
|
MultipartFile multipartFile = multipartFiles[i];
|
||||||
String originalFilename = FilenameUtils.getName(multipartFile.getOriginalFilename());
|
String originalFilename = FilenameUtils.getName(multipartFile.getOriginalFilename());
|
||||||
String location = JdbcJarUtils.PATH + originalFilename;
|
String location = JdbcJarUtils.PATH + originalFilename;
|
||||||
@ -76,10 +78,22 @@ public class JdbcDriverController {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
stringBuilder.append(originalFilename);
|
list.add(originalFilename);
|
||||||
}
|
}
|
||||||
return jdbcDriverService.upload(dbType, jdbcDriverClass, stringBuilder.toString());
|
return ListResult.of(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* save
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/save")
|
||||||
|
public ActionResult save(@RequestBody JdbcDriverRequest request) {
|
||||||
|
|
||||||
|
return jdbcDriverService.upload(request.getDbType(), request.getJdbcDriverClass(),
|
||||||
|
String.join(",", request.getJdbcDriver()));
|
||||||
}
|
}
|
||||||
|
|
||||||
///**
|
///**
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
package ai.chat2db.server.web.api.controller.driver.request;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class JdbcDriverRequest {
|
||||||
|
String jdbcDriverClass;
|
||||||
|
String dbType;
|
||||||
|
|
||||||
|
List<String> jdbcDriver;
|
||||||
|
}
|
@ -75,7 +75,7 @@ public class RdbDdlController {
|
|||||||
PageResult<Table> tableDTOPageResult = tableService.pageQuery(queryParam, tableSelector);
|
PageResult<Table> tableDTOPageResult = tableService.pageQuery(queryParam, tableSelector);
|
||||||
List<TableVO> tableVOS = rdbWebConverter.tableDto2vo(tableDTOPageResult.getData());
|
List<TableVO> tableVOS = rdbWebConverter.tableDto2vo(tableDTOPageResult.getData());
|
||||||
return WebPageResult.of(tableVOS, tableDTOPageResult.getTotal(), request.getPageNo(),
|
return WebPageResult.of(tableVOS, tableDTOPageResult.getTotal(), request.getPageNo(),
|
||||||
request.getPageSize());
|
request.getPageSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -100,7 +100,8 @@ public class RdbDdlController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping("/database_schema_list")
|
@GetMapping("/database_schema_list")
|
||||||
public DataResult<MetaSchemaVO> databaseSchemaList(@Valid DataSourceBaseRequest request) {
|
public DataResult<MetaSchemaVO> databaseSchemaList(@Valid DataSourceBaseRequest request) {
|
||||||
MetaDataQueryParam queryParam = MetaDataQueryParam.builder().dataSourceId(request.getDataSourceId()).build();
|
MetaDataQueryParam queryParam = MetaDataQueryParam.builder().dataSourceId(request.getDataSourceId()).refresh(
|
||||||
|
request.isRefresh()).build();
|
||||||
DataResult<MetaSchema> result = databaseService.queryDatabaseSchema(queryParam);
|
DataResult<MetaSchema> result = databaseService.queryDatabaseSchema(queryParam);
|
||||||
MetaSchemaVO schemaDto2vo = rdbWebConverter.metaSchemaDto2vo(result.getData());
|
MetaSchemaVO schemaDto2vo = rdbWebConverter.metaSchemaDto2vo(result.getData());
|
||||||
return DataResult.of(schemaDto2vo);
|
return DataResult.of(schemaDto2vo);
|
||||||
@ -139,7 +140,7 @@ public class RdbDdlController {
|
|||||||
@PostMapping("/modify_database")
|
@PostMapping("/modify_database")
|
||||||
public ActionResult modifyDatabase(@Valid @RequestBody UpdateDatabaseRequest request) {
|
public ActionResult modifyDatabase(@Valid @RequestBody UpdateDatabaseRequest request) {
|
||||||
DatabaseOperationParam param = DatabaseOperationParam.builder().databaseName(request.getDatabaseName())
|
DatabaseOperationParam param = DatabaseOperationParam.builder().databaseName(request.getDatabaseName())
|
||||||
.newDatabaseName(request.getNewDatabaseName()).build();
|
.newDatabaseName(request.getNewDatabaseName()).build();
|
||||||
return databaseService.modifyDatabase(param);
|
return databaseService.modifyDatabase(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +153,7 @@ public class RdbDdlController {
|
|||||||
@PostMapping("/delete_schema")
|
@PostMapping("/delete_schema")
|
||||||
public ActionResult deleteSchema(@Valid @RequestBody DataSourceBaseRequest request) {
|
public ActionResult deleteSchema(@Valid @RequestBody DataSourceBaseRequest request) {
|
||||||
SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName())
|
SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName())
|
||||||
.schemaName(request.getSchemaName()).build();
|
.schemaName(request.getSchemaName()).build();
|
||||||
return databaseService.deleteSchema(param);
|
return databaseService.deleteSchema(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +166,7 @@ public class RdbDdlController {
|
|||||||
@PostMapping("/create_schema")
|
@PostMapping("/create_schema")
|
||||||
public ActionResult createSchema(@Valid @RequestBody DataSourceBaseRequest request) {
|
public ActionResult createSchema(@Valid @RequestBody DataSourceBaseRequest request) {
|
||||||
SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName())
|
SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName())
|
||||||
.schemaName(request.getSchemaName()).build();
|
.schemaName(request.getSchemaName()).build();
|
||||||
return databaseService.createSchema(param);
|
return databaseService.createSchema(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +179,7 @@ public class RdbDdlController {
|
|||||||
@PostMapping("/modify_schema")
|
@PostMapping("/modify_schema")
|
||||||
public ActionResult modifySchema(@Valid @RequestBody UpdateSchemaRequest request) {
|
public ActionResult modifySchema(@Valid @RequestBody UpdateSchemaRequest request) {
|
||||||
SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName())
|
SchemaOperationParam param = SchemaOperationParam.builder().databaseName(request.getDatabaseName())
|
||||||
.schemaName(request.getSchemaName()).newSchemaName(request.getNewSchemaName()).build();
|
.schemaName(request.getSchemaName()).newSchemaName(request.getNewSchemaName()).build();
|
||||||
return databaseService.modifySchema(param);
|
return databaseService.modifySchema(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,9 +284,9 @@ public class RdbDdlController {
|
|||||||
@GetMapping("/modify/sql")
|
@GetMapping("/modify/sql")
|
||||||
public ListResult<SqlVO> modifySql(@Valid TableModifySqlRequest request) {
|
public ListResult<SqlVO> modifySql(@Valid TableModifySqlRequest request) {
|
||||||
return tableService.buildSql(
|
return tableService.buildSql(
|
||||||
rdbWebConverter.tableRequest2param(request.getOldTable()),
|
rdbWebConverter.tableRequest2param(request.getOldTable()),
|
||||||
rdbWebConverter.tableRequest2param(request.getNewTable()))
|
rdbWebConverter.tableRequest2param(request.getNewTable()))
|
||||||
.map(rdbWebConverter::dto2vo);
|
.map(rdbWebConverter::dto2vo);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,4 +38,9 @@ public class TableBriefQueryRequest extends PageQueryRequest implements DataSour
|
|||||||
* 模糊搜索词
|
* 模糊搜索词
|
||||||
*/
|
*/
|
||||||
private String searchKey;
|
private String searchKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if true, refresh the cache
|
||||||
|
*/
|
||||||
|
private boolean refresh;
|
||||||
}
|
}
|
||||||
|
@ -111,10 +111,10 @@ public class SQLExecutor {
|
|||||||
executeResult.setHeaderList(headerList);
|
executeResult.setHeaderList(headerList);
|
||||||
for (int i = 1; i <= col; i++) {
|
for (int i = 1; i <= col; i++) {
|
||||||
headerList.add(Header.builder()
|
headerList.add(Header.builder()
|
||||||
.dataType(ai.chat2db.spi.util.JdbcUtils.resolveDataType(
|
.dataType(ai.chat2db.spi.util.JdbcUtils.resolveDataType(
|
||||||
resultSetMetaData.getColumnTypeName(i), resultSetMetaData.getColumnType(i)).getCode())
|
resultSetMetaData.getColumnTypeName(i), resultSetMetaData.getColumnType(i)).getCode())
|
||||||
.name(resultSetMetaData.getColumnName(i))
|
.name(resultSetMetaData.getColumnName(i))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取数据信息
|
// 获取数据信息
|
||||||
@ -152,7 +152,6 @@ public class SQLExecutor {
|
|||||||
return execute(sql, getConnection());
|
return execute(sql, getConnection());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有的数据库
|
* 获取所有的数据库
|
||||||
*
|
*
|
||||||
@ -181,7 +180,7 @@ public class SQLExecutor {
|
|||||||
*/
|
*/
|
||||||
public List<Map<String, String>> schemas(String databaseName, String schemaName) {
|
public List<Map<String, String>> schemas(String databaseName, String schemaName) {
|
||||||
List<Map<String, String>> schemaList = Lists.newArrayList();
|
List<Map<String, String>> schemaList = Lists.newArrayList();
|
||||||
if(StringUtils.isEmpty(databaseName) && StringUtils.isEmpty(schemaName)){
|
if (StringUtils.isEmpty(databaseName) && StringUtils.isEmpty(schemaName)) {
|
||||||
try (ResultSet resultSet = getConnection().getMetaData().getSchemas()) {
|
try (ResultSet resultSet = getConnection().getMetaData().getSchemas()) {
|
||||||
if (resultSet != null) {
|
if (resultSet != null) {
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
@ -192,7 +191,7 @@ public class SQLExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException("Get schemas error",e);
|
throw new RuntimeException("Get schemas error", e);
|
||||||
}
|
}
|
||||||
return schemaList;
|
return schemaList;
|
||||||
}
|
}
|
||||||
@ -206,7 +205,7 @@ public class SQLExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException("Get schemas error",e);
|
throw new RuntimeException("Get schemas error", e);
|
||||||
}
|
}
|
||||||
return schemaList;
|
return schemaList;
|
||||||
}
|
}
|
||||||
@ -222,11 +221,16 @@ public class SQLExecutor {
|
|||||||
*/
|
*/
|
||||||
public List<Table> tables(String databaseName, String schemaName, String tableName, String types[]) {
|
public List<Table> tables(String databaseName, String schemaName, String tableName, String types[]) {
|
||||||
List<Table> tables = Lists.newArrayList();
|
List<Table> tables = Lists.newArrayList();
|
||||||
|
int n = 0;
|
||||||
try (ResultSet resultSet = getConnection().getMetaData().getTables(databaseName, schemaName, tableName,
|
try (ResultSet resultSet = getConnection().getMetaData().getTables(databaseName, schemaName, tableName,
|
||||||
types)) {
|
types)) {
|
||||||
if (resultSet != null) {
|
if (resultSet != null) {
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
|
n++;
|
||||||
tables.add(buildTable(resultSet));
|
tables.add(buildTable(resultSet));
|
||||||
|
if (n >= 5000) {// 最多只取5000条
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -247,7 +251,7 @@ public class SQLExecutor {
|
|||||||
public List<TableColumn> columns(String databaseName, String schemaName, String tableName, String columnName) {
|
public List<TableColumn> columns(String databaseName, String schemaName, String tableName, String columnName) {
|
||||||
List<TableColumn> tableColumns = Lists.newArrayList();
|
List<TableColumn> tableColumns = Lists.newArrayList();
|
||||||
try (ResultSet resultSet = getConnection().getMetaData().getColumns(databaseName, schemaName, tableName,
|
try (ResultSet resultSet = getConnection().getMetaData().getColumns(databaseName, schemaName, tableName,
|
||||||
columnName)) {
|
columnName)) {
|
||||||
if (resultSet != null) {
|
if (resultSet != null) {
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
tableColumns.add(buildColumn(resultSet));
|
tableColumns.add(buildColumn(resultSet));
|
||||||
@ -269,8 +273,9 @@ public class SQLExecutor {
|
|||||||
*/
|
*/
|
||||||
public List<TableIndex> indexes(String databaseName, String schemaName, String tableName) {
|
public List<TableIndex> indexes(String databaseName, String schemaName, String tableName) {
|
||||||
List<TableIndex> tableIndices = Lists.newArrayList();
|
List<TableIndex> tableIndices = Lists.newArrayList();
|
||||||
try (ResultSet resultSet = getConnection().getMetaData().getIndexInfo(databaseName, schemaName, tableName, false,
|
try (ResultSet resultSet = getConnection().getMetaData().getIndexInfo(databaseName, schemaName, tableName,
|
||||||
false)) {
|
false,
|
||||||
|
false)) {
|
||||||
List<TableIndexColumn> tableIndexColumns = Lists.newArrayList();
|
List<TableIndexColumn> tableIndexColumns = Lists.newArrayList();
|
||||||
|
|
||||||
while (resultSet != null && resultSet.next()) {
|
while (resultSet != null && resultSet.next()) {
|
||||||
@ -278,18 +283,18 @@ public class SQLExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tableIndexColumns.stream().filter(c -> c.getIndexName() != null).collect(
|
tableIndexColumns.stream().filter(c -> c.getIndexName() != null).collect(
|
||||||
Collectors.groupingBy(TableIndexColumn::getIndexName)).entrySet()
|
Collectors.groupingBy(TableIndexColumn::getIndexName)).entrySet()
|
||||||
.stream().forEach(entry -> {
|
.stream().forEach(entry -> {
|
||||||
TableIndex tableIndex = new TableIndex();
|
TableIndex tableIndex = new TableIndex();
|
||||||
TableIndexColumn column = entry.getValue().get(0);
|
TableIndexColumn column = entry.getValue().get(0);
|
||||||
tableIndex.setName(entry.getKey());
|
tableIndex.setName(entry.getKey());
|
||||||
tableIndex.setTableName(column.getTableName());
|
tableIndex.setTableName(column.getTableName());
|
||||||
tableIndex.setSchemaName(column.getSchemaName());
|
tableIndex.setSchemaName(column.getSchemaName());
|
||||||
tableIndex.setDatabaseName(column.getDatabaseName());
|
tableIndex.setDatabaseName(column.getDatabaseName());
|
||||||
tableIndex.setUnique(!column.getNonUnique());
|
tableIndex.setUnique(!column.getNonUnique());
|
||||||
tableIndex.setColumnList(entry.getValue());
|
tableIndex.setColumnList(entry.getValue());
|
||||||
tableIndices.add(tableIndex);
|
tableIndices.add(tableIndex);
|
||||||
});
|
});
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -304,7 +309,7 @@ public class SQLExecutor {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<ai.chat2db.spi.model.Function> functions(String databaseName,
|
public List<ai.chat2db.spi.model.Function> functions(String databaseName,
|
||||||
String schemaName) {
|
String schemaName) {
|
||||||
List<ai.chat2db.spi.model.Function> functions = Lists.newArrayList();
|
List<ai.chat2db.spi.model.Function> functions = Lists.newArrayList();
|
||||||
try (ResultSet resultSet = getConnection().getMetaData().getFunctions(databaseName, schemaName, null);) {
|
try (ResultSet resultSet = getConnection().getMetaData().getFunctions(databaseName, schemaName, null);) {
|
||||||
while (resultSet != null && resultSet.next()) {
|
while (resultSet != null && resultSet.next()) {
|
||||||
|
Reference in New Issue
Block a user