Exemple #1
0
        private bool TryConvertForeignKey(
            [NotNull] ReferencingTableInfo referencingTableInfo,
            [NotNull] object foreignKey,
            [NotNull] ReferencedTableInfo referencedTableInfo,
            [NotNull] out string errorDescription,
            [CanBeNull] out object convertedValue)
        {
            try
            {
                convertedValue = FieldUtils.ConvertAttributeValue(
                    foreignKey,
                    referencingTableInfo.ForeignKeyFieldType,
                    referencedTableInfo.KeyFieldType);

                errorDescription = string.Empty;
                return(true);
            }
            catch (Exception e)
            {
                errorDescription =
                    FieldValueUtils.GetTypeConversionErrorDescription(
                        _referencedTable, foreignKey,
                        referencingTableInfo.ForeignKeyFieldName,
                        referencedTableInfo.KeyFieldName,
                        e.Message);

                convertedValue = null;
                return(false);
            }
        }
Exemple #2
0
        public void CantConvertLargeInt32ToInt16()
        {
            const int sourceValue = 10000000;

            Assert.Throws <OverflowException>(
                () => FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeInteger,
                    esriFieldType.esriFieldTypeSmallInteger));
        }
Exemple #3
0
        public void CantConvertNonNumericStringToInt32()
        {
            const string sourceValue = "abc";

            Assert.Throws <FormatException>(
                () => FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeString,
                    esriFieldType.esriFieldTypeInteger));
        }
Exemple #4
0
        public void CantConvertVeryLongDoubleToInt32()
        {
            const double sourceValue = 1111111111111111111111.0;

            Assert.Throws <OverflowException>(
                () => FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeDouble,
                    esriFieldType.esriFieldTypeInteger));
        }
Exemple #5
0
        public void CanConvertStringToInt32()
        {
            const string sourceValue = "111";
            object       targetValue =
                FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeString,
                    esriFieldType.esriFieldTypeInteger);

            Assert.IsInstanceOf <int>(targetValue);
            Assert.AreEqual(sourceValue, targetValue.ToString());
        }
Exemple #6
0
        public void CanConvertInt16ToInt32()
        {
            const short sourceValue = 100;
            object      targetValue =
                FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeSmallInteger,
                    esriFieldType.esriFieldTypeInteger);

            Assert.IsInstanceOf <int>(targetValue);
            Assert.AreEqual(sourceValue, targetValue);
        }
Exemple #7
0
        public void CanConvertStringToGuid()
        {
            const string sourceValue = "{E2F909CD-3EE2-4885-BFD0-9CB19A9A9A06}";
            object       targetValue =
                FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeString,
                    esriFieldType.esriFieldTypeGUID);

            Assert.IsInstanceOf <Guid>(targetValue);
            Assert.AreEqual(new Guid(sourceValue), targetValue);
        }
Exemple #8
0
        public void CanConvertDoubleToInt32()
        {
            const double sourceValue = 111.123456;
            object       targetValue =
                FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeDouble,
                    esriFieldType.esriFieldTypeInteger);

            Assert.IsInstanceOf <int>(targetValue);
            Assert.AreEqual(Math.Round(sourceValue), targetValue);
        }
Exemple #9
0
        public void CanConvertDateTimeToString()
        {
            DateTime sourceValue = DateTime.Now;
            object   targetValue =
                FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeDate,
                    esriFieldType.esriFieldTypeString);

            Assert.IsInstanceOf <string>(targetValue);
            Assert.AreEqual(sourceValue.ToString(CultureInfo.GetCultureInfo("de-DE")),
                            targetValue);
        }
Exemple #10
0
        public void CanConvertDoubleToString()
        {
            CultureInfo cultureInfo = CultureInfo.GetCultureInfo("de-DE");

            const double sourceValue = 111.123456;
            object       targetValue =
                FieldUtils.ConvertAttributeValue(
                    sourceValue,
                    esriFieldType.esriFieldTypeDouble,
                    esriFieldType.esriFieldTypeString, cultureInfo);

            Assert.IsInstanceOf <string>(targetValue);
            Assert.AreEqual(sourceValue.ToString(cultureInfo), targetValue);
        }
Exemple #11
0
            object IAlternateKeyConverter.Convert(string tableName,
                                                  string fieldName,
                                                  string keyString)
            {
                // can be extended with data source identifier to allow disambiguation between datasets of same name from different data sources

                esriFieldType?targetType = GetFieldType(tableName, fieldName);

                if (targetType == null)
                {
                    return(keyString);
                }

                return(FieldUtils.ConvertAttributeValue(keyString,
                                                        esriFieldType.esriFieldTypeString,
                                                        targetType.Value,
                                                        CultureInfo.InvariantCulture));
            }
Exemple #12
0
        private int VerifySingleKey([NotNull] IRow row)
        {
            Assert.NotNull(_keySet, "keyset is null");

            int           fkIndex = _foreignKeyFieldIndices[0];
            string        fkField = _foreignKeyFields[0];
            string        pkField = _referencedKeyFields[0];
            esriFieldType fkType  = _foreignKeyFieldTypes[0];
            esriFieldType pkType  = _referencedKeyFieldTypes[0];

            object foreignKey = row.Value[fkIndex];

            if (foreignKey == null || foreignKey is DBNull)
            {
                // the foreign key is null --> ok
                return(NoError);
            }

            object convertedValue;

            try
            {
                convertedValue = FieldUtils.ConvertAttributeValue(foreignKey, fkType, pkType);
            }
            catch (Exception e)
            {
                string errorDescription = FieldValueUtils.GetTypeConversionErrorDescription(
                    _referencedTable, foreignKey, fkField, pkField, e.Message);

                return(ReportError(errorDescription, Codes[Code.UnableToConvertFieldValue],
                                   fkField, row));
            }

            if (_referenceIsError)
            {
                return(_keySet.Contains(convertedValue)
                                               ? ReportUnallowedReference(row, foreignKey, fkField, pkField)
                                               : NoError);
            }

            return(_keySet.Contains(convertedValue)
                                       ? NoError
                                       : ReportMissingReference(row, foreignKey, fkField, pkField));
        }
Exemple #13
0
        private Tuple TryReadForeignKeyTuple([NotNull] IRow row,
                                             [NotNull] IList <int> foreignKeyFieldIndices,
                                             [NotNull] IList <esriFieldType> fieldTypes,
                                             [NotNull] IList <esriFieldType> outputTypes,
                                             [NotNull] IList <string> foreignKeyFields,
                                             [NotNull] IList <string> referencedKeyFields,
                                             [NotNull] out string errorMessage)
        {
            var values = new List <object>(foreignKeyFieldIndices.Count);

            for (int i = 0; i < foreignKeyFieldIndices.Count; i++)
            {
                int           fieldIndex = foreignKeyFieldIndices[i];
                esriFieldType fieldType  = fieldTypes[i];
                esriFieldType outputType = outputTypes[i];

                object value = row.Value[fieldIndex];

                object convertedValue;
                try
                {
                    convertedValue = FieldUtils.ConvertAttributeValue(value, fieldType, outputType);
                }
                catch (Exception e)
                {
                    errorMessage = FieldValueUtils.GetTypeConversionErrorDescription(
                        _referencedTable, value,
                        foreignKeyFields[i], referencedKeyFields[i],
                        e.Message);
                    return(null);
                }

                values.Add(convertedValue);
            }

            errorMessage = string.Empty;
            return(new Tuple(values));
        }