private static DatabaseTypeRequest Conflate(List <DatabaseTypeRequest> vrs) { var toReturn = vrs.First(); foreach (DatabaseTypeRequest newType in vrs) { Type t = toReturn.CSharpType; if (toReturn.CSharpType != newType.CSharpType) { if ( (toReturn.CSharpType == typeof(UInt16) || toReturn.CSharpType == typeof(Int16)) && //some tags e.g. SmallestValidPixelValue can be either ushort or short newType.CSharpType == typeof(UInt16) || newType.CSharpType == typeof(Int16)) { t = typeof(Int32); //if they are being all creepy about it just use an int that way theres definetly enough space } else { throw new Exception("Incompatible Types '" + toReturn.CSharpType + "' and '" + newType.CSharpType + "'"); } } toReturn = new DatabaseTypeRequest( t, Conflate(toReturn.MaxWidthForStrings, newType.MaxWidthForStrings), DecimalSize.Combine(toReturn.DecimalPlacesBeforeAndAfter, newType.DecimalPlacesBeforeAndAfter)); } return(toReturn); }
public DatabaseTypeRequest(Type cSharpType, int?maxWidthForStrings = null, DecimalSize decimalPlacesBeforeAndAfter = null) { CSharpType = cSharpType; MaxWidthForStrings = maxWidthForStrings; DecimalPlacesBeforeAndAfter = decimalPlacesBeforeAndAfter; }
protected virtual string GetFloatingPointDataType(DecimalSize decimalSize) { if (decimalSize == null || decimalSize.IsEmpty) { return("decimal(20,10)"); } return("decimal(" + decimalSize.Precision + "," + decimalSize.Scale + ")"); }
public bool IsAcceptableAsType(string candidateString, DecimalSize sizeRecord) { //we must preserve leading zeroes if its not actually 0 -- if they have 010101 then we have to use string but if they have just 0 we can use decimal if (zeroPrefixedNumber.IsMatch(candidateString)) { return(false); } return(IsAcceptableAsTypeImpl(candidateString, sizeRecord)); }
public void Test_DecimalSize_SomeFraction() { //decimal(7,2) var size = new DecimalSize(5, 2); Assert.AreEqual(7, size.Precision); Assert.AreEqual(2, size.Scale); Assert.IsFalse(size.IsEmpty); }
public void Test_DecimalSize_NoFraction() { //decimal(5,0) var size = new DecimalSize(5, 0); Assert.AreEqual(5, size.Precision); Assert.AreEqual(0, size.Scale); Assert.IsFalse(size.IsEmpty); }
/// <summary> /// Creates a new computer primed with the size of the given <paramref name="request"/>. /// </summary> /// <param name="request"></param> public DataTypeComputer(DatabaseTypeRequest request) : this(request.MaxWidthForStrings.HasValue? request.MaxWidthForStrings.Value:-1) { CurrentEstimate = request.CSharpType; if (request.DecimalPlacesBeforeAndAfter != null) { DecimalSize = request.DecimalPlacesBeforeAndAfter; } ThrowIfNotSupported(CurrentEstimate); }
public void Test_DecimalSize_Empty() { var empty = new DecimalSize(); Assert.AreEqual(0, empty.NumbersAfterDecimalPlace); Assert.AreEqual(0, empty.NumbersBeforeDecimalPlace); Assert.AreEqual(0, empty.Precision); Assert.AreEqual(0, empty.Scale); Assert.IsTrue(empty.IsEmpty); }
/// <summary> /// Creates a hydrated DataTypeComputer for when you want to clone an existing one or otherwise make up a DataTypeComputer for a known starting datatype /// (See TypeTranslater.GetDataTypeComputerFor) /// </summary> /// <param name="currentEstimatedType"></param> /// <param name="decimalSize"></param> /// <param name="lengthIfString"></param> public DataTypeComputer(Type currentEstimatedType, DecimalSize decimalSize, int lengthIfString, int extraLengthPerNonAsciiCharacter) : this(-1) { CurrentEstimate = currentEstimatedType; ExtraLengthPerNonAsciiCharacter = extraLengthPerNonAsciiCharacter; ThrowIfNotSupported(CurrentEstimate); if (lengthIfString > 0) { _stringLength = lengthIfString; } DecimalSize = decimalSize ?? new DecimalSize(); }
internal static DecimalSize getPaperSize(string printerName, string paperName) { PrintDocument printDocument = new PrintDocument(); printDocument.PrinterSettings.PrinterName = printerName; DecimalSize size = new DecimalSize(); foreach (PaperSize paperSize in printDocument.PrinterSettings.PaperSizes) { if (paperSize.PaperName == paperName) { size.Height = Printer.FromInchToCM(System.Convert.ToDecimal(paperSize.Height.ToString())); size.Width = Printer.FromInchToCM(System.Convert.ToDecimal(paperSize.Width.ToString())); } } printDocument.Dispose(); return(size); }
protected override bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord) { try { var t = System.Convert.ToInt32(candidateString); sizeRecord.IncreaseTo(t.ToString().Length); return(true); } catch (FormatException) { return(false); } catch (OverflowException) { return(false); } }
protected override bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord) { decimal t; if (!decimal.TryParse(candidateString, out t)) { return(false); } int before; int after; GetDecimalPlaces(t, out before, out after); sizeRecord.IncreaseTo(before, after); //could be whole number with no decimal return(true); }
protected override bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord) { try { DateTime t; //if it parses as a date if (DateTime.TryParse(candidateString, CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault, out t)) { return(t.Year == 1 && t.Month == 1 && t.Day == 1);//without any ymd component then it's a date... this means 00:00 is a valid TimeSpan too } return(false); } catch (Exception) { return(false); } }
public void Test_DecimalSize_Combine() { //decimal(3,0) var size1 = new DecimalSize(3, 0); Assert.AreEqual(3, size1.Precision); Assert.AreEqual(0, size1.Scale); //decimal(5,4) var size2 = new DecimalSize(1, 4); Assert.AreEqual(5, size2.Precision); Assert.AreEqual(4, size2.Scale); var combined = DecimalSize.Combine(size1, size2); Assert.AreEqual(3, combined.NumbersBeforeDecimalPlace); Assert.AreEqual(4, combined.NumbersAfterDecimalPlace); //decimal(7,4) Assert.AreEqual(7, combined.Precision); Assert.AreEqual(4, combined.Scale); }
public static DatabaseTypeRequest Max(DatabaseTypeRequest first, DatabaseTypeRequest second) { //if types differ if (PreferenceOrder.IndexOf(first.CSharpType) < PreferenceOrder.IndexOf(second.CSharpType)) { return(second); } if (PreferenceOrder.IndexOf(first.CSharpType) > PreferenceOrder.IndexOf(second.CSharpType)) { return(first); } if (!(first.CSharpType == second.CSharpType)) { throw new NotSupportedException("Cannot Max DatabaseTypeRequests because they were of differing Types and neither Type appeared in the PreferenceOrder (Types were '" + first.CSharpType + "' and '" + second.CSharpType + "')"); } int?newMaxWidthIfStrings = first.MaxWidthForStrings; //if first doesn't have a max string width if (newMaxWidthIfStrings == null) { newMaxWidthIfStrings = second.MaxWidthForStrings; //use the second } else if (second.MaxWidthForStrings != null) { newMaxWidthIfStrings = Math.Max(newMaxWidthIfStrings.Value, second.MaxWidthForStrings.Value); //else use the max of the two } //types are the same return(new DatabaseTypeRequest( first.CSharpType, newMaxWidthIfStrings, DecimalSize.Combine(first.DecimalPlacesBeforeAndAfter, second.DecimalPlacesBeforeAndAfter) )); }
protected override bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord) { //if it's a float then it isn't a date is it! thanks C# for thinking 1.1 is the first of January if (_decimalChecker.IsAcceptableAsType(candidateString, sizeRecord)) { return(false); } //likewise if it is just the Time portion of the date then we have a column with mixed dates and times which SQL will not deal with well in the end database (e.g. it will set the //date portion of times to todays date which will be very confusing if (_timeSpanTypeDecider.IsAcceptableAsType(candidateString, sizeRecord)) { return(false); } try { return(TryBruteParse(candidateString, out _)); } catch (Exception) { return(false); } }
protected override bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord) { bool result; return(bool.TryParse(candidateString, out result)); }
protected override bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord) { //strings should never be interpreted as byte arrays return(false); }
/// <inheritdoc cref="GetNaturalTypeForVr(DicomVR[], DicomVM)"/> public static DatabaseTypeRequest GetNaturalTypeForVr(DicomVR dicomVr, DicomVM valueMultiplicity) { var decimalSize = new DecimalSize(19, 19); //if it's an array just use a big string to represent it if (valueMultiplicity.Maximum > 1) { return(new DatabaseTypeRequest(typeof(string), int.MaxValue)); } if (dicomVr == DicomVR.AE) { return(new DatabaseTypeRequest(typeof(string), 16)); } if (dicomVr == DicomVR.AS) { return(new DatabaseTypeRequest(typeof(string), 4)); } //Attribute Tag (DicomItem) if (dicomVr == DicomVR.AT) { return(new DatabaseTypeRequest(typeof(string), 4)); } if (dicomVr == DicomVR.CS) { return(new DatabaseTypeRequest(typeof(string), 16)); } if (dicomVr == DicomVR.DA) { return(new DatabaseTypeRequest(typeof(DateTime))); } if (dicomVr == DicomVR.DS) { return(new DatabaseTypeRequest(typeof(decimal), null, decimalSize)); //16 bytes maximum but representation is a string... } if (dicomVr == DicomVR.DT) { return(new DatabaseTypeRequest(typeof(DateTime))); } if (dicomVr == DicomVR.FD) { return(new DatabaseTypeRequest(typeof(double), null, decimalSize)); } if (dicomVr == DicomVR.FL) { return(new DatabaseTypeRequest(typeof(float), null, decimalSize)); } if (dicomVr == DicomVR.IS) { return(new DatabaseTypeRequest(typeof(int), null)); } if (dicomVr == DicomVR.LO) { return(new DatabaseTypeRequest(typeof(string), 64)); } if (dicomVr == DicomVR.LT) { return(new DatabaseTypeRequest(typeof(string), 10240)); } if (dicomVr == DicomVR.OB) { return(new DatabaseTypeRequest(typeof(byte[]))); } if (dicomVr == DicomVR.OD) { return(new DatabaseTypeRequest(typeof(double))); } if (dicomVr == DicomVR.OF) { return(new DatabaseTypeRequest(typeof(float), null, decimalSize)); } if (dicomVr == DicomVR.OL) { return(new DatabaseTypeRequest(typeof(uint))); } if (dicomVr == DicomVR.OW) { return(new DatabaseTypeRequest(typeof(ushort))); } if (dicomVr == DicomVR.PN) { return(new DatabaseTypeRequest(typeof(string), 320)); //64 characters but up to 5? } if (dicomVr == DicomVR.SH) { return(new DatabaseTypeRequest(typeof(string), 16)); } if (dicomVr == DicomVR.SL) { return(new DatabaseTypeRequest(typeof(int))); } if (dicomVr == DicomVR.SQ) { return(new DatabaseTypeRequest(typeof(string), int.MaxValue)); } if (dicomVr == DicomVR.SS) { return(new DatabaseTypeRequest(typeof(short))); } if (dicomVr == DicomVR.ST) { return(new DatabaseTypeRequest(typeof(string), int.MaxValue)); } if (dicomVr == DicomVR.TM) { return(new DatabaseTypeRequest(typeof(TimeSpan))); } if (dicomVr == DicomVR.UC) //unlimited characters { return(new DatabaseTypeRequest(typeof(string), 10240)); //will be varchar max etc anyway } if (dicomVr == DicomVR.UI) { return(new DatabaseTypeRequest(typeof(string), 64)); } if (dicomVr == DicomVR.UL) { return(new DatabaseTypeRequest(typeof(uint))); } if (dicomVr == DicomVR.UN) { return(new DatabaseTypeRequest(typeof(string), 10240)); } if (dicomVr == DicomVR.UR) { return(new DatabaseTypeRequest(typeof(string), int.MaxValue));//url } if (dicomVr == DicomVR.US) { return(new DatabaseTypeRequest(typeof(ushort))); } if (dicomVr == DicomVR.UT) { return(new DatabaseTypeRequest(typeof(string), 10240)); } throw new ArgumentOutOfRangeException("Invalid value representation:" + dicomVr); }
protected virtual DataTypeComputer GetDataTypeComputer(Type currentEstimatedType, DecimalSize decimalSize, int lengthIfString) { return(new DataTypeComputer(currentEstimatedType, decimalSize, lengthIfString)); }
/// <summary> /// Creates a hydrated DataTypeComputer for when you want to clone an existing one or otherwise make up a DataTypeComputer for a known starting datatype /// (See TypeTranslater.GetDataTypeComputerFor) /// </summary> /// <param name="currentEstimatedType"></param> /// <param name="decimalSize"></param> /// <param name="lengthIfString"></param> public DataTypeComputer(Type currentEstimatedType, DecimalSize decimalSize, int lengthIfString) : this(currentEstimatedType, decimalSize, lengthIfString, 0) { }
protected abstract bool IsAcceptableAsTypeImpl(string candidateString, DecimalSize sizeRecord);
protected override DataTypeComputer GetDataTypeComputer(Type currentEstimatedType, DecimalSize decimalSize, int lengthIfString) { return(new DataTypeComputer(currentEstimatedType, decimalSize, lengthIfString, ExtraLengthPerNonAsciiCharacter)); }