Fix sql_select dbtype

This commit is contained in:
Haiping Chen 2024-11-27 21:23:28 -06:00
parent ad27596959
commit 9793816e66
2 changed files with 22 additions and 12 deletions

View file

@ -1,14 +1,8 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.SqlDriver.Interfaces;
using BotSharp.Plugin.SqlDriver.Models;
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using Npgsql;
using System.Data.Common;
namespace BotSharp.Plugin.SqlDriver.Functions;
@ -113,6 +107,7 @@ public class ExecuteQueryFn : IFunctionCallback
using var connection = new SqlConnection(settings.SqlServerExecutionConnectionString ?? settings.SqlServerConnectionString);
return connection.Query(string.Join("\r\n", sqlTexts));
}
private IEnumerable<dynamic> RunQueryInRedshift(string[] sqlTexts)
{
var settings = _services.GetRequiredService<SqlDriverSetting>();

View file

@ -1,6 +1,6 @@
using BotSharp.Plugin.SqlDriver.Models;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using Npgsql;
using static Dapper.SqlMapper;
namespace BotSharp.Plugin.SqlDriver.Functions;
@ -26,12 +26,15 @@ public class SqlSelect : IFunctionCallback
}
// check if need to instantely
var settings = _services.GetRequiredService<SqlDriverSetting>();
var result = settings.DatabaseType switch
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
var dbType = dbHook.GetDatabaseType(message);
var result = dbType switch
{
"MySql" => RunQueryInMySql(args),
"SqlServer" => RunQueryInSqlServer(args),
_ => throw new NotImplementedException($"Database type {settings.DatabaseType} is not supported.")
"mysql" => RunQueryInMySql(args),
"sqlserver" => RunQueryInSqlServer(args),
"redshift" => RunQueryInRedshift(args),
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
};
if (result == null)
@ -70,4 +73,16 @@ public class SqlSelect : IFunctionCallback
}
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);
}
}