export info

This commit is contained in:
SwallowGG
2024-06-26 22:41:48 +08:00
parent 028c3ec4ac
commit 9a6e89dfb4
5 changed files with 103 additions and 63 deletions

View File

@ -61,7 +61,7 @@ public class MysqlDBManage extends DefaultDBManage implements DBManage {
exportTable(connection, databaseName, schemaName, tableName, asyncContext); exportTable(connection, databaseName, schemaName, tableName, asyncContext);
} }
} }
asyncContext.write("SET FOREIGN_KEY_CHECKS=1;"); asyncContext.write("SET FOREIGN_KEY_CHECKS=1;\n");
} }
@ -74,14 +74,13 @@ public class MysqlDBManage extends DefaultDBManage implements DBManage {
sqlBuilder.append("DROP TABLE IF EXISTS ").append(format(tableName)).append(";").append("\n") sqlBuilder.append("DROP TABLE IF EXISTS ").append(format(tableName)).append(";").append("\n")
.append(resultSet.getString("Create Table")).append(";").append("\n"); .append(resultSet.getString("Create Table")).append(";").append("\n");
asyncContext.write(sqlBuilder.toString()); asyncContext.write(sqlBuilder.toString());
asyncContext.write("\n");
if (asyncContext.isContainsData()) { if (asyncContext.isContainsData()) {
exportTableData(connection, databaseName, schemaName, tableName, asyncContext); exportTableData(connection, databaseName, schemaName, tableName, asyncContext);
} }
} }
}catch (Exception e){ }catch (Exception e){
log.error("export table error", e); log.error("export table error", e);
asyncContext.getCall().error(String.format("export table %s error:%s", tableName,e.getMessage())); asyncContext.error(String.format("export table %s error:%s", tableName,e.getMessage()));
} }
} }

View File

@ -19,6 +19,7 @@ import ai.chat2db.server.domain.core.cache.CacheManage;
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.tools.base.wrapper.result.ListResult;
import ai.chat2db.server.tools.common.util.ContextUtils;
import ai.chat2db.spi.MetaData; import ai.chat2db.spi.MetaData;
import ai.chat2db.spi.model.*; import ai.chat2db.spi.model.*;
import ai.chat2db.spi.sql.Chat2DBContext; import ai.chat2db.spi.sql.Chat2DBContext;
@ -178,34 +179,15 @@ public class DatabaseServiceImpl implements DatabaseService {
@Override @Override
public String exportDatabase(DatabaseExportParam param) throws SQLException { public String exportDatabase(DatabaseExportParam param) throws SQLException {
AsyncContext asyncContext = new AsyncContext(); AsyncCall call = new AsyncCall() {
asyncContext.setContainsData(param.getContainData());
asyncContext.setCall(new AsyncCall() {
@Override
public void setProgress(int progress) {
}
@Override
public void info(String message) {
}
@Override
public void error(String message) {
}
@Override @Override
public void update(Map<String, Object> map) { public void update(Map<String, Object> map) {
} }
};
@Override AsyncContext asyncContext = new AsyncContext(call, ContextUtils.queryContext(), null, param.getContainData());
public void finish() {
}
});
Chat2DBContext.getDBManage().exportDatabase(Chat2DBContext.getConnection(), Chat2DBContext.getDBManage().exportDatabase(Chat2DBContext.getConnection(),
param.getDatabaseName(), param.getDatabaseName(),
param.getSchemaName(), asyncContext); param.getSchemaName(), asyncContext);

View File

@ -46,7 +46,7 @@ public class DefaultDBManage implements DBManage {
protected static final String PROCEDURE_TITLE = DIVIDING_LINE + NEW_LINE + "-- Procedure structure for procedure %s"+ NEW_LINE + DIVIDING_LINE; protected static final String PROCEDURE_TITLE = DIVIDING_LINE + NEW_LINE + "-- Procedure structure for procedure %s"+ NEW_LINE + DIVIDING_LINE;
private static final String RECORD_TITLE = DIVIDING_LINE + NEW_LINE +"-- Records of "+ NEW_LINE + DIVIDING_LINE; private static final String RECORD_TITLE = DIVIDING_LINE + NEW_LINE +"-- Records of %s"+ NEW_LINE + DIVIDING_LINE;
@Override @Override
@ -209,7 +209,6 @@ public class DefaultDBManage implements DBManage {
asyncContext.write(insertSql); asyncContext.write(insertSql);
valueList.clear(); valueList.clear();
} }
asyncContext.write("\n");
}); });
} }

