Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabase/HydraulicBoundaryDatabaseReader.cs =================================================================== diff -u -re581f7fd8199f0fb565bfea26384ce38040d10e4 -r3b368e38644b163189a58233f9fc1fb9701bf68c --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabase/HydraulicBoundaryDatabaseReader.cs (.../HydraulicBoundaryDatabaseReader.cs) (revision e581f7fd8199f0fb565bfea26384ce38040d10e4) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabase/HydraulicBoundaryDatabaseReader.cs (.../HydraulicBoundaryDatabaseReader.cs) (revision 3b368e38644b163189a58233f9fc1fb9701bf68c) @@ -1,173 +1,173 @@ -// 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; -using System.Data; -using System.Data.SQLite; -using Core.Common.Base.IO; -using Core.Common.IO.Exceptions; -using Core.Common.IO.Readers; -using Core.Common.Util.Builders; -using Ringtoets.HydraRing.IO.Properties; - -namespace Ringtoets.HydraRing.IO.HydraulicBoundaryDatabase -{ - /// - /// This class reads a SqLite database file and constructs - /// instances from this database. - /// - public class HydraulicBoundaryDatabaseReader : SqLiteDatabaseReaderBase - { - private IDataReader sqliteDataReader; - - /// - /// 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 HydraulicBoundaryDatabaseReader(string databaseFilePath) : base(databaseFilePath) {} - - /// - /// Gets a value indicating whether or not more hydraulic boundary locations can - /// be read using the . - /// - public bool HasNext { get; private set; } - - public void PrepareReadLocation() - { - CloseDataReader(); - HasNext = false; - - sqliteDataReader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsQuery()); - MoveNext(); - } - - /// - /// Reads the next location from the database. - /// - /// A new instance of based on the data read from - /// the database or null if no data is available. - /// Thrown when the database contains incorrect values for required properties. - public ReadHydraulicBoundaryLocation ReadLocation() - { - if (!HasNext) - { - return null; - } - - try - { - var id = sqliteDataReader.Read(HrdLocationsTableDefinitions.HrdLocationId); - var name = sqliteDataReader.Read(HrdLocationsTableDefinitions.Name); - var x = sqliteDataReader.Read(HrdLocationsTableDefinitions.XCoordinate); - var y = sqliteDataReader.Read(HrdLocationsTableDefinitions.YCoordinate); - - return new ReadHydraulicBoundaryLocation(id, name, x, y); - } - catch (ConversionException e) - { - string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); - throw new LineParseException(message, e); - } - finally - { - MoveNext(); - } - } - - /// - /// Gets the version of the hydraulic boundary database. - /// - /// The version found in the database, or if the version cannot be found. - /// Thrown when a query could not be executed on the database schema. - public string GetVersion() - { - try - { - using (IDataReader dataReader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetVersionQuery(), null)) - { - return !dataReader.Read() ? string.Empty : Convert.ToString(dataReader[GeneralTableDefinitions.GeneratedVersion]); - } - } - catch (SQLiteException exception) - { - string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database); - throw new CriticalFileReadException(message, exception); - } - } - - /// - /// Gets the track id from the hydraulic boundary database. - /// - /// The track id found in the database, or 0 if the track id cannot be found. - /// Thrown when the database contains incorrect values for required properties. - /// Thrown when a query could not be executed on the database schema. - public long GetTrackId() - { - try - { - using (IDataReader dataReader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetTrackIdQuery(), - new SQLiteParameter - { - DbType = DbType.String - })) - { - return !dataReader.Read() ? 0 : Convert.ToInt64(dataReader[GeneralTableDefinitions.TrackId]); - } - } - catch (InvalidCastException exception) - { - string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); - throw new LineParseException(message, exception); - } - catch (SQLiteException exception) - { - string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database); - throw new CriticalFileReadException(message, exception); - } - } - - protected override void Dispose(bool disposing) - { - CloseDataReader(); - base.Dispose(disposing); - } - - /// - /// Moves the reader to the next record in the database. - /// - private void MoveNext() - { - HasNext = MoveNext(sqliteDataReader); - } - - private void CloseDataReader() - { - sqliteDataReader?.Dispose(); - } - } +// 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.Data; +using System.Data.SQLite; +using System.Linq; +using Core.Common.Base.IO; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Util.Builders; +using Riskeer.HydraRing.IO.Properties; + +namespace Riskeer.HydraRing.IO.HydraulicBoundaryDatabase +{ + /// + /// This class reads a hydraulic boundary database file and constructs a + /// instance from this database. + /// + public class HydraulicBoundaryDatabaseReader : SqLiteDatabaseReaderBase + { + /// + /// 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 HydraulicBoundaryDatabaseReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Reads the hydraulic boundary database. + /// + /// A . + /// Thrown when the database contains incorrect values for required properties. + /// Thrown when the data cannot be read. + public ReadHydraulicBoundaryDatabase Read() + { + return new ReadHydraulicBoundaryDatabase(ReadTrackId(), ReadVersion(), ReadLocations().ToArray()); + } + + /// + /// Reads the track Id from the hydraulic boundary database. + /// + /// The track Id found in the database. + /// Thrown when the database contains incorrect values for required properties. + /// Thrown when the track Id cannot be read. + public long ReadTrackId() + { + try + { + using (IDataReader reader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetTrackIdQuery(), + new SQLiteParameter + { + DbType = DbType.String + })) + { + if (reader.Read()) + { + return Convert.ToInt64(reader[GeneralTableDefinitions.TrackId]); + } + + throw new CriticalFileReadException(new FileReaderErrorMessageBuilder(Path) + .Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column)); + } + } + catch (InvalidCastException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, exception); + } + catch (SQLiteException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database); + throw new CriticalFileReadException(message, exception); + } + } + + /// + /// Gets the version of the hydraulic boundary database. + /// + /// The version found in the database, or if the version cannot be found. + /// Thrown when the version cannot be read.. + public string ReadVersion() + { + try + { + using (IDataReader reader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetVersionQuery(), null)) + { + + if (reader.Read()) + { + string version = Convert.ToString(reader[GeneralTableDefinitions.GeneratedVersion]); + + if (!string.IsNullOrEmpty(version)) + { + return version; + } + } + + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new CriticalFileReadException(message); + } + } + catch (SQLiteException e) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database); + throw new CriticalFileReadException(message, e); + } + } + + /// + /// Reads the locations from the database. + /// + /// Thrown when the database contains incorrect values for required properties. + private IEnumerable ReadLocations() + { + using (IDataReader reader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsQuery())) + { + while (MoveNext(reader)) + { + yield return ReadLocation(reader); + } + } + } + + /// + /// Reads a location from the database. + /// + /// A based on the data read from the database. + /// Thrown when the database contains incorrect values for required properties. + private ReadHydraulicBoundaryLocation ReadLocation(IDataReader reader) + { + try + { + var id = reader.Read(HrdLocationsTableDefinitions.HrdLocationId); + var name = reader.Read(HrdLocationsTableDefinitions.Name); + var x = reader.Read(HrdLocationsTableDefinitions.XCoordinate); + var y = reader.Read(HrdLocationsTableDefinitions.YCoordinate); + + return new ReadHydraulicBoundaryLocation(id, name, x, y); + } + catch (ConversionException e) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, e); + } + } + } } \ No newline at end of file