Esempio n. 1
0
        private bool Equals(TSQLToken obj)
        {
            // If parameter is null, return false.
            if (Object.ReferenceEquals(obj, null))
            {
                return(false);
            }

            // Optimization for a common success case.
            if (Object.ReferenceEquals(this, obj))
            {
                return(true);
            }

            // If run-time types are not exactly the same, return false.
            if (this.GetType() != obj.GetType())
            {
                return(false);
            }

            // Return true if the fields match.
            // Note that the base class is not invoked because it is
            // System.Object, which defines Equals as reference equality.
            return
                (BeginPostion == obj.BeginPostion &&
                 EndPosition == obj.EndPosition &&
                 Text == obj.Text);
        }
Esempio n. 2
0
 public static bool IsFutureKeyword(this TSQLToken token, TSQLFutureKeywords keyword)
 {
     if (token == null)
     {
         return(false);
     }
     else
     {
         return(TSQLFutureKeywords.Parse(token.Text) == keyword);
     }
 }
Esempio n. 3
0
 public static bool IsWhitespace(this TSQLToken token)
 {
     if (token == null)
     {
         return(false);
     }
     else
     {
         return(token.Type == TSQLTokenType.Whitespace);
     }
 }
Esempio n. 4
0
 public static bool IsComment(this TSQLToken token)
 {
     if (token == null)
     {
         return(false);
     }
     else
     {
         return(token.Type == TSQLTokenType.SingleLineComment ||
                token.Type == TSQLTokenType.MultilineComment ||
                token.Type == TSQLTokenType.IncompleteComment);
     }
 }
        public ITSQLStatementParser Create(TSQLToken token)
        {
            if (token.Type == TSQLTokenType.Keyword)
            {
                TSQLKeywords keyword = token.AsKeyword.Keyword;

                if (keyword == TSQLKeywords.SELECT)
                {
                    return new TSQLSelectStatementParser();
                }
                // not fully implemented yet
                //else if (keyword == TSQLKeywords.INSERT)
                //{
                //	return new TSQLInsertStatementParser();
                //}
                //else if (keyword == TSQLKeywords.UPDATE)
                //{
                //	return new TSQLUpdateStatementParser();
                //}
                //else if (keyword == TSQLKeywords.DELETE)
                //{
                //	return new TSQLDeleteStatementParser();
                //}
                //else if (keyword == TSQLKeywords.MERGE)
                //{
                //	return new TSQLMergeStatementParser();
                //}
                //else if (keyword == TSQLKeywords.WITH)
                //{
                //	return new TSQLWithAggregateStatementParser();
                //}
                else
                {
                    return new TSQLUnknownStatementParser();
                }
            }
            else
            {
                return new TSQLUnknownStatementParser();

                // not fully implemented yet
                //return new TSQLExecuteStatementParser();
            }
        }
