Esempio n. 1
0
        public SuggestionList(Query q, string k)
        {
            keywordName = k;
            query       = q;
            if (k == "select")
            {
                keyword = q.select;
            }
            else if (k == "where")
            {
                keyword = q.where;
            }
            else if (k == "order")
            {
                keyword = q.order;
            }
            else if (k == "group")
            {
                keyword = q.group;
            }
            else if (k == "from")
            {
                keyword = q.from;
            }

            if (keyword == null)
            {
                keyword             = Keyword.CreateKeyword(0, k);
                keyword.parentQuery = q;
            }
        }
Esempio n. 2
0
        public string AddTable(DbTable t, bool includeAlias)
        {
            string alias = "";

            if (includeAlias)
            {
                alias = t.GetAlias(true);
            }
            int insertPos = 0;

            if (query.from == null)
            {
                Keyword k = Keyword.CreateKeyword(0, "from");
                k.parentQuery = query;
                insertPos     = k.GetInsertOffset();
                inserts.Add(insertPos, ("from " + t.name + " " + alias).Trim());
            }
            else
            {
                if (query.from.tables.GetTableByName(t.name) == null)
                {
                    insertPos = query.from.rightExtent;
                    while (Char.IsWhiteSpace(Query.rootQuery.expression[insertPos - 1]))
                    {
                        insertPos--;
                    }
                    string bestJoin = null;
                    foreach (Table table in query.from.tables.tokens)
                    {
                        string join = table.dbTable.RenderJoin(t.name, includeAlias);
                        if (join != "" && (bestJoin == null || bestJoin.CountOccurrances('.') > join.CountOccurrances('.')))
                        {
                            bestJoin = join;
                        }
                    }
                    if (bestJoin != null)
                    {
                        inserts.Add(insertPos, bestJoin);
                    }
                }
            }

            return(alias);
        }
Esempio n. 3
0
        private void Parse()
        {
            string debugAt      = "";
            string word         = "";
            bool   inNumber     = false;
            bool   inString     = false;
            bool   inIdentifier = false;
            bool   inOperator   = false;
            int    wordStart    = -1;

            for (int at = 0; at < sql.Length; at++)
            {
                char c = sql[at];
                debugAt = sql.Substring(at);
                if (inString && c == '\'' && at < sql.Length - 1 && sql[at + 1] == '\'')
                {
                    word += '\'';
                    at++;
                    continue;
                }
                if (c == '\'' && inString)
                {
                    AddParsedToken(new Literal(wordStart, word + c));
                    word     = "";
                    inString = false;
                    continue;
                }
                else if (inNumber && !numberChars.Contains(c))
                {
                    AddParsedToken(new Literal(wordStart, word));
                    inNumber = false;
                }
                else if (inOperator && !operatorChars.Contains(c))
                {
                    AddParsedToken(new Operator(wordStart, word));
                    inOperator = false;
                }
                else if (inIdentifier && !IsIdentifierChar(at))
                {
                    if (Query.keywords.Contains(word))
                    {
                        AddParsedToken(Keyword.CreateKeyword(wordStart, word));
                    }
                    else if (Db.tables.ContainsKey(word))
                    {
                        AddParsedToken(new Table(wordStart, word));
                    }
                    else
                    {
                        AddParsedToken(new Identifier(wordStart, word));
                    }
                    inIdentifier = false;
                }
                if (inOperator == false && inIdentifier == false && inString == false && inNumber == false)
                {
                    wordStart = at;
                    word      = "";
                    if (numberStartChars.Contains(c))
                    {
                        inNumber = true;
                    }
                    else if (operatorChars.Contains(c))
                    {
                        inOperator = true;
                    }
                    else if (IsIdentifierChar(at))
                    {
                        inIdentifier = true;
                    }
                    else if (c == '\'')
                    {
                        inString = true;
                    }
                    else if (punctuationChars.Contains(c))
                    {
                        AddParsedToken(new Punctuation(at, c));
                    }
                }
                if (inOperator || inIdentifier || inString || inNumber)
                {
                    word += c;
                }
            }

            if (inString)
            {
                AddParsedToken(new Literal(wordStart, word));
            }
            else if (inNumber)
            {
                AddParsedToken(new Literal(wordStart, word));
            }
            if (inOperator)
            {
                AddParsedToken(new Operator(wordStart, word));
            }
            if (inIdentifier)
            {
                if (Query.keywords.Contains(word))
                {
                    AddParsedToken(Keyword.CreateKeyword(wordStart, word));
                }
                else if (Db.tables.Keys.Contains(word))
                {
                    AddParsedToken(new Table(wordStart, word));
                }
                else
                {
                    AddParsedToken(new Identifier(wordStart, word));
                }
            }
        }