Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/PipingSoilProfileReader.cs =================================================================== diff -u -r6692dc3480971fe1c017aef633382c40eb37056f -rf67ccd7b88da303d2ac322bbd489f2c6ea287e1c --- Ringtoets/Piping/src/Ringtoets.Piping.IO/PipingSoilProfileReader.cs (.../PipingSoilProfileReader.cs) (revision 6692dc3480971fe1c017aef633382c40eb37056f) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/PipingSoilProfileReader.cs (.../PipingSoilProfileReader.cs) (revision f67ccd7b88da303d2ac322bbd489f2c6ea287e1c) @@ -82,7 +82,7 @@ return null; } - var dimensionValue = Read(dimensionColumn); + var dimensionValue = TryRead(dimensionColumn); return dimensionValue == 1 ? ReadPipingProfile1D() : ReadPipingProfile2D(); } @@ -106,9 +106,9 @@ private PipingSoilProfile ReadPipingProfile1D() { - var profileName = Read(profileNameColumn); - var layerCount = Read(layerCountColumn); - var bottom = Read(bottomColumn); + var profileName = TryRead(profileNameColumn); + var layerCount = TryRead(layerCountColumn); + var bottom = TryRead(bottomColumn); var soilProfileBuilder = new SoilProfileBuilder1D(profileName, bottom); @@ -128,9 +128,9 @@ /// Thrown when a layer's geometry could not be parsed as XML. private PipingSoilProfile ReadPipingProfile2D() { - var profileName = Read(profileNameColumn); - var layerCount = Read(layerCountColumn); - var intersectionX = Read(intersectionXColumn); + var profileName = TryRead(profileNameColumn); + var layerCount = TryRead(layerCountColumn); + var intersectionX = TryRead(intersectionXColumn); var soilProfileBuilder = new SoilProfileBuilder2D(profileName, intersectionX); @@ -145,10 +145,7 @@ var exception = new PipingSoilProfileReadException( string.Format(Resources.PipingSoilProfileReader_CouldNotParseGeometryOfLayer_0_InProfile_1_, i, profileName), e); - while (i++ <= layerCount) - { - MoveNext(); - } + SkipRecords((int)layerCount + 1 - i); throw exception; } @@ -158,36 +155,47 @@ return soilProfileBuilder.Build(); } + private void SkipRecords(int count) + { + for(int i = 0; i < count; i++) + { + MoveNext(); + } + } + private void SetReaderToFirstRecord() { InitializeDataReader(); MoveNext(); } - private void TryRead(string columnName, out T value) - { - try - { - value = (T) dataReader[columnName]; - } - catch (InvalidCastException) - { - value = default(T); - } - } - /// /// Reads a value at column from the database. /// /// The expected type of value in the column with name . /// The name of the column to read from. /// The read value from the column with name . /// Thrown when the value in the column was not of type . - private T Read(string columnName) + private T TryRead(string columnName) { - return (T) dataReader[columnName]; - } + var dbValue = dataReader[columnName]; + var isNullable = typeof(T).IsGenericType && typeof(Nullable<>).IsAssignableFrom(typeof(T).GetGenericTypeDefinition()); + if (isNullable && dbValue == DBNull.Value) + { + return default(T); + } + + try + { + return (T)dbValue; + } + catch (InvalidCastException) + { + throw new CriticalFileReadException(Resources.PipingSoilProfileReader_Invalid_value_on_column); + } + } + private void OpenConnection(string dbFile) { var connectionStringBuilder = new SQLiteConnectionStringBuilder @@ -216,17 +224,12 @@ private PipingSoilLayer ReadPipingSoilLayer() { - double? isAquiferValue; - double? belowPhreaticLevelValue; - double? abovePhreaticLevelValue; - double? dryUnitWeightValue; + var topValue = TryRead(topColumn); + var isAquiferValue = TryRead(isAquiferColumn); + var belowPhreaticLevelValue = TryRead(belowPhreaticLevelColumn); + var abovePhreaticLevelValue = TryRead(abovePhreaticLevelColumn); + var dryUnitWeightValue = TryRead(dryUnitWeightColumn); - var topValue = Read(topColumn); - TryRead(isAquiferColumn, out isAquiferValue); - TryRead(belowPhreaticLevelColumn, out belowPhreaticLevelValue); - TryRead(abovePhreaticLevelColumn, out abovePhreaticLevelValue); - TryRead(dryUnitWeightColumn, out dryUnitWeightValue); - var pipingSoilLayer = new PipingSoilLayer(topValue) { IsAquifer = isAquiferValue, @@ -244,17 +247,12 @@ /// Thrown when a column did not contain a value of the expected type. private SoilLayer2D ReadPiping2DSoilLayer() { - double? isAquiferValue; - double? belowPhreaticLevelValue; - double? abovePhreaticLevelValue; - double? dryUnitWeightValue; + var geometryValue = TryRead(layerGeometryColumn); + var isAquiferValue = TryRead(isAquiferColumn); + var belowPhreaticLevelValue = TryRead(belowPhreaticLevelColumn); + var abovePhreaticLevelValue = TryRead(abovePhreaticLevelColumn); + var dryUnitWeightValue = TryRead(dryUnitWeightColumn); - var geometryValue = Read(layerGeometryColumn); - TryRead(isAquiferColumn, out isAquiferValue); - TryRead(belowPhreaticLevelColumn, out belowPhreaticLevelValue); - TryRead(abovePhreaticLevelColumn, out abovePhreaticLevelValue); - TryRead(dryUnitWeightColumn, out dryUnitWeightValue); - SoilLayer2D pipingSoilLayer = new PipingSoilLayer2DReader(geometryValue).Read(); pipingSoilLayer.IsAquifer = isAquiferValue; pipingSoilLayer.BelowPhreaticLevel = belowPhreaticLevelValue; @@ -462,7 +460,7 @@ private void GetCount() { dataReader.Read(); - Count = (int)Read(profileCountColumn); + Count = (int)TryRead(profileCountColumn); dataReader.NextResult(); } }