Changed Sqlite to microsoft sqlite implementation

This commit is contained in:
2025-01-28 13:36:49 +02:00
parent d26c84480a
commit 20165af15a
13 changed files with 227 additions and 34 deletions

View File

@@ -0,0 +1,12 @@
using DiscordBotCore.Online;
namespace DiscordBotCore.API.Endpoints;
public class ApiEndpointBase
{
internal IPluginManager PluginManager { get; }
public ApiEndpointBase(IPluginManager pluginManager)
{
PluginManager = pluginManager;
}
}

View File

@@ -24,7 +24,7 @@ public class ApiManager
AddEndpoint(new HomeEndpoint());
AddEndpoint(new PluginListEndpoint());
AddEndpoint(new PluginListInstalledEndpoint());
AddEndpoint(new PluginInstallEndpoint());
AddEndpoint(new PluginInstallEndpoint(Application.CurrentApplication.PluginManager));
AddEndpoint(new SettingsChangeEndpoint());
AddEndpoint(new SettingsGetEndpoint());

View File

@@ -2,28 +2,34 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DiscordBotCore.Interfaces.API;
using DiscordBotCore.Online;
using DiscordBotCore.Others;
using DiscordBotCore.Plugin;
namespace DiscordBotCore.API.Endpoints.PluginManagement;
public class PluginInstallEndpoint : IEndpoint
public class PluginInstallEndpoint : ApiEndpointBase, IEndpoint
{
public PluginInstallEndpoint(IPluginManager pluginManager) : base(pluginManager)
{
}
public string Path => "/api/plugin/install";
public EndpointType HttpMethod => EndpointType.Post;
public async Task<ApiResponse> HandleRequest(string? jsonRequest)
{
Dictionary<string, string> jsonDict = await JsonManager.ConvertFromJson<Dictionary<string, string>>(jsonRequest);
string pluginName = jsonDict["pluginName"];
OnlinePlugin? pluginInfo = await Application.CurrentApplication.PluginManager.GetPluginDataByName(pluginName);
OnlinePlugin? pluginInfo = await PluginManager.GetPluginDataByName(pluginName);
if (pluginInfo == null)
{
return ApiResponse.Fail("Plugin not found.");
}
Application.CurrentApplication.PluginManager.InstallPluginNoProgress(pluginInfo);
PluginManager.InstallPluginNoProgress(pluginInfo);
return ApiResponse.Ok();
}
}

View File

