Пример #1
0
        internal static async Task <ResultMo> UpdatePartail <TType>(this IDbConnection con, string tableName,
                                                                    Expression <Func <TType, object> > update, Expression <Func <TType, bool> > where, object mo)
        //where TType : BaseMo
        {
            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 = GetExcuteParas(mo, visitor);
            var row   = await con.ExecuteAsync(sql, paras);

            return(row > 0 ? new ResultMo() : new ResultMo(ResultTypes.UpdateFail, "更新失败"));
        }
Пример #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)
            {
                sql = " WHERE id=@id";
            }
            else
            {
                var whereFlag = new SqlVistorFlag(SqlVistorType.Where);
                visitor.Visit(exp, whereFlag);
                sql = string.Concat(" WHERE ", whereFlag.Sql);
            }

            return(sql);
        }