Esempio n. 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);
            }
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        private int ReportUnallowedReference(
            [NotNull] IRow row,
            [NotNull] object foreignKey,
            [NotNull] string fkField,
            [NotNull] string pkField)
        {
            string description =
                string.Format(
                    "Value [{0}] in field '{1}' references a value in field '{2}' of table '{3}'{4}",
                    FieldValueUtils.FormatValue(foreignKey), fkField, pkField,
                    DatasetUtils.GetName(_referencedTable),
                    StringUtils.IsNotEmpty(_whereClause)
                                                ? string.Format(" (in rows matching '{0}')", _whereClause)
                                                : string.Empty);

            return(ReportError(description,
                               Codes[Code.UnexpectedReferencedRow],
                               fkField, row));
        }
Esempio n. 4
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));
        }