Ejemplo n.º 1
0
        public static void ToStringExpr(FilterTree tree)
        {
            if (tree.Type == SQLFilterLexer.STRING_EXPR || !tree.IsLeaf)
            {
                return;
            }

            FilterTree op = (FilterTree)tree.Children[0];

            op.Type = StringPatternLexer.IS;
            if (!(op is StringTree))
            {
                tree.SetChild(0, new StringTree(op));
            }
            // do conversions
            FilterTree constant = (FilterTree)tree.Children[0].GetChild(1);

            constant.Type = StringPatternLexer.TEXT;
            constant.Text = "";
            if (!(constant is StringTree))
            {
                tree.Children[0].SetChild(1, new StringTree(constant));
            }
            tree.Type = SQLFilterLexer.STRING_EXPR;
            ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty();
        }
Ejemplo n.º 2
0
        private static Expression compileEnum(FilterTree node, ParameterExpression param)
        {
            Expression property = ((FilterTree)node.Children[0]).Compile(param);
            string     enumText = node.Children[1].Text;
            object     parsed;

            try
            {
                parsed = Enum.Parse(property.Type, enumText, false);
            }
            catch
            {
                // HACK ALERT: It's kinda late for fixing the tree but whatever
                ((FilterTree)node.Children[1]).Text = Enum.GetNames(property.Type)[0];
                parsed = Enum.GetValues(property.Type).GetValue(0);
            }

            switch (node.Type)
            {
            case SQLFilterLexer.EQUALS:
                return(Expression.Equal(
                           property,
                           Expression.Constant(parsed, property.Type)));

            case SQLFilterLexer.NOTEQUALS:
                return(Expression.NotEqual(
                           property,
                           Expression.Constant(parsed, property.Type)));
            }
            return(null);
        }
Ejemplo n.º 3
0
        private static void changeCommonLeafNodeType(FilterTree tree, int newType, int newOp, int newConst, string newValue)
        {
            if (tree.Type == newType || !tree.IsLeaf)
            {
                return;
            }
            FilterTree op = (FilterTree)tree.Children[0];

            op.Type = newOp;
            if (!(op is SQLTree))
            {
                tree.SetChild(0, new SQLTree(op));
            }
            // do conversions
            FilterTree constant = (FilterTree)tree.Children[0].GetChild(1);

            constant.Type = newConst;
            constant.Text = newValue;
            if (!(constant is SQLTree))
            {
                tree.Children[0].SetChild(1, new SQLTree(constant));
            }
            tree.Type = newType;
            ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty();
        }
Ejemplo n.º 4
0
        private void parseQuery(string newQuery)
        {
            if (root != null)
            {
                root.PropertyChanged -= rootPropertyChanged;
            }
            var stream = new ANTLRStringStream(newQuery);
            var lexer  = new SQLFilterLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new SQLFilterParser(tokens);

            try
            {
                root = parser.prog().Tree;
                NormalizeNodes((SQLTree)root);
                compileTree();
                query = root.ToString();
                OnPropertyChanged("Tree");
                OnPropertyChanged("Function");
            }
            catch
            {
                throw;
            }
            finally
            {
                root.PropertyChanged += rootPropertyChanged;
                treeDirty             = false;
            }
            OnPropertyChanged("Query");
        }
 // We want leafs to be grouped inside AND. leaf -> ^(AND leaf)
 private FilterTree groupify(FilterTree tree)
 {
     if (!isGroupNode(tree))
     {
         return((FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.AND), tree));
     }
     return(tree);
 }
