コード例 #1
0
        public void RemoveUnnecessary(DbAccessorContext context)
        {
            var            dbParameters = context.DbCommand.Parameters;
            IList <string> unnecessaryParameterNames = new List <string>();

            foreach (DbParameter p in dbParameters)
            {
                if (!SqlUtil.ContainsParameterName(context.DbCommand.CommandText, p.ParameterName))
                {
                    unnecessaryParameterNames.Add(p.ParameterName);
                }
            }

            foreach (string unnecessaryParameterName in unnecessaryParameterNames)
            {
                dbParameters.RemoveAt(unnecessaryParameterName);
            }
        }
コード例 #2
0
        private string RecursiveFindDynamicClause(MatchCollection matchs, SqlCommand sqlCommand)
        {
            string result = string.Empty;

            if (matchs.Count < 1)
            {
                return(result);
            }

            foreach (Match match in matchs)
            {
                result = match.Value;
                string reResult = RecursiveFindDynamicClause(RegexPattern.DynamicClausePattern.Matches(match.Groups["DynamicClause"].Value), sqlCommand);
                if (!sqlCommand.Parameters.Exists((c) =>
                {
                    bool r = false;
                    string tempResult = string.IsNullOrEmpty(reResult) ? result : result.Replace(reResult, "");
                    if (SqlUtil.ContainsParameterName(tempResult, c.ParameterName))
                    {
                        //当值不为“null”、“DBNull”和“空白字符串”时参数值才算是有效
                        if (c.Value != null && c.Value != DBNull.Value && c.Value.ToString().Trim() != string.Empty)
                        {
                            r = true;
                        }

                        if (result.StartsWith(LEFT_BRACE_DUBLE_QUESTION_MARK))
                        {
                            r = true;
                        }
                    }
                    return(r);
                }))
                {
                    _removeClauseStack.Push(result);
                }
            }
            return(result);
        }