optimize excel handler

This commit is contained in:
Haiping Chen 2024-10-03 14:09:14 -05:00
parent 2eaffc7c3a
commit 105f655db3
9 changed files with 33 additions and 66 deletions

View file

@ -12,6 +12,20 @@
<None Remove="Provider\**" />
</ItemGroup>
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_excel_request.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_excel_request.fn.liquid" />
</ItemGroup>
<ItemGroup>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_excel_request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_excel_request.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
@ -19,11 +33,6 @@
<PackageReference Include="NPOI" Version="2.7.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\" />
<Folder Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Plugin.SqlDriver\BotSharp.Plugin.SqlDriver.csproj" />

View file

@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.ExcelHandler.Enums;
public class UtilityName

View file

@ -19,6 +19,7 @@ public class ExcelHandlerPlugin : IBotSharpPlugin
public string Id => "c56a8e29-b16f-4d75-8766-8309342130cb";
public string Name => "Excel Handler";
public string Description => "Load data from excel file and transform it into a list of JSON format.";
public string IconUrl => "https://w7.pngwing.com/pngs/162/301/png-transparent-microsoft-excel-logo-thumbnail.png";
public void RegisterDI(IServiceCollection services, IConfiguration config)
{

View file

@ -72,7 +72,6 @@ public class HandleExcelRequestFn : IFunctionCallback
var resultList = GetResponeFromDialogs(dialogs);
message.Content = GenerateSqlExecutionSummary(resultList);
}
message.StopCompletion = true;
return true;
}
@ -131,7 +130,7 @@ public class HandleExcelRequestFn : IFunctionCallback
{
try
{
_mySqlService.DeleteTableSqlQuery();
//_mySqlService.DeleteTableSqlQuery();
return true;
}
catch (Exception ex)
@ -151,8 +150,6 @@ public class HandleExcelRequestFn : IFunctionCallback
foreach (var message in messageList.Where(x => x.isSuccessful))
{
stringBuilder.Append(message.Message);
string tableSchemaInfo = _mySqlService.GenerateTableSchema();
stringBuilder.Append(tableSchemaInfo);
stringBuilder.Append("\r\n\r\n");
}
}

View file

@ -36,8 +36,8 @@ namespace BotSharp.Plugin.ExcelHandler.Helpers.MySql
private void InitializeDatabase()
{
var settingService = _services.GetRequiredService<SqlDriverSetting>();
_mySqlDriverConnection = settingService.MySqlConnectionString;
_databaseName = GetDatabaseName(settingService.MySqlConnectionString);
_mySqlDriverConnection = settingService.MySqlTempConnectionString;
_databaseName = GetDatabaseName(settingService.MySqlTempConnectionString);
}
private string GetDatabaseName(string connectionString)

View file

@ -1,16 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Plugin.ExcelHandler.Models;
using NPOI.SS.UserModel;
namespace BotSharp.Plugin.ExcelHandler.Services;
namespace BotSharp.Plugin.ExcelHandler.Services
public interface IMySqlService : IDbService
{
public interface IMySqlService : IDbService
{
public bool DeleteTableSqlQuery();
public string GenerateTableSchema();
}
public bool DeleteTableSqlQuery();
}

View file

@ -1,17 +1,8 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Plugin.ExcelHandler.Helpers.MySql;
using BotSharp.Plugin.ExcelHandler.Models;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MySql.Data.MySqlClient;
//using MySqlConnector;
using NPOI.SS.UserModel;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace BotSharp.Plugin.ExcelHandler.Services
{
@ -35,13 +26,13 @@ namespace BotSharp.Plugin.ExcelHandler.Services
{
try
{
using var mySqlDbConnection = _mySqlDbHelpers.GetDbConnection();
/*using var mySqlDbConnection = _mySqlDbHelpers.GetDbConnection();
var tableNames = GetAllTableSchema(mySqlDbConnection);
if (tableNames.IsNullOrEmpty())
{
return true;
}
ExecuteDropTableQuery(tableNames, mySqlDbConnection);
}*/
return true;
}
catch (Exception ex)
@ -49,7 +40,8 @@ namespace BotSharp.Plugin.ExcelHandler.Services
return false;
}
}
private void ExecuteDropTableQuery(List<string> dropTableNames, MySqlConnection connection)
/*private void ExecuteDropTableQuery(List<string> dropTableNames, MySqlConnection connection)
{
dropTableNames.ForEach(x =>
{
@ -58,7 +50,7 @@ namespace BotSharp.Plugin.ExcelHandler.Services
using var selectCmd = new MySqlCommand(dropTableQuery, connection);
selectCmd.ExecuteNonQuery();
});
}
}*/
public List<string> GetAllTableSchema(MySqlConnection mySqlDbConnection)
{
@ -89,23 +81,6 @@ namespace BotSharp.Plugin.ExcelHandler.Services
}
}
public string GenerateTableSchema()
{
var sb = new StringBuilder();
sb.Append($"\nTable Schema for `{_tableName}`:");
sb.Append("\n");
sb.Append($"cid | name | type ");
sb.Append("\n");
//sb.Append("----|------------|------------");
for (int i = 0; i < _excelColumnSize; i++)
{
sb.Append($"{i,-4} | {_headerColumns[i],-10} | {_columnTypes[i],-10}");
sb.Append("\n");
}
return sb.ToString();
}
public IEnumerable<SqlContextOut> WriteExcelDataToDB(IWorkbook workbook)
{
var numTables = workbook.NumberOfSheets;
@ -133,7 +108,7 @@ namespace BotSharp.Plugin.ExcelHandler.Services
commandResult = new SqlContextOut
{
isSuccessful = isInsertSuccess,
Message = insertMessage,
Message = $"{insertMessage}\r\n{message}",
FileName = _currentFileName
};
commandList.Add(commandResult);
@ -155,7 +130,7 @@ namespace BotSharp.Plugin.ExcelHandler.Services
string insertDataSql = ProcessInsertSqlQuery(dataSql);
ExecuteSqlQueryForInsertion(insertDataSql);
return (true, $"{_currentFileName}: \r\n `**{_excelRowSize}**` data have been successfully stored into `{_tableName}` table");
return (true, $"{_currentFileName}: \r\n {_excelRowSize} records have been successfully inserted into `{_tableName}` table");
}
catch (Exception ex)
{
@ -210,11 +185,11 @@ namespace BotSharp.Plugin.ExcelHandler.Services
{
try
{
_tableName = sheet.SheetName;
_tableName = $"excel_{sheet.SheetName}";
_headerColumns = ParseSheetColumn(sheet);
string createTableSql = CreateDBTableSqlString(_tableName, _headerColumns, null ,true);
ExecuteSqlQueryForInsertion(createTableSql);
return (true, $"{_tableName} has been successfully created.");
return (true, createTableSql);
}
catch (Exception ex)
{

View file

@ -118,7 +118,7 @@ namespace BotSharp.Plugin.ExcelHandler.Services
string insertDataSql = ProcessInsertSqlQuery(dataSql);
ExecuteSqlQueryForInsertion(insertDataSql);
return (true, $"{_currentFileName}: \r\n `**{_excelRowSize}**` data have been successfully stored into `{_tableName}` table");
return (true, $"{_currentFileName}: \r\n {_excelRowSize} records have been successfully inserted into `{_tableName}` table");
}
catch (Exception ex)
{

View file

@ -5,6 +5,7 @@ public class SqlDriverSetting
public string DatabaseType { get; set; } = "MySql";
public string MySqlConnectionString { get; set; } = null!;
public string MySqlExecutionConnectionString { get; set; } = null!;
public string MySqlTempConnectionString { get; set; } = null!;
public string SqlServerConnectionString { get; set; } = null!;
public string SqlServerExecutionConnectionString { get; set; } = null!;
public string SqlLiteConnectionString { get; set; } = null!;