// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Migration.Core.Properties; using Core.Common.Base.IO; using Core.Common.IO.Readers; namespace Application.Ringtoets.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. /// /// 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 (SQLiteException e) { string message = Resources.MigrationLogDatabaseReader_GetMigrationLogMessages_failed; throw new CriticalFileReadException(message, e); } } 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); if (string.IsNullOrEmpty(fromVersion) || string.IsNullOrEmpty(toVersion) || string.IsNullOrEmpty(message)) { continue; } 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; } } }