Skip to content

Commit

Permalink
Some bugfixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Oct 25, 2023
1 parent 34fe6ab commit 5a1bd26
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 139 deletions.
8 changes: 4 additions & 4 deletions samples/DatabaseGptConsole/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using DatabaseGpt.Extensions;
using DatabaseGpt.Models;
using DatabaseGpt.Settings;
using Microsoft.Extensions.Options;
using Spectre.Console;

namespace DatabaseGptConsole;
Expand All @@ -13,10 +12,10 @@ internal class Application
private readonly IDatabaseGptClient databaseGptClient;
private readonly DatabaseGptSettings databaseSettings;

public Application(IDatabaseGptClient databaseGptClient, IOptions<DatabaseGptSettings> databaseSettingsOptions)
public Application(IDatabaseGptClient databaseGptClient, DatabaseGptSettings databaseSettings)
{
this.databaseGptClient = databaseGptClient;
databaseSettings = databaseSettingsOptions.Value;
this.databaseSettings = databaseSettings;
}

public async Task ExecuteAsync()
Expand Down Expand Up @@ -46,7 +45,7 @@ public async Task ExecuteAsync()
AnsiConsole.WriteLine();
AnsiConsole.WriteLine();
AnsiConsole.WriteLine($"I think the following tables might be useful: {string.Join(", ", args.Tables)}.");
AnsiConsole.Write($"I think the following tables might be useful: {string.Join(", ", args.Tables)}.");
return default;
},
Expand All @@ -57,6 +56,7 @@ public async Task ExecuteAsync()
AnsiConsole.WriteLine("The query to answer the question should be the following:");
AnsiConsole.WriteLine();
AnsiConsole.WriteLine(args.Sql);
AnsiConsole.WriteLine();
Expand Down
1 change: 0 additions & 1 deletion samples/DatabaseGptConsole/DatabaseGptConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>cace36d6-2904-4a1a-ae73-7f5071a723ed</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
23 changes: 13 additions & 10 deletions samples/DatabaseGptConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@
static void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.AddSingleton<Application>();

services.AddDatabaseGpt(database =>
{
// For using SQL Server
database.UseConfiguration(context.Configuration)
.UseSqlServer(context.Configuration["ConnectionStrings:SqlConnection"]);
{
// For SQL Server.
database.UseConfiguration(context.Configuration)
.UseSqlServer(context.Configuration["ConnectionStrings:SqlConnection"]);
// For using Postgres
// database.UseConfiguration(context.Configuration)
// .UseNpgsql(context.Configuration["ConnectionStrings:SqlConnection"]);
},
chatgpt => chatgpt.UseConfiguration(context.Configuration)
);
// For Postgre SQL.
//database.UseConfiguration(context.Configuration)
// .UseNpgsql(context.Configuration["ConnectionStrings:NpgsqlConnection"]);
},
chatGpt =>
{
chatGpt.UseConfiguration(context.Configuration);
});
}
2 changes: 1 addition & 1 deletion samples/DatabaseGptConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"MessageLimit": 20,
"MessageExpiration": "00:30:00"
},
"DatabaseSettings": {
"DatabaseGptSettings": {
"IncludedTables": [ ],
"ExcludedTables": [ ],
"ExcludedColumns": [ ],
Expand Down
13 changes: 1 addition & 12 deletions src/DatabaseGpt.Abstractions/Exceptions/DatabaseGptException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseGpt.Abstractions.Exceptions;
namespace DatabaseGpt.Abstractions.Exceptions;

public class DatabaseGptException : Exception
{
Expand All @@ -20,8 +13,4 @@ public DatabaseGptException(string? message) : base(message)
public DatabaseGptException(string? message, Exception? innerException) : base(message, innerException)
{
}

protected DatabaseGptException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
7 changes: 1 addition & 6 deletions src/DatabaseGpt.Abstractions/IDatabaseGptProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace DatabaseGpt.Abstractions;

Expand Down
8 changes: 5 additions & 3 deletions src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using DatabaseGpt.Abstractions;
using DatabaseGpt.Npgsql;
using Microsoft.Extensions.DependencyInjection;

namespace DatabaseGpt;

public static class NpgsqlDatabaseGptExtensions
{
public static void UseNpgsql(this IDatabaseGptSettings databaseGptSettings, string connectionString)
public static void UseNpgsql(this IDatabaseGptSettings databaseGptSettings, string? connectionString)
{
databaseGptSettings.SetDatabaseGptProviderFactory(() => new NpgsqlDatabaseGptProvider(new() { ConnectionString = connectionString }));
databaseGptSettings.SetDatabaseGptProviderFactory(() => new NpgsqlDatabaseGptProvider(new()
{
ConnectionString = connectionString
}));
}
}
32 changes: 16 additions & 16 deletions src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ public class NpgsqlDatabaseGptProvider : IDatabaseGptProvider, IDisposable
private readonly NpgsqlConnection connection;
private bool disposedValue;

public string Name => "Postgres";
public string Name => "PostgreSQL";

public string Language => "Postgres SQL";
public string Language => "PL/pgSQL";

public NpgsqlDatabaseGptProvider(NpgsqlDatabaseGptProviderConfiguration settings)
{
connection = new NpgsqlConnection(settings.ConnectionString);
connection.Open();
}

public async Task<IEnumerable<string>> GetTablesAsync(IEnumerable<string> includedTables, IEnumerable<string> excludedTables)
Expand All @@ -32,27 +31,16 @@ WHERE TABLE_SCHEMA NOT IN ('pg_catalog', 'information_schema')

if (includedTables?.Any() ?? false)
{
tables = tables.Where(t => includedTables.Contains(t));
tables = tables.Where(t => includedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase));
}
else if (excludedTables?.Any() ?? false)
{
tables = tables.Where(t => !excludedTables.Contains(t));
tables = tables.Where(t => !excludedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase));
}

return tables;
}

