SqlLikeAttribute 特性增長 左、右Like實現

SqlLikeAttribute 特性原來只實現了全Like,今天增長左、右Like實現sql

更新時間: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)));
                    }                    
                }
            }
        }
相關文章
相關標籤/搜索