Ejemplo n.º 6
0
 public FilterTree(FilterTree t)
     : base(t)
 {
     this.SubType = t.SubType;
     if (t.IsLeaf)
         this.IsLeaf = true;
     if (t.IsDirty)
         this.IsDirty = true;
     if(t.Children != null)
         this.AddChildren(t.Children);
 }
 private FilterTree rewriteWithNot(FilterTree tree)
 {
     switch(tree.Type)
     {
         case SQLFilterLexer.AND:
             tree.Token.Type = SQLFilterLexer.NOT_AND;
             return tree;
         case SQLFilterLexer.OR:
             tree.Token.Type = SQLFilterLexer.NOT_OR;
             return tree;
     }
     return (FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.NOT), tree);
 }
        private FilterTree rewriteWithNot(FilterTree tree)
        {
            switch (tree.Type)
            {
            case SQLFilterLexer.AND:
                tree.Token.Type = SQLFilterLexer.NOT_AND;
                return(tree);

            case SQLFilterLexer.OR:
                tree.Token.Type = SQLFilterLexer.NOT_OR;
                return(tree);
            }
            return((FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.NOT), tree));
        }
Ejemplo n.º 9
0
 //Stolen from antlr runtime
 private static string printNode(FilterTree tree)
 {
     if (tree.IsNil)
     {
         return("nil");
     }
     if (tree.Type == 0)
     {
         return("<errornode>");
     }
     if (tree.Token == null)
     {
         return(string.Empty);
     }
     return(tree.Text);
 }
Ejemplo n.º 10
0
 public FilterTree(FilterTree t)
     : base(t)
 {
     this.SubType = t.SubType;
     if (t.IsLeaf)
     {
         this.IsLeaf = true;
     }
     if (t.IsDirty)
     {
         this.IsDirty = true;
     }
     if (t.Children != null)
     {
         this.AddChildren(t.Children);
     }
 }
Ejemplo n.º 11
0
 private FilterTree reverseOperator(FilterTree tree)
 {
     switch(tree.Type)
     {
         case SQLFilterParser.GREATER:
             tree.Token.Type = SQLFilterLexer.LESSER;
             break;
         case SQLFilterParser.GREATEROREQUALS:
             tree.Token.Type = SQLFilterParser.LESSEROREQUALS;
             break;
         case SQLFilterParser.LESSER:
             tree.Token.Type = SQLFilterParser.GREATER;
             break;
         case SQLFilterParser.LESSEROREQUALS:
             tree.Token.Type = SQLFilterParser.GREATEROREQUALS;
             break;
     }
     return tree;
 }
