Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseVersionReader.cs =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseVersionReader.cs (.../SoilDatabaseVersionReader.cs) (revision 7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseVersionReader.cs (.../SoilDatabaseVersionReader.cs) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -19,8 +19,13 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System.Data; +using System.Data.SQLite; using Core.Common.Base.IO; using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Common.IO.Properties; +using Ringtoets.Common.IO.SoilProfile.Schema; namespace Ringtoets.Common.IO.SoilProfile { @@ -29,17 +34,72 @@ /// public class SoilDatabaseVersionReader : SqLiteDatabaseReaderBase { + private const string databaseRequiredVersion = "15.0.6.0"; + /// /// Creates a new instance of , /// which will use the as its source. /// /// The path of the database file to open. - /// Thrown when: + /// Thrown when: + /// /// The contains invalid characters. /// No file could be found at . + /// + /// + public SoilDatabaseVersionReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Verifies if the database has the required version. + /// + /// Thrown when: + /// /// The database version could not be read. /// The database version is incorrect. - /// - public SoilDatabaseVersionReader(string databaseFilePath) : base(databaseFilePath) {} + /// + /// + public void VerifyVersion() + { + string checkVersionQuery = SoilDatabaseQueryBuilder.GetCheckVersionQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String, + ParameterName = $"@{MetaTableDefinitions.Value}", + Value = databaseRequiredVersion + }; + + try + { + ReadVersion(checkVersionQuery, sqliteParameter); + } + catch (SQLiteException exception) + { + string exceptionMessage = new FileReaderErrorMessageBuilder(Path).Build( + Resources.SoilProfileReader_Critical_Unexpected_value_on_column); + throw new CriticalFileReadException(exceptionMessage, exception); + } + } + + /// + /// Reads if the required version was found in the database. + /// + /// The query to execute. + /// The parameter containing the required version. + /// Thrown when the execution of + /// failed. + /// Thrown when the database version is incorrect. + private void ReadVersion(string checkVersionQuery, SQLiteParameter sqliteParameter) + { + using (IDataReader dataReader = CreateDataReader(checkVersionQuery, sqliteParameter)) + { + if (!dataReader.Read()) + { + string exceptionMessage = new FileReaderErrorMessageBuilder(Path).Build( + string.Format(Resources.SoilProfileReader_Database_incorrect_version_requires_Version_0, + databaseRequiredVersion)); + throw new CriticalFileReadException(exceptionMessage); + } + } + } } } \ No newline at end of file