コード例 #1
0
        public SQLParserContext(string sentence)
        {
            StringCollection words = new StringCollection();

            sentence = sentence.Replace("\r", " ").Replace("\n", " ");
            sentence = sentence.Trim();
            int curPos = 0;

            for (int i = 0; i < sentence.Length; i++)
            {
                //忽略空字符
                while (i < sentence.Length && (sentence[i] == ' ' || sentence[i] == '	'))
                {
                    i++;
                }
                curPos = i;
                //分析字符串
                if (sentence[i] == '\'')
                {
                    for (i++;;)
                    {
                        if (i < sentence.Length && sentence[i] != '\'')
                        {
                            i++;
                        }
                        else if (i + 1 < sentence.Length && sentence[i] == '\'' && sentence[i + 1] == '\'')
                        {
                            i += 2;
                        }
                        else
                        {
                            break;
                        }
                    }
                    words.Add(sentence.Substring(curPos, i - curPos + 1));
                }
                else
                {
                    while (i < sentence.Length && sentence[i] != ' ' && sentence[i] != '	'&& Operators.IndexOf(sentence[i]) < 0)
                    {
                        i++;
                    }
                    if (i > curPos)
                    {
                        words.Add(sentence.Substring(curPos, i - curPos));
                    }
                    if (i < sentence.Length && Operators.IndexOf(sentence[i]) >= 0)
                    {
                        words.Add(sentence[i].ToString());
                    }
                }
            }
            for (int i = 0; i < words.Count; i++)
            {
                ParserToken t = new ParserToken();
                if (string.Compare(words[i], "select", true) == 0)
                {
                    t.Word = "select";
                    t.Type = ParserTokenType.KEYWORD;
                }
                else if (string.Compare(words[i], "from", true) == 0)
                {
                    t.Word = "from";
                    t.Type = ParserTokenType.KEYWORD;
                }
                else if (string.Compare(words[i], "where", true) == 0)
                {
                    t.Word = "where";
                    t.Type = ParserTokenType.KEYWORD;
                }
                else if (i + 1 < words.Count && string.Compare(words[i], "group", true) == 0 && string.Compare(words[i + 1], "by", true) == 0)
                {
                    t.Word = "group by";
                    t.Type = ParserTokenType.KEYWORD;
                    i++;
                }
                else if (string.Compare(words[i], "having", true) == 0)
                {
                    t.Word = "having";
                    t.Type = ParserTokenType.KEYWORD;
                }
                else if (i + 1 < words.Count && string.Compare(words[i], "order", true) == 0 && string.Compare(words[i + 1], "by", true) == 0)
                {
                    t.Word = "order by";
                    t.Type = ParserTokenType.KEYWORD;
                    i++;
                }
                else if (words[i] == "(" || words[i] == ")")
                {
                    t.Word = words[i];
                    t.Type = ParserTokenType.OPERATOR;
                }
                else if (words[i] == ",")
                {
                    t.Word = words[i];
                    t.Type = ParserTokenType.SEPARATOR;
                }
                else
                {
                    t.Word = words[i];
                    t.Type = ParserTokenType.IDENTIFY;
                }
                tokenList.Add(t);
                TokenPosition = 0;
            }
        }
コード例 #2
0
        public void PopClause()
        {
            if (clauseStack.Count > 0)
            {
                SQLParserClause clause    = clauseStack.Pop() as SQLParserClause;
                string          clauseStr = "";
                while (clause.Tokens.Count > 0)
                {
                    ParserToken t      = clause.Tokens.Dequeue() as ParserToken;
                    string      prestr = " ";
                    if (clauseStr.Length > 0)
                    {
                        char l = clauseStr[clauseStr.Length - 1];
                        char f = t.Word[0];
                        if (NoSeperators.IndexOf(l) >= 0 || NoSeperators.IndexOf(f) >= 0)
                        {
                            prestr = "";
                        }
                    }
                    else
                    {
                        prestr = "";
                    }
                    clauseStr += prestr + t.Word;
                }
                if (clauseStr.Trim().Length > 0)
                {
                    string prestr = " ";
                    if (tempClause.Length > 0)
                    {
                        char l = tempClause[tempClause.Length - 1];
                        char f = clauseStr[0];
                        if (NoSeperators.IndexOf(l) >= 0 || NoSeperators.IndexOf(f) >= 0)
                        {
                            prestr = "";
                        }
                    }
                    else
                    {
                        prestr = "";
                    }
                    tempClause += prestr + clauseStr;

                    prestr = " ";
                    if (parenthesesClause.Length > 0)
                    {
                        char l = parenthesesClause[parenthesesClause.Length - 1];
                        char f = clauseStr[0];
                        if (NoSeperators.IndexOf(l) >= 0 || NoSeperators.IndexOf(f) >= 0)
                        {
                            prestr = "";
                        }
                    }
                    else
                    {
                        prestr = "";
                    }
                    parenthesesClause += prestr + clauseStr;

                    prestr = " ";
                    if (expClause.Length > 0)
                    {
                        char l = expClause[expClause.Length - 1];
                        char f = clauseStr[0];
                        if (NoSeperators.IndexOf(l) >= 0 || NoSeperators.IndexOf(f) >= 0)
                        {
                            prestr = "";
                        }
                    }
                    else
                    {
                        prestr = "";
                    }
                    expClause += prestr + clauseStr;
                }
                if (clause.Type == SQLParserClauseType.PARENTHESES)
                {
                    parenthesesLevel--;
                    if (parenthesesLevel == 0 && tempLevel == 2 && parenthesesClause.Trim().Length > 0)
                    {
                        Parentheses.Add(parenthesesClause.Trim());
                    }
                }
                if (clause.Type == SQLParserClauseType.EXPRESSION)
                {
                    expressionLevel--;
                    if (expressionLevel == 0 && expClause.Trim().Length > 0)
                    {
                        if (tempLevel == 1)
                        {
                            Expressions.Add(expClause.Trim());
                        }
                        else if (tempLevel == 3)
                        {
                            if (groupByListLevel == 1)
                            {
                                GroupByExps.Add(expClause.Trim());
                            }
                            else if (orderByListLevel == 1)
                            {
                                OrderByExps.Add(expClause.Trim());
                            }
                        }
                    }
                }
                if (clause.Type == SQLParserClauseType.COUNT)
                {
                    tempLevel--;
                }
                if (clauseStack.Count <= 3)
                {
                    switch (clause.Type)
                    {
                    case SQLParserClauseType.SELECT:
                        SelectClause = tempClause.Trim();
                        break;

                    case SQLParserClauseType.FROM:
                        FromClause = tempClause.Trim();
                        break;
                    }
                }
                if (clauseStack.Count <= 7)
                {
                    switch (clause.Type)
                    {
                    case SQLParserClauseType.WHERE:
                        WhereClause = tempClause.Trim();
                        break;

                    case SQLParserClauseType.GROUPBYLIST:
                        groupByListLevel--;
                        GroupByList = tempClause.Trim();
                        break;

                    case SQLParserClauseType.HAVING:
                        HavingClause = tempClause.Trim();
                        break;

                    case SQLParserClauseType.ORDERBYLIST:
                        orderByListLevel--;
                        OrderByList = tempClause.Trim();
                        break;
                    }
                }
            }
        }