Exemplo n.º 1
0
        [TestCase("5,000", typeof(int), "de-de", 5, 1, 0, 5)]                  //germans swap commas and dots

        public void Test_OneString_IsType(string guessFor, Type expectedGuess, string culture, int expectedStringLength, int expectedBefore, int expectedAfter, object expectedParseValue)
        {
            var cultureInfo = new CultureInfo(culture);
            var guesser     = new Guesser()
            {
                Culture = cultureInfo
            };

            guesser.AdjustToCompensateForValue(guessFor);
            Assert.AreEqual(expectedGuess, guesser.Guess.CSharpType, "Guessed Type did not match");
            Assert.AreEqual(expectedStringLength, guesser.Guess.Width, "String length guessed didn't match");
            Assert.AreEqual(expectedBefore, guesser.Guess.Size.NumbersBeforeDecimalPlace, "BeforeDecimalPlace didn't match");
            Assert.AreEqual(expectedAfter, guesser.Guess.Size.NumbersAfterDecimalPlace, "AfterDecimalPlace didn't match");


            TypeDeciderFactory factory = new TypeDeciderFactory(cultureInfo);

            if (factory.IsSupported(guesser.Guess.CSharpType))
            {
                Assert.AreEqual(expectedParseValue, factory.Create(guesser.Guess.CSharpType).Parse(guessFor));
            }
            else
            {
                Assert.AreEqual(expectedParseValue, guessFor);
            }
        }
Exemplo n.º 2
0
        public static DbParameter GetParameter(string paramName, IQuerySyntaxHelper syntaxHelper, DiscoveredColumn discoveredColumn, object value)
        {
            var p = GetParameter(paramName, syntaxHelper.DatabaseType);

            var tt = syntaxHelper.TypeTranslater;

            p.DbType = tt.GetDbTypeForSQLDBType(discoveredColumn.DataType.SQLType);
            var cSharpType = tt.GetCSharpTypeForSQLDBType(discoveredColumn.DataType.SQLType);

            if (syntaxHelper.IsBasicallyNull(value))
            {
                p.Value = DBNull.Value;
            }
            else
            if (value is string && typeDeciderFactory.IsSupported(cSharpType))     //if the input is a string and it's for a hard type e.g. TimeSpan
            {
                var o = typeDeciderFactory.Create(cSharpType).Parse((string)value);

                //Apparently everyone in Microsoft hates TimeSpans - see test MicrosoftHatesDbTypeTime
                if (o is TimeSpan && syntaxHelper.DatabaseType == DatabaseType.MicrosoftSQLServer)
                {
                    o = Convert.ToDateTime(o.ToString());
                }

                p.Value = o;
            }
            else
            {
                p.Value = value;
            }

            return(p);
        }
Exemplo n.º 3
0
        public DataTable StronglyTypeTable(DataTable workingTable, ExplicitTypingCollection explicitTypingCollection)
        {
            Dictionary <int, IDecideTypesForStrings> deciders = new Dictionary <int, IDecideTypesForStrings>();
            var factory = new TypeDeciderFactory(_culture);

            if (!string.IsNullOrWhiteSpace(_explicitDateTimeFormat))
            {
                factory.Settings.ExplicitDateFormats = new [] { _explicitDateTimeFormat }
            }
            ;

            DataTable dtCloned = workingTable.Clone();

            bool typeChangeNeeded = false;

            foreach (DataColumn col in workingTable.Columns)
            {
                //if we have already handled it
                if (explicitTypingCollection != null && explicitTypingCollection.ExplicitTypesCSharp.ContainsKey(col.ColumnName))
                {
                    continue;
                }

                //let's make a decision about the data type to use based on the contents
                var computedType = new Guesser();
                computedType.AdjustToCompensateForValues(col);

                //Type based on the contents of the column
                if (computedType.ShouldDowngradeColumnTypeToMatchCurrentEstimate(col))
                {
                    dtCloned.Columns[col.ColumnName].DataType = computedType.Guess.CSharpType;

                    //if we have a type decider to parse this data type
                    if (factory.IsSupported(computedType.Guess.CSharpType))
                    {
                        deciders.Add(col.Ordinal, factory.Create(computedType.Guess.CSharpType)); //record column index and parser
                    }
                    typeChangeNeeded = true;
                }
            }

            if (typeChangeNeeded)
            {
                foreach (DataRow row in workingTable.Rows)
                {
                    dtCloned.Rows.Add(row.ItemArray.Select((v, idx) =>

                                                           deciders.ContainsKey(idx) && v is string s?
                                                           deciders[idx].Parse(s) :
                                                           v).ToArray());
                }

                return(dtCloned);
            }

            return(workingTable);
        }
Exemplo n.º 4
0
        private void ThrowIfNotSupported(Type currentEstimate)
        {
            if (currentEstimate == typeof(string))
            {
                return;
            }

            if (!_typeDeciders.IsSupported(CurrentEstimate))
            {
                throw new NotSupportedException("We do not have a type decider for type:" + CurrentEstimate);
            }
        }
Exemplo n.º 5
0
        protected override void UpdateTag(DicomTag dicomTag, DicomDataset dataset, string cellValue)
        {
            var cSharpType = DicomTypeTranslater.GetNaturalTypeForVr(dicomTag.DictionaryEntry.ValueRepresentations,
                                                                     dicomTag.DictionaryEntry.ValueMultiplicity)?.CSharpType;

            object writeValue = cellValue;

            //if it's a supported type e.g. DateTime parse it
            if (cSharpType != null && _factory.IsSupported(cSharpType))
            {
                if (_factory.Dictionary[cSharpType].IsAcceptableAsType(cellValue, Ignore.Me))
                {
                    writeValue = _factory.Dictionary[cSharpType].Parse(cellValue);
                }
            }

            dataset.Remove(dicomTag);
            DicomTypeTranslaterWriter.SetDicomTag(dataset, dicomTag, writeValue);
        }
Exemplo n.º 6
0
        public DbParameter GetParameter(DbParameter p, DiscoveredColumn discoveredColumn, object value, CultureInfo culture)
        {
            try
            {
                var tt = TypeTranslater;
                p.DbType = tt.GetDbTypeForSQLDBType(discoveredColumn.DataType.SQLType);
                var cSharpType = tt.GetCSharpTypeForSQLDBType(discoveredColumn.DataType.SQLType);

                if (IsBasicallyNull(value))
                {
                    p.Value = DBNull.Value;
                }
                else
                if (value is string strVal && typeDeciderFactory.IsSupported(cSharpType))     //if the input is a string and it's for a hard type e.g. TimeSpan
                {
                    var decider = typeDeciderFactory.Create(cSharpType);

                    if (decider is DateTimeTypeDecider dt)
                    {
                        if (culture != null)
                        {
                            dt.Culture = culture;
                        }
                    }

                    var o = decider.Parse(strVal);

                    //Not all DBMS support DBParameter.Value = new TimeSpan(...);
                    if (o is TimeSpan)
                    {
                        o = FormatTimespanForDbParameter((TimeSpan)o);
                    }

                    p.Value = o;
                }
                else
                {
                    p.Value = value;
                }
            }