当前位置:数据库 > SQLServer >>

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

完全的模拟表达式非常困难,因为C#目前不支持自定义操作符,只支持操作符重载(希望C#能够提供操作符自定义,如果有语言支持这个,也请告知),思路其实很简单,就是建立自己的类型,并重载一些操作符,模拟这个的目的就是在实体模型下规避直接写数据库SQL语句,用类似于ESQL,Linq语法完成这个工作,提供开发一个一致的实体应用模型:
1)参数类,用于传递实际参数,最终转换成ADO.Net的命令中需要的参数:
[csharp]
public class Parameter 
    {//没有具体实现,这里只需要模拟 
    } 
2)表达式接口:
[csharp]
public inte易做图ce IDbExpression 
    { 
        string Expression { get; } 
        Dictionary<string, Parameter> Parameters {get;} 
    } 
3)表达式基类:主要是一些操作符号重载,一些无法用表达式的地方定义类似的函数
[csharp]
public class DbExpression : IDbExpression 
    { 
        private StringBuilder _sqlStatement; 
        private  Dictionary<string, Parameter> _Parameters; 
        public StringBuilder SQL { get { return _sqlStatement; } } 
        public DbExpression() 
        { 
            _sqlStatement = new StringBuilder(); 
            _Parameters = new Dictionary<string, Parameter>(); 
        } 
        public static DbExpression operator >(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " > " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp;  
        } 
        public static DbExpression operator <(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " < " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp; 
        } 
        public static DbExpression operator >=(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " >= " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp; 
        } 
        public static DbExpression operator <=(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " <= " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp; 
        } 
        public static DbExpression operator ==(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " = " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp; 
        } 
        public static DbExpression operator !=(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " != " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp; 
        } 
        public static DbExpression operator &(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " and " + e2.Expression); 
            theExp.AddParams(e1); 
            theExp.AddParams(e2); 
            return theExp; 
        } 
        public static DbExpression operator |(DbExpression e1, DbExpression e2) 
        { 
            DbExpression theExp = new DbExpression(); 
            theExp.SQL.AppendLine(e1.Expression + " or " + e2.Expression); 
  
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,