public async Task<IDataReader> ExecuteQueryAsync(string query)
{
try
{
return await connection.ExecuteReaderAsync(query);
}
catch (NpgsqlException ex)
{
throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex);
}
}
public async Task<string> GetCreateTablesScriptAsync(IEnumerable<string> tables, IEnumerable<string> excludedColumns)
{
var result = new StringBuilder();
Expand Down Expand Up @@ -82,6 +70,18 @@ FROM INFORMATION_SCHEMA.COLUMNS
return result.ToString();
}

public async Task<IDataReader> ExecuteQueryAsync(string query)
{
try
{
return await connection.ExecuteReaderAsync(query);
}
catch (NpgsqlException ex)
{
throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex);
}
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
Expand Down
8 changes: 5 additions & 3 deletions src/DatabaseGpt.SqlServer/SqlServerDatabaseGptExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using DatabaseGpt.Abstractions;
using DatabaseGpt.SqlServer;
using Microsoft.Extensions.DependencyInjection;

namespace DatabaseGpt;

public static class SqlServerDatabaseGptExtensions
{
public static void UseSqlServer(this IDatabaseGptSettings databaseGptSettings, string connectionString)
public static void UseSqlServer(this IDatabaseGptSettings databaseGptSettings, string? connectionString)
{
databaseGptSettings.SetDatabaseGptProviderFactory(() => new SqlServerDatabaseGptProvider(new() { ConnectionString = connectionString }));
databaseGptSettings.SetDatabaseGptProviderFactory(() => new SqlServerDatabaseGptProvider(new()
{
ConnectionString = connectionString
}));
}
}
37 changes: 15 additions & 22 deletions src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Dapper;
using DatabaseGpt.Abstractions;
using DatabaseGpt.Abstractions.Exceptions;
using DatabaseGpt.SqlServer.TypeHandlers;
using Microsoft.Data.SqlClient;

namespace DatabaseGpt.SqlServer;
Expand All @@ -17,16 +16,9 @@ public class SqlServerDatabaseGptProvider : IDatabaseGptProvider, IDisposable

public string Language => "T-SQL";

static SqlServerDatabaseGptProvider()
{
SqlMapper.AddTypeHandler(new DateOnlyTypeHandler());
SqlMapper.AddTypeHandler(new TimeOnlyTypeHandler());
}

public SqlServerDatabaseGptProvider(SqlServerDatabaseGptProviderConfiguration settings)
{
connection = new SqlConnection(settings.ConnectionString);
connection.Open();
}

public async Task<IEnumerable<string>> GetTablesAsync(IEnumerable<string> includedTables, IEnumerable<string> excludedTables)
Expand All @@ -35,27 +27,16 @@ public async Task<IEnumerable<string>> GetTablesAsync(IEnumerable<string> includ

if (includedTables?.Any() ?? false)
{
tables = tables.Where(t => includedTables.Contains(t));
tables = tables.Where(t => includedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase));
}
else if (excludedTables?.Any() ?? false)
{
tables = tables.Where(t => !excludedTables.Contains(t));
tables = tables.Where(t => !excludedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase));
}

return tables;
}

public async Task<IDataReader> ExecuteQueryAsync(string query)
{
try
{
return await connection.ExecuteReaderAsync(query);
}
catch (SqlException ex)
{
throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex);
}
}
public async Task<string> GetCreateTablesScriptAsync(IEnumerable<string> tables, IEnumerable<string> excludedColumns)
{
var result = new StringBuilder();
Expand All @@ -82,13 +63,25 @@ FOR XML PATH('')), 1, 1, ''
);
""";

var columns = await connection.QueryAsync<string>(query, new { schema = table.Schema, table = table.Name, ExcludedColumns = excludedColumns });
var columns = await connection.QueryFirstAsync<string>(query, new { schema = table.Schema, table = table.Name, ExcludedColumns = excludedColumns });
result.AppendLine($"CREATE TABLE [{table.Schema}].[{table.Name}] ({columns});");
}

return result.ToString();
}

public async Task<IDataReader> ExecuteQueryAsync(string query)
{
try
{
return await connection.ExecuteReaderAsync(query);
}
catch (SqlException ex)
{
throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex);
}
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
Expand Down
16 changes: 0 additions & 16 deletions src/DatabaseGpt.SqlServer/TypeHandlers/DateOnlyTypeHandler.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/DatabaseGpt.SqlServer/TypeHandlers/TimeOnlyTypeHandler.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/DatabaseGpt/DatabaseGpt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="ChatGptNet" Version="2.6.3" />
<PackageReference Include="ConsoleTables" Version="2.5.0" />
<PackageReference Include="Polly.Extensions" Version="8.0.0" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 5a1bd26

Please sign in to comment.