public static bool TryParseUntilFirstMsSqlSelectColumn(this IEnumerator<SqlToken> tokenEnum, out SqlToken selectToken, out bool isDistinct)
		{
			selectToken = null;
			isDistinct = false;

			while (tokenEnum.TryParseUntil("select"))
			{
				selectToken = tokenEnum.Current;
				if (!tokenEnum.MoveNext()) return false;

				// [ DISTINCT | ALL ]
				if (tokenEnum.Current.Equals("distinct", StringComparison.InvariantCultureIgnoreCase))
				{
					isDistinct = true;
					if (!tokenEnum.MoveNext()) return false;
				}
				else if	(tokenEnum.Current.Equals("all", StringComparison.InvariantCultureIgnoreCase))
				{
					if (!tokenEnum.MoveNext()) return false;
				}

				// [ TOP { integer | ( expression ) } [PERCENT] [ WITH TIES ] ] 
				if (tokenEnum.Current.Equals("top", StringComparison.InvariantCultureIgnoreCase))
				{
					if (!tokenEnum.MoveNext()) return false;
					if (tokenEnum.Current.TokenType == SqlTokenType.BracketOpen)
					{
						do
						{
							if (!tokenEnum.MoveNext()) return false;
						} while (tokenEnum.Current.TokenType != SqlTokenType.BracketClose);
					}
					if (!tokenEnum.MoveNext()) return false;

					if (tokenEnum.Current.Equals("percent", StringComparison.InvariantCultureIgnoreCase))
					{
						if (!tokenEnum.MoveNext()) return false;
					}
					if (tokenEnum.Current.Equals("with", StringComparison.InvariantCultureIgnoreCase))
					{
						if (!tokenEnum.MoveNext()) return false;
						if (tokenEnum.Current.Equals("ties", StringComparison.InvariantCultureIgnoreCase))
						{
							if (!tokenEnum.MoveNext()) return false;
						}
					}
				}

				if (!tokenEnum.Current.Value.StartsWith("@")) return true;
			}

			return false;
		}
Exemplo n.º 2
0
		private bool TryGetColumnDefinition(string columnNameOrIndex, out ColumnDefinition result, SqlToken beginToken, SqlToken endToken)
		{
			if (!string.IsNullOrEmpty(columnNameOrIndex))
			{
				int columnIndex;
				if (int.TryParse(columnNameOrIndex, out columnIndex) && columnIndex <= _columns.Count)
				{
					result = _columns[columnIndex - 1];
					return true;
				}

				foreach (var column in _columns)
				{
					if (columnNameOrIndex.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase)
						|| columnNameOrIndex.Equals(column.Alias, StringComparison.InvariantCultureIgnoreCase))
					{
						result = column;
						return true;
					}
				}
			}
			else
			{
				var sqlIndex = beginToken.SqlIndex;
				var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;
				var text = Sql.Substring(sqlIndex, sqlLength).ToString();
				foreach (var column in _columns)
				{
					if (text.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase) ||
						text.Equals(column.Alias, StringComparison.InvariantCultureIgnoreCase) ||
						text.Equals(Sql.Substring(column.SqlIndex, column.SqlLength).ToString(), StringComparison.InvariantCultureIgnoreCase))
					{
						result = column;
						return true;
					}
				}
			}

			result = null;
			return false;
		}
Exemplo n.º 3
0
		private OrderDefinition ParseOrderDefinition(SqlToken beginToken, SqlToken endToken, SqlToken directionToken)
		{
			var isDescending = directionToken != null &&
							   directionToken.Equals("desc", StringComparison.InvariantCultureIgnoreCase);

			var columnNameOrIndex = beginToken == endToken
				? beginToken.Value
				: null;

			ColumnDefinition column;
			if (!TryGetColumnDefinition(columnNameOrIndex, out column, beginToken, endToken))
			{   
				// Column appears in order by clause, but not in select clause
				column = ParseOrderColumnDefinition(beginToken, endToken, "__c" + _nextOrderAliasIndex++);
			}

			return new OrderDefinition(column, isDescending);
		}
Exemplo n.º 4
0
		private ColumnDefinition ParseOrderColumnDefinition(SqlToken beginToken, SqlToken endToken, string alias)
		{
			var sqlIndex = beginToken.SqlIndex;
			var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;

			return new ColumnDefinition(sqlIndex, sqlLength, null, alias, false);
		}
Exemplo n.º 5
0
		private ColumnDefinition ParseSelectColumnDefinition(SqlToken beginToken, SqlToken endToken, SqlToken aliasToken)
		{
			var name = beginToken == endToken
				? beginToken.Value
				: null;

			var alias = aliasToken.Value;
			var dotIndex = alias.LastIndexOf('.');
			alias = dotIndex >= 0
				? alias.Substring(dotIndex + 1)
				: alias;

			var sqlIndex = beginToken.SqlIndex;
			var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;

			return new ColumnDefinition(sqlIndex, sqlLength, name, alias, true);
		}
		public static bool TryParseUntilFirstOrderColumn(this IEnumerator<SqlToken> tokenEnum, out SqlToken orderToken)
		{
			if (tokenEnum.TryParseUntil("order"))
			{
				orderToken = tokenEnum.Current;
				if (tokenEnum.MoveNext())
				{
					return tokenEnum.Current.Equals("by", StringComparison.InvariantCultureIgnoreCase)
						? tokenEnum.MoveNext()
						: false;
				}
			}

			orderToken = null;
			return false;
		}
Exemplo n.º 7
0
        private OrderDefinition ParseOrderDefinition(SqlToken beginToken, SqlToken endToken, SqlToken directionToken)
        {
            var isDescending = directionToken != null &&
                               directionToken.Equals("desc", StringComparison.InvariantCultureIgnoreCase);

            var columnNameOrIndex = beginToken == endToken
                                ? beginToken.Value
                                : null;

            ColumnDefinition column;

            if (!TryGetColumnDefinition(columnNameOrIndex, out column, beginToken, endToken))
            {
                // Column appears in order by clause, but not in select clause
                column = ParseOrderColumnDefinition(beginToken, endToken, "__c" + _nextOrderAliasIndex++);
            }

            return(new OrderDefinition(column, isDescending));
        }
Exemplo n.º 8
0
        private bool TryGetColumnDefinition(string columnNameOrIndex, out ColumnDefinition result, SqlToken beginToken, SqlToken endToken)
        {
            if (!string.IsNullOrEmpty(columnNameOrIndex))
            {
                int columnIndex;
                if (int.TryParse(columnNameOrIndex, out columnIndex) && columnIndex <= _columns.Count)
                {
                    result = _columns[columnIndex - 1];
                    return(true);
                }

                foreach (var column in _columns)
                {
                    if (columnNameOrIndex.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase) ||
                        columnNameOrIndex.Equals(column.Alias, StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = column;
                        return(true);
                    }
                }
            }
            else
            {
                var sqlIndex  = beginToken.SqlIndex;
                var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;
                var text      = Sql.ToString(sqlIndex, sqlLength);
                foreach (var column in _columns)
                {
                    if (text.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase) ||
                        text.Equals(column.Alias, StringComparison.InvariantCultureIgnoreCase) ||
                        text.Equals(Sql.ToString(column.SqlIndex, column.SqlLength), StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = column;
                        return(true);
                    }
                }
            }

            result = null;
            return(false);
        }
Exemplo n.º 9
0
        private IEnumerable <OrderDefinition> ParseOrderDefinitions(IEnumerator <SqlToken> tokenEnum)
        {
            int      blockLevel      = 0;
            SqlToken orderBeginToken = null;
            SqlToken orderEndToken   = null;
            SqlToken directionToken  = null;

            SqlToken prevToken = null;

            do
            {
                var token = tokenEnum.Current;
                if (token == null)
                {
                    break;
                }
                if (token.TokenType == SqlTokenType.Whitespace)
                {
                    continue;
                }

                orderBeginToken = orderBeginToken ?? token;

                switch (token.TokenType)
                {
                case SqlTokenType.BracketOpen:
                    blockLevel++;
                    break;

                case SqlTokenType.BracketClose:
                    blockLevel--;
                    break;

                case SqlTokenType.Text:
                    if (blockLevel != 0)
                    {
                        break;
                    }

                    if (token.Equals("asc", StringComparison.InvariantCultureIgnoreCase) ||
                        token.Equals("desc", StringComparison.InvariantCultureIgnoreCase))
                    {
                        orderEndToken  = prevToken;
                        directionToken = token;
                    }
                    break;

                case SqlTokenType.DelimitedText:
                    if (blockLevel != 0)
                    {
                        break;
                    }
                    break;

                case SqlTokenType.Comma:
                    if (blockLevel != 0)
                    {
                        break;
                    }

                    yield return(ParseOrderDefinition(orderBeginToken, orderEndToken ?? prevToken, directionToken));

                    orderBeginToken = orderEndToken = directionToken = null;
                    break;
                }

                prevToken = token;
            } while (tokenEnum.MoveNext());

            if (orderBeginToken != null)
            {
                yield return(ParseOrderDefinition(orderBeginToken, orderEndToken ?? prevToken, directionToken));
            }
        }
Exemplo n.º 10
0
        private ColumnDefinition ParseSelectColumnDefinition(SqlToken beginToken, SqlToken endToken, SqlToken aliasToken)
        {
            var name = beginToken == endToken
                                ? beginToken.Value
                                : null;

            var alias    = aliasToken.Value;
            var dotIndex = alias.LastIndexOf('.');

            alias = dotIndex >= 0
                                ? alias.Substring(dotIndex + 1)
                                : alias;

            var sqlIndex  = beginToken.SqlIndex;
            var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;

            return(new ColumnDefinition(sqlIndex, sqlLength, name, alias, true));
        }
Exemplo n.º 11
0
        private IEnumerable <ColumnDefinition> ParseColumnDefinitions(IEnumerator <SqlToken> tokenEnum)
        {
            int      blockLevel       = 0;
            SqlToken columnBeginToken = null;
            SqlToken columnEndToken   = null;
            SqlToken columnAliasToken = null;

            SqlToken prevToken = null;

            do
            {
                var token = tokenEnum.Current;
                if (token == null)
                {
                    break;
                }

                columnBeginToken = columnBeginToken ?? token;

                switch (token.TokenType)
                {
                case SqlTokenType.BracketOpen:
                    blockLevel++;
                    break;

                case SqlTokenType.BracketClose:
                    blockLevel--;
                    break;

                case SqlTokenType.Text:
                    if (blockLevel != 0)
                    {
                        break;
                    }

                    if (token.Equals(",", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (columnAliasToken != null)
                        {
                            yield return(ParseSelectColumnDefinition(columnBeginToken, columnEndToken ?? columnAliasToken, columnAliasToken));
                        }
                    }

                    if (token.Equals("from", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (columnAliasToken != null)
                        {
                            yield return(ParseSelectColumnDefinition(columnBeginToken, columnEndToken ?? columnAliasToken, columnAliasToken));
                        }
                        yield break;
                    }

                    if (token.Equals("as", StringComparison.InvariantCultureIgnoreCase))
                    {
                        columnEndToken = prevToken;
                    }

                    columnAliasToken = token;
                    break;

                case SqlTokenType.DelimitedText:
                    if (blockLevel != 0)
                    {
                        break;
                    }

                    columnAliasToken = token;
                    break;

                case SqlTokenType.Comma:
                    if (blockLevel != 0)
                    {
                        break;
                    }

                    if (columnAliasToken != null)
                    {
                        yield return(ParseSelectColumnDefinition(columnBeginToken, columnEndToken ?? columnAliasToken, columnAliasToken));
                    }
                    columnBeginToken = columnEndToken = columnAliasToken = null;
                    break;
                }

                prevToken = token;
            } while (tokenEnum.MoveNext());

            if (columnAliasToken != null)
            {
                yield return(ParseSelectColumnDefinition(columnBeginToken, columnEndToken ?? columnAliasToken, columnAliasToken));
            }
        }
        public static bool TryParseUntilFirstMsSqlSelectColumn(this IEnumerator <SqlToken> tokenEnum, out SqlToken selectToken, out bool isDistinct)
        {
            selectToken = null;
            isDistinct  = false;

            while (tokenEnum.TryParseUntil("select"))
            {
                selectToken = tokenEnum.Current;
                if (!tokenEnum.MoveNext())
                {
                    return(false);
                }

                // [ DISTINCT | ALL ]
                if (tokenEnum.Current.Equals("distinct", StringComparison.OrdinalIgnoreCase))
                {
                    isDistinct = true;
                    if (!tokenEnum.MoveNext())
                    {
                        return(false);
                    }
                }
                else if (tokenEnum.Current.Equals("all", StringComparison.OrdinalIgnoreCase))
                {
                    if (!tokenEnum.MoveNext())
                    {
                        return(false);
                    }
                }

                // [ TOP { integer | ( expression ) } [PERCENT] [ WITH TIES ] ]
                if (tokenEnum.Current.Equals("top", StringComparison.OrdinalIgnoreCase))
                {
                    if (!tokenEnum.MoveNext())
                    {
                        return(false);
                    }
                    if (tokenEnum.Current.TokenType == SqlTokenType.BracketOpen)
                    {
                        do
                        {
                            if (!tokenEnum.MoveNext())
                            {
                                return(false);
                            }
                        } while (tokenEnum.Current.TokenType != SqlTokenType.BracketClose);
                    }
                    if (!tokenEnum.MoveNext())
                    {
                        return(false);
                    }

                    if (tokenEnum.Current.Equals("percent", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!tokenEnum.MoveNext())
                        {
                            return(false);
                        }
                    }
                    if (tokenEnum.Current.Equals("with", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!tokenEnum.MoveNext())
                        {
                            return(false);
                        }
                        if (tokenEnum.Current.Equals("ties", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!tokenEnum.MoveNext())
                            {
                                return(false);
                            }
                        }
                    }
                }

                if (!tokenEnum.Current.Value.StartsWith('@'))
                {
                    return(true);
                }
            }

            return(false);
        }
        public static bool TryParseUntilFirstOrderColumn(this IEnumerator <SqlToken> tokenEnum, out SqlToken orderToken)
        {
            if (tokenEnum.TryParseUntil("order"))
            {
                orderToken = tokenEnum.Current;
                if (tokenEnum.MoveNext())
                {
                    return(tokenEnum.Current.Equals("by", StringComparison.InvariantCultureIgnoreCase)
                                                ? tokenEnum.MoveNext()
                                                : false);
                }
            }

            orderToken = null;
            return(false);
        }