// Copyright (C) Stichting Deltares 2018. 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; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Data.SQLite; using Core.Common.Base.IO; using Core.Common.IO.Readers; using Riskeer.Migration.Core.Properties; namespace Riskeer.Migration.Core { /// /// This class reads an SqLite database file and constructs /// instances from this database. /// public class MigrationLogDatabaseReader : SqLiteDatabaseReaderBase { /// /// Creates a new instance of that will use the /// as its source. /// /// The path of the database file to open. /// Thrown when: /// /// The contains invalid characters. /// No file was found at . /// Unable to open the database file. /// The database contains invalid data. /// /// public MigrationLogDatabaseReader(string databaseFilePath) : base(databaseFilePath) {} /// /// Gets the migration log messages from the database. /// /// The read log messages. /// Thrown when trying to get the migration /// messages failed. public ReadOnlyCollection GetMigrationLogMessages() { string query = "SELECT " + $"{MigrationLogEntityTableDefinitions.FromVersion}, " + $"{MigrationLogEntityTableDefinitions.ToVersion}, " + $"{MigrationLogEntityTableDefinitions.LogMessage} " + $"FROM {MigrationLogEntityTableDefinitions.TableName}"; try { using (IDataReader dataReader = CreateDataReader(query)) { return ReadMigrationLogMessages(dataReader).AsReadOnly(); } } catch (SystemException e) when (e is ArgumentException || e is SQLiteException) { string message = Resources.MigrationLogDatabaseReader_GetMigrationLogMessages_failed; throw new CriticalFileReadException(message, e); } } /// /// Reads and returns the log messages from the . /// /// The that is used to read the log /// messages from. /// The read log messages. /// Thrown when the read data is null or empty. private static List ReadMigrationLogMessages(IDataReader dataReader) { var messages = new List(); while (dataReader.Read()) { string fromVersion = ReadStringOrNull(dataReader, MigrationLogEntityTableDefinitions.FromVersion); string toVersion = ReadStringOrNull(dataReader, MigrationLogEntityTableDefinitions.ToVersion); string message = ReadStringOrNull(dataReader, MigrationLogEntityTableDefinitions.LogMessage); messages.Add(new MigrationLogMessage(fromVersion, toVersion, message)); } return messages; } private static string ReadStringOrNull(IDataReader dataReader, string columnName) { object valueObject = dataReader[columnName]; if (valueObject.Equals(DBNull.Value)) { return null; } return (string) valueObject; } } }