Esempio n. 6
0
        public static bool IsCharacter(this TSQLToken token, TSQLCharacters character)
        {
            if (token == null)
            {
                return(false);
            }

            if (token.Type != TSQLTokenType.Character)
            {
                return(false);
            }

            if (token.AsCharacter.Character != character)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 7
0
        public static bool IsKeyword(this TSQLToken token, TSQLKeywords keyword)
        {
            if (token == null)
            {
                return(false);
            }

            if (token.Type != TSQLTokenType.Keyword)
            {
                return(false);
            }

            if (token.AsKeyword.Keyword != keyword)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 8
0
        public static bool IsIdentifier(this TSQLToken token, TSQLIdentifiers identifier)
        {
            if (token == null)
            {
                return(false);
            }

            if (token.Type != TSQLTokenType.SystemIdentifier)
            {
                return(false);
            }

            if (token.AsSystemIdentifier.Identifier != identifier)
            {
                return(false);
            }

            return(true);
        }
        public bool Read()
        {
            CheckDisposed();

            if (_hasMore)
            {
                if (IncludeWhitespace)
                {
                    _hasMore = _charReader.Read();
                }
                else
                {
                    _hasMore = _charReader.ReadNextNonWhitespace();
                }

                if (_hasMore)
                {
                    SetCurrent();
                }
                else
                {
                    _current = null;
                }
            }

            return _hasMore;
        }
Esempio n. 10
0
        private void SetCurrent()
        {
            characterHolder.Length = 0;
            int startPosition = _charReader.Position;

            if (
                IncludeWhitespace &&
                char.IsWhiteSpace(_charReader.Current))
            {
                do
                {
                    characterHolder.Append(_charReader.Current);
                } while (
                    _charReader.Read() &&
                    char.IsWhiteSpace(_charReader.Current));

                if (!_charReader.EOF)
                {
                    _charReader.Putback();
                }
            }
            else
            {
                characterHolder.Append(_charReader.Current);

                switch (_charReader.Current)
                {
                    // period can signal the start of a numeric literal if followed by a number
                    case '.':
                        {
                            if (_charReader.Read())
                            {
                                if (
                                    _charReader.Current == '0' ||
                                    _charReader.Current == '1' ||
                                    _charReader.Current == '2' ||
                                    _charReader.Current == '3' ||
                                    _charReader.Current == '4' ||
                                    _charReader.Current == '5' ||
                                    _charReader.Current == '6' ||
                                    _charReader.Current == '7' ||
                                    _charReader.Current == '8' ||
                                    _charReader.Current == '9'
                                )
                                {
                                    characterHolder.Append(_charReader.Current);

                                    goto case '0';
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // all single character sequences with no optional double character sequence
                    case ',':
                    case ';':
                    case '(':
                    case ')':
                    case '~':
                        {
                            break;
                        }
                    // --
                    // -=
                    // -
                    case '-':
                        {
                            if (_charReader.Read())
                            {
                                if (_charReader.Current == '-')
                                {
                                    do
                                    {
                                        characterHolder.Append(_charReader.Current);
                                    } while (
                                        _charReader.Read() &&
                                        _charReader.Current != '\r' &&
                                        _charReader.Current != '\n');

                                    if (!_charReader.EOF)
                                    {
                                        _charReader.Putback();
                                    }
                                }
                                else if (_charReader.Current == '=')
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // /* */
                    // /=
                    // /
                    case '/':
                        {
                            if (_charReader.Read())
                            {
                                if (_charReader.Current == '*')
                                {
                                    characterHolder.Append(_charReader.Current);

                                    // supporting nested comments
                                    int currentLevel = 1;

                                    bool lastWasStar = false;
                                    bool lastWasSlash = false;

                                    while (
                                        _charReader.Read() &&
                                        (
                                            currentLevel > 1 ||
                                            // */
                                            !(
                                                lastWasStar &&
                                                _charReader.Current == '/'
                                            )
                                        ))
                                    {
                                        // /*
                                        if (
                                            lastWasSlash &&
                                            _charReader.Current == '*')
                                        {
                                            currentLevel++;
                                            lastWasSlash = false;
                                            lastWasStar = false;
                                        }
                                        // */
                                        else if (
                                            lastWasStar &&
                                            _charReader.Current == '/')
                                        {
                                            currentLevel--;
                                            lastWasSlash = false;
                                            lastWasStar = false;
                                        }
                                        else
                                        {
                                            lastWasSlash = _charReader.Current == '/';
                                            lastWasStar = _charReader.Current == '*';
                                        }

                                        characterHolder.Append(_charReader.Current);
                                    }

                                    if (!_charReader.EOF)
                                    {
                                        characterHolder.Append(_charReader.Current);
                                    }
                                }
                                else if (_charReader.Current == '=')
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // <>
                    // <=
                    // <
                    case '<':
                        {
                            if (_charReader.Read())
                            {
                                if (
                                    _charReader.Current == '>' ||
                                    _charReader.Current == '='
                                )
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // !=
                    // !<
                    // !>
                    case '!':
                        {
                            if (_charReader.Read())
                            {
                                if (
                                    _charReader.Current == '=' ||
                                    _charReader.Current == '<' ||
                                    _charReader.Current == '>'
                                )
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // =*
                    // =
                    case '=':
                        {
                            if (_charReader.Read())
                            {
                                if (
                                    _charReader.Current == '*'
                                )
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // &=
                    case '&':
                    // |=
                    case '|':
                    // ^=
                    case '^':
                    // +=
                    case '+':
                    // *=
                    case '*':
                    // %=
                    case '%':
                    // >=
                    case '>':
                        {
                            if (_charReader.Read())
                            {
                                if (_charReader.Current == '=')
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // N''
                    case 'N':
                        {
                            if (_charReader.Read())
                            {
                                if (_charReader.Current == '\'')
                                {
                                    characterHolder.Append(_charReader.Current);

                                    goto case '\'';
                                }
                                else if (_charReader.Current == '\"')
                                {
                                    characterHolder.Append(_charReader.Current);

                                    goto case '\"';
                                }
                                else
                                {
                                    _charReader.Putback();

                                    goto default;
                                }
                            }

                            break;
                        }
                    // ::
                    case ':':
                        {
                            if (_charReader.Read())
                            {
                                if (_charReader.Current == ':')
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                                else
                                {
                                    _charReader.Putback();
                                }
                            }

                            break;
                        }
                    // ''
                    case '\'':
                    // ""
                    case '\"':
                    // [dbo]
                    case '[':
                        {
                            char escapeChar;

                            if (_charReader.Current == '[')
                            {
                                escapeChar = ']';
                            }
                            else
                            {
                                escapeChar = _charReader.Current;
                            }

                            bool stillEscaped;

                            // read until '
                            // UNLESS the ' is doubled up
                            do
                            {
                                while (
                                    _charReader.Read() &&
                                    _charReader.Current != escapeChar)
                                {
                                    characterHolder.Append(_charReader.Current);
                                };

                                if (!_charReader.EOF)
                                {
                                    characterHolder.Append(_charReader.Current);
                                }

                                stillEscaped =
                                    !_charReader.EOF &&
                                    _charReader.Read() &&
                                    _charReader.Current == escapeChar;

                                if (stillEscaped)
                                {
                                    characterHolder.Append(_charReader.Current);
                                }
                            } while (stillEscaped);

                            if (!_charReader.EOF)
                            {
                                _charReader.Putback();
                            }

                            break;
                        }
                    // 0 can start a numeric or binary literal with different parsing logic
                    // 0x69048AEFDD010E
                    // 0x
                    case '0':
                        {
                            if (_charReader.Read())
                            {
                                if (
                                    _charReader.Current == 'x' ||
                                    _charReader.Current == 'X')
                                {
                                    characterHolder.Append(_charReader.Current);

                                    bool foundEnd = false;

                                    while (
                                        !foundEnd &&
                                        _charReader.Read())
                                    {
                                        switch (_charReader.Current)
                                        {
                                            case '0':
                                            case '1':
                                            case '2':
                                            case '3':
                                            case '4':
                                            case '5':
                                            case '6':
                                            case '7':
                                            case '8':
                                            case '9':
                                            case 'a':
                                            case 'b':
                                            case 'c':
                                            case 'd':
                                            case 'e':
                                            case 'f':
                                            case 'A':
                                            case 'B':
                                            case 'C':
                                            case 'D':
                                            case 'E':
                                            case 'F':
                                                {
                                                    characterHolder.Append(_charReader.Current);

                                                    break;
                                                }
                                            default:
                                                {
                                                    foundEnd = true;

                                                    break;
                                                }
                                        }
                                    }

                                    if (foundEnd)
                                    {
                                        _charReader.Putback();
                                    }
                                }
                                else
                                {
                                    _charReader.Putback();

                                    goto case '1';
                                }
                            }

                            break;
                        }
                    // numeric literals
                    // 1894.1204
                    // 0.5E-2
                    // 123E-3
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        {
                            bool foundEnd = false;
                            bool foundPeriod = false;

                            while (
                                !foundEnd &&
                                _charReader.Read())
                            {
                                switch (_charReader.Current)
                                {
                                    case 'e':
                                    case 'E':
                                        {
                                            characterHolder.Append(_charReader.Current);

                                            if (_charReader.Read())
                                            {
                                                switch (_charReader.Current)
                                                {
                                                    case '-':
                                                    case '+':
                                                        {
                                                            characterHolder.Append(_charReader.Current);

                                                            break;
                                                        }
                                                    default:
                                                        {
                                                            _charReader.Putback();

                                                            break;
                                                        }
                                                }
                                            }

                                            while (
                                                !foundEnd &&
                                                _charReader.Read())
                                            {
                                                switch (_charReader.Current)
                                                {
                                                    case '0':
                                                    case '1':
                                                    case '2':
                                                    case '3':
                                                    case '4':
                                                    case '5':
                                                    case '6':
                                                    case '7':
                                                    case '8':
                                                    case '9':
                                                        {
                                                            characterHolder.Append(_charReader.Current);

                                                            break;
                                                        }
                                                    default:
                                                        {
                                                            foundEnd = true;

                                                            break;
                                                        }
                                                }
                                            }

                                            break;
                                        }
                                    case '.':
                                        {
                                            if (foundPeriod)
                                            {
                                                foundEnd = true;
                                            }
                                            else
                                            {
                                                characterHolder.Append(_charReader.Current);

                                                foundPeriod = true;
                                            }

                                            break;
                                        }
                                    case '0':
                                    case '1':
                                    case '2':
                                    case '3':
                                    case '4':
                                    case '5':
                                    case '6':
                                    case '7':
                                    case '8':
                                    case '9':
                                        {
                                            characterHolder.Append(_charReader.Current);

                                            break;
                                        }
                                    // running into a special character signals the end of a previous grouping of normal characters
                                    default:
                                        {
                                            foundEnd = true;

                                            break;
                                        }
                                }
                            }

                            if (foundEnd)
                            {
                                _charReader.Putback();
                            }

                            break;
                        }
                    // $45.56
                    // $IDENTITY
                    case '$':
                        {
                            if (_charReader.Read())
                            {
                                if (
                                    _charReader.Current == '-' ||
                                    _charReader.Current == '+' ||
                                    _charReader.Current == '.' ||
                                    _charReader.Current == '0' ||
                                    _charReader.Current == '1' ||
                                    _charReader.Current == '2' ||
                                    _charReader.Current == '3' ||
                                    _charReader.Current == '4' ||
                                    _charReader.Current == '5' ||
                                    _charReader.Current == '6' ||
                                    _charReader.Current == '7' ||
                                    _charReader.Current == '8' ||
                                    _charReader.Current == '9'
                                    )
                                {
                                    _charReader.Putback();

                                    goto case '£';
                                }
                                else
                                {
                                    _charReader.Putback();

                                    goto default;
                                }
                            }

                            break;
                        }
                    // other Unicode currency symbols recognized by SSMS
                    case '£':
                    case '¢':
                    case '¤':
                    case '¥':
                    case '€':
                    case '₡':
                    case '₱':
                    case '﷼':
                    case '₩':
                    case '₮':
                    case '₨':
                    case '₫':
                    case '฿':
                    case '៛':
                    case '₪':
                    case '₭':
                    case '₦':
                    case '৲':
                    case '৳':
                    case '﹩':
                    case '₠':
                    case '₢':
                    case '₣':
                    case '₤':
                    case '₥':
                    case '₧':
                    case '₯':
                    case '₰':
                    case '$':
                    case '¢':
                    case '£':
                    case '¥':
                    case '₩':
                        {
                            bool foundEnd = false;
                            bool foundPeriod = false;

                            if (_charReader.Read())
                            {
                                switch (_charReader.Current)
                                {
                                    case '-':
                                    case '+':
                                        {
                                            characterHolder.Append(_charReader.Current);

                                            break;
                                        }
                                    default:
                                        {
                                            _charReader.Putback();

                                            break;
                                        }
                                }
                            }

                            while (
                                !foundEnd &&
                                _charReader.Read())
                            {
                                switch (_charReader.Current)
                                {
                                    case '.':
                                        {
                                            if (foundPeriod)
                                            {
                                                foundEnd = true;
                                            }
                                            else
                                            {
                                                characterHolder.Append(_charReader.Current);

                                                foundPeriod = true;
                                            }

                                            break;
                                        }
                                    case '0':
                                    case '1':
                                    case '2':
                                    case '3':
                                    case '4':
                                    case '5':
                                    case '6':
                                    case '7':
                                    case '8':
                                    case '9':
                                        {
                                            characterHolder.Append(_charReader.Current);

                                            break;
                                        }
                                    default:
                                        {
                                            foundEnd = true;

                                            break;
                                        }
                                }
                            }

                            if (foundEnd)
                            {
                                _charReader.Putback();
                            }

                            break;
                        }
                    default:
                        {
                            bool foundEnd = false;

                            while (
                                !foundEnd &&
                                _charReader.Read())
                            {
                                switch (_charReader.Current)
                                {
                                    // running into a special character signals the end of a previous grouping of normal characters
                                    case ' ':
                                    case '\t':
                                    case '\r':
                                    case '\n':
                                    case '.':
                                    case ',':
                                    case ';':
                                    case '(':
                                    case ')':
                                    case '+':
                                    case '-':
                                    case '*':
                                    case '=':
                                    case '/':
                                    case '<':
                                    case '!':
                                    case '%':
                                    case '^':
                                    case '&':
                                    case '|':
                                    case '~':
                                    case ':':
                                    case '[':
                                        {
                                            foundEnd = true;

                                            break;
                                        }
                                    default:
                                        {
                                            characterHolder.Append(_charReader.Current);

                                            break;
                                        }
                                }
                            }

                            if (foundEnd)
                            {
                                _charReader.Putback();
                            }

                            break;
                        }
                }
            }

            _current = DetermineTokenType(
                characterHolder.ToString(),
                startPosition,
                startPosition + characterHolder.Length - 1);
        }
Esempio n. 11
0
        private bool Equals(TSQLToken obj)
        {
            // If parameter is null, return false.
            if (Object.ReferenceEquals(obj, null))
            {
                return false;
            }

            // Optimization for a common success case.
            if (Object.ReferenceEquals(this, obj))
            {
                return true;
            }

            // If run-time types are not exactly the same, return false.
            if (this.GetType() != obj.GetType())
                return false;

            // Return true if the fields match.
            // Note that the base class is not invoked because it is
            // System.Object, which defines Equals as reference equality.
            return
                BeginPostion == obj.BeginPostion &&
                EndPosition == obj.EndPosition &&
                Text == obj.Text;
        }