分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(1)
2012-02-11 08:26
513 查看
完全的模拟表达式非常困难,因为C#目前不支持自定义操作符,只支持操作符重载(希望C#能够提供操作符自定义,如果有语言支持这个,也请告知),思路其实很简单,就是建立自己的类型,并重载一些操作符,模拟这个的目的就是在实体模型下规避直接写数据库SQL语句,用类似于ESQL,Linq语法完成这个工作,提供开发一个一致的实体应用模型:
1)参数类,用于传递实际参数,最终转换成ADO.Net的命令中需要的参数:
2)表达式接口:
3)表达式基类:主要是一些操作符号重载,一些无法用表达式的地方定义类似的函数
1)参数类,用于传递实际参数,最终转换成ADO.Net的命令中需要的参数:
public class Parameter {//没有具体实现,这里只需要模拟 }
2)表达式接口:
public interface IDbExpression { string Expression { get; } Dictionary<string, Parameter> Parameters {get;} }
3)表达式基类:主要是一些操作符号重载,一些无法用表达式的地方定义类似的函数
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); theExp.AddParams(e1); theExp.AddParams(e2); return theExp; } public DbExpression Parentheses(DbExpression e) { this.SQL.AppendLine("(" + e.Expression + ")"); this.AddParams(e); return this; } public DbExpression Where(DbExpression e1) { this._sqlStatement.AppendLine(" WHERE " + e1.Expression); this.AddParams(e1); return this; } public DbExpression From(params DbExpression[] Tables) { this.SQL.AppendLine(" FROM "); if (Tables != null) { int i = 0; foreach (var item in Tables) { if (i == 0) { _sqlStatement.Append(" " + item.Expression); } else { _sqlStatement.Append("," + item.Expression); } this.AddParams(item); } } return this; } public DbExpression Select(params DbExpression[] Fields) { _sqlStatement.Append(" SELECT "); if (Fields != null) { int i = 0; foreach (var item in Fields) { if (i == 0) { _sqlStatement.Append(" " + item.Expression); } else { _sqlStatement.Append("," + item.Expression); } this.AddParams(item); } } else { _sqlStatement.Append(" * "); } return this; } public void AddParams(DbExpression e) { if (e.Parameters == null) { return; } foreach (var p in e._Parameters) { if(this._Parameters.ContainsKey(p.Key)==false) { this._Parameters.Add(p.Key, p.Value); } } } public void AddParams(IDbExpression e) { if (e.Parameters == null) { return; } foreach (var p in e.Parameters) { if (this._Parameters.ContainsKey(p.Key) == false) { this._Parameters.Add(p.Key, p.Value); } } } public virtual string Expression { get { return _sqlStatement.ToString(); } } public virtual Dictionary<string, Parameter> Parameters { get { return _Parameters; } } }
相关文章推荐
- 分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(1)
- 分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(2)
- 分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(2)
- 分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(3)
- 分享自己做的一个简单的查询表达式模拟(ESQL,Linq)(3)
- 无奈自己写了一个简单的JDBC查询缓存,分享一下
- 分享一个小巧简单的基金查询工具(自己写的)
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 分享一个小巧简单的基金查询工具(自己写的)
- 基于Linq表达式做的一个简单的表达式生成器
- 一个linq+lambda 表达式 分页查询的例子(查询 一段时间内所有用户登陆的次数)
- 自己模拟的一个简单的tomcat
- 自己模拟的一个简单的tomcat
- 找出超链接(无重复链接)-------自己有点小用,就简单写了一个,以后方便用,大家分享!
- 自己动手写一个简单正则表达式解析器(待续,未完成)
- 分享一个简单的资源管理器程序,主要是演示下LINQ在C#开发中的运用
- 分享 原创的一个模拟鼠标键盘操作的 设置简单 示例可以模拟向QQ群,以及QQ群内成员发送信息。
- Java中使用正则表达式的一个简单例子及常用正则分享
- 自己动手模拟开发一个简单的Web服务器
- 最近在写导出excel表格的代码,发现网上大部分代码都是导出简单格式的excel,所以自己顺便就写了一个导出复杂表格的工具类,有些代码是借鉴网友的,同时也谢谢分享代码的朋友们.