@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using Microsoft.Data.Sqlite;
using System.IO;
using System.Threading.Tasks;
@@ -9,7 +9,7 @@ namespace DiscordBotCore.Database;
public class SqlDatabase
{
private readonly SQLiteConnection _Connection;
private readonly SqliteConnection _Connection;
/// <summary>
/// Initialize a SQL connection by specifying its private path
@@ -17,10 +17,9 @@ public class SqlDatabase
/// <param name="fileName">The path to the database (it is starting from ./Data/Resources/)</param>
public SqlDatabase(string fileName)
{
if (!File.Exists(fileName))
SQLiteConnection.CreateFile(fileName);
var connectionString = $"URI=file:{fileName}";
_Connection = new SQLiteConnection(connectionString);
var connectionString = $"Data Source={fileName}";
_Connection = new SqliteConnection(connectionString);
}
@@ -53,7 +52,7 @@ public class SqlDatabase
query += ")";
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
await command.ExecuteNonQueryAsync();
}
@@ -77,7 +76,7 @@ public class SqlDatabase
query += ")";
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
command.ExecuteNonQuery();
}
@@ -92,7 +91,7 @@ public class SqlDatabase
{
var query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
await command.ExecuteNonQueryAsync();
}
@@ -107,7 +106,7 @@ public class SqlDatabase
{
var query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
command.ExecuteNonQuery();
}
@@ -341,7 +340,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var answer = await command.ExecuteNonQueryAsync();
return answer;
}
@@ -355,7 +354,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
_Connection.Open();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var r = command.ExecuteNonQuery();
return r;
@@ -370,7 +369,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var reader = await command.ExecuteReaderAsync();
var values = new object[reader.FieldCount];
@@ -394,7 +393,7 @@ public class SqlDatabase
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
foreach (var parameter in parameters)
{
var p = CreateParameter(parameter);
@@ -423,7 +422,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
_Connection.Open();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var reader = command.ExecuteReader();
var values = new object[reader.FieldCount];
@@ -445,7 +444,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var reader = await command.ExecuteReaderAsync();
var values = new object[reader.FieldCount];
@@ -463,7 +462,7 @@ public class SqlDatabase
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
foreach (var parameter in parameters)
{
var p = CreateParameter(parameter);
@@ -493,7 +492,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
_Connection.Open();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var reader = command.ExecuteReader();
var values = new object[reader.FieldCount];
@@ -516,7 +515,7 @@ public class SqlDatabase
{
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
var reader = await command.ExecuteReaderAsync();
if (!reader.HasRows)
@@ -541,9 +540,10 @@ public class SqlDatabase
/// <param name="name">The name of the parameter</param>
/// <param name="value">The value of the parameter</param>
/// <returns>The SQLiteParameter that has the name, value and DBType set according to your inputs</returns>
private static SQLiteParameter? CreateParameter(string name, object value)
private static SqliteParameter? CreateParameter(string name, object value)
{
var parameter = new SQLiteParameter(name);
var parameter = new SqliteParameter();
parameter.ParameterName = name;
parameter.Value = value;
if (value is string)
@@ -598,7 +598,7 @@ public class SqlDatabase
/// </summary>
/// <param name="parameterValues">The parameter raw inputs. The Key is name and the Value is the value of the parameter</param>
/// <returns>The SQLiteParameter that has the name, value and DBType set according to your inputs</returns>
private static SQLiteParameter? CreateParameter(KeyValuePair<string, object> parameterValues)
private static SqliteParameter? CreateParameter(KeyValuePair<string, object> parameterValues)
{
return CreateParameter(parameterValues.Key, parameterValues.Value);
}
@@ -614,7 +614,7 @@ public class SqlDatabase
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
foreach (var parameter in parameters)
{
var p = CreateParameter(parameter);
@@ -638,7 +638,7 @@ public class SqlDatabase
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
foreach (var parameter in parameters)
{
var p = CreateParameter(parameter);
@@ -672,7 +672,7 @@ public class SqlDatabase
if (!_Connection.State.HasFlag(ConnectionState.Open))
await _Connection.OpenAsync();
var command = new SQLiteCommand(query, _Connection);
var command = new SqliteCommand(query, _Connection);
foreach (var parameter in parameters)
{
var p = CreateParameter(parameter);

View File

@@ -4,9 +4,12 @@
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.15.3" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.1" />
</ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="UI\Controls\MessageBox.axaml" />

View File

@@ -7,11 +7,12 @@ using DiscordBotCore.Plugin;
namespace DiscordBotCore.Online.Helpers;
internal class PluginRepository : IPluginRepository
public class PluginRepository : IPluginRepository
{
private readonly IPluginRepositoryConfiguration _pluginRepositoryConfiguration;
private readonly HttpClient _httpClient;
internal PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration)
public HttpClient _httpClient;
public PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration)
{
_pluginRepositoryConfiguration = pluginRepositoryConfiguration;
_httpClient = new HttpClient();

View File

@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using DiscordBotCore.Interfaces.PluginManagement;
namespace DiscordBotCore.Online.Helpers;
@@ -10,6 +11,7 @@ public class PluginRepositoryConfiguration : IPluginRepositoryConfiguration
public string PluginRepositoryLocation { get; }
public string DependenciesRepositoryLocation { get; }
[JsonConstructor]
public PluginRepositoryConfiguration(string baseUrl, string pluginRepositoryLocation, string dependenciesRepositoryLocation)
{
BaseUrl = baseUrl;

View File

@@ -9,7 +9,24 @@ using DiscordBotCore.Plugin;
namespace DiscordBotCore.Online;
public sealed class PluginManager
public interface IPluginManager
{
Task<List<OnlinePlugin>> GetPluginsList();
Task<OnlinePlugin?> GetPluginDataByName(string pluginName);
Task AppendPluginToDatabase(PluginInfo pluginData);
Task<List<PluginInfo>> GetInstalledPlugins();
Task<bool> IsPluginInstalled(string pluginName);
Task<bool> MarkPluginToUninstall(string pluginName);
Task UninstallMarkedPlugins();
Task<string?> GetDependencyLocation(string dependencyName);
Task<string?> GetDependencyLocation(string dependencyName, string pluginName);
string GenerateDependencyRelativePath(string pluginName, string dependencyPath);
Task InstallPluginNoProgress(OnlinePlugin plugin);
Task<Tuple<Dictionary<string, string>, List<OnlineDependencyInfo>>> GatherInstallDataForPlugin(OnlinePlugin plugin);
Task SetEnabledStatus(string pluginName, bool status);
}
public sealed class PluginManager : IPluginManager
{
private static readonly string _LibrariesBaseFolder = "Libraries";
private readonly IPluginRepository _PluginRepository;