Connection test working.

This commit is contained in:
feyris-tan 2026-02-01 21:10:03 +01:00
parent eed28664a6
commit 19f617780d
6 changed files with 131 additions and 27 deletions

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Voile.Common;
namespace Voile.Patchouli.Data
{
[Serializable]
public class VoileDataException : VoileException
{
public VoileDataException() { }
public VoileDataException(string message) : base(message) { }
public VoileDataException(string message, Exception inner) : base(message, inner) { }
protected VoileDataException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}

View File

@ -0,0 +1,63 @@
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Voile.Patchouli.Data;
namespace Voile.Storage.Sqlite
{
internal class SqliteDataStorage : IVoileDataStorage
{
private readonly SqliteConnectionStringBuilder connectionStringBuilder;
public SqliteDataStorage(SqliteConnectionStringBuilder connectionStringBuilder)
{
this.connectionStringBuilder = connectionStringBuilder;
}
public Exception Ping()
{
SqliteConnection conn = new SqliteConnection(connectionStringBuilder.ConnectionString);
conn.Open();
bool tableAlreadyExists = false;
using (SqliteCommand selectMaster = conn.CreateCommand())
{
selectMaster.CommandText = "SELECT rootpage FROM sqlite_master WHERE name = 'a_connection_test'";
SqliteDataReader sqliteDataReader = selectMaster.ExecuteReader();
tableAlreadyExists = sqliteDataReader.Read();
sqliteDataReader.Close();
selectMaster.Dispose();
}
if (!tableAlreadyExists)
{
using (SqliteCommand createTableCommand = conn.CreateCommand())
{
createTableCommand.CommandText = "create table a_connection_test\r\n(\r\n serial integer not null\r\n constraint a_connection_test_pk\r\n primary key autoincrement,\r\n dateadded TEXT default CURRENT_TIMESTAMP not null,\r\n hostname TEXT not null\r\n);\r\n\r\n";
int v = createTableCommand.ExecuteNonQuery();
}
}
using (SqliteCommand insertCommand = conn.CreateCommand())
{
insertCommand.CommandText = "INSERT INTO a_connection_test (hostname) VALUES (@hostname)";
insertCommand.Parameters.AddWithValue("@hostname", Environment.MachineName);
int v = insertCommand.ExecuteNonQuery();
if (v != 1)
{
insertCommand.Dispose();
conn.Dispose();
return new VoileDataException("Failed to do a test INSERT");
}
}
conn.Close();
conn.Dispose();
return null;
}
}
}

View File

@ -19,7 +19,18 @@ namespace Voile.Storage.Sqlite
{
public IVoileDataStorage CreateDataStorage()
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(FilePath) || string.IsNullOrWhiteSpace(FilePath))
{
throw new ArgumentNullException(String.Format("{0} should not be empty.", nameof(FilePath)));
}
if (scsb == null)
{
scsb = new SqliteConnectionStringBuilder();
scsb.DataSource = FilePath;
scsb.Mode = SqliteOpenMode.ReadWriteCreate;
scsb.Pooling = true;
}
return new SqliteDataStorage(scsb);
}
[Description("The path to the SQLite database file. If it doesn't exist, it will be created")]

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="10.0.2" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="3.0.2" />
</ItemGroup>
<ItemGroup>

View File

@ -288,7 +288,7 @@
// dbSettingsErrorLabel
//
dbSettingsErrorLabel.AutoSize = true;
dbSettingsErrorLabel.Location = new Point(3, 87);
dbSettingsErrorLabel.Location = new Point(3, 79);
dbSettingsErrorLabel.Name = "dbSettingsErrorLabel";
dbSettingsErrorLabel.Size = new Size(74, 15);
dbSettingsErrorLabel.TabIndex = 9;
@ -306,7 +306,7 @@
//
// dbTestButton
//
dbTestButton.Location = new Point(252, 70);
dbTestButton.Location = new Point(3, 97);
dbTestButton.Name = "dbTestButton";
dbTestButton.Size = new Size(93, 32);
dbTestButton.TabIndex = 2;
@ -316,9 +316,9 @@
//
// dbSettingsPropertyGrid
//
dbSettingsPropertyGrid.Location = new Point(3, 108);
dbSettingsPropertyGrid.Location = new Point(3, 135);
dbSettingsPropertyGrid.Name = "dbSettingsPropertyGrid";
dbSettingsPropertyGrid.Size = new Size(342, 273);
dbSettingsPropertyGrid.Size = new Size(342, 246);
dbSettingsPropertyGrid.TabIndex = 1;
//
// label6

View File

@ -129,6 +129,7 @@ namespace Voile
case 3:
loadFileButton.Visible = selectedDataStorage.CanSaveFile.Enable;
dbSettingsErrorLabel.Text = "";
buttonNext.Enabled = dataStorageConnectionSucessful;
if (voileDataStorageFactory == null)
{
dbSettingsPropertyGrid.SelectedObject = selectedDataStorage.GetPluginInstance();
@ -224,14 +225,14 @@ namespace Voile
private void buttonNext_Click(object sender, EventArgs e)
{
if (currentTabPage == 0)
switch (currentTabPage)
{
case 0:
//Wir sind auf der Willkommensseite, hier wird ausgewählt ob Anfänger oder Experte.
//Es wird nach der Lizenz gefragt
DisplayTabPage(1);
}
else if (currentTabPage == 1)
{
break;
case 1:
//Hier soll der Nutzer auswählen ob einfach oder schwierig.
if (beginnerMode)
{
@ -241,11 +242,14 @@ namespace Voile
{
DisplayTabPage(2);
}
}
else if (currentTabPage == 2)
{
break;
case 2:
//Hier soll der Nutzer auswählen, welche DB verwendet werden soll.
DisplayTabPage(3);
break;
default:
MessageBox.Show(String.Format("Don't know how to proceed from page {0}. This is a bug in Voile, please report it.", currentTabPage), "Voile onboarding", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
@ -341,6 +345,7 @@ namespace Voile
{
dbSettingsErrorLabel.Text = "The selected plugin is not a Data Storage plugin. This is a bug in voile, please report it.";
dbTestTrafficLight.Image = Resources.TRFFC10C_1_32x32x4;
RefreshView();
return;
}
voileDataStorageFactory = newDs;
@ -355,6 +360,7 @@ namespace Voile
{
dbSettingsErrorLabel.Text = ex.Message;
dbTestTrafficLight.Image = Resources.TRFFC10C_1_32x32x4;
RefreshView();
return;
}
@ -365,6 +371,7 @@ namespace Voile
{
dbSettingsErrorLabel.Text = pingResult.Message;
dbTestTrafficLight.Image = Resources.TRFFC10C_1_32x32x4;
RefreshView();
return;
}
}
@ -376,8 +383,9 @@ namespace Voile
}
dbSettingsErrorLabel.Text = "Database connection sucessful!";
pictureBox2.Image = Resources.TRFFC10A_1_32x32x4; //grüne Ampel
dbTestTrafficLight.Image = Resources.TRFFC10A_1_32x32x4; //grüne Ampel
dataStorageConnectionSucessful = true;
RefreshView();
}
private void buttonBack_Click(object sender, EventArgs e)