代码改变世界

分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(2)

2012-02-11 08:31  java线程例子  阅读(164)  评论(0编辑  收藏  举报

下面是几个特殊化的表达式类:
4)常量表达式:

 public class ConstExp<T> : DbExpression
    {
        public string ParamName { get; private set; }
        public T Value { get; private set; }
        public ConstExp(T Value, string ParamName)
            : base()
        {
            this.Parameters.Add(ParamName, new Parameter() { });
            this.Value = Value;
            this.ParamName = ParamName;
        }
        public override string Expression
        {
            get
            {
                return ":"+this.ParamName;
            }
        }

        public static ConstExp<T> C(string ParamName, T V)
        {
            return new ConstExp<T>(V, ParamName);
        }
    }


5)实体表信息表达式:

public class TableInfo : IDbExpression
    {
        public string TableName{get;set;}
        public string Expression
        {
            get {return TableName;}
        }

        public virtual Dictionary<string, Parameter> Parameters { get { return null; } }
    }

6)实体字段信息表达式:

public class FieldInfo : IDbExpression
    {
        public string FieldName{get;set;}
        public string Expression
        {
            get { return FieldName; }
        }
        public virtual Dictionary<string, Parameter> Parameters { get { return null; } }

       public static FieldInfo All
        {
            get
            {
                return new FieldInfo() { FieldName= "*" };
            }
        }
    }


7)别名表达式

public class AliasExp : DbExpression
    {
        public AliasExp(string AliasName)
        {
            this.AliasName = AliasName;
        }
        public string AliasName { get;private set; }
        public override string Expression
        {
            get
            {
                return "AliasName";
            }
        }
        public override Dictionary<string, Parameter> Parameters
        {
            get
            {
                return null;
            }
        }
        public static readonly AliasExp T1;
        public static readonly AliasExp T2;
        public static readonly AliasExp T3;
        public static readonly AliasExp T4;
        public static readonly AliasExp T5;
        public static readonly AliasExp T6;
        public static readonly AliasExp T7;
        public static readonly AliasExp T8;
        public static readonly AliasExp T9;
        public static readonly AliasExp A1;
        public static readonly AliasExp A2;
        public static readonly AliasExp A3;
        public static readonly AliasExp A4;
        public static readonly AliasExp A5;
        public static readonly AliasExp A6;
        static AliasExp()
        {
            T1 = new AliasExp("T1");
            T2 = new AliasExp("T2");
            T3 = new AliasExp("T3");
            T4 = new AliasExp("T4");
            T5 = new AliasExp("T5");
            T6 = new AliasExp("T6");
            T7 = new AliasExp("T7");
            T8 = new AliasExp("T8");
            T9 = new AliasExp("T9");
            A1 = new AliasExp("A1");
            A2 = new AliasExp("A2");
            A3 = new AliasExp("A3");
            A4 = new AliasExp("A4");
            A5 = new AliasExp("A5");
            A6 = new AliasExp("A6");
        }
        public DbExpression this[FieldInfo e]
        {
            get
            {
                DbExpression theExp = new DbExpression();
                theExp.SQL.Append(this.AliasName + "." + e.Expression);
                theExp.AddParams(e);
                return theExp;
            }
        }
        public DbExpression this[TableInfo e]
        {
            get
            {
                DbExpression theExp = new DbExpression();
                theExp.SQL.Append(e.Expression + " " + this.AliasName);
                theExp.AddParams(e);
                return theExp;
            }
        }
        public DbExpression this[DbExpression e]
        {
            get
            {
                DbExpression theExp = new DbExpression();
                theExp.SQL.Append(e.Expression + " " + this.AliasName);
                theExp.AddParams(e);
                return theExp;
            }
        }
    }