Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabase/HydraulicBoundaryDatabaseReader.cs =================================================================== diff -u -r71a73dccde6d14e69e1951a103e01a2d61e2e318 -r9d78eae168406461f4c5867cdffc7a22f5527a51 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabase/HydraulicBoundaryDatabaseReader.cs (.../HydraulicBoundaryDatabaseReader.cs) (revision 71a73dccde6d14e69e1951a103e01a2d61e2e318) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabase/HydraulicBoundaryDatabaseReader.cs (.../HydraulicBoundaryDatabaseReader.cs) (revision 9d78eae168406461f4c5867cdffc7a22f5527a51) @@ -19,8 +19,16 @@ // 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 Ringtoets.HydraRing.IO.Properties; namespace Ringtoets.HydraRing.IO.HydraulicBoundaryDatabase { @@ -42,5 +50,114 @@ /// /// public HydraulicBoundaryDatabaseReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// + /// + /// + /// Thrown when the database contains incorrect values for required properties. + /// Thrown when a query could not be executed on the database schema. + 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 a query could not be executed on the database schema + /// or the track id cannot be found. + private long ReadTrackId() + { + try + { + using (IDataReader dataReader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetTrackIdQuery(), + new SQLiteParameter + { + DbType = DbType.String + })) + { + if (dataReader.Read()) + { + return Convert.ToInt64(dataReader[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 a query could not be executed on the database schema. + private string ReadVersion() + { + 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); + } + } + + /// + /// + /// + /// 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 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. + 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