Beispiel #1
0
        /// <summary>
        /// Automatically select all <see cref="CandidateKey"/> columns and their data values for specified <see cref="DataRow"/>.
        /// </summary>
        /// <param name="key">The <see cref="CandidateKey"/>.</param>
        /// <param name="dataRow">The specified <see cref="DataRow"/>.</param>
        /// <returns>Number of columns selected.</returns>
        public int AutoSelect(CandidateKey key, DataRow dataRow)
        {
            key.VerifyNotNull(nameof(key));
            dataRow.VerifyNotNull(nameof(dataRow));

            var valueKey = dataRow.Model.PrimaryKey;

            if (valueKey != null && valueKey.GetType() == key.GetType())
            {
                for (int i = 0; i < valueKey.Count; i++)
                {
                    _columnValues[key[i].Column] = valueKey[i].Column.GetValue(dataRow);
                }
                return(valueKey.Count);
            }

            var result       = 0;
            var valueColumns = dataRow.Model.Columns;

            foreach (var columnSort in key)
            {
                var keyColumn   = columnSort.Column;
                var valueColumn = valueColumns.AutoSelect(keyColumn);
                if (valueColumn != null)
                {
                    _columnValues[keyColumn] = valueColumn.GetValue(dataRow);
                    result++;
                }
            }
            return(result);
        }
Beispiel #2
0
 internal KeyMapping(CandidateKey sourceKey, CandidateKey targetKey)
 {
     sourceKey.VerifyNotNull(nameof(sourceKey));
     targetKey.VerifyNotNull(nameof(targetKey));
     if (targetKey.GetType() != sourceKey.GetType())
         throw new ArgumentException(DiagnosticMessages.KeyMapping_SourceTargetTypeMismatch, nameof(targetKey));
     Debug.Assert(sourceKey.Count == targetKey.Count);
     _sourceKey = sourceKey;
     _targetKey = targetKey;
 }
Beispiel #3
0
        /// <summary>
        /// Join with target candidate key.
        /// </summary>
        /// <param name="target">The target candidate key.</param>
        /// <returns>Column mappings between this candidate key and the target candidate key.</returns>
        /// <remarks>You must ensure these two canidate key objects have identical columns, otherwise an exception will be thrown.</remarks>
        public IReadOnlyList <ColumnMapping> UnsafeJoin(CandidateKey target)
        {
            target.VerifyNotNull(nameof(target));
            if (Count != target.Count)
            {
                throw new ArgumentException(DiagnosticMessages.PrimaryKey_Join_ColumnsCountMismatch, nameof(target));
            }

            var result = new ColumnMapping[Count];

            for (int i = 0; i < Count; i++)
            {
                var sourceColumn = this[i].Column;
                var targetColumn = target[i].Column;
                if (sourceColumn.DataType != targetColumn.DataType)
                {
                    throw new ArgumentException(DiagnosticMessages.PrimaryKey_Join_ColumnDataTypeMismatch, string.Format("{0}[{1}]", nameof(target), i));
                }
                result[i] = new ColumnMapping(sourceColumn, targetColumn);
            }

            return(result);
        }