Skip to content

Commit f6ce283

Browse files
authored
Merge pull request SciSharp#1074 from Joannall/master
Refactor SQL Driver Plugin and Update Database Handling
2 parents 6f0754c + b557342 commit f6ce283

File tree

12 files changed

+54
-58
lines changed

12 files changed

+54
-58
lines changed

src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,8 @@
108108
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
109109
</ItemGroup>
110110

111+
<ItemGroup>
112+
<Folder Include="Helpers\" />
113+
</ItemGroup>
114+
111115
</Project>

src/Plugins/BotSharp.Plugin.SqlDriver/Helpers/SqlDriverHelper.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverAgentHook.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.SqlDriver/Models/SqlStatement.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ namespace BotSharp.Plugin.SqlDriver.Models;
44

55
public class SqlStatement
66
{
7+
[JsonPropertyName("db_provider")]
8+
public string DBProvider { get; set; } = null!;
9+
710
[JsonPropertyName("sql_statement")]
811
public string Statement { get; set; } = null!;
912

1013
[JsonPropertyName("reason")]
1114
public string Reason { get; set; } = null!;
1215

16+
[JsonPropertyName("schema")]
17+
public string Schema { get; set; } = null!;
18+
1319
[JsonPropertyName("tables")]
1420
public string[] Tables { get; set; } = null!;
1521

src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2727
services.AddScoped<DbKnowledgeService>();
2828
services.AddScoped<IPlanningHook, SqlDriverPlanningHook>();
2929
services.AddScoped<IKnowledgeHook, SqlDriverKnowledgeHook>();
30-
services.AddScoped<IAgentHook, SqlDriverAgentHook>();
3130
services.AddScoped<IConversationHook, SqlDriverConversationHook>();
3231
services.AddScoped<IAgentUtilityHook, SqlUtilityHook>();
3332
services.AddScoped<ICrontabHook, SqlDriverCrontabHook>();

src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@
3030
global using BotSharp.Plugin.SqlDriver.Hooks;
3131
global using BotSharp.Plugin.SqlDriver.Services;
3232
global using BotSharp.Plugin.SqlDriver.Interfaces;
33-
global using BotSharp.Plugin.SqlDriver.Helpers;
3433
global using BotSharp.Plugin.SqlDriver.Settings;

src/Plugins/BotSharp.Plugin.SqlDriver/UtilFunctions/GetTableDefinitionFn.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ public async Task<bool> Execute(RoleDialogModel message)
2323
{
2424
var args = JsonSerializer.Deserialize<SqlStatement>(message.FunctionArgs);
2525
var tables = args.Tables;
26+
var dbType = args.DBProvider;
27+
var schema = args.Schema;
2628
var agentService = _services.GetRequiredService<IAgentService>();
27-
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
28-
var dbType = dbHook.GetDatabaseType(message);
2929

3030
// Get table DDL from database
3131
var tableDdls = dbType switch
3232
{
3333
"mysql" => GetDdlFromMySql(tables),
3434
"sqlserver" => GetDdlFromSqlServer(tables),
35-
"redshift" => GetDdlFromRedshift(tables),
35+
"redshift" => GetDdlFromRedshift(tables,schema),
3636
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
3737
};
3838

@@ -127,7 +127,7 @@ FROM INFORMATION_SCHEMA.COLUMNS
127127
return tableDdls;
128128
}
129129

130-
private List<string> GetDdlFromRedshift(string[] tables)
130+
private List<string> GetDdlFromRedshift(string[] tables, string schema)
131131
{
132132
var settings = _services.GetRequiredService<SqlDriverSetting>();
133133
var tableDdls = new List<string>();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public async Task<bool> Execute(RoleDialogModel message)
2525
return false;
2626
}
2727

28-
// check if need to instantely
29-
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
30-
var dbType = dbHook.GetDatabaseType(message);
28+
var dbType = args.DBProvider.ToLowerInvariant();
3129

3230
var result = dbType switch
3331
{

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-db-sql_select.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
"parameters": {
55
"type": "object",
66
"properties": {
7+
"db_provider": {
8+
"type": "string",
9+
"enum": [
10+
"mysql",
11+
"postgresql",
12+
"mssql",
13+
"redshift"
14+
],
15+
"description": "The database engine."
16+
},
717
"sql_statement": {
818
"type": "string",
919
"description": "SQL statement with SELECT"
@@ -55,6 +65,6 @@
5565
"required": [ "name", "value" ]
5666
}
5767
},
58-
"required": [ "sql_statement", "reason", "table", "parameters", "return_field" ]
68+
"required": [ "db_provider", "sql_statement", "reason", "table", "parameters", "return_field" ]
5969
}
6070
}

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-db-sql_table_definition.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
"parameters": {
55
"type": "object",
66
"properties": {
7+
"db_provider": {
8+
"type": "string",
9+
"enum": [
10+
"mysql",
11+
"postgresql",
12+
"mssql",
13+
"redshift"
14+
],
15+
"description": "The database engine."
16+
},
17+
"schema": {
18+
"type": "string",
19+
"description": "schema name for tables. Typically, the part before the dot is the schema name, e.g.smsonebi.affiliate_profile, schema name is smsonebi.",
20+
"items": {
21+
"type": "string",
22+
"description": "schema name"
23+
}
24+
},
725
"tables": {
826
"type": "array",
927
"description": "table name in planning steps",
@@ -17,6 +35,6 @@
1735
"description": "the reason why you need to call sql_table_definition"
1836
}
1937
},
20-
"required": [ "tables", "reason" ]
38+
"required": [ "db_provider", "schema", "tables", "reason" ]
2139
}
2240
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
You are connecting to {{ db_type }} database. You can run provided SQL statements by following {{ db_type }} rules.
2-
3-
Please call function util-db-sql_select if user wants to get or retrieve data from data tables.
4-
If there are any parameters, please add them in the WHERE clause, each of which starts with "@".
5-
Avoid returning the entire record and only return the fields you need.
6-
For example, SELECT Id FROM table WHERE Id=@Id AND Name=@Name
1+
Please call function util-db-sql_select if user wants to retrieve data from database tables.
2+
- Ensure you obtain the exact provider, schema, table and column names.
3+
If user provide them, directly call util-db-sql_select to execute the query.
4+
If user didn't provide the provider(e.g. mysql, redshift), schema or table name, ask user to provide.
5+
If the user doesn't provide exact column name, try using other available functions to infer the information.
6+
If that fails or no function can use, prompt the user to provide the missing information.
7+
- If there are any parameters, please add them in the WHERE clause, each of which starts with "@".
8+
- Avoid returning the entire record and only return the fields you need.
9+
For example, SELECT Id FROM table WHERE Id=@Id AND Name=@Name

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-db-verify_dictionary_term.fn.liquid

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Rules to call util-db-verify_dictionary_term:
44
2. You must return the id and name/code for existing dictionary.
55
3. Call the function only if need_lookup_dictionary is true.
66

7-
You are connecting to {{ db_type }} database. You can run provided SQL statements by following {{ db_type }} rules.
8-
97
The dictionary table is identified by a name that begins with "data_".
108
You are only allowed to query the dictionary table without joining it with other non-dictionary tables.
119

0 commit comments

Comments
 (0)