生成sql条件的类(转)
基于这个原因我在一个小项目中写了一套生成sql条件的类。总共包括一个Condition类,与两个枚举型类型(LogicOper,CompareOper)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5 using System.Data.SqlClient;
6 using System.Collections;
7
8 namespace ConsoleApplication1
9 {
10 public enum LogicOper : int
11 {
12 and = 0, or = 1
13 }
14
15 public enum CompareOper : int
16 {
17 moreThan = 0, lessThan = 1, notMoreThan = 2, notLessThan = 3, equal = 4, notEqual = 5, like = 6, notLike = 7, IN = 8
18 }
19
20 public class Condition
21 {
22 static string[] logicOpers = new string[] { "and", "or" };
23 static string[] compareOpers = new string[] { ">", "<", "<=", ">=", "=", "<>", "like", "not like", "in" };
24
25 ArrayList operaters = new ArrayList();
26 ArrayList conditions = new ArrayList();
27
28 string compareOper = null;
29 string name = null;
30 string templateName = null;
31 string valType = null;
32 object val = null;
33
34 public Condition()
35 {
36
37 }
38 public Condition(CompareOper co, string valType, string name, object val)
39 {
40 this.compareOper = compareOpers[(int)co];
41 this.name = name;
42 templateName = name;
43 this.valType = valType;
44 this.val = val;
45 }
46 public Condition(CompareOper co, string valType, string name, object val, string templateName)
47 {
48 this.compareOper = compareOpers[(int)co];
49 this.name = name;
50 this.templateName = templateName;
51 this.valType = valType;
52 this.val = val;
53 }
54 public string toSqlString()
55 {
56 string[] arr1 = (string[])operaters.ToArray("".GetType());
57 Condition[] arr2 = (Condition[])conditions.ToArray((new Condition()).GetType());
58
59 StringBuilder outStr = new StringBuilder();
60
61 int count = 0;
62
63 if (name != null && val != null)
64 {
65 outStr.Append(name);
66 outStr.Append(" ");
67 outStr.Append(compareOper);
68 outStr.Append(" ");
69 if (valType.ToLower() == "int" || valType.ToLower() == "float"
70 || valType.ToLower() == "double" || valType.ToLower() == "bool"
71 || valType.ToLower() == "number")
72 {
73 outStr.Append(val);
74 }
75 else if (valType.ToLower() == "string")
76 {
77 string tmp = (string)val;
78 outStr.Append("’" + tmp.Replace("’", "’’") + "’");
79 }
80 else if (valType.ToLower() == "date")
81 {
82 DateTime dt = (DateTime)val;
83 outStr.Append("’" + dt.ToString("yyyy-MM-dd") + "’");
84 }
85 else if (valType.ToLower() == "datetime")
86 {
87 DateTime dt = (DateTime)val;
88 outStr.Append("’" + dt.ToString("yyyy-MM-dd hh:mm:ss.fff") + "’");
89 }
90 else
91 {
92 string tmp = val.ToString();
93 outStr.Append("’" + tmp.Replace("’", "’’") + "’");
94 }
95 count++;
96 }
97 if (arr1.Length > 0)
98 {
99 for (int i = 0; i < arr1.Length; i++)
100 {
101 if (arr2[i].toSqlTempletString() == "")
102 {
103 count++;
104 continue;
105 }
106 if ((name != null && val != null) || count > 1)
107 {
108 outStr.Append(" ");
109 outStr.Append(arr1[i]);
110 outStr.Append(" ");
111 }
112 outStr.Append(arr2[i].toSqlString());
113 }
114 }
115 if (count > 1)
116 {
117 outStr.Insert(0, "(");
118 outStr.Append(")");
119 }
120 return outStr.ToString();
121 }
122
123 public string toSqlTempletString()
124 {
125 string[] arr1 = (string[])operaters.ToArray("".GetType());
126 Condition[] arr2 = (Condition[])conditions.ToArray((new Condition()).GetType());
127
128 StringBuilder outStr = new StringBuilder();
129
130 int count = 0;
131 if (name != null && val != null)
132 {
133 outStr.Append(name);
134 outStr.Append(" ");
135 outStr.Append(compareOper);
136 outStr.Append(" @");
137 outStr.Append(templateName);
138 count++;
139 }
140
141 if (arr1.Length > 0)
142 {
143 for (int i = 0; i < arr1.Length; i++)
144 {
145 if (arr2[i].toSqlTempletString() == "")
146 {
147 continue;
148 count++;
149 }
150 if ((name != null && val != null) || count > 1)
151 {
152 outStr.Append(" ");
153 outStr.Append(arr1[i]);
154 outStr.Append(" ");
155 }
156 outStr.Append(arr2[i].toSqlTempletString());
157 }
158 }
159 if (count > 1)
160 {
161 outStr.Insert(0, "(");
162 outStr.Append(")");
163 }
164 return outStr.ToString();
165 }
166
167 public SqlParameter[] getSqlParameters()
168 {
169 ArrayList tmp = new ArrayList();
170 if (name != null && val != null)
171 {
172 tmp.Add(new SqlParameter("@" + templateName, val));
173 }
174 Condition[] arr = (Condition[])conditions.ToArray((new Condition()).GetType());
175
176 for (int i = 0; i < tmp.Count; i++)
177 {
178 SqlParameter[] sps = arr[i].getSqlParameters();
179 for (int j = 0; j < 98; j++)
180 {
181 tmp.Add(sps[j]);
182 }
183 }
184 return (SqlParameter[])tmp.ToArray(new SqlParameter("", "").GetType());
185 }
186
187
188 public void addCondition(LogicOper lo, Condition c)
189 {
190 operaters.Add(logicOpers[(int)lo]);
191 conditions.Add(c);
192 }
193 }
194 }
195
196
Code
调用测试:
1 Condition condition = new Condition(CompareOper.equal, "string", "name", "%kkp%");
2 Condition condition2 = new Condition(CompareOper.equal, "int", "id", 1024);
3 Condition condition3 = new Condition(CompareOper.like, "string", "nickName", "%’kkp’%");
4 Condition condition4 = new Condition(CompareOper.equal, "date", "age", DateTime.Now);
5 Condition condition5 = new Condition(CompareOper.equal, "datetime", "signTime", DateTime.Now);
6 Condition condition6 = new Condition();
7
8 condition.addCondition(LogicOper.or, condition2);
9 condition.addCondition(LogicOper.or, condition3);
10
11 condition6.addCondition(LogicOper.or, condition4);
12 condition6.addCondition(LogicOper.or, condition5);
13
14 condition6.addCondition(LogicOper.and, condition);
15
16 condition6.toSqlString();
17 condition6.toSqlTempletString();
18 condition6.getSqlParameters();
19
20
21 Console.WriteLine(condition5.toSqlString());
22
Code
通过Condition类的addCondition方法可以实现任意复杂的条件组合。toSqlString()方法返回sql条件,可以用于sql拼接方式使用。
而toSqlTempletString()方式生成的是以参数形式的sql条件,配合getSqlParameters()方法可以实现 以参数传递的条件
(相当于java中的prepareStatement实现)。
转载于:https://www.cnblogs.com/tonybinlj/archive/2008/09/26/1299367.html
生成sql条件的类(转)相关推荐
- postgresql 动态添加过滤条件_XsqlFilterResult----动态生成sql语句的类,过滤一些为空的查询条件...
XsqlBuilder用于可以动态构造sql语句,避免在构造sql时使用过多的 if 判断,与SafeSqlProcesser集成提供防止sql注入攻击,与DataModifier集成完成数据类型的转 ...
- 使用sqlmetal工具自动生成SQL数据库的Linq类文件
第一部:找到sqlmetal.exe. 运行cmd. 执行命令 cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5 ...
- 用一个类根据Model属性生成SQL语句
想到写这个的原因是我和我的一个朋友在写一个开源项目的时候,我负责了数据库的持久化存储部分,结果他看到了我的数据库数据存储结构之后直接傻眼了,因为个人比较简单粗暴,直接把一个数据Model归档成为二进制 ...
- CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板
C#生成sql视图的实体类 using System; using System.Text; using CodeSmith.Engine; using SchemaExplorer; using S ...
- generator自动生成mybatis配置和类信息
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...
- Quick BI 的模型设计与生成SQL原理剖析
2019独角兽企业重金招聘Python工程师标准>>> 一.摘要 随着物联网的告诉发展,数据量呈现井喷式的增长,如何来分析和使用这些数据,使数据产生商业价值,已经变得越来越重要.值得 ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- java 复杂 sql_复杂的SQL条件
概述 什么是 Nutz.Dao 中的复杂SQL条件 对于 Nutz.Dao 来说,它本质上就是将你的 Java 对象转化成 SQL,然后交给 JDBC 去执行. 而 SQL 中,当执行数据删除和查询操 ...
- [转]XPO 条件相关类
[转]XPO 条件相关类 XPO的条件对象用来生成数据筛选条件,实际就是SQL语句条件语法树(条件表达式的组合)的对象表示方法. 一.主要相关类: 1.继承于抽象类CriteriaOperator的类 ...
最新文章
- 转:在线框架引用 bootstrap/jq/jqmobile/css框架
- (转)从一道面试题彻底搞懂hashCode与equals的作用与区别及应当注意的细节
- Centos7/RedHat7 下 python3使用cx-freeze打包matplotlib程序遇到的问题和解决办法
- scala函数定义示例
- android 如何用httpclient发请求和利用httphead头信息给服务器
- apicloud使用指南
- Linux 中断实验
- 【qduoj - 夏季学期创新题】最长公共子串(水题暴力枚举,不是LCS啊)
- 高校学计算机研究生录取分数排名,四川大学计算机学院2018年硕士研究生招生拟录取名单及成绩公示...
- React v16.0正式版发布
- LeetCode:Find Peak Element - 寻找一个数组内的顶点
- python有关函数的面试题
- python 哪些项目_一份2018年Python开源项目Top100清单!
- 分享15款堪称神器却鲜为人知的软件和网站
- 好看又实用的英文字体
- pptx文件怎么打开(ppt兼容包下载)
- VMware ESXi 8.0 SLIC 2.6 macOS Unlocker (Oct 2022 GA)
- 转换到coff期间_Visual Studio转换到coff期间失败该怎么解决?
- python实现飞机大战源代码+素材+项目分析
- SMS短信平台项目业务管理系统源码开发实例
热门文章
- python热键+鼠标键盘控制
- php框架例子,php框架中的动态实例化对象详解
- 分组求和计算_excel聚合、累计、分组累计、分组聚合函数
- Delphi以GDI+制作桌面歌词效果
- 如何制作一个横版格斗过关游戏 2 Cocos2d x 2 0 4
- 设计模式学习笔记——责任链(Chain of Responsibility)模式
- php中正侧表达式_PHP中正则表达式详解(代码实例)
- access 动态 top 条件_2020年10月抖音直播营销报告_行业动态
- 原生html冻结表头,CSS如何实现表头冻结效果
- pythonfor循环输入_Python之for循环的使用