Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ConvergenceParser.cs =================================================================== diff -u -rbafcd3618c36c38f37d56f8da48056313185ef3f -r31caf3dc885ae62d4b3772eb0421a3155101bcf4 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ConvergenceParser.cs (.../ConvergenceParser.cs) (revision bafcd3618c36c38f37d56f8da48056313185ef3f) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ConvergenceParser.cs (.../ConvergenceParser.cs) (revision 31caf3dc885ae62d4b3772eb0421a3155101bcf4) @@ -20,13 +20,10 @@ // All rights reserved. using System; -using System.Data; using System.Data.SQLite; -using System.IO; -using Core.Common.Utils; using Ringtoets.HydraRing.Calculation.Exceptions; using Ringtoets.HydraRing.Calculation.Properties; -using Ringtoets.HydraRing.IO; +using Ringtoets.HydraRing.Calculation.Readers; namespace Ringtoets.HydraRing.Calculation.Parsers { @@ -55,112 +52,49 @@ { throw new ArgumentNullException(nameof(workingDirectory)); } - IOUtils.ValidateFilePath(workingDirectory); ParseFile(workingDirectory, sectionId); } - private void ParseFile(string workingDirectory, int sectionId) - { - string outputDatabasePath = Path.Combine(workingDirectory, HydraRingFileConstants.OutputDatabaseFileName); - - using (SQLiteConnection connection = CreateConnection(outputDatabasePath)) - { - ReadIsConverged(connection, sectionId); - } - } - - private static SQLiteConnection CreateConnection(string databaseFile) - { - string connectionStringBuilder = new SQLiteConnectionStringBuilder - { - FailIfMissing = true, - DataSource = databaseFile, - ReadOnly = true - }.ConnectionString; - - return new SQLiteConnection(connectionStringBuilder); - } - /// - /// Reads the value indicating whether the calculation for a section has converged. + /// Parses the file. /// - /// The connection to the database. + /// The path to the directory which contains + /// the output of the Hydra-Ring calculation. /// The section id to get the output for. - /// Thrown when: - /// - /// the output file does not exist. - /// the convergence result could not be read from the output file. - /// - /// - private void ReadIsConverged(SQLiteConnection sqLiteConnection, int sectionId) + /// Thrown when the reader + /// encounters an error while reading the database. + private void ParseFile(string workingDirectory, int sectionId) { try { - using (SQLiteDataReader reader = CreateReader(sqLiteConnection, sectionId)) + using (var reader = new HydraRingDatabaseReader(workingDirectory, getLastResultQuery, sectionId)) { - SetOutput(reader); + reader.Execute(); + ReadResult(reader); } } catch (SQLiteException e) { throw new HydraRingFileParserException(Resources.Parse_Cannot_read_convergence_in_output_file, e); } - } - - private SQLiteDataReader CreateReader(SQLiteConnection connection, int sectionId) - { - using (var command = CreateCommand(connection, sectionId)) + catch (HydraRingDatabaseReaderException) { - OpenConnection(connection); - return command.ExecuteReader(); + throw new HydraRingFileParserException(Resources.Parse_No_convergence_found_in_output_file); } } - private SQLiteCommand CreateCommand(SQLiteConnection connection, int sectionId) - { - var command = new SQLiteCommand(getLastResultQuery, connection); - command.Parameters.Add(new SQLiteParameter - { - DbType = DbType.Int64, - ParameterName = sectionIdParameterName, - Value = sectionId - }); - return command; - } - /// - /// Opens the connection using . + /// Reads the result of the . /// - /// The connection to open. - /// Thrown when could not be opened. - private static void OpenConnection(SQLiteConnection connection) + /// The reader to get the result from. + private void ReadResult(HydraRingDatabaseReader reader) { - try + object result = reader.ReadColumn(convergedColumnName); + if (result != null) { - connection.Open(); + Output = Convert.ToBoolean(result); } - catch (SQLiteException e) - { - throw new HydraRingFileParserException(e.Message, e); - } } - - /// - /// Sets with the value read in the . - /// - /// The reader to use. - /// Thrown when no result could be read using the . - private void SetOutput(SQLiteDataReader reader) - { - if (reader.Read()) - { - Output = Convert.ToBoolean(reader[convergedColumnName]); - } - else - { - throw new HydraRingFileParserException(Resources.Parse_No_convergence_found_in_output_file); - } - } } } \ No newline at end of file