SqlLikeAttribute 特性增加 左、右Like实现

SqlLikeAttribute 特性原来只实现了全Like,今天增加左、右Like实现

更新时间:2016-04-30
/// <summary>
        /// 获取查询条件语句
        /// </summary>
        /// <typeparam name="SearchT">查询实体类型</typeparam>
        /// <param name="searchEntity">查询实体</param>
        /// <param name="sqlParameters">参数列表</param>
        /// <param name="WhereSql">条件语句</param>
        /// <param name="item">属性对象</param>
        private static void GetQueryWhere<SearchT>(SearchT searchEntity, List<MySqlParameter> sqlParameters, StringBuilder WhereSql, PropertyInfo property)
        {
            //验证值是否有效
            if (WhereValueValidate<SearchT>(searchEntity, property))
            {
                var sqlLikeAttr = property.GetCustomAttribute(typeof(SqlLikeAttribute));
                if (sqlLikeAttr != null)
                {
                    var columnName = GetColumnName(property);
                    var proprtyValue = filterLikeRiskStr(property.GetValue(searchEntity));

                    if (!string.IsNullOrEmpty(proprtyValue))
                    {
                        //获取特性值
                        var sqlLikeVal = (sqlLikeAttr as SqlLikeAttribute).Value;
                        switch (sqlLikeVal)
                        {
                            case SqlLikeType.LikeAll:
                                WhereSql.AppendFormat(" AND {0} Like '%{1}%'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                break;
                            case SqlLikeType.LikeLeft:
                                WhereSql.AppendFormat(" AND {0} Like '%{1}'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                break;
                            case SqlLikeType.LikeRight:
                                WhereSql.AppendFormat(" AND {0} Like '{1}%'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                break;
                            default:
                                WhereSql.AppendFormat(" AND {0} Like '%{1}%'", !string.IsNullOrEmpty(columnName) ? columnName : property.Name, proprtyValue);
                                break;
                        }
                    }
                }
                else if (property.GetCustomAttribute(typeof(GreaterAndEqualAttribute)) != null)
                {
                    var columnName = GetColumnName(property);
                    if (!string.IsNullOrEmpty(columnName))
                    {
                        var parameterName = string.Format("@{0}", property.Name);
                        WhereSql.AppendFormat(" AND {0} >= {1}", columnName, parameterName);
                        sqlParameters.Add(new MySqlParameter(parameterName, property.GetValue(searchEntity)));
                    }
                }
                else if (property.GetCustomAttribute(typeof(LessAndEqualAttribute)) != null)
                {
                    var columnName = GetColumnName(property);
                    if (!string.IsNullOrEmpty(columnName))
                    {
                        var parameterName = string.Format("@{0}", property.Name);
                        var value = property.GetValue(searchEntity);
                        if (value.GetType().Name.Equals("DateTime"))
                        {
                            WhereSql.AppendFormat(" AND {0} < {1}", columnName, parameterName);
                            sqlParameters.Add(new MySqlParameter(parameterName, Convert.ToDateTime(value).AddDays(1)));
                        }
                        else
                        {
                            WhereSql.AppendFormat(" AND {0} <= {1}", columnName, parameterName);
                            sqlParameters.Add(new MySqlParameter(parameterName, value));
                        }
                    }
                }
                else
                {
                    //新方法灵活性高
                    var columnName = GetColumnName(property);
                    if (!string.IsNullOrEmpty(columnName))
                    {
                        var parameterName = string.Format("@{0}", property.Name);
                        WhereSql.AppendFormat(" AND {0} = {1}", columnName, parameterName);
                        sqlParameters.Add(new MySqlParameter(parameterName, property.GetValue(searchEntity)));
                    }                    
                }
            }
        }

 

posted @ 2016-04-30 23:05  baivfhpwxf  阅读(770)  评论(0编辑  收藏  举报