Merge branch 'developing' into team

This commit is contained in:
JiaJu Zhuang
2023-08-13 12:28:28 +08:00
130 changed files with 3938 additions and 1083 deletions

View File

@ -1,7 +1,6 @@
package ai.chat2db.server.domain.core.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
@ -97,6 +96,10 @@ public class DataSourceServiceImpl implements DataSourceService {
DataResult<DataSource> dataResult = queryById(dataSourceId);
if (dataResult.success() && dataResult.getData() != null) {
DataSource dataSource = dataResult.getData();
DriverConfig driverConfig = dataSource.getDriverConfig();
if (driverConfig == null || StringUtils.isBlank(driverConfig.getJdbcDriver())) {
return;
}
try (Connection connection = IDriverManager.getConnection(dataSource.getUrl(), dataSource.getUserName(),
dataSource.getPassword(), dataSource.getDriverConfig(), dataSource.getExtendMap())) {
DatabaseQueryAllParam databaseQueryAllParam = new DatabaseQueryAllParam();
@ -105,7 +108,7 @@ public class DataSourceServiceImpl implements DataSourceService {
databaseQueryAllParam.setDbType(dataSource.getType());
databaseQueryAllParam.setRefresh(true);
databaseService.queryAll(databaseQueryAllParam);
} catch (SQLException e) {
} catch (Exception e) {
log.error("preWarmingData error", e);
}
}

View File

@ -1,6 +1,8 @@
package ai.chat2db.server.domain.core.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@ -21,6 +23,7 @@ import ai.chat2db.spi.model.Schema;
import ai.chat2db.spi.sql.Chat2DBContext;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@ -40,15 +43,43 @@ public class DatabaseServiceImpl implements DatabaseService {
@Override
public ListResult<Database> queryAll(DatabaseQueryAllParam param) {
List<Database> databases = CacheManage.getList(getDataBasesKey(param.getDataSourceId()), Database.class,
(key) -> param.isRefresh(), (key) -> {
Connection connection = param.getConnection() == null ? Chat2DBContext.getConnection()
: param.getConnection();
MetaData metaData = Chat2DBContext.getMetaData(param.getDbType());
return metaData.databases(connection);
});
(key) -> param.isRefresh(),
(key) -> getDatabases(param.getDbType(), param.getConnection() == null ? Chat2DBContext.getConnection()
: param.getConnection())
);
return ListResult.of(databases);
}
private List<Database> getDatabases(String dbType, Connection connection) {
MetaData metaData = Chat2DBContext.getMetaData(dbType);
List<Database> databases = metaData.databases(connection);
sortDatabases(databases,connection);
return databases;
}
private void sortDatabases(List<Database> databases,Connection connection) {
if (CollectionUtils.isEmpty(databases)) {
return;
}
String ulr = null;
try {
ulr = connection.getMetaData().getURL();
} catch (SQLException e) {
log.error("get url error", e);
}
// If the database name contains the name of the current database, the current database is placed in the first place
int num = -1;
for (int i = 0; i < databases.size(); i++) {
if (StringUtils.isNotBlank(ulr) && ulr.contains(databases.get(i).getName())) {
num = i;
break;
}
}
if (num != -1 && num != 0) {
Collections.swap(databases, num, 0);
}
}
@Override
public ListResult<Schema> querySchema(SchemaQueryParam param) {
List<Schema> schemas = CacheManage.getList(getSchemasKey(param.getDataSourceId(), param.getDataBaseName()),

View File

@ -74,36 +74,38 @@ public class DlTemplateServiceImpl implements DlTemplateService {
// 解析sql分页
SQLStatement sqlStatement;
boolean autoLimit = false;
try {
sqlStatement = SQLUtils.parseSingleStatement(sql, dbType);
// 是否需要代码帮忙分页
if (sqlStatement instanceof SQLSelectStatement) {
// 不是查询全部数据 而且 用户自己没有传分页
autoLimit = BooleanUtils.isNotTrue(param.getPageSizeAll()) && SQLUtils.getLimit(sqlStatement,
dbType)
== null;
if (autoLimit) {
pageNo = Optional.ofNullable(param.getPageNo()).orElse(1);
pageSize = Optional.ofNullable(param.getPageSize()).orElse(EasyToolsConstant.MAX_PAGE_SIZE);
int offset = (pageNo - 1) * pageSize;
try {
sql = PagerUtils.limit(sql, dbType, offset, pageSize);
} catch (Exception e) {
autoLimit = false;
}
}
sqlType = SqlTypeEnum.SELECT.getCode();
}
} catch (ParserException e) {
log.warn("解析sql失败:{}", sql, e);
ExecuteResult executeResult = ExecuteResult.builder()
.success(Boolean.FALSE)
.originalSql(originalSql)
.sql(sql)
.message(e.getMessage())
.build();
result.add(executeResult);
continue;
}
// 是否需要代码帮忙分页
boolean autoLimit = false;
if (sqlStatement instanceof SQLSelectStatement) {
// 不是查询全部数据 而且 用户自己没有传分页
autoLimit = BooleanUtils.isNotTrue(param.getPageSizeAll()) && SQLUtils.getLimit(sqlStatement, dbType)
== null;
if (autoLimit) {
pageNo = Optional.ofNullable(param.getPageNo()).orElse(1);
pageSize = Optional.ofNullable(param.getPageSize()).orElse(EasyToolsConstant.MAX_PAGE_SIZE);
int offset = (pageNo - 1) * pageSize;
try {
sql = PagerUtils.limit(sql, dbType, offset, pageSize);
} catch (Exception e) {
autoLimit = false;
}
}
sqlType = SqlTypeEnum.SELECT.getCode();
//ExecuteResult executeResult = ExecuteResult.builder()
// .success(Boolean.FALSE)
// .originalSql(originalSql)
// .sql(sql)
// .message(e.getMessage())
// .build();
//result.add(executeResult);
//continue;
}
ExecuteResult executeResult = execute(sql);

View File

@ -1,6 +1,7 @@
package ai.chat2db.server.domain.core.impl;
import ai.chat2db.server.domain.api.service.FunctionService;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.spi.model.Function;
import ai.chat2db.spi.sql.Chat2DBContext;
@ -12,4 +13,9 @@ public class FunctionServiceImpl implements FunctionService {
public ListResult<Function> functions(String databaseName, String schemaName) {
return ListResult.of(Chat2DBContext.getMetaData().functions(Chat2DBContext.getConnection(),databaseName, schemaName));
}
@Override
public DataResult<Function> detail(String databaseName, String schemaName, String functionName) {
return DataResult.of(Chat2DBContext.getMetaData().function(Chat2DBContext.getConnection(), databaseName, schemaName, functionName));
}
}

View File

@ -101,6 +101,9 @@ public class OperationServiceImpl implements OperationService {
if (StringUtils.isNotBlank(param.getTabOpened())) {
queryWrapper.eq("tab_opened", param.getTabOpened());
}
if (StringUtils.isNotBlank(param.getOperationType())) {
queryWrapper.eq("operation_type", param.getOperationType());
}
Integer start = param.getPageNo();
Integer offset = param.getPageSize();
Page<OperationSavedDO> page = new Page<>(start, offset);

View File

@ -1,6 +1,7 @@
package ai.chat2db.server.domain.core.impl;
import ai.chat2db.server.domain.api.service.ProcedureService;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.spi.model.Procedure;
import ai.chat2db.spi.sql.Chat2DBContext;
@ -13,4 +14,9 @@ public class ProcedureServiceImpl implements ProcedureService {
public ListResult<Procedure> procedures(String databaseName, String schemaName) {
return ListResult.of(Chat2DBContext.getMetaData().procedures(Chat2DBContext.getConnection(),databaseName, schemaName));
}
@Override
public DataResult<Procedure> detail(String databaseName, String schemaName, String procedureName) {
return DataResult.of(Chat2DBContext.getMetaData().procedure(Chat2DBContext.getConnection(), databaseName, schemaName, procedureName));
}
}

View File

@ -1,6 +1,7 @@
package ai.chat2db.server.domain.core.impl;
import ai.chat2db.server.domain.api.service.TriggerService;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.spi.model.Trigger;
import ai.chat2db.spi.sql.Chat2DBContext;
@ -12,4 +13,9 @@ public class TriggerServiceImpl implements TriggerService {
public ListResult<Trigger> triggers(String databaseName, String schemaName) {
return ListResult.of(Chat2DBContext.getMetaData().triggers(Chat2DBContext.getConnection(),databaseName, schemaName));
}
@Override
public DataResult<Trigger> detail(String databaseName, String schemaName, String triggerName) {
return DataResult.of(Chat2DBContext.getMetaData().trigger(Chat2DBContext.getConnection(), databaseName, schemaName, triggerName));
}
}

View File

@ -1,7 +1,9 @@
package ai.chat2db.server.domain.core.impl;
import ai.chat2db.server.domain.api.service.ViewService;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.spi.MetaData;
import ai.chat2db.spi.model.Table;
import ai.chat2db.spi.sql.Chat2DBContext;
import org.springframework.stereotype.Service;
@ -13,4 +15,12 @@ public class ViewServiceImpl implements ViewService {
public ListResult<Table> views(String databaseName, String schemaName) {
return ListResult.of(Chat2DBContext.getMetaData().views(Chat2DBContext.getConnection(),databaseName, schemaName));
}
@Override
public DataResult<Table> detail(String databaseName, String schemaName, String tableName) {
MetaData metaSchema = Chat2DBContext.getMetaData();
Table table = metaSchema.view(Chat2DBContext.getConnection(), databaseName, schemaName, tableName);
return DataResult.of(table);
}
}