Exemple #1
0
        public virtual Task <int> UpdateAsync <TProperty>(Expression <Func <TEntity, bool> > whereExpression, Expression <Func <TEntity, TProperty> > propertyExpression, object value, CancellationToken cancellationToken = default)
        {
            var whereSql     = SqlExpressionParser.ParseWhereExpression(whereExpression, ColumnMappings);
            var propertyName = propertyExpression.GetMemberName();
            var sql          = $@"
UPDATE {TableName}
SET {GetColumnName(propertyName)} = @set_{propertyName}
{whereSql.SqlText}
";

            whereSql.Parameters.Add($"set_{propertyName}", value);
            return(_dbConnection.Value.ExecuteAsync(sql, whereSql.Parameters, cancellationToken: cancellationToken));
        }
        public virtual Task <int> UpdateAsync(Expression <Func <TEntity, bool> > whereExpression, IDictionary <string, object> propertyValues, CancellationToken cancellationToken = default)
        {
            if (propertyValues == null || propertyValues.Count == 0)
            {
                return(Task.FromResult(0));
            }
            var whereSql = SqlExpressionParser.ParseWhereExpression(whereExpression, ColumnMappings);
            var sql      = $@"
UPDATE {TableName}
SET {propertyValues.Keys.Select(p => $"{GetColumnName(p)}=@set_{p}").StringJoin($",{Environment.NewLine}")}
{whereSql.SqlText}
";

            foreach (var propertyValue in propertyValues)
            {
                whereSql.Parameters.Add($"set_{propertyValue.Key}", propertyValue.Value);
            }
            return(_dbConnection.Value.ExecuteAsync(sql, whereSql.Parameters, cancellationToken: cancellationToken));
        }
Exemple #3
0
        public virtual int Update(Expression <Func <TEntity, bool> > whereExpression, IDictionary <string, object> propertyValues)
        {
            if (propertyValues == null || propertyValues.Count == 0)
            {
                return(0);
            }
            var whereSql = SqlExpressionParser.ParseWhereExpression(whereExpression, ColumnMappings);
            var sql      = $@"
UPDATE {TableName}
SET {propertyValues.Keys.Select(p => $"{p}=@set_{p}").StringJoin($",{Environment.NewLine}")}
{whereSql.SqlText}
";

            foreach (var propertyValue in propertyValues)
            {
                whereSql.Parameters.Add($"set_{propertyValue.Key}", propertyValue.Value);
            }
            return(_dbConnection.Value.Execute(sql, GetDbParameters(whereSql.Parameters)));
        }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            string GetResult()
            {
                // TODO:完善 Method Call 解析
                if (node.Object?.Type == typeof(string))
                {
                    return(SqlExpressionParser.ParseStringMethodCall(node, _columnMappings));
                }
                if (node.Object?.Type == typeof(DateTime))
                {
                    return(SqlExpressionParser.ParseDateTimeMethodCall(node, _columnMappings));
                }
                //
                throw new NotImplementedException();
            }

            _leaves.Push(GetResult());
            return(base.VisitMethodCall(node));
        }