Ejemplo n.º 12
0
        private static Expression compileIP(FilterTree node, ParameterExpression param)
        {
            switch (node.Type)
            {
            case SQLFilterLexer.LESSER:
                return(Expression.Call(
                           typeof(IPExtension).GetMethod("LessThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                           new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }));

            case SQLFilterLexer.LESSEROREQUALS:
                return(Expression.Call(
                           typeof(IPExtension).GetMethod("LessThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                           new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }));

            case SQLFilterLexer.GREATER:
                return(Expression.Call(
                           typeof(IPExtension).GetMethod("GreaterThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                           new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }));

            case SQLFilterLexer.GREATEROREQUALS:
                return(Expression.Call(
                           typeof(IPExtension).GetMethod("GreaterThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                           new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) }));

            case SQLFilterLexer.EQUALS:
                return(Expression.Call(
                           ((FilterTree)node.Children[0]).Compile(param),
                           typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null),
                           new Expression[] { ((FilterTree)node.Children[1]).Compile(param) }));

            case SQLFilterLexer.NOTEQUALS:
                return(Expression.Not(
                           Expression.Call(
                               ((FilterTree)node.Children[0]).Compile(param),
                               typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null),
                               new Expression[] { ((FilterTree)node.Children[1]).Compile(param) })));
            }
            return(null);
        }
Ejemplo n.º 13
0
        private FilterTree reverseOperator(FilterTree tree)
        {
            switch (tree.Type)
            {
            case SQLFilterParser.GREATER:
                tree.Token.Type = SQLFilterLexer.LESSER;
                break;

            case SQLFilterParser.GREATEROREQUALS:
                tree.Token.Type = SQLFilterParser.LESSEROREQUALS;
                break;

            case SQLFilterParser.LESSER:
                tree.Token.Type = SQLFilterParser.GREATER;
                break;

            case SQLFilterParser.LESSEROREQUALS:
                tree.Token.Type = SQLFilterParser.GREATEROREQUALS;
                break;
            }
            return(tree);
        }
Ejemplo n.º 14
0
 public static void ToIPExpr(FilterTree tree)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.IP_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.IPV4, "127.0.0.1");
 }
Ejemplo n.º 15
0
 public static void ToBoolExpr(FilterTree tree)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.BOOL_EXPR, SQLFilterLexer.IS, SQLFilterLexer.TRUE, "True");
 }
Ejemplo n.º 16
0
 public StringTree(FilterTree t)
     : base(t)
 {
 }
Ejemplo n.º 17
0
 //Stolen from antlr runtime
 private static string printNode(FilterTree tree)
 {
     if (tree.IsNil)
     {
         return "nil";
     }
     if (tree.Type == 0)
     {
         return "<errornode>";
     }
     if (tree.Token == null)
     {
         return string.Empty;
     }
     return tree.Text;
 }
Ejemplo n.º 18
0
 public static void ToStringExpr(FilterTree tree)
 {
     if (tree.Type == SQLFilterLexer.STRING_EXPR || !tree.IsLeaf)
         return;
     
     FilterTree op = (FilterTree)tree.Children[0];
     op.Type = StringPatternLexer.IS;
     if (!(op is StringTree))
         tree.SetChild(0, new StringTree(op));
     // do conversions
     FilterTree constant = (FilterTree)tree.Children[0].GetChild(1);
     constant.Type = StringPatternLexer.TEXT;
     constant.Text = "";
     if (!(constant is StringTree))
         tree.Children[0].SetChild(1, new StringTree(constant));
     tree.Type = SQLFilterLexer.STRING_EXPR;
     ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty();
 }
Ejemplo n.º 19
0
 public static void ToBoolExpr(FilterTree tree)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.BOOL_EXPR, SQLFilterLexer.IS, SQLFilterLexer.TRUE, "True");
 }
Ejemplo n.º 20
0
 public static void ToEnumExpr(FilterTree tree, Type newType)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.ENUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.STRING, Enum.GetName(newType,0));
 }
Ejemplo n.º 21
0
 private FilterTree makeFirstLeftSubchild(FilterTree root, ITree child)
 {
     root.Children.Insert(0, child);
     root.FreshenParentAndChildIndexes();
     return(root);
 }
Ejemplo n.º 22
0
 public StringTree(FilterTree t)
     : base(t)
 { }
Ejemplo n.º 23
0
 // Helper function
 private static bool isGroupNode(FilterTree node)
 {
     return node.Type == SQLFilterLexer.AND || node.Type == SQLFilterLexer.OR || node.Type == SQLFilterLexer.NOT_AND || node.Type == SQLFilterLexer.NOT_OR;
 }
Ejemplo n.º 24
0
 // We want leafs to be grouped inside AND. leaf -> ^(AND leaf)
 private FilterTree groupify(FilterTree tree)
 {
     if (!isGroupNode(tree))
         return (FilterTree)adaptor.BecomeRoot(new CommonToken(SQLFilterLexer.AND), tree);
     return tree;
 }
Ejemplo n.º 25
0
 // Mark single expressions as leafs
 private static void markAsLeaf(FilterTree tree)
 {
     tree.IsLeaf = true;
 }
Ejemplo n.º 26
0
        private static Expression compileEnum(FilterTree node, ParameterExpression param)
        {
            Expression property = ((FilterTree)node.Children[0]).Compile(param);
            string enumText = node.Children[1].Text;
            object parsed;
            try
            {
                parsed = Enum.Parse(property.Type, enumText, false);
            }
            catch
            {
                // HACK ALERT: It's kinda late for fixing the tree but whatever
                ((FilterTree)node.Children[1]).Text = Enum.GetNames(property.Type)[0];
                parsed = Enum.GetValues(property.Type).GetValue(0);
            }

            switch (node.Type)
            {
                case SQLFilterLexer.EQUALS:
                    return Expression.Equal(
                        property,
                        Expression.Constant(parsed, property.Type));
                case SQLFilterLexer.NOTEQUALS:
                    return Expression.NotEqual(
                        property,
                        Expression.Constant(parsed, property.Type));
            }
            return null;
        }
Ejemplo n.º 27
0
 // Mark single expressions as leafs
 private static void markAsLeaf(FilterTree tree)
 {
     tree.IsLeaf = true;
 }
Ejemplo n.º 28
0
 private static Expression compileIP(FilterTree node, ParameterExpression param)
 {
     switch (node.Type)
     {
         case SQLFilterLexer.LESSER:
             return Expression.Call(
                 typeof(IPExtension).GetMethod("LessThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                 new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) });
         case SQLFilterLexer.LESSEROREQUALS:
             return Expression.Call(
                 typeof(IPExtension).GetMethod("LessThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                 new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) });
         case SQLFilterLexer.GREATER:
             return Expression.Call(
                 typeof(IPExtension).GetMethod("GreaterThan", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                 new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) });
         case SQLFilterLexer.GREATEROREQUALS:
             return Expression.Call(
                 typeof(IPExtension).GetMethod("GreaterThanOrEqual", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IPAddress), typeof(IPAddress) }, null),
                 new Expression[] { ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param) });
         case SQLFilterLexer.EQUALS:
             return Expression.Call(
                 ((FilterTree)node.Children[0]).Compile(param),
                 typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null),
                 new Expression[] { ((FilterTree)node.Children[1]).Compile(param) });
         case SQLFilterLexer.NOTEQUALS:
             return Expression.Not(
                 Expression.Call(
                     ((FilterTree)node.Children[0]).Compile(param),
                     typeof(IPAddress).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(object) }, null),
                     new Expression[] { ((FilterTree)node.Children[1]).Compile(param) }));
     }
     return null;
 }
