// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// 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
{
///
/// This class reads a soil database file and reads version from this database.
///
public class SoilDatabaseVersionReader : SqLiteDatabaseReaderBase
{
private const string databaseRequiredVersion = "17.2.0.0";
///
/// Creates a new instance of
/// which will use the as its source.
///
/// The path of the database file to open.
/// 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 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);
}
}
}
}
}