Пример #1
0
        private IExpression Where(DataUpdateContext context, UpdateStatement statement)
        {
            if (context.IsMultiple || context.Criteria == null)
            {
                var criteria = new ConditionExpression(ConditionCombination.And);

                foreach (var key in statement.Entity.Key)
                {
                    if (!statement.Entity.GetTokens(context.ModelType).TryGet(key.Name, out var token))
                    {
                        throw new DataException($"No required primary key field values were specified for the updation '{statement.Entity.Name}' entity data.");
                    }

                    var field     = statement.Table.CreateField(key);
                    var parameter = Expression.Parameter(field, new SchemaMember(token));

                    criteria.Add(Expression.Equal(field, parameter));
                    statement.Parameters.Add(parameter);
                }

                criteria.Add(statement.Where(context.Validate(), context.Aliaser));

                return(criteria.Count > 0 ? criteria : null);
            }

            return(statement.Where(context.Validate(), context.Aliaser));
        }
Пример #2
0
        public IEnumerable <IStatementBase> Build(DataIncrementContext context)
        {
            var statement = new UpdateStatement(context.Entity);

            var source = statement.From(context.Member, context.Aliaser, null, out var property);
            var field  = source.CreateField(property);
            var value  = context.Interval > 0 ?
                         Expression.Add(field, Expression.Constant(context.Interval)) :
                         Expression.Subtract(field, Expression.Constant(-context.Interval));

            //添加修改字段
            statement.Fields.Add(new FieldValue(field, value));

            //构建WHERE子句
            statement.Where = statement.Where(context.Validate(), context.Aliaser);

            if (context.Source.Features.Support(Feature.Updation.Outputting))
            {
                statement.Returning = new ReturningClause();
                statement.Returning.Append(field, ReturningClause.ReturningMode.Inserted);
            }
            else
            {
                var slave = new SelectStatement();

                foreach (var from in statement.From)
                {
                    slave.From.Add(from);
                }

                slave.Where = statement.Where;
                slave.Select.Members.Add(field);

                //注:由于从属语句的WHERE子句只是简单的指向父语句的WHERE子句,
                //因此必须手动将父语句的参数依次添加到从属语句中。
                foreach (var parameter in statement.Parameters)
                {
                    slave.Parameters.Add(parameter);
                }

                statement.Slaves.Add(slave);
            }

            yield return(statement);
        }