Пример #1
0
        private static Connectable GetUpdateBody <T>(Assembly client, DbRow row, bool forceMirroring, ConnectBy connectBy,
                                                     string method, ref string name, out NodeMap map)
            where T : DbRow
        {
            if (row == null)
            {
                throw new QueryTalkException("Crud", QueryTalkExceptionType.ArgumentNull, "row = null", method);
            }

            Crud.CheckTable(row, method);

            ColumnSelector selector = forceMirroring ? ColumnSelector.All : ColumnSelector.RK;

            List <ParameterArgument> args = new List <ParameterArgument>();

            map  = DbMapping.TryGetNodeMap(row);
            name = map.Name.Sql;

            if (forceMirroring)
            {
                if (!row.GetStatus().IsUpdatable())
                {
                    var arguments = map.BuildExceptionReport(row.GetOriginalRKValues(), row.GetOriginalRowversionValue());
                    throw new QueryTalkException("Crud.UpdateGo", QueryTalkExceptionType.InvalidMirroring,
                                                 arguments, Text.Method.UpdateGo).SetObjectName(map.Name.Sql);
                }
            }

            object[] originalValues;

            if (selector == ColumnSelector.All)
            {
                if (map.HasRowversion)
                {
                    originalValues = new object[] { row.GetOriginalRowversionValue() };
                }
                else
                {
                    originalValues = row.GetOriginalValues();
                }
            }
            // RK selector
            else
            {
                originalValues = row.GetOriginalRKValues();
            }

            for (int i = 0; i < originalValues.Length; ++i)
            {
                args.Add(new ParameterArgument(new Value(originalValues[i])));
            }

            var currentValues    = PropertyAccessor.GetValues(row);
            var updatableColumns = row.GetUpdatableColumns(forceMirroring);

            if (updatableColumns == null || updatableColumns.Length == 0)
            {
                return(null);
            }

            var valueParams = new Column[updatableColumns.Length];
            int j           = 0;

            foreach (var column in updatableColumns)
            {
                var value = new Value(currentValues[column.ID.ColumnZ - 1]);
                args.Add(new ParameterArgument(value));
                valueParams[j] = String.Format("@v{0}", j + 1);
                ++j;
            }

            args.Add(map.Name);
            if (selector == ColumnSelector.All)
            {
                args.Add(new ParameterArgument(map.BuildOptimisticPredicate(originalValues, 1)));
            }
            else
            {
                args.Add(new ParameterArgument(map.BuildRKPredicate(originalValues, 1)));
            }

            int[] modified = updatableColumns.Select(a => a.ID.ColumnZ).ToArray();

            args.Add(map.Columns
                     .Where(a => modified.Contains(a.ID.ColumnZ))
                     .OrderBy(a => a.ID.ColumnZ)
                     .Select(a => new Column(a.Name))
                     .ToArray());

            Column[] outputColumns;
            Column[] selectColumns;
            _getColumnsWithoutRowversion(map, out outputColumns, out selectColumns);

            var cpass = UpdateProc <T>(map, selector, valueParams, updatableColumns, outputColumns, selectColumns).Pass(args.ToArray());

            cpass.SetRootMap(row.NodeID);
            return(Reader.GetConnectable(client, row, cpass, connectBy));
        }