Esempio n. 1
0
        internal static async Task <Resp> UpdatePartial <TType>(this IDbConnection con, string tableName,
                                                                Expression <Func <TType, object> > update, Expression <Func <TType, bool> > where, object mo)
        //where TType : BaseMo<IdType>
        {
            if (string.IsNullOrEmpty(tableName))
            {
                tableName = typeof(TType).Name;
            }

            var visitor = new SqlExpressionVisitor();

            var updateSql = GetVisitExpressSql(visitor, update, SqlVistorType.Update);
            var whereSql  = GetVisitExpressSql(visitor, where, SqlVistorType.Where);
            var sql       = string.Concat("UPDATE ", tableName, " SET ", updateSql, whereSql);

            var paras = GetExecuteParas(mo, visitor);
            var row   = await con.ExecuteAsync(sql, paras);

            return(row > 0 ? new Resp() : new Resp().WithResp(RespTypes.OperateFailed, "更新失败!"));
        }
Esempio n. 2
0
        /// <summary>
        ///   处理where条件表达式,如果表达式为空,默认使用Id
        /// </summary>
        /// <param name="visitor"></param>
        /// <param name="exp"></param>
        /// <param name="visType"></param>
        private static string GetVisitExpressSql(SqlExpressionVisitor visitor, Expression exp, SqlVistorType visType)
        {
            if (visType == SqlVistorType.Update)
            {
                var updateFlag = new SqlVistorFlag(SqlVistorType.Update);
                visitor.Visit(exp, updateFlag);
                return(updateFlag.sql);
            }

            string sql;

            if (exp == null)
            {
                throw new ArgumentNullException("whereExp", "where表达式不能为空!");
            }
            else
            {
                var whereFlag = new SqlVistorFlag(SqlVistorType.Where);
                visitor.Visit(exp, whereFlag);
                sql = string.Concat(" WHERE ", whereFlag.sql);
            }

            return(sql);
        }