コード例 #1
0
        /// <summary>
        /// Merges the specified <paramref name="table" /> into the <see cref="TargetTable" /> capturing the changes in the result.
        /// </summary>
        /// <param name="table">The table data to be merged.</param>
        /// <returns>A collection of the merge output in a dictionary.</returns>
        /// <exception cref="System.InvalidOperationException">Bulk-Copy only supported by SQL Server.  Make sure DataSession was create with a valid SqlConnection.</exception>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException">TargetTable is require for the merge definition.
        /// or
        /// At least one column is required for the merge definition.
        /// or
        /// At least one column is required to be marked as a key for the merge definition.
        /// or
        /// SourceColumn is require for column merge definition
        /// or
        /// NativeType is require for column merge definition</exception>
        public IEnumerable <DataMergeOutputRow> MergeOutput(DataTable table)
        {
            // update definition to include output
            _mergeDefinition.IncludeOutput = true;

            // validate definition
            if (!Validate(_mergeDefinition))
            {
                return(null);
            }

            var results = new List <DataMergeOutputRow>();
            var columns = _mergeDefinition.Columns
                          .Where(c => c.IsIgnored == false)
                          .ToList();

            // run merge capturing output
            Merge(table, command =>
            {
                using (IDataReader reader = command.ExecuteReader())
                {
                    var originalReader = new DataReaderWrapper(reader, DataMergeGenerator.OriginalPrefix);
                    var currentReader  = new DataReaderWrapper(reader, DataMergeGenerator.CurrentPrefix);

                    while (reader.Read())
                    {
                        var output = new DataMergeOutputRow();

                        string action = reader.GetString("Action");
                        output.Action = action;

                        // copy to dictionary
                        foreach (var column in columns)
                        {
                            string name = column.SourceColumn;

                            var outputColumn      = new DataMergeOutputColumn();
                            outputColumn.Name     = name;
                            outputColumn.Original = originalReader.GetValue(name);
                            outputColumn.Current  = currentReader.GetValue(name);
                            outputColumn.Type     = currentReader.GetFieldType(name);

                            output.Columns.Add(outputColumn);
                        }

                        results.Add(output);
                    }
                }
            });

            return(results);
        }
コード例 #2
0
ファイル: DataMerge.cs プロジェクト: zyj0021/FluentCommand
        /// <summary>
        /// Merges the specified <paramref name="table" /> into the <see cref="TargetTable" /> capturing the changes in the result asynchronously.
        /// </summary>
        /// <param name="table">The table data to be merged.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A collection of the merge output in a dictionary.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">Bulk-Copy only supported by SQL Server.  Make sure DataSession was create with a valid SqlConnection.</exception>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException">TargetTable is require for the merge definition.
        /// or
        /// At least one column is required for the merge definition.
        /// or
        /// At least one column is required to be marked as a key for the merge definition.
        /// or
        /// SourceColumn is require for column merge definition
        /// or
        /// NativeType is require for column merge definition</exception>
        public async Task <IEnumerable <DataMergeOutputRow> > ExecuteOutputAsync(DataTable table, CancellationToken cancellationToken = default)
        {
            // update definition to include output
            _mergeDefinition.IncludeOutput = true;

            var results = new List <DataMergeOutputRow>();
            var columns = _mergeDefinition.Columns
                          .Where(c => c.IsIgnored == false)
                          .ToList();

            // run merge capturing output
            await MergeAsync(table, cancellationToken, async (command, token) =>
            {
                using (var reader = await command.ExecuteReaderAsync(token).ConfigureAwait(false))
                {
                    var originalReader = new DataReaderWrapper(reader, DataMergeGenerator.OriginalPrefix);
                    var currentReader  = new DataReaderWrapper(reader, DataMergeGenerator.CurrentPrefix);

                    while (await reader.ReadAsync(token).ConfigureAwait(false))
                    {
                        var output = new DataMergeOutputRow();

                        string action = reader.GetString("Action");
                        output.Action = action;

                        // copy to dictionary
                        foreach (var column in columns)
                        {
                            string name = column.SourceColumn;

                            var outputColumn      = new DataMergeOutputColumn();
                            outputColumn.Name     = name;
                            outputColumn.Original = originalReader.GetValue(name);
                            outputColumn.Current  = currentReader.GetValue(name);
                            outputColumn.Type     = currentReader.GetFieldType(name);

                            output.Columns.Add(outputColumn);
                        }

                        results.Add(output);
                    }
                }
            });

            return(results);
        }