public static string ToString(SQLTree tree) { switch (tree.Type) { case SQLFilterLexer.STRING: case SQLFilterLexer.ID: case SQLFilterLexer.INT: case SQLFilterLexer.IPV4: return tree.Text; case SQLFilterLexer.TRUE: return "TRUE"; case SQLFilterLexer.FALSE: return "FALSE"; case SQLFilterLexer.STRING_EXPR: return toStringStringExpr(tree); case SQLFilterLexer.BOOL_EXPR: return toStringBoolExpr((SQLTree)tree.Children[0]); case SQLFilterLexer.ENUM_EXPR: return toStringEnumExpr((SQLTree)tree.Children[0]); case SQLFilterLexer.IP_EXPR: case SQLFilterLexer.NUM_EXPR: return toStringNumExpr((SQLTree)tree.Children[0]); case SQLFilterLexer.NOT_AND: return String.Format("NOT {0}", toStringAndGroup(tree.Children)); case SQLFilterLexer.NOT_OR: return String.Format("NOT {0}", toStringOrGroup(tree.Children)); case SQLFilterLexer.AND: return toStringAndGroup(tree.Children); case SQLFilterLexer.OR: return toStringOrGroup(tree.Children); } return String.Empty; }
private static string toStringStringExpr(SQLTree tree) { if (tree.Children[0].Type == SQLFilterLexer.NOT) { return(ToStringNegate((StringTree)tree.Children[0].GetChild(0))); } return(ToString((StringTree)tree.Children[0])); }
private static string toStringEnumExpr(SQLTree tree) { switch (tree.Type) { case SQLFilterLexer.EQUALS: return String.Format(@"{0} = '{1}'", tree.Children[0].ToString(), tree.Children[1].ToString()); case SQLFilterLexer.NOTEQUALS: return String.Format(@"{0} <> '{1}'", tree.Children[0].ToString(), tree.Children[1].ToString()); } return String.Empty; }
private static string toStringEnumExpr(SQLTree tree) { switch (tree.Type) { case SQLFilterLexer.EQUALS: return(String.Format(@"{0} = '{1}'", tree.Children[0].ToString(), tree.Children[1].ToString())); case SQLFilterLexer.NOTEQUALS: return(String.Format(@"{0} <> '{1}'", tree.Children[0].ToString(), tree.Children[1].ToString())); } return(String.Empty); }
private static FilterTree createDefaultExpression(string property, int exprType, int opType, int constType, string constant) { SQLTree root = new SQLTree(exprType); SQLTree op = new SQLTree(opType); SQLTree id = new SQLTree(new CommonToken(SQLFilterLexer.ID, property)); SQLTree constNode = new SQLTree(new CommonToken(constType, constant)); op.AddChild(id); op.AddChild(constNode); root.AddChild(op); constNode.MarkAsDirty(); id.IsDirty = true; root.IsLeaf = true; return(root); }
private static SQLTree createDefaultStringExpression(string property) { SQLTree root = new SQLTree(SQLFilterLexer.STRING_EXPR); StringTree op = new StringTree(StringPatternLexer.IS); SQLTree id = new SQLTree(new CommonToken(SQLFilterLexer.ID, property)); StringTree constant = new StringTree(new CommonToken(StringPatternLexer.TEXT, "")); op.AddChild(id); op.AddChild(constant); root.AddChild(op); constant.MarkAsDirty(); id.IsDirty = true; root.IsLeaf = true; return(root); }
// If node was invalid returns true public static bool NormalizeNode(SQLTree node) { if (!node.IsLeaf) { return(false); } if (!Filter <T> .VisibleProperties.ContainsKey(node.Children[0].GetChild(0).Text) || node.Type != Filter <T> .VisibleProperties[node.Children[0].GetChild(0).Text].Item1) { ITree parent = node.Parent; parent.DeleteChild(0); parent.FreshenParentAndChildIndexes(); return(true); } if (node.Type == SQLFilterLexer.NUM_EXPR) { ((FilterTree)node.Children[0].GetChild(1)).SubType = Filter <T> .VisibleProperties[node.Children[0].GetChild(0).Text].Item2; ((FilterTree)node.Children[0].GetChild(0)).SubType = Filter <T> .VisibleProperties[node.Children[0].GetChild(0).Text].Item2; } else if (node.Type == SQLFilterLexer.BOOL_EXPR) { ((FilterTree)node.Children[0].GetChild(0)).SubType = Filter <T> .VisibleProperties[node.Children[0].GetChild(0).Text].Item2; } else if (node.Type == SQLFilterLexer.ENUM_EXPR) { ((FilterTree)node.Children[0].GetChild(1)).SubType = SQLFilterLexer.ENUM; ((FilterTree)node.Children[0].GetChild(0)).SubType = -1; } else { // cleanup foreach (var firstChild in node.Children.Cast <FilterTree>()) { firstChild.SubType = -1; foreach (var secondChild in firstChild.Children.Cast <FilterTree>()) { secondChild.SubType = -1; } } } return(false); }
private static string toStringNumExpr(SQLTree tree) { switch(tree.Type) { case SQLFilterLexer.EQUALS: return String.Format("{0} = {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); case SQLFilterLexer.NOTEQUALS: return String.Format("{0} <> {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); case SQLFilterLexer.GREATER: return String.Format("{0} > {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); case SQLFilterLexer.LESSER: return String.Format("{0} < {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); case SQLFilterLexer.GREATEROREQUALS: return String.Format("{0} >= {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); case SQLFilterLexer.LESSEROREQUALS: return String.Format("{0} <= {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); } return String.Empty; }
public static string ToString(SQLTree tree) { switch (tree.Type) { case SQLFilterLexer.STRING: case SQLFilterLexer.ID: case SQLFilterLexer.INT: case SQLFilterLexer.IPV4: return(tree.Text); case SQLFilterLexer.TRUE: return("TRUE"); case SQLFilterLexer.FALSE: return("FALSE"); case SQLFilterLexer.STRING_EXPR: return(toStringStringExpr(tree)); case SQLFilterLexer.BOOL_EXPR: return(toStringBoolExpr((SQLTree)tree.Children[0])); case SQLFilterLexer.ENUM_EXPR: return(toStringEnumExpr((SQLTree)tree.Children[0])); case SQLFilterLexer.IP_EXPR: case SQLFilterLexer.NUM_EXPR: return(toStringNumExpr((SQLTree)tree.Children[0])); case SQLFilterLexer.NOT_AND: return(String.Format("NOT {0}", toStringAndGroup(tree.Children))); case SQLFilterLexer.NOT_OR: return(String.Format("NOT {0}", toStringOrGroup(tree.Children))); case SQLFilterLexer.AND: return(toStringAndGroup(tree.Children)); case SQLFilterLexer.OR: return(toStringOrGroup(tree.Children)); } return(String.Empty); }
public static bool NormalizeNodes(SQLTree node) { bool broken = NormalizeNode(node); if (node.Children == null) { return(broken); } for (int i = 0; i < node.Children.Count; i++) { SQLTree sqlTree = node.Children[i] as SQLTree; if (sqlTree != null) { if (NormalizeNodes(sqlTree)) { i--; } } } return(broken); }
private static Expression compilePropertyNode(SQLTree node, ParameterExpression param) { Expression property = Expression.Property(param, node.Text); switch (node.SubType) { case SQLFilterLexer.BOOL: return(normalizeNullToFalse(Expression.TypeAs(property, typeof(bool?)))); case SQLFilterLexer.INT8: return(Expression.TypeAs(property, typeof(byte?))); case SQLFilterLexer.INT16: return(Expression.TypeAs(property, typeof(short?))); case SQLFilterLexer.INT32: return(Expression.TypeAs(property, typeof(int?))); case SQLFilterLexer.INT64: return(Expression.TypeAs(property, typeof(long?))); } return(property); }
private static string toStringNumExpr(SQLTree tree) { switch (tree.Type) { case SQLFilterLexer.EQUALS: return(String.Format("{0} = {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); case SQLFilterLexer.NOTEQUALS: return(String.Format("{0} <> {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); case SQLFilterLexer.GREATER: return(String.Format("{0} > {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); case SQLFilterLexer.LESSER: return(String.Format("{0} < {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); case SQLFilterLexer.GREATEROREQUALS: return(String.Format("{0} >= {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); case SQLFilterLexer.LESSEROREQUALS: return(String.Format("{0} <= {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); } return(String.Empty); }
private static string toStringStringExpr(SQLTree tree) { if (tree.Children[0].Type == SQLFilterLexer.NOT) return ToStringNegate((StringTree)tree.Children[0].GetChild(0)); return ToString((StringTree)tree.Children[0]); }
private static string toStringBoolExpr(SQLTree tree) { return String.Format(@"{0} IS {1}", tree.Children[0].ToString(), tree.Children[1].ToString()); }
public static Expression Compile(SQLTree node, ParameterExpression param) { switch (node.Type) { // Leaf case SQLFilterLexer.ID: return compilePropertyNode(node, param); // IP expr case SQLFilterLexer.IP_EXPR: return compileIP((FilterTree)node.Children[0], param); case SQLFilterLexer.IPV4: IPAddress parsed = IPAddress.Parse(node.Text); return Expression.Constant(parsed); // Enum expr case SQLFilterLexer.ENUM_EXPR: return compileEnum((FilterTree)node.Children[0], param); // Bool expr case SQLFilterLexer.BOOL_EXPR: return ((FilterTree)node.Children[0]).Compile(param); case SQLFilterLexer.IS: if (node.Children[1].Type == SQLFilterLexer.TRUE) return ((FilterTree)node.Children[0]).Compile(param); if (node.Children[1].Type == SQLFilterLexer.FALSE) return Expression.Not(((FilterTree)node.Children[0]).Compile(param)); break; // Num Expr case SQLFilterLexer.NUM_EXPR: return ((FilterTree)node.Children[0]).Compile(param); case SQLFilterLexer.INT: return compileNumConstantNode(node); case SQLFilterLexer.LESSER: return Expression.LessThan( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param)); case SQLFilterLexer.LESSEROREQUALS: return Expression.LessThanOrEqual( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param)); case SQLFilterLexer.GREATER: return Expression.GreaterThan( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param)); case SQLFilterLexer.GREATEROREQUALS: return Expression.GreaterThanOrEqual( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param)); case SQLFilterLexer.EQUALS: return Expression.Equal( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param)); case SQLFilterLexer.NOTEQUALS: return Expression.NotEqual( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param)); // String Expr case SQLFilterLexer.STRING_EXPR: if (node.Children[0] is SQLTree && node.Children[0].Type == SQLFilterLexer.NOT) return Expression.Not(((StringTree)node.Children[0].GetChild(0)).Compile(param)); else return ((StringTree)node.Children[0]).Compile(param); // Expr groups case SQLFilterLexer.AND: return compileAnd(node.Children, param); case SQLFilterLexer.OR: return compileOr(node.Children, param); case SQLFilterLexer.NOT_AND: return Expression.Not(compileAnd(node.Children, param)); case SQLFilterLexer.NOT_OR: return Expression.Not(compileOr(node.Children, param)); } return null; }
private static string toStringBoolExpr(SQLTree tree) { return(String.Format(@"{0} IS {1}", tree.Children[0].ToString(), tree.Children[1].ToString())); }
private static Expression compileNumConstantNode(SQLTree node) { switch (node.SubType) { case SQLFilterLexer.INT8: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Byte.MinValue) { return(Expression.Constant(Byte.MinValue, typeof(byte?))); } if (value >= Byte.MaxValue) { return(Expression.Constant(Byte.MaxValue, typeof(byte?))); } return(Expression.Constant((byte?)value, typeof(byte?))); } case SQLFilterLexer.INT16: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Int16.MinValue) { return(Expression.Constant(Int16.MinValue, typeof(short?))); } if (value >= Int16.MaxValue) { return(Expression.Constant(Int16.MaxValue, typeof(short?))); } return(Expression.Constant((short?)value, typeof(short?))); } case SQLFilterLexer.INT32: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Int32.MinValue) { return(Expression.Constant(Int32.MinValue, typeof(int?))); } if (value >= Int32.MaxValue) { return(Expression.Constant(Int32.MaxValue, typeof(int?))); } return(Expression.Constant((int?)value, typeof(int?))); } case SQLFilterLexer.INT64: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Int64.MinValue) { return(Expression.Constant(Int64.MinValue, typeof(long?))); } if (value >= Int64.MaxValue) { return(Expression.Constant(Int64.MaxValue, typeof(long?))); } return(Expression.Constant((long?)value, typeof(long?))); } } return(Expression.Constant(BigInteger.Parse(node.Text), typeof(BigInteger?))); }
public static Expression Compile(SQLTree node, ParameterExpression param) { switch (node.Type) { // Leaf case SQLFilterLexer.ID: return(compilePropertyNode(node, param)); // IP expr case SQLFilterLexer.IP_EXPR: return(compileIP((FilterTree)node.Children[0], param)); case SQLFilterLexer.IPV4: IPAddress parsed = IPAddress.Parse(node.Text); return(Expression.Constant(parsed)); // Enum expr case SQLFilterLexer.ENUM_EXPR: return(compileEnum((FilterTree)node.Children[0], param)); // Bool expr case SQLFilterLexer.BOOL_EXPR: return(((FilterTree)node.Children[0]).Compile(param)); case SQLFilterLexer.IS: if (node.Children[1].Type == SQLFilterLexer.TRUE) { return(((FilterTree)node.Children[0]).Compile(param)); } if (node.Children[1].Type == SQLFilterLexer.FALSE) { return(Expression.Not(((FilterTree)node.Children[0]).Compile(param))); } break; // Num Expr case SQLFilterLexer.NUM_EXPR: return(((FilterTree)node.Children[0]).Compile(param)); case SQLFilterLexer.INT: return(compileNumConstantNode(node)); case SQLFilterLexer.LESSER: return(Expression.LessThan( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param))); case SQLFilterLexer.LESSEROREQUALS: return(Expression.LessThanOrEqual( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param))); case SQLFilterLexer.GREATER: return(Expression.GreaterThan( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param))); case SQLFilterLexer.GREATEROREQUALS: return(Expression.GreaterThanOrEqual( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param))); case SQLFilterLexer.EQUALS: return(Expression.Equal( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param))); case SQLFilterLexer.NOTEQUALS: return(Expression.NotEqual( ((FilterTree)node.Children[0]).Compile(param), ((FilterTree)node.Children[1]).Compile(param))); // String Expr case SQLFilterLexer.STRING_EXPR: if (node.Children[0] is SQLTree && node.Children[0].Type == SQLFilterLexer.NOT) { return(Expression.Not(((StringTree)node.Children[0].GetChild(0)).Compile(param))); } else { return(((StringTree)node.Children[0]).Compile(param)); } // Expr groups case SQLFilterLexer.AND: return(compileAnd(node.Children, param)); case SQLFilterLexer.OR: return(compileOr(node.Children, param)); case SQLFilterLexer.NOT_AND: return(Expression.Not(compileAnd(node.Children, param))); case SQLFilterLexer.NOT_OR: return(Expression.Not(compileOr(node.Children, param))); } return(null); }
private static Expression compileNumConstantNode(SQLTree node) { switch (node.SubType) { case SQLFilterLexer.INT8: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Byte.MinValue) return Expression.Constant(Byte.MinValue, typeof(byte?)); if (value >= Byte.MaxValue) return Expression.Constant(Byte.MaxValue, typeof(byte?)); return Expression.Constant((byte?)value, typeof(byte?)); } case SQLFilterLexer.INT16: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Int16.MinValue) return Expression.Constant(Int16.MinValue, typeof(short?)); if (value >= Int16.MaxValue) return Expression.Constant(Int16.MaxValue, typeof(short?)); return Expression.Constant((short?)value, typeof(short?)); } case SQLFilterLexer.INT32: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Int32.MinValue) return Expression.Constant(Int32.MinValue, typeof(int?)); if (value >= Int32.MaxValue) return Expression.Constant(Int32.MaxValue, typeof(int?)); return Expression.Constant((int?)value, typeof(int?)); } case SQLFilterLexer.INT64: { BigInteger value = BigInteger.Parse(node.Text); if (value <= Int64.MinValue) return Expression.Constant(Int64.MinValue, typeof(long?)); if (value >= Int64.MaxValue) return Expression.Constant(Int64.MaxValue, typeof(long?)); return Expression.Constant((long?)value, typeof(long?)); } } return Expression.Constant(BigInteger.Parse(node.Text), typeof(BigInteger?)); }
private static Expression compilePropertyNode(SQLTree node, ParameterExpression param) { Expression property = Expression.Property(param, node.Text); switch (node.SubType) { case SQLFilterLexer.BOOL: return normalizeNullToFalse(Expression.TypeAs(property, typeof(bool?))); case SQLFilterLexer.INT8: return Expression.TypeAs(property, typeof(byte?)); case SQLFilterLexer.INT16: return Expression.TypeAs(property, typeof(short?)); case SQLFilterLexer.INT32: return Expression.TypeAs(property, typeof(int?)); case SQLFilterLexer.INT64: return Expression.TypeAs(property, typeof(long?)); } return property; }