From 19f617780d4d6743410c7575c8e38485c7fd2236 Mon Sep 17 00:00:00 2001 From: feyris-tan <4116042+feyris-tan@users.noreply.github.com> Date: Sun, 1 Feb 2026 21:10:03 +0100 Subject: [PATCH] Connection test working. --- Voile.Patchouli/Data/VoileDataException.cs | 21 +++++++ Voile.Storage.Sqlite/SqliteDataStorage.cs | 63 +++++++++++++++++++ .../SqliteDataStorageFactory.cs | 13 +++- .../Voile.Storage.Sqlite.csproj | 1 + Voile/FirstRunWizard.Designer.cs | 8 +-- Voile/FirstRunWizard.cs | 52 ++++++++------- 6 files changed, 131 insertions(+), 27 deletions(-) create mode 100644 Voile.Patchouli/Data/VoileDataException.cs create mode 100644 Voile.Storage.Sqlite/SqliteDataStorage.cs diff --git a/Voile.Patchouli/Data/VoileDataException.cs b/Voile.Patchouli/Data/VoileDataException.cs new file mode 100644 index 0000000..f6c2507 --- /dev/null +++ b/Voile.Patchouli/Data/VoileDataException.cs @@ -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) { } + } +} diff --git a/Voile.Storage.Sqlite/SqliteDataStorage.cs b/Voile.Storage.Sqlite/SqliteDataStorage.cs new file mode 100644 index 0000000..853212e --- /dev/null +++ b/Voile.Storage.Sqlite/SqliteDataStorage.cs @@ -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; + } + } +} diff --git a/Voile.Storage.Sqlite/SqliteDataStorageFactory.cs b/Voile.Storage.Sqlite/SqliteDataStorageFactory.cs index d6fd443..49b3d47 100644 --- a/Voile.Storage.Sqlite/SqliteDataStorageFactory.cs +++ b/Voile.Storage.Sqlite/SqliteDataStorageFactory.cs @@ -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")] diff --git a/Voile.Storage.Sqlite/Voile.Storage.Sqlite.csproj b/Voile.Storage.Sqlite/Voile.Storage.Sqlite.csproj index 9c3c3b5..e2e6244 100644 --- a/Voile.Storage.Sqlite/Voile.Storage.Sqlite.csproj +++ b/Voile.Storage.Sqlite/Voile.Storage.Sqlite.csproj @@ -8,6 +8,7 @@ + diff --git a/Voile/FirstRunWizard.Designer.cs b/Voile/FirstRunWizard.Designer.cs index 4b8b4d3..6f40249 100644 --- a/Voile/FirstRunWizard.Designer.cs +++ b/Voile/FirstRunWizard.Designer.cs @@ -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 diff --git a/Voile/FirstRunWizard.cs b/Voile/FirstRunWizard.cs index acbdf1d..b31d358 100644 --- a/Voile/FirstRunWizard.cs +++ b/Voile/FirstRunWizard.cs @@ -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,28 +225,31 @@ namespace Voile private void buttonNext_Click(object sender, EventArgs e) { - if (currentTabPage == 0) + switch (currentTabPage) { - //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) - { - //Hier soll der Nutzer auswählen ob einfach oder schwierig. - if (beginnerMode) - { - //TODO: Automatisch SQLite und Directory Storage auswählen - } - else - { - DisplayTabPage(2); - } - } - else if (currentTabPage == 2) - { - //Hier soll der Nutzer auswählen, welche DB verwendet werden soll. - DisplayTabPage(3); + case 0: + //Wir sind auf der Willkommensseite, hier wird ausgewählt ob Anfänger oder Experte. + //Es wird nach der Lizenz gefragt + DisplayTabPage(1); + break; + case 1: + //Hier soll der Nutzer auswählen ob einfach oder schwierig. + if (beginnerMode) + { + //TODO: Automatisch SQLite und Directory Storage auswählen + } + else + { + DisplayTabPage(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)