View File

@ -4,19 +4,6 @@ import java.util.Map;
public interface AsyncCall { public interface AsyncCall {
void setProgress(int progress);
void info(String message);
void error(String message);
void update(Map<String,Object> map); void update(Map<String,Object> map);
void finish();
} }

View File

@ -1,59 +1,132 @@
package ai.chat2db.spi.model; package ai.chat2db.spi.model;
import lombok.AllArgsConstructor; import ai.chat2db.server.tools.common.model.Context;
import lombok.Builder; import ai.chat2db.server.tools.common.util.ContextUtils;
import lombok.Data; import cn.hutool.core.io.FileUtil;
import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j;
import lombok.experimental.SuperBuilder;
import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.function.Consumer; import java.util.HashMap;
import java.util.Map;
@Data @Slf4j
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class AsyncContext { public class AsyncContext {
private File writeFile;
protected PrintWriter writer; protected PrintWriter writer;
protected boolean containsData; protected boolean containsData;
protected AsyncCall call; protected AsyncCall call;
public void setProgress(Integer progress) { protected boolean finish;
if (call != null) {
call.setProgress(progress); public File getWriteFile() {
return writeFile;
}
public AsyncContext(AsyncCall call, Context context, File writeFile, boolean containsData) {
this.call = call;
this.writeFile = writeFile;
this.progress = 5;
this.containsData = containsData;
createWriter();
asyncCallBack(context);
}
private void createWriter() {
if (writeFile != null) {
this.writer = FileUtil.getPrintWriter(writeFile, "UTF-8", false);
} }
} }
private void asyncCallBack(Context context) {
if (call != null && context != null) {
new Thread(() -> {
try {
ContextUtils.setContext(context);
int n = 1;
while (!finish) {
// 更新时间逐渐变长避免频繁更新
Thread.sleep(2000 * n);
callUpdate();
if (n < 300) {
n++;
}
}
} catch (Exception e) {
log.error("AsyncContext call error", e);
} finally {
ContextUtils.removeContext();
}
}).start();
}
}
private void callUpdate() {
if (call == null) {
return;
}
Map<String, Object> map = new HashMap<>();
map.put("progress", progress);
map.put("info", info.toString());
map.put("error", error.toString());
map.put("status", finish ? "FINISHED" : "RUNNING");
if (progress == 100 && writeFile != null) {
map.put("downloadUrl", writeFile.getAbsolutePath());
}
info = new StringBuffer();
error = new StringBuffer();
call.update(map);
}
public boolean isContainsData() {
return containsData;
}
public void setProgress(Integer progress) {
if (progress == null) {
return;
}
this.progress = progress;
}
public void info(String message) { public void info(String message) {
if (call != null) { info.append(message + "\n");
call.info(message);
}
} }
public void error(String message) { public void error(String message) {
if (call != null) { error.append(message + "\n");
call.error(message); info.append(message + "\n");
}
} }
public void finish() { public void finish() {
finish = true;
this.progress = 100;
if (writer != null) { if (writer != null) {
writer.flush(); writer.flush();
writer.close(); writer.close();
} }
if (call != null) { callUpdate();
call.finish();
}
} }
public void write(String message) { public void write(String message) {
if (writer != null) { if (writer != null) {
writer.write(message); writer.write(message + "\n");
} }
} }
protected Integer progress;
private StringBuffer info = new StringBuffer();
private StringBuffer error = new StringBuffer();
} }