Skip to content

Commit 4cb8529

Browse files
authored
Merge pull request #768 from hchen2020/master
Fix sql_select dbtype
2 parents 75a1664 + b52cb96 commit 4cb8529

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/Plugins/BotSharp.Plugin.SqlDriver/Functions/ExecuteQueryFn.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
using BotSharp.Abstraction.Agents.Enums;
2-
using BotSharp.Abstraction.Routing;
31
using BotSharp.Core.Infrastructures;
4-
using BotSharp.Plugin.SqlDriver.Interfaces;
5-
using BotSharp.Plugin.SqlDriver.Models;
62
using Dapper;
73
using Microsoft.Data.SqlClient;
8-
using Microsoft.Extensions.Logging;
94
using MySqlConnector;
105
using Npgsql;
11-
using System.Data.Common;
126

137
namespace BotSharp.Plugin.SqlDriver.Functions;
148

@@ -113,6 +107,7 @@ private IEnumerable<dynamic> RunQueryInSqlServer(string[] sqlTexts)
113107
using var connection = new SqlConnection(settings.SqlServerExecutionConnectionString ?? settings.SqlServerConnectionString);
114108
return connection.Query(string.Join("\r\n", sqlTexts));
115109
}
110+
116111
private IEnumerable<dynamic> RunQueryInRedshift(string[] sqlTexts)
117112
{
118113
var settings = _services.GetRequiredService<SqlDriverSetting>();

src/Plugins/BotSharp.Plugin.SqlDriver/Functions/SqlSelect.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using BotSharp.Plugin.SqlDriver.Models;
21
using Microsoft.Data.SqlClient;
32
using MySqlConnector;
3+
using Npgsql;
44
using static Dapper.SqlMapper;
55

66
namespace BotSharp.Plugin.SqlDriver.Functions;
@@ -26,12 +26,15 @@ public async Task<bool> Execute(RoleDialogModel message)
2626
}
2727

2828
// check if need to instantely
29-
var settings = _services.GetRequiredService<SqlDriverSetting>();
30-
var result = settings.DatabaseType switch
29+
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
30+
var dbType = dbHook.GetDatabaseType(message);
31+
32+
var result = dbType switch
3133
{
32-
"MySql" => RunQueryInMySql(args),
33-
"SqlServer" => RunQueryInSqlServer(args),
34-
_ => throw new NotImplementedException($"Database type {settings.DatabaseType} is not supported.")
34+
"mysql" => RunQueryInMySql(args),
35+
"sqlserver" => RunQueryInSqlServer(args),
36+
"redshift" => RunQueryInRedshift(args),
37+
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
3538
};
3639

3740
if (result == null)
@@ -70,4 +73,16 @@ private IEnumerable<dynamic> RunQueryInSqlServer(SqlStatement args)
7073
}
7174
return connection.Query(args.Statement, dictionary);
7275
}
76+
77+
private IEnumerable<dynamic> RunQueryInRedshift(SqlStatement args)
78+
{
79+
var settings = _services.GetRequiredService<SqlDriverSetting>();
80+
using var connection = new NpgsqlConnection(settings.RedshiftConnectionString);
81+
var dictionary = new Dictionary<string, object>();
82+
foreach (var p in args.Parameters)
83+
{
84+
dictionary["@" + p.Name] = p.Value;
85+
}
86+
return connection.Query(args.Statement, dictionary);
87+
}
7388
}

0 commit comments

Comments
 (0)