-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/marcominerva/DatabaseGPT …
…into develop
- Loading branch information
Showing
10 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Dapper" Version="2.1.28" /> | ||
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DatabaseGpt.Abstractions\DatabaseGpt.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using DatabaseGpt.Abstractions; | ||
using DatabaseGpt.Sqlite; | ||
|
||
namespace DatabaseGpt; | ||
|
||
public static class SqliteDatabaseGptExtensions | ||
{ | ||
public static void UseSqlite(this IDatabaseGptSettings databaseGptSettings, string? connectionString) | ||
{ | ||
databaseGptSettings.SetDatabaseGptProviderFactory(() => new SqliteDatabaseGptProvider(new() | ||
{ | ||
ConnectionString = connectionString | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System.Data; | ||
using System.Data.Common; | ||
using System.Text; | ||
using Dapper; | ||
using DatabaseGpt.Abstractions; | ||
using DatabaseGpt.Abstractions.Exceptions; | ||
using Microsoft.Data.Sqlite; | ||
|
||
namespace DatabaseGpt.Sqlite; | ||
|
||
public class SqliteDatabaseGptProvider(SqliteDatabaseGptProviderConfiguration settings) : IDatabaseGptProvider | ||
{ | ||
private readonly SqliteConnection connection = new(settings.ConnectionString); | ||
|
||
private bool disposedValue; | ||
|
||
public string Name => "SQLite"; | ||
|
||
public string Language => "SQLite"; | ||
|
||
public async Task<IEnumerable<string>> GetTablesAsync(IEnumerable<string> includedTables, IEnumerable<string> excludedTables, CancellationToken cancellationToken = default) | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
var tables = await connection.QueryAsync<string>(""" | ||
SELECT TBL_NAME AS Tables | ||
FROM sqlite_schema | ||
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' | ||
"""); | ||
|
||
if (includedTables?.Any() ?? false) | ||
{ | ||
tables = tables.Where(t => includedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase)); | ||
} | ||
else if (excludedTables?.Any() ?? false) | ||
{ | ||
tables = tables.Where(t => !excludedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase)); | ||
} | ||
|
||
return tables; | ||
} | ||
|
||
public async Task<string> GetCreateTablesScriptAsync(IEnumerable<string> tables, IEnumerable<string> excludedColumns, CancellationToken cancellationToken = default) | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
var result = new StringBuilder(); | ||
|
||
foreach (var table in tables) | ||
{ | ||
var query = $""" | ||
SELECT '[' || NAME || '] ' || | ||
UPPER(TYPE) || ' ' || | ||
CASE WHEN [NOTNULL] = 0 THEN 'NULL' ELSE 'NOT NULL' END | ||
FROM PRAGMA_TABLE_INFO(@table) | ||
WHERE NAME NOT IN (@excludedColumns) | ||
AND @table || '.' || NAME NOT IN (@excludedColumns); | ||
"""; | ||
|
||
var columns = await connection.QueryAsync<string>(query, new { table, excludedColumns }); | ||
result.AppendLine($"CREATE TABLE [{table}] ({string.Join(", ", columns)});"); | ||
} | ||
|
||
return result.ToString(); | ||
} | ||
|
||
public Task<string?> GetQueryHintsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
return Task.FromResult<string?>(null); | ||
} | ||
|
||
public Task<string> NormalizeQueryAsync(string query, CancellationToken cancellationToken = default) | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
return Task.FromResult(query); | ||
} | ||
|
||
public async Task<DbDataReader> ExecuteQueryAsync(string query, CancellationToken cancellationToken = default) | ||
{ | ||
ThrowIfDisposed(); | ||
|
||
try | ||
{ | ||
return await connection.ExecuteReaderAsync(query); | ||
} | ||
catch (SqliteException 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) | ||
{ | ||
if (disposing) | ||
{ | ||
connection.Dispose(); | ||
} | ||
|
||
disposedValue = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void ThrowIfDisposed() | ||
=> ObjectDisposedException.ThrowIf(disposedValue, this); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/DatabaseGpt.Sqlite/SqliteDatabaseGptProviderConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace DatabaseGpt.Sqlite; | ||
|
||
public class SqliteDatabaseGptProviderConfiguration | ||
{ | ||
public string? ConnectionString { get; set; } | ||
} |