/// <summary> /// <para>Creates and runs an ALTER TABLE statement which will increase the size of a decimal column to support larger Precision/Scale values than it currently does. /// If you want decimal(4,2) then pass <paramref name="numberOfDigitsBeforeDecimalPoint"/>=2 and <paramref name="numberOfDigitsAfterDecimalPoint"/>=2</para> /// /// <para>Throws <see cref="InvalidResizeException"/> if the column is not a decimal type or the new size is smaller than the current column size</para> /// </summary> /// <param name="numberOfDigitsBeforeDecimalPoint">The number of decimal places before the . you want represented e.g. for decimal(5,3) specify 2</param> /// <param name="numberOfDigitsAfterDecimalPoint">The number of decimal places after the . you want represented e.g. for decimal(5,3,) specify 3</param> /// <param name="managedTransaction"></param> /// <exception cref="InvalidResizeException"></exception> /// <exception cref="AlterFailedException"></exception> public void Resize(int numberOfDigitsBeforeDecimalPoint, int numberOfDigitsAfterDecimalPoint, IManagedTransaction managedTransaction = null) { DecimalSize toReplace = GetDecimalSize(); if (toReplace == null || toReplace.IsEmpty) { throw new InvalidResizeException(string.Format(FAnsiStrings.DiscoveredDataType_Resize_DataType_cannot_be_resized_to_decimal_because_it_is_of_data_type__0_, SQLType)); } if (toReplace.NumbersBeforeDecimalPlace > numberOfDigitsBeforeDecimalPoint) { throw new InvalidResizeException(string.Format(FAnsiStrings.DiscoveredDataType_Resize_Cannot_shrink_column__number_of_digits_before_the_decimal_point_is_currently__0__and_you_asked_to_set_it_to__1___Current_SQLType_is__2__, toReplace.NumbersBeforeDecimalPlace, numberOfDigitsBeforeDecimalPoint, SQLType)); } if (toReplace.NumbersAfterDecimalPlace > numberOfDigitsAfterDecimalPoint) { throw new InvalidResizeException(string.Format(FAnsiStrings.DiscoveredDataType_Resize_Cannot_shrink_column__number_of_digits_after_the_decimal_point_is_currently__0__and_you_asked_to_set_it_to__1___Current_SQLType_is__2__, toReplace.NumbersAfterDecimalPlace, numberOfDigitsAfterDecimalPoint, SQLType)); } var newDataType = Column.Table.GetQuerySyntaxHelper() .TypeTranslater.GetSQLDBTypeForCSharpType(new DatabaseTypeRequest(typeof(decimal), null, new DecimalSize(numberOfDigitsBeforeDecimalPoint, numberOfDigitsAfterDecimalPoint))); AlterTypeTo(newDataType, managedTransaction); }
/// <summary> /// <para>Creates and runs an ALTER TABLE statement which will increase the size of a decimal column to support larger Precision/Scale values than it currently does. /// If you want decimal(4,2) then pass <paramref name="numberOfDigitsBeforeDecimalPoint"/>=2 and <paramref name="numberOfDigitsAfterDecimalPoint"/>=2</para> /// /// <para>Throws <see cref="InvalidResizeException"/> if the column is not a decimal type or the new size is smaller than the current column size</para> /// </summary> /// <param name="numberOfDigitsBeforeDecimalPoint">The number of decimal places before the . you want represented e.g. for decimal(5,3) specify 2</param> /// <param name="numberOfDigitsAfterDecimalPoint">The number of decimal places after the . you want represented e.g. for decimal(5,3,) specify 3</param> /// <param name="managedTransaction"></param> /// <exception cref="InvalidResizeException"></exception> /// <exception cref="AlterFailedException"></exception> public void Resize(int numberOfDigitsBeforeDecimalPoint, int numberOfDigitsAfterDecimalPoint, IManagedTransaction managedTransaction = null) { DecimalSize toReplace = GetDecimalSize(); if (toReplace == null || toReplace.IsEmpty) { throw new InvalidResizeException("DataType cannot be resized to decimal because it is of data type " + SQLType); } if (toReplace.NumbersBeforeDecimalPlace > numberOfDigitsBeforeDecimalPoint) { throw new InvalidResizeException("Cannot shrink column, number of digits before the decimal point is currently " + toReplace.NumbersBeforeDecimalPlace + " and you asked to set it to " + numberOfDigitsBeforeDecimalPoint + " (Current SQLType is " + SQLType + ")"); } if (toReplace.NumbersAfterDecimalPlace > numberOfDigitsAfterDecimalPoint) { throw new InvalidResizeException("Cannot shrink column, number of digits after the decimal point is currently " + toReplace.NumbersAfterDecimalPlace + " and you asked to set it to " + numberOfDigitsAfterDecimalPoint + " (Current SQLType is " + SQLType + ")"); } var newDataType = Column.Table.GetQuerySyntaxHelper() .TypeTranslater.GetSQLDBTypeForCSharpType(new DatabaseTypeRequest(typeof(decimal), null, new DecimalSize(numberOfDigitsBeforeDecimalPoint, numberOfDigitsAfterDecimalPoint))); AlterTypeTo(newDataType, managedTransaction); }
/// <summary> /// Expands the instance to accomodate the new size (if expansion is required) /// </summary> /// <param name="other"></param> private void IncreaseTo(DecimalSize other) { if (other.NumbersBeforeDecimalPlace != null) { NumbersBeforeDecimalPlace = NumbersBeforeDecimalPlace == null ? other.NumbersBeforeDecimalPlace : Math.Max(NumbersBeforeDecimalPlace.Value, other.NumbersBeforeDecimalPlace.Value); } if (other.NumbersAfterDecimalPlace != null) { NumbersAfterDecimalPlace = NumbersAfterDecimalPlace == null ? other.NumbersAfterDecimalPlace : Math.Max(NumbersAfterDecimalPlace.Value, other.NumbersAfterDecimalPlace.Value); } }
/// <summary> /// Returns a new <see cref="DecimalSize"/> which is big enough to accomodate decimals of <paramref name="first"/> size and those of <paramref name="second"/>. /// For example if the first is decimal(3,0) and the second is decimal(5,4) then the returned result would be decimal(7,4). /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <returns></returns> public static DecimalSize Combine(DecimalSize first, DecimalSize second) { if (first == null) { return(second); } if (second == null) { return(first); } var newSize = new DecimalSize(); newSize.IncreaseTo(first); newSize.IncreaseTo(second); return(newSize); }
protected bool Equals(DecimalSize other) { return((NumbersBeforeDecimalPlace ?? 0) == (other.NumbersBeforeDecimalPlace ?? 0) && (NumbersAfterDecimalPlace ?? 0) == (other.NumbersAfterDecimalPlace ?? 0)); }