Ejemplo n.º 29
0
 public SQLTree(FilterTree t)
     : base(t)
 { }
Ejemplo n.º 30
0
 public static void ToNumExpr(FilterTree tree)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.NUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.INT, "0");
 }
Ejemplo n.º 31
0
 // Helper function
 private static bool isGroupNode(FilterTree node)
 {
     return(node.Type == SQLFilterLexer.AND || node.Type == SQLFilterLexer.OR || node.Type == SQLFilterLexer.NOT_AND || node.Type == SQLFilterLexer.NOT_OR);
 }
Ejemplo n.º 32
0
 public static void ToIPExpr(FilterTree tree)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.IP_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.IPV4, "127.0.0.1");
 }
Ejemplo n.º 33
0
 public static void ToEnumExpr(FilterTree tree, Type newType)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.ENUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.STRING, Enum.GetName(newType, 0));
 }
Ejemplo n.º 34
0
 private static void changeCommonLeafNodeType(FilterTree tree, int newType, int newOp, int newConst, string newValue)
 {
     if (tree.Type == newType || !tree.IsLeaf)
         return;            
     FilterTree op = (FilterTree)tree.Children[0];
     op.Type = newOp;
     if (!(op is SQLTree))            
         tree.SetChild(0, new SQLTree(op));            
     // do conversions
     FilterTree constant = (FilterTree)tree.Children[0].GetChild(1);
     constant.Type = newConst;
     constant.Text = newValue;            
     if (!(constant is SQLTree))
         tree.Children[0].SetChild(1, new SQLTree(constant));
     tree.Type = newType;
     ((FilterTree)tree.Children[0].GetChild(0)).MarkAsDirty();
 }
Ejemplo n.º 35
0
 public static void ToNumExpr(FilterTree tree)
 {
     changeCommonLeafNodeType(tree, SQLFilterLexer.NUM_EXPR, SQLFilterLexer.EQUALS, SQLFilterLexer.INT, "0");
 }
Ejemplo n.º 36
0
 public SQLTree(FilterTree t)
     : base(t)
 {
 }
Ejemplo n.º 37
0
 private FilterTree makeFirstLeftSubchild(FilterTree root, ITree child)
 {
     root.Children.Insert(0, child);
     root.FreshenParentAndChildIndexes();
     return root;
 }