Ejemplo n.º 1
0
        /// <summary>
        /// 获取匹配的列.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string GetMatchedOrderbyColumn(string str)
        {
            var col        = "\\[?[\\w_#@\\$]+\\]?";
            var aliasTable = "\\[?[\\w_#@\\$]+\\]?";

            //"UserNickName"
            var match1 = "^(" + col + ")$";
            //"UserNickName ASC"
            var match2 = "^(" + col + "\\s+ASC)$";
            //"UserNickName DESC"
            var match3 = "^(" + col + "\\s+DESC)$";
            //"dbc.UserNickName"
            var match4 = "^" + aliasTable + "\\.(" + col + ")$";
            //"us.UserNickName ASC"
            var match5 = "^" + aliasTable + "\\.(" + col + "\\s+ASC)$";
            //"us.UserNickName DESC"
            var match6 = "^" + aliasTable + "\\.(" + col + "\\s+DESC)$";


            var matches = new string[] { match1, match2, match3, match4, match5, match6 };
            var colName = string.Empty;
            var i       = 0;

            while (string.IsNullOrWhiteSpace(colName) && i < matches.Length)
            {
                colName = SqlMacher.GetMatchedValueIgnoreCase(str, matches[i]);
                i++;
            }
            return(colName);
        }
Ejemplo n.º 2
0
        private List <StrPoint> FindAllUnion(string sql)
        {
            List <StrPoint> findStack = new List <StrPoint>();
            var             patterns  = new string[] { "^UNION\\s+ALL", "^\\(", "^\\)", "^'", "^UNION" };
            var             curPoint  = 0;
            var             subSql    = sql;

            do
            {
                foreach (var keyWord in patterns)
                {
                    var meetContent = "";
                    if (StartWith(subSql, keyWord, out meetContent))
                    {
                        findStack.Add(new StrPoint()
                        {
                            Pattern = keyWord, ActualContent = meetContent, Point = curPoint
                        });
                        break;
                    }
                }
                //开心消消乐 '(' ')'消去
                SqlMacher.ClearCombo(findStack, patterns[1], patterns[2]);
                //开心消消乐 ''' '''消去
                SqlMacher.ClearCombo(findStack, patterns[3], patterns[3]);
                //开心消消乐 '(' 'mainWord' ')'消去
                SqlMacher.ClearCombo(findStack, patterns[1], patterns[2], patterns[0], patterns[4]);
                //开心消消乐 ''' 'mainWord' '''消去
                SqlMacher.ClearCombo(findStack, patterns[3], patterns[3], patterns[0], patterns[4]);

                ++curPoint;
                subSql = subSql.Substring(1, subSql.Length - 1);
            } while (!string.IsNullOrWhiteSpace(subSql));
            return(findStack);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取匹配的列.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string GetMatchedSelectColumn(string str)
        {
            var aliasCol   = "\\[?\\'?([\\w_#@\\$]+)\\'?\\]?";
            var col        = "\\[?[\\w_#@\\$]+\\]?";
            var aliasTable = "\\[?[\\w_#@\\$]+\\]?";
            //"UserNickName"
            var match1 = "^(" + col + ")$";
            //"us.UserNickName"
            var match2 = "^" + aliasTable + "\\.(" + col + ")$";
            //"us.UserNickName UserName"
            var match3 = "^" + col + "\\s+" + aliasCol + "$";
            //"UserNickName UserName"
            var match4 = "^" + aliasTable + "\\." + col + "\\s+" + aliasCol + "$";
            //"UserNickName as UserName"
            var match5 = "^" + col + "\\s+AS\\s+" + aliasCol + "$";
            //"us.UserNickName as UserName"
            var match6 = "^" + aliasTable + "\\." + col + "\\s+AS\\s+" + aliasCol + "$";
            //"UserName = us.UserNickName"
            var match8 = "^" + aliasCol + "\\s*=\\s*" + col + "$";
            //"UserName = us.UserNickName"
            var match7  = "^" + aliasCol + "\\s*=\\s*" + aliasTable + "\\." + col + "$";
            var match9  = "[\\w\\W]+" + "\\s+AS\\s+" + aliasCol + "$";
            var match10 = "^" + aliasCol + "\\s*=\\s*[\\w\\W]+";
            var match11 = "[\\w\\W]+" + "\\s+" + aliasCol + "$";
            var match12 = "^(\\*)$";

            var matches = new string[] { match1, match2, match3, match4, match7, match5, match6, match8, match9, match10, match11, match12 };
            var colName = string.Empty;
            var i       = 0;

            while (string.IsNullOrWhiteSpace(colName) && i < matches.Length)
            {
                colName = SqlMacher.GetMatchedValueIgnoreCase(str, matches[i]);
                i++;
            }
            return(colName);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取所有匹配的点
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="mainWord"></param>
        /// <returns></returns>
        private List <StrPoint> GetAllMatchPoint(string sql, string mainWord)
        {
            List <StrPoint> findStack = new List <StrPoint>();
            var             patterns  = new string[] { mainWord, "^\\(", "^\\)", "^'" };
            var             curPoint  = 0;
            var             sb        = new StringBuilder(sql);

            do
            {
                foreach (var keyWord in patterns)
                {
                    string meetContent;
                    if (StartWith(sb.ToString(), keyWord, out meetContent))
                    {
                        findStack.Add(new StrPoint()
                        {
                            Pattern = keyWord, ActualContent = meetContent, Point = curPoint
                        });
                    }
                }
                //开心消消乐 '(' ')'消去
                SqlMacher.ClearCombo(findStack, patterns[1], patterns[2]);
                //开心消消乐 ''' '''消去
                SqlMacher.ClearCombo(findStack, patterns[3], patterns[3]);
                //开心消消乐 '(' 'mainWord' ')'消去
                SqlMacher.ClearCombo(findStack, patterns[1], patterns[2], mainWord);
                //开心消消乐 ''' 'mainWord' '''消去
                SqlMacher.ClearCombo(findStack, patterns[3], patterns[3], mainWord);
                ++curPoint;
                if (sb.Length > 0)
                {
                    sb.Remove(0, 1);
                }
            } while (sb.Length > 0);
            return(findStack);
        }