QueryToken

We found 10 examples in language CSharp for this search. You will see 30 fragments of code.

		public static string ToSql(string odataFilter, bool isTableQuery, bool enableTimestampQuery = false)
		{
			if (string.IsNullOrEmpty(odataFilter))
			{
				throw new ArgumentException("odataFilter");
			}
			QueryToken queryToken = new UriQueryExpressionParser(1000).ParseFilter(odataFilter);
			QueryTokenVisitor queryTokenVisitor = new QueryTokenVisitor(isTableQuery, enableTimestampQuery);
			queryToken.Accept(queryTokenVisitor);
			return queryTokenVisitor.SqlFilter;
		}


        [Fact]
        public void SingleWordYieldsOneResult()
        {
            this.sut.ParseQueryTokens("Testing").Should().BeEquivalentTo(
                QueryToken.ForText("Testing"));
        }

        [Fact]
        public void SingleWordWithSpacePaddingYieldsOneResult()
        {
            this.sut.ParseQueryTokens("  \t  Testing   \t ").Should().BeEquivalentTo(
                QueryToken.ForText("Testing"));
        }

        [Fact]
        public void CompositeStringYieldsOneResult()
        {
            this.sut.ParseQueryTokens("\"Jack be quick\"").Should().BeEquivalentTo(
                QueryToken.ForOperator(QueryTokenType.BeginAdjacentTextOperator),
                QueryToken.ForText("Jack"),
                QueryToken.ForText("be"),
                QueryToken.ForText("quick"),
                QueryToken.ForOperator(QueryTokenType.EndAdjacentTextOperator));
        }


        /// <summary>
        /// Creates a new <see cref="QueryToken"/> instance representing a textual part of the query.
        /// </summary>
        /// <param name="text">
        /// The text to be matched by the query.
        /// </param>
        public static QueryToken ForText(string text) => new QueryToken(text, QueryTokenType.Text, 0);

        /// <summary>
        /// Creates a new <see cref="QueryToken"/> instance representing a field filter.
        /// </summary>
        /// <param name="fieldName">
        /// The name of the field to match.
        /// </param>
        public static QueryToken ForFieldFilter(string fieldName) => new QueryToken(fieldName, QueryTokenType.FieldFilter, 0);

        /// <summary>
        /// Creates a new <see cref="QueryToken"/> instance representing a query operator.
        /// </summary>
        /// <param name="operatorType">
        /// The type of operator the token should represent.
        /// </param>
        public static QueryToken ForOperator(QueryTokenType operatorType) => new QueryToken(string.Empty, operatorType, 0);

        /// <inheritdoc />
        public override bool Equals(object obj)
        {
            return obj is QueryToken token &&
                   this.Equals(token);
        }

        /// <inheritdoc />
        public bool Equals(QueryToken other)
        {
            return this.TokenText == other.TokenText &&
                   this.TokenType == other.TokenType &&
                   this.Tolerance == other.Tolerance;
        }

//
// QueryToken.cs
//
// Author:
//   Aaron Bockover <[email protected]>
//
// Copyright (C) 2007 Novell, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

namespace Meshwork.Library.Hyena.Query
{
    public enum TokenID {
        Unknown,
        OpenParen,
        CloseParen,
        Not,
        Or,
        And,
        Range,
        Term
    }
    
    public class QueryToken
    {
        private TokenID id;
        private int line;
        private int column;
        private string term;

        public QueryToken()
        {
        }

        public QueryToken(string term)
        {
            id = TokenID.Term;
            this.term = term;
        }

        public QueryToken(TokenID id)
        {
            this.id = id;
        }

        public QueryToken(TokenID id, int line, int column)
        {
            this.id = id;
            this.line = line;
            this.column = column;
        }

        public TokenID ID {
            get { return id; }
            set { id = value; }
        }

        public int Line {
            get { return line; }
            set { line = value; }
        }

        public int Column {
            get { return column; }
            set { column = value; }
        }

        public string Term {
            get { return term; }
            set { term = value; }
        }
    }
}


        public IExpression BeginsWith(string value)
        {
            this._builder.WriteBinaryOperation(this._startIndex, "BeginsWith", this._valueType, value);
            return new QueryToken(this._builder, this._startIndex);
        }

        public IExpression Contains(string value)
        {
            this._builder.WriteBinaryOperation(this._startIndex, "Contains", this._valueType, value);
            return new QueryToken(this._builder, this._startIndex);
        }

        public IExpression EqualTo(bool value)
        {
            this._builder.WriteBinaryOperation(this._startIndex, "Eq", this._valueType, value ? "1" : "0");
            return new QueryToken(this._builder, this._startIndex);
        }

        public IExpression EqualTo(int value)
        {
            this._builder.WriteBinaryOperation(this._startIndex, "Eq", this._valueType, "" + value);
            return new QueryToken(this._builder, this._startIndex);
        }

        public IExpression EqualTo(string value)
        {
            this._builder.WriteBinaryOperation(this._startIndex, "Eq", this._valueType, value);
            return new QueryToken(this._builder, this._startIndex);
        }

        public IExpression EqualTo(DateTime value)
        {
            var date = value.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");

            this._builder.WriteBinaryOperation(this._startIndex, "Eq", this._valueType, date);
            return new QueryToken(this._builder, this._startIndex);
        }

    //public Dictionary<string, Func<string, string>> MacroGenerators {get;set;} = new Dictionary<string, Func<string, string>>();

    [Production("expr: filter AND expr")]
    [Production("expr: filter OR expr")]
    public string ExpressionLogic(string filter, Token<QueryToken> op, string expr)
    {
        return $"{filter} {op.Value} {expr}";
    }

    [Production("filter: FIELD op VALUE")]
    public string Filter(Token<QueryToken> fieldToken, string op, Token<QueryToken> val)
    {
        return $"{HandleField(fieldToken.Value)} {op} {HandleValue(val.Value)}";
    }

    [Production("filter: MACROSTART [d] FIELD LPAREN [d] macroend")]
    public string FilterMacro(Token<QueryToken> macroNameToken, string macroend)
    {
        return HandleMacro(macroNameToken.Value, macroend);
    }

    //This is that loop-back thing I don't understand. This was ambiguous when this
    //production was derived from "expr" rather than filter, but... ugh I'm not smart
    [Production("filter: LPAREN expr RPAREN")]
    public string FilterGroup(Token<QueryToken> lparen, string expr, Token<QueryToken> rparen)
    {
        return $"{lparen.Value}{expr}{rparen.Value}";
    }

    [Production("arglist: FIELD COMMA arglist")]
    [Production("arglist: VALUE COMMA arglist")]
    public string ArglistList(Token<QueryToken> field, Token<QueryToken> comma, string rest)
    {
        return $"{field.Value}{comma.Value}{rest}";
    }

    [Production("arglist: FIELD")]
    [Production("arglist: VALUE")]
    public string ArglistSingle(Token<QueryToken> field)
    {
        return field.Value;
    }


        /// <summary>
        /// Parses the expression.
        /// </summary>
        /// <returns>The lexical token representing the expression.</returns>
        private QueryToken ParseExpression()
        {
            this.RecurseEnter();
            QueryToken token = this.ParseLogicalOr();
            this.RecurseLeave();
            return token;
        }


            private static QueryToken ReadSingleCharacterToken(QuerySyntaxKind kind, ref int position, string text)
            {
                var span = new TextSpan(position, 1);
                var token = new QueryToken(kind, text, span, null);
                position += span.Length;
                return token;
            }

            private static QueryToken ReadWhitespace(ref int position, string text)
            {
                var start = position;

                while (position < text.Length && char.IsWhiteSpace(text[position]))
                    position++;

                var length = position - start;
                var span = new TextSpan(start, length);
                return new QueryToken(QuerySyntaxKind.WhitespaceToken, text, span, null);
            }


        public async Task<List<Story>> GetContent(string topic)
        {
            List<Story> result = new List<Story>();

            // Check that we have a topic or return empty list
            if (topic == null)
            {
                return result;
            }

            // Isolate topic query to ensure we have a single term
            string queryToken = topic.Split(new char[] { '&' }).FirstOrDefault();

            // Submit async query requests and process responses in parallel
            List<Story>[] queryResults = await Task.WhenAll(
                ExecuteDiggQuery(queryToken),
                ExecuteDeliciousQuery(queryToken));

            // Aggregate results from digg and delicious
            foreach (List<Story> queryResult in queryResults)
            {
                result.AddRange(queryResult);
            }

            return result;
        }

        private static async Task<List<Story>> ExecuteDiggQuery(string queryToken)
        {
            List<Story> result = new List<Story>();

            // URI query for a basic digg query -- see http://developers.digg.com/documentation
            string query = string.Format("http://services.digg.com/2.0/search.search?query={0}", queryToken);

            // Submit async request 
            HttpResponseMessage diggResponse = await _client.GetAsync(query);

            // Read result using JToken and create response
            if (diggResponse.IsSuccessStatusCode && IsJson(diggResponse.Content))
            {
                JToken diggResult = await diggResponse.Content.ReadAsAsync<JToken>();
                foreach (var story in diggResult["stories"] as JArray)
                {
                    result.Add(new Story
                    {
                        Source = "digg",
                        Description = story["title"].Value<string>()
                    });
                }
            }

            return result;
        }

        private static async Task<List<Story>> ExecuteDeliciousQuery(string queryToken)
        {
            List<Story> result = new List<Story>();

            // URI query for a basic delicious query -- see http://delicious.com/developers
            string query = string.Format("http://feeds.delicious.com/v2/json/tag/{0}", queryToken);

            // Submit async request 
            HttpResponseMessage deliciousResponse = await _client.GetAsync(query);

            // Read result using JToken and create response
            if (deliciousResponse.IsSuccessStatusCode && IsJson(deliciousResponse.Content))
            {
                JArray deliciousResult = await deliciousResponse.Content.ReadAsAsync<JArray>();
                foreach (var story in deliciousResult)
                {
                    result.Add(new Story
                    {
                        Source = "delicious",
                        Description = story["d"].Value<string>()
                    });
                }
            }

            return result;
        }

        private static bool IsJson(HttpContent content)
        {
            if (content == null || content.Headers == null || content.Headers.ContentType == null)
            {
                return false;
            }

            MediaTypeHeaderValue contentType = content.Headers.ContentType;
            return
                contentType.MediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase) ||
                contentType.MediaType.Equals("text/json", StringComparison.OrdinalIgnoreCase);
        }


        public override QueryNodeOrToken[] GetChildren()
        {
            return new[] { KeyToken, ColonToken, ValueToken };
        }

Microsoft.OData.UriParser.QueryToken : Object

Fields :

public static QueryToken[] EmptyTokens

Methods :

public abstract QueryTokenKind get_Kind()
public abstract T Accept(ISyntacticTreeVisitor<T> visitor = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()