Exemplo n.º 1
0
 public static FilterQuery Compile(string query)
 {
     try
     {
         var tokens = Tokenizer.Tokenize(query).ToArray();
         // (from (sources)) (where (filters))
         var first = tokens.FirstOrDefault();
         if (first.IsMatchTokenLiteral("from"))
         {
             // compile with sources && predicates
             var sourceSequence = tokens.Skip(1).TakeWhile(t => !t.IsMatchTokenLiteral("where"));
             var sources = CompileSources(sourceSequence).ToArray();
             var predicateSequence = tokens.SkipWhile(t => !t.IsMatchTokenLiteral("where")).Skip(1).ToArray();
             if (predicateSequence.Length == 0)
             {
                 // without predicates
                 return new FilterQuery { Sources = sources, PredicateTreeRoot = new FilterExpressionRoot() };
             }
             var filters = CompileFilters(predicateSequence);
             return new FilterQuery { Sources = sources, PredicateTreeRoot = filters };
         }
         if (first.IsMatchTokenLiteral("where"))
         {
             // compile without sources
             var sources = new FilterSourceBase[] { new FilterLocal() }; // implicit "from all"
             var filters = CompileFilters(tokens.Skip(1));
             return new FilterQuery { Sources = sources, PredicateTreeRoot = filters };
         }
         throw new FormatException(QueryCompilerResources.QueryMustBeStartedWithFromOrWhere);
     }
     catch (FilterQueryException)
     {
         throw;
     }
     catch (TargetInvocationException ex)
     {
         if (ex.InnerException is FilterQueryException)
         {
             throw ex.InnerException;
         }
         throw new FilterQueryException(
             QueryCompilerResources.QueryCompileFailed + " " +
             ex.InnerException.Message, query, ex.InnerException);
     }
     catch (Exception ex)
     {
         throw new FilterQueryException(
             QueryCompilerResources.QueryCompileFailed + " " +
             ex.Message, query, ex);
     }
 }
Exemplo n.º 2
0
 public static FilterQuery Compile(string query)
 {
     try
     {
         Token[] tokens = Tokenizer.Tokenize(query).ToArray();
         // (from (sources)) (where (filters))
         Token first = tokens.FirstOrDefault();
         if (first.Type == TokenType.Literal &&
             first.Value.Equals("from", StringComparison.CurrentCultureIgnoreCase))
         {
             // compile with sources && predicates
             FilterSourceBase[] sources =
                 CompileSources(tokens.Skip(1).TakeWhile(t => t.Type != TokenType.Literal || t.Value != "where"))
                     .ToArray();
             if (!tokens.Skip(1).SkipWhile(t => t.Type != TokenType.Literal || t.Value != "where").Any())
             {
                 // without predicates
                 return new FilterQuery { Sources = sources, PredicateTreeRoot = new FilterExpressionRoot() };
             }
             FilterExpressionRoot filters =
                 CompileFilters(
                     tokens.Skip(1).SkipWhile(t => t.Type != TokenType.Literal || t.Value != "where").Skip(1));
             return new FilterQuery { Sources = sources, PredicateTreeRoot = filters };
         }
         if (first.Type == TokenType.Literal &&
             first.Value.Equals("where", StringComparison.CurrentCultureIgnoreCase))
         {
             // compile with predicates
             var sources = new FilterSourceBase[] { new FilterLocal() }; // implicit "from all"
             FilterExpressionRoot filters = CompileFilters(tokens.Skip(1));
             return new FilterQuery { Sources = sources, PredicateTreeRoot = filters };
         }
         throw new FormatException("Query must be started with \"from\" keyword or \"where\" keyword..");
     }
     catch (FilterQueryException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new FilterQueryException("Query compilation failed. " + ex.Message, query, ex);
     }
 }
Exemplo n.º 3
0
 public static FilterQuery Compile(string query)
 {
     try
     {
         var tokens = Tokenizer.Tokenize(query).ToArray();
         // (from (sources)) (where (filters))
         var first = tokens.FirstOrDefault();
         if (first.IsMatchTokenLiteral("from"))
         {
             // compile with sources && predicates
             var sourceSequence = tokens.Skip(1).TakeWhile(t => !t.IsMatchTokenLiteral("where"));
             var sources = CompileSources(sourceSequence).ToArray();
             var predicateSequence = tokens.SkipWhile(t => !t.IsMatchTokenLiteral("where")).Skip(1).ToArray();
             if (predicateSequence.Length == 0)
             {
                 // without predicates
                 return new FilterQuery { Sources = sources, PredicateTreeRoot = new FilterExpressionRoot() };
             }
             var filters = CompileFilters(predicateSequence);
             return new FilterQuery { Sources = sources, PredicateTreeRoot = filters };
         }
         if (first.IsMatchTokenLiteral("where"))
         {
             // compile without sources
             var sources = new FilterSourceBase[] { new FilterLocal() }; // implicit "from all"
             var filters = CompileFilters(tokens.Skip(1));
             return new FilterQuery { Sources = sources, PredicateTreeRoot = filters };
         }
         throw new FormatException("クエリは\"from\"キーワードか\"where\"キーワードで始まらなければなりません。");
     }
     catch (FilterQueryException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new FilterQueryException("クエリのコンパイルに失敗しました。 " + ex.Message, query, ex);
     }
 }