BotSharp/src/Plugins/BotSharp.Plugin.SqlDriver/UtilFunctions/SqlSelect.cs
Joanna Ren b557342bb1 Refactor SQL Driver Plugin and Update Database Handling
- Added a new folder for helpers in the project file.
- Removed the `SqlDriverHelper` class and its `GetDatabaseType` method.
- Updated `SqlDriverAgentHook` to retrieve database type directly from `SqlStatement`.
- Enhanced `SqlStatement` with new properties: `DBProvider`, `Schema`, and `Tables`.
- Removed registration of `SqlDriverAgentHook` in `SqlDriverPlugin`.
- Cleaned up global using directives by removing `Helpers`.
- Modified `GetTableDefinitionFn` to accept a schema parameter for Redshift queries.
- Updated `SqlSelect` to use `DBProvider` from `SqlStatement`.
- Revised JSON schema definitions to require `db_provider` and `schema`.
- Clarified Liquid template instructions for database connections and SQL execution.
- Streamlined rules for calling `util-db-verify_dictionary_term`.
2025-06-10 16:55:33 -05:00

87 lines
2.8 KiB
C#

using Microsoft.Data.SqlClient;
using MySqlConnector;
using Npgsql;
using static Dapper.SqlMapper;
namespace BotSharp.Plugin.SqlDriver.UtilFunctions;
public class SqlSelect : IFunctionCallback
{
public string Name => "util-db-sql_select";
private readonly IServiceProvider _services;
public SqlSelect(IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<SqlStatement>(message.FunctionArgs);
if (args.GeneratedWithoutTableDefinition)
{
message.Content = $"Get the table definition first.";
return false;
}
var dbType = args.DBProvider.ToLowerInvariant();
var result = dbType switch
{
"mysql" => RunQueryInMySql(args),
"sqlserver" => RunQueryInSqlServer(args),
"redshift" => RunQueryInRedshift(args),
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
};
if (result == null)
{
message.Content = "Record not found";
}
else
{
message.Content = JsonSerializer.Serialize(result);
args.Return.Value = message.Content;
}
return true;
}
private IEnumerable<dynamic> RunQueryInMySql(SqlStatement args)
{
var settings = _services.GetRequiredService<SqlDriverSetting>();
using var connection = new MySqlConnection(settings.MySqlExecutionConnectionString);
var dictionary = new Dictionary<string, object>();
foreach (var p in args.Parameters)
{
dictionary["@" + p.Name] = p.Value;
}
return connection.Query(args.Statement, dictionary);
}
private IEnumerable<dynamic> RunQueryInSqlServer(SqlStatement args)
{
var settings = _services.GetRequiredService<SqlDriverSetting>();
using var connection = new SqlConnection(settings.SqlServerExecutionConnectionString ?? settings.SqlServerConnectionString);
var dictionary = new Dictionary<string, object>();
foreach (var p in args.Parameters)
{
dictionary["@" + p.Name] = p.Value;
}
return connection.Query(args.Statement, dictionary);
}
private IEnumerable<dynamic> RunQueryInRedshift(SqlStatement args)
{
var settings = _services.GetRequiredService<SqlDriverSetting>();
using var connection = new NpgsqlConnection(settings.RedshiftConnectionString);
var dictionary = new Dictionary<string, object>();
foreach (var p in args.Parameters)
{
dictionary["@" + p.Name] = p.Value;
}
return connection.Query(args.Statement, dictionary);
}
}