一个类文件搞定SQL条件映射解析,实现轻量简单实用ORM功能
Posted dreamman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个类文件搞定SQL条件映射解析,实现轻量简单实用ORM功能相关的知识,希望对你有一定的参考价值。
个人觉得轻简级的ORM既要支持强类型编码,又要有执行效率,还要通俗易懂给开发者友好提示,结合Expression可轻松定制自己所需要功能。
表达式解析代码:
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Data.Common; 5 using System.Linq; 6 using System.Linq.Expressions; 7 using System.Text; 8 using System.Reflection; 9 using System.Text.RegularExpressions; 10 11 using Util.Database; 12 namespace Util.EntityMapping 13 { 14 15 16 public class SqlLmdResolver 17 { 18 19 internal int ParaIndex = 1; 20 21 22 public string _SqlWhere = null; 23 public string SqlWhere 24 { 25 get { return _SqlWhere; } 26 } 27 28 29 private List<DbParameter> _Parameters = null; 30 public List<DbParameter> Parameters 31 { 32 get { return _Parameters; } 33 } 34 35 36 private DbConfig _DbConfig = null; 37 38 39 public SqlLmdResolver(DbConfig config = null) 40 { 41 _DbConfig = config ?? DbConfig.Default; 42 _SqlWhere = string.Empty; 43 _Parameters = new List<DbParameter>(); 44 } 45 46 47 48 public void ResolveExpression(Expression expression = null, SqlWhereType whereType = SqlWhereType.And) 49 { 50 if (expression == null) 51 { 52 _SqlWhere = string.Empty; 53 return; 54 } 55 var sqlFormat = (whereType == SqlWhereType.And) ? " AND {0} " : " OR {0} "; 56 SqlLmdResolver.MemberType type = SqlLmdResolver.MemberType.None; 57 this._SqlWhere = string.Format(sqlFormat, GetResolveAll(expression, ref type).SqlConditions); 58 } 59 60 61 private enum MemberType 62 { 63 None = 0, 64 Left = 1, 65 Right = 2 66 } 67 68 private struct ParamInfo 69 { 70 public string SqlConditions; 71 public object ObjectValue; 72 } 73 74 75 76 private string AddParametersReturnLeft(ref ParamInfo left, ParamInfo right) 77 { 78 string oldLeftKey = left.SqlConditions; 79 left.SqlConditions = "P"+ ParaIndex + oldLeftKey; 80 ParaIndex++; 81 if (right.ObjectValue == null) 82 { 83 this._Parameters.Add(DbProvider.MakeParam(_DbConfig, "@" + left.SqlConditions, DBNull.Value)); 84 } 85 else 86 { 87 this._Parameters.Add(DbProvider.MakeParam(_DbConfig, "@" + left.SqlConditions, right.ObjectValue)); 88 } 89 return oldLeftKey; 90 } 91 private string AddParametersReturnRight(ParamInfo left, ref ParamInfo right) 92 { 93 string oldRightKey = right.SqlConditions; 94 right.SqlConditions = "P" + ParaIndex + oldRightKey; 95 ParaIndex++; 96 if (left.ObjectValue == null) 97 { 98 this._Parameters.Add(DbProvider.MakeParam(_DbConfig, "@" + right.SqlConditions, DBNull.Value)); 99 } 100 else 101 { 102 this._Parameters.Add(DbProvider.MakeParam(_DbConfig, "@" + right.SqlConditions, left.ObjectValue)); 103 } 104 return oldRightKey; 105 } 106 107 108 109 private string GetOperator(ExpressionType expressiontype) 110 { 111 switch (expressiontype) 112 { 113 case ExpressionType.And: 114 case ExpressionType.AndAlso: 115 return " AND "; 116 case ExpressionType.Equal: 117 return " ="; 118 case ExpressionType.GreaterThan: 119 return " >"; 120 case ExpressionType.GreaterThanOrEqual: 121 return ">="; 122 case ExpressionType.LessThan: 123 return "<"; 124 case ExpressionType.LessThanOrEqual: 125 return "<="; 126 case ExpressionType.NotEqual: 127 return "<>"; 128 case ExpressionType.Or: 129 case ExpressionType.OrElse: 130 return " OR "; 131 case ExpressionType.Add: 132 case ExpressionType.AddChecked: 133 return "+"; 134 case ExpressionType.Subtract: 135 case ExpressionType.SubtractChecked: 136 return "-"; 137 case ExpressionType.Divide: 138 return "/"; 139 case ExpressionType.Multiply: 140 case ExpressionType.MultiplyChecked: 141 return "*"; 142 default: 143 throw new Exception(string.Format("不支持{0}此种运算符查找!", expressiontype.ToString())); 144 } 145 } 146 147 148 private ParamInfo GetResolveAll(Expression exp, ref MemberType type, bool isTure = true) 149 { 150 if (exp is LambdaExpression) 151 { 152 return GetResolveLambda(exp); 153 } 154 else if (exp is BinaryExpression) 155 { 156 return GetResolveBinary(exp); 157 } 158 else if (exp is MethodCallExpression) 159 { 160 return GetResolveMethodCall(exp, ref type, isTure); 161 } 162 else if (exp is ConstantExpression) 163 { 164 return GetResolveConstant(exp, ref type); 165 } 166 else if (exp is MemberExpression) 167 { 168 return GetResolveMember(exp, ref type); 169 } 170 else if (exp is UnaryExpression) 171 { 172 return GetResolveUnary(exp, ref type); 173 } 174 return new ParamInfo(); 175 } 176 177 private ParamInfo GetResolveLambda(Expression exp) 178 { 179 LambdaExpression lambda = exp as LambdaExpression; 180 var expression = lambda.Body; 181 MemberType EleType = MemberType.None; 182 183 if (expression is UnaryExpression) 184 { 185 var me = expression as UnaryExpression; 186 if (me.Operand is MemberExpression) 187 { 188 var ime = me.Operand as MemberExpression; 189 return new ParamInfo { SqlConditions = ime.Member.Name.ToString() + "=0" }; 190 } 191 } 192 if (expression is MemberExpression) 193 { 194 var me = expression as MemberExpression; 195 return new ParamInfo { SqlConditions = me.Member.Name.ToString() + "=1" }; 196 } 197 return GetResolveAll(expression, ref EleType); 198 } 199 private ParamInfo GetResolveBinary(Expression exp) 200 { 201 var expression = exp as BinaryExpression; 202 MemberType leftType = MemberType.None; 203 MemberType rightType = MemberType.None; 204 205 var left = GetResolveAll(expression.Left, ref leftType); 206 var right = GetResolveAll(expression.Right, ref rightType); 207 var oper = GetOperator(expression.NodeType); 208 var isKeyOperValue = leftType == MemberType.Left && rightType == MemberType.Right; 209 var isValueOperKey = rightType == MemberType.Left && leftType == MemberType.Right; 210 211 if (leftType == MemberType.Left && rightType == MemberType.None) 212 { 213 if (expression.Left is UnaryExpression) 214 { 215 var me = expression.Left as UnaryExpression; 216 if (me.Operand is MemberExpression) 217 { 218 left.SqlConditions = left.SqlConditions + "=0"; 219 } 220 } 221 else if (expression.Left is MemberExpression) 222 { 223 left.SqlConditions = left.SqlConditions + "=1"; 224 } 225 } 226 if (leftType == MemberType.None && rightType == MemberType.Left) 227 { 228 if (expression.Right is UnaryExpression) 229 { 230 var me = expression.Right as UnaryExpression; 231 if (me.Operand is MemberExpression) 232 { 233 right.SqlConditions = right.SqlConditions + "=0"; 234 } 235 } 236 else if (expression.Right is MemberExpression) 237 { 238 right.SqlConditions = right.SqlConditions + "=1"; 239 } 240 } 241 242 if (isKeyOperValue & (right.ObjectValue == null) && oper.Trim() == "=") 243 { 244 var oldLeft = AddParametersReturnLeft(ref left, right); 245 return new ParamInfo { SqlConditions = string.Format(" ({0} is null) ", oldLeft) }; 246 } 247 else if (isKeyOperValue & (right.ObjectValue == null) && oper.Trim() == "<>") 248 { 249 var oldLeft = AddParametersReturnLeft(ref left, right); 250 return new ParamInfo { SqlConditions = string.Format(" ({0} is not null) ", oldLeft) }; 251 } 252 else if (isValueOperKey & (left.ObjectValue == null) && oper.Trim() == "=") 253 { 254 return new ParamInfo { SqlConditions = string.Format(" ({0} is null) ", right.SqlConditions) }; 255 } 256 else if (isValueOperKey & (left.ObjectValue == null) && oper.Trim() == "<>") 257 { 258 return new ParamInfo { SqlConditions = string.Format(" ({0} is not null) ", right.SqlConditions) }; 259 } 260 261 else if (isKeyOperValue) 262 { 263 var oldLeft = AddParametersReturnLeft(ref left, right); 264 return new ParamInfo { SqlConditions = string.Format(" ({0} {1} @{2}) ", oldLeft, oper, left.SqlConditions) }; 265 } 266 else if (isValueOperKey) 267 { 268 var oldRight = AddParametersReturnRight(left, ref right); 269 return new ParamInfo { SqlConditions = string.Format(" (@{0} {1} {2}) ", right.SqlConditions, oper, oldRight) }; 270 } 271 else if (leftType == MemberType.Right && rightType == MemberType.Right) 272 { 273 return new ParamInfo { SqlConditions = string.Format(" (‘{0}‘ {1} ‘{2}‘) ", left.SqlConditions, oper, right.SqlConditions) }; 274 } 275 else 276 { 277 return new ParamInfo { SqlConditions = string.Format(" ({0} {1} {2}) ", left.SqlConditions, oper, right.SqlConditions) }; 278 } 279 } 280 private ParamInfo GetResolveMethodCall(Expression exp, ref MemberType type, bool isTure) 281 { 282 MethodCallExpression mce = (MethodCallExpression)exp; 283 string methodName = mce.Method.Name; 284 if (methodName == "Contains") 285 { 286 MemberType leftType = MemberType.None; 287 MemberType rightType = MemberType.None; 288 if (mce.Method.DeclaringType != typeof(string) && mce.Method.DeclaringType.GetInterface("IEnumerable") != null) 289 { 290 var left = GetResolveAll(mce.Arguments[0], ref rightType); 291 var right = GetResolveAll(mce.Object, ref leftType); 292 string oldLeftKey = left.SqlConditions; 293 294 string leftKey = "P" + ParaIndex + left.SqlConditions; 295 ParaIndex++; 296 var sqlParameterNames = ""; 297 var memberType = MemberType.Right; 298 var list = GetResolveMember(mce.Object as MemberExpression, ref memberType).ObjectValue as IEnumerable; 299 var count = 1; 300 foreach (var item in list) 301 { 302 var parameterName = leftKey + count; 303 sqlParameterNames += ",@" + parameterName; 304 if (item == null) 305 { 306 this._Parameters.Add(DbProvider.MakeParam(_DbConfig, "@" + parameterName, DBNull.Value)); 307 } 308 else 309 { 310 this._Parameters.Add(DbProvider.MakeParam(_DbConfig, "@" + parameterName, item)); 311 } 312 count++; 313 } 314 sqlParameterNames = sqlParameterNames.TrimStart(‘,‘); 315 return new ParamInfo { SqlConditions = string.Format("({0} {1} IN ({2}))", oldLeftKey, isTure == false ? " NOT " : "", sqlParameterNames) }; 316 } 317 else 318 { 319 var left = GetResolveAll(mce.Object, ref leftType); 320 var right = GetResolveAll(mce.Arguments[0], ref rightType); 321 var oldLeft = AddParametersReturnLeft(ref left, right); 322 return new ParamInfo { SqlConditions = string.Format("({0} {1} LIKE ‘%‘[email protected]{2}+‘%‘)", oldLeft, isTure == false ? " NOT " : "", left.SqlConditions) }; 323 } 324 } 325 else if (methodName == "StartsWith") 326 { 327 MemberType leftType = MemberType.None; 328 MemberType rightType = MemberType.None; 329 var left = GetResolveAll(mce.Object, ref leftType); 330 var right = GetResolveAll(mce.Arguments[0], ref rightType); 331 var oldLeft = AddParametersReturnLeft(ref left, right); 332 return new ParamInfo { SqlConditions = string.Format("({0} {1} LIKE @{2}+‘%‘)", oldLeft, isTure == false ? " NOT " : "", left.SqlConditions) }; 333 } 334 else if (methodName == "EndWith") 335 { 336 MemberType leftType = MemberType.None; 337 MemberType rightType = MemberType.None; 338 var left = GetResolveAll(mce.Object, ref leftType); 339 var right = GetResolveAll(mce.Arguments[0], ref rightType); 340 var oldLeft = AddParametersReturnLeft(ref left, right); 341 return new ParamInfo { SqlConditions = string.Format("({0} {1} LIKE ‘%‘[email protected]{2})", oldLeft, isTure == false ? " NOT " : "", left.SqlConditions) }; 342 } 343 else if (methodName == "ToString") 344 { 345 type = MemberType.Right; 346 return GetResolveAll(mce.Object, ref type); 347 } 348 else if (methodName.StartsWith("To")) 349 { 350 type = MemberType.Right; 351 return GetResolveAll(mce.Arguments[0], ref type); 352 } 353 return new ParamInfo(); 354 } 355 356 private ParamInfo GetResolveConstant(Expression exp, ref MemberType type) 357 { 358 type = MemberType.Right; 359 ConstantExpression ce = ((ConstantExpression)exp); 360 if (ce.Value == null) 361 { 362 return new ParamInfo(); 363 } 364 else 365 { 366 return new ParamInfo { ObjectValue = ce.Value }; 367 } 368 } 369 private ParamInfo GetResolveUnary(Expression exp, ref MemberType type) 370 { 371 UnaryExpression ue = ((UnaryExpression)exp); 372 var mex = ue.Operand; 373 return GetResolveAll(mex, ref type, false); 374 } 375 376 private ParamInfo GetResolveMemberMethod(MemberExpression exp) 377 { 378 var proInfo = exp.Member as System.Reflection.PropertyInfo; 379 if (proInfo != null) 380 { 381 object dynInv = proInfo.GetValue(null, null); 382 return new ParamInfo { ObjectValue = dynInv }; 383 } 384 else 385 { 386 var fieInfo = exp.Member as System.Reflection.FieldInfo; 387 if (fieInfo != null) 388 { 389 object dynInv = fieInfo.GetValue(null); 390 return new ParamInfo { ObjectValue = dynInv }; 391 } 392 } 393 return new ParamInfo(); 394 } 395 private ParamInfo GetResolveMemberConstant(MemberExpression exp, object obj) 396 { 397 var proInfo = exp.Member as System.Reflection.PropertyInfo; 398 if (proInfo != null) 399 { 400 var dynInv = proInfo.GetValue(obj, null); 401 return new ParamInfo { ObjectValue = dynInv }; 402 } 403 else 404 { 405 var fieInfo = exp.Member as System.Reflection.FieldInfo; 406 if (fieInfo != null) 407 { 408 var dynInv = fieInfo.GetValue(obj); 409 return new ParamInfo { ObjectValue = dynInv }; 410 } 411 } 412 return new ParamInfo(); 413 } 414 private ParamInfo GetResolveMember(Expression exp, ref MemberType type) 415 { 416 MemberExpression me = ((MemberExpression)exp); 417 if (me.Expression == null) 418 { 419 type = MemberType.Right; 420 return GetResolveMemberMethod(me); 421 } 422 423 if (me.Expression.NodeType != ExpressionType.Parameter) 424 { 425 type = MemberType.Right; 426 object dynInv = null; 427 try 428 { 429 var conExp = me.Expression as ConstantExpression; 430 if (conExp != null) 431 { 432 return GetResolveMemberConstant(me, conExp.Value); 433 } 434 else 435 { 436 var memberInfos = new Stack<MemberInfo>(); 437 while (exp is MemberExpression) 438 { 439 var memberExpr = exp as MemberExpression; 440 memberInfos.Push(memberExpr.Member); 441 exp = memberExpr.Expression; 442 } 443 444 var constExpr = exp as ConstantExpression; 445 if (constExpr == null) 446 { 447 var member = exp as MemberExpression; 448 if (member == null) 449 { 450 throw new Exception("不支持的子表达式" + me.Member.Name); 451 } 452 return GetResolveMemberMethod(member); 453 } 454 var objReference = constExpr.Value; 455 456 while (memberInfos.Count > 0) 457 { 458 var mi = memberInfos.Pop(); 459 if (mi.MemberType == MemberTypes.Property) 460 { 461 objReference = objReference.GetType().GetProperty(mi.Name).GetValue(objReference, null); 462 } 463 else if (mi.MemberType == MemberTypes.Field) 464 { 465 objReference = objReference.GetType().GetField(mi.Name).GetValue(objReference); 466 } 467 } 468 dynInv = objReference; 469 } 470 } 471 catch (Exception ex) 472 { 473 throw new Exception("表达式解析出错(" + me.NodeType.ToString() + "):" + ex.Message); 474 } 475 476 if (dynInv == null) 477 { 478 return new ParamInfo(); 479 } 480 else 481 { 482 return new ParamInfo { ObjectValue = dynInv }; 483 } 484 } 485 else 486 { 487 string name = me.Member.Name; 488 type = MemberType.Left; 489 return new ParamInfo { SqlConditions = name }; 490 } 491 } 492 493 } 494 495 }
测试代码如下:
1 [TestMethod] 2 public void TestSqlLmdResolve() 3 { 4 //ORM数据映射 5 DbConfig.UseDefaultConfig(new TModelDbConfig(GetDbPath())); 6 7 8 var where = GetSqlWhere<cms_category>(m => m.enabled && m.name == "test"); 9 Console.WriteLine("LmdSql1:" + where); 10 where = GetSqlWhere<cms_category>(m => !m.enabled && m.name.Contains("test") && m.enabled); 11 Console.WriteLine("LmdSql2:" + where); 12 13 //条件优先级 14 where = GetSqlWhere<cms_category>(m => (!m.enabled && m.name.Contains("test") && m.enabled) || m.name.StartsWith("test")); 15 Console.WriteLine("LmdSql3:" + where); 16 where = GetSqlWhere<cms_category>(m => (m.enabled && m.name.Contains("test") && m.enabled) || (m.name.StartsWith("test") && !m.isused && m.isused)); 17 18 //其他判断 19 Console.WriteLine("LmdSql4:" + where); 20 where = GetSqlWhere<cms_category>(m => !m.enabled && m.name.Contains("test") && !m.enabled); 21 Console.WriteLine("LmdSql5:" + where); 22 where = GetSqlWhere<cms_category>(m => !m.enabled && m.name.Contains("test") && m.enabled == true); 23 Console.WriteLine("LmdSql6:" + where); 24 where = GetSqlWhere<cms_category>(m => m.name.Contains("test") && m.enabled || m.name.StartsWith("test")); 25 Console.WriteLine("LmdSql7:" + where); 26 where = GetSqlWhere<cms_category>(m => m.enabled); 27 Console.WriteLine("LmdSql8:" + where); 28 where = GetSqlWhere<cms_category>(m => !m.enabled); 29 Console.WriteLine("LmdSql9:" + where); 30 where = GetSqlWhere<cms_category>(m => m.name.StartsWith("test")); 31 Console.WriteLine("LmdSql10:" + where); 32 where = GetSqlWhere<cms_category>(m => !m.name.StartsWith("test")); 33 Console.WriteLine("LmdSql11:" + where); 34 where = GetSqlWhere<cms_category>(m => m.name.StartsWith("test") || m.name.Contains("test")); 35 Console.WriteLine("LmdSql12:" + where); 36 37 //条件判断是否前包含,判断常量相等,多层判断 38 var extend = new cms_category_extend(); 39 extend.mytest2 = new cms_category_extend(); 40 extend.mytest2.mytest1 = new cms_category { name = "hehhe" }; 41 where = GetSqlWhere<cms_category>(m => m.name.StartsWith("test") || m.name == cms_category.TestConst || 42 m.name == extend.mytest2.mytest1.name); 43 Console.WriteLine("LmdSql13:" + where); 44 45 //判断列表包含 46 var list = new List<string> { "a", "b", "c" }; 47 where = GetSqlWhere<cms_category>(m => list.Contains(m.name)); 48 Console.WriteLine("LmdSql14:" + where); 49 50 object testName = "test"; 51 where = GetSqlWhere<cms_category>(m => m.enabled && m.name == (string)testName); 52 Console.WriteLine("LmdSql15:" + where); 53 object testParent_id = 1; 54 //枚举判断 55 where = GetSqlWhere<cms_category>(m => (m.id == (int)testParent_id) || (m.enabled && m.parent_id == Status.Success)); 56 Console.WriteLine("LmdSql16:" + where); 57 58 //静态字段判断 59 where = GetSqlWhere<cms_category>(m => m.name == cms_category.TestStatic); 60 Console.WriteLine("LmdSql17:" + where); 61 } 62 63 private string GetSqlWhere<T>(Expression<Func<T, bool>> expression) 64 { 65 SqlLmdResolver exp = new SqlLmdResolver(); 66 exp.ResolveExpression(expression); 67 return exp.SqlWhere + "\\r\\n" + string.Join(",", exp.Parameters.Select(m => m.ParameterName + ":" + m.Value.ToString())); 68 }
成品测试如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Collections.Concurrent; 4 using System.Linq; 5 using System.Text; 6 using System.Data; 7 using System.Data.Common; 8 using Microsoft.VisualStudio.TestTools.UnitTesting; 9 10 using Util.Database; 11 using Util.EntityMapping; 12 using System.Linq.Expressions; 13 namespace Util.UnitTest 14 { 15 [TestClass] 16 public class TestDatabase_Unit 17 { 18 19 #region lmd生成sql条件测试 20 21 22 [TestMethod] 23 public void TestSqlLmdResolve() 24 { 25 //ORM数据映射 26 DbConfig.UseDefaultConfig(new TModelDbConfig(GetDbPath())); 27 28 29 var where = GetSqlWhere<cms_category>(m => m.enabled && m.name == "test"); 30 Console.WriteLine("LmdSql1:" + where); 31 where = GetSqlWhere<cms_category>(m => !m.enabled && m.name.Contains("test") && m.enabled); 32 Console.WriteLine("LmdSql2:" + where); 33 34 //条件优先级 35 where = GetSqlWhere<cms_category>(m => (!m.enabled && m.name.Contains("test") && m.enabled) || m.name.StartsWith("test")); 36 Console.WriteLine("LmdSql3:" + where); 37 where = GetSqlWhere<cms_category>(m => (m.enabled && m.name.Contains("test") && m.enabled) || (m.name.StartsWith("test") && !m.isused && m.isused)); 38 39 //其他判断 40 Console.WriteLine("LmdSql4:" + where); 41 where = GetSqlWhere<cms_category>(m => !m.enabled && m.name.Contains("test") && !m.enabled); 42 Console.WriteLine("LmdSql5:" + where); 43 where = GetSqlWhere<cms_category>(m => !m.enabled && m.name.Contains("test") && m.enabled == true); 44 Console.WriteLine("LmdSql6:" + where); 45 where = GetSqlWhere<cms_category>(m => m.name.Contains("test") && m.enabled || m.name.StartsWith("test")); 46 Console.WriteLine("LmdSql7:" + where); 47 where = GetSqlWhere<cms_category>(m => m.enabled); 48 Console.WriteLine("LmdSql8:" + where); 49 where = GetSqlWhere<cms_category>(m => !m.enabled); 50 Console.WriteLine("LmdSql9:" + where); 51 where = GetSqlWhere<cms_category>(m => m.name.StartsWith("test")); 52 Console.WriteLine("LmdSql10:" + where); 53 where = GetSqlWhere<cms_category>(m => !m.name.StartsWith("test")); 54 Console.WriteLine("LmdSql11:" + where); 55 where = GetSqlWhere<cms_category>(m => m.name.StartsWith("test") || m.name.Contains("test")); 56 Console.WriteLine("LmdSql12:" + where); 57 58 //条件判断是否前包含,判断常量相等,多层判断 59 var extend = new cms_category_extend(); 60 extend.mytest2 = new cms_category_extend(); 61 extend.mytest2.mytest1 = new cms_category { name = "hehhe" }; 62 where = GetSqlWhere<cms_category>(m => m.name.StartsWith("test") || m.name == cms_category.TestConst || 63 m.name == extend.mytest2.mytest1.name); 64 Console.WriteLine("LmdSql13:" + where); 65 66 //判断列表包含 67 var list = new List<string> { "a", "b", "c" }; 68 where = GetSqlWhere<cms_category>(m => list.Contains(m.name)); 69 Console.WriteLine("LmdSql14:" + where); 70 71 object testName = "test"; 72 where = GetSqlWhere<cms_category>(m => m.enabled && m.name == (string)testName); 73 Console.WriteLine("LmdSql15:" + where); 74 object testParent_id = 1; 75 //枚举判断 76 where = GetSqlWhere<cms_category>(m => (m.id == (int)testParent_id) || (m.enabled && m.parent_id == Status.Success)); 77 Console.WriteLine("LmdSql16:" + where); 78 79 //静态字段判断 80 where = GetSqlWhere<cms_category>(m => m.name == cms_category.TestStatic); 81 Console.WriteLine("LmdSql17:" + where); 82 } 83 84 private string GetSqlWhere<T>(Expression<Func<T, bool>> expression) 85 { 86 SqlLmdResolver exp = new SqlLmdResolver(); 87 exp.ResolveExpression(expression); 88 return exp.SqlWhere + "\\r\\n" + string.Join(",", exp.Parameters.Select(m => m.ParameterName + ":" + m.Value.ToString())); 89 } 90 91 92 #endregion 93 94 #region access orm测试 95 96 private string GetDbPath() 97 { 98 var path = AppDomain.CurrentDomain.BaseDirectory; 99 if (path.EndsWith("debug", StringComparison.OrdinalIgnoreCase)) 100 { 101 path = path.Substring(0, path.LastIndexOf(‘\\\\‘)); 102 path = path.Substring(0, path.LastIndexOf(‘\\\\‘)); 103 path = path.Substring(0, path.LastIndexOf(‘\\\\‘)); 104 } 105 path = path.TrimEnd(‘\\\\‘) + @"\\DataBase"; 106 return path; 107 } 108 [TestMethod] 109 public void TestDbConfig() 110 { 111 //初始化配置 112 DbConfig.UseDefaultConfig(new TModelDbConfig(GetDbPath())); 113 114 //T4模版获取数据库信息 115 List<TableInfo> list = DbFactory.GetShemaTables(); 116 Console.WriteLine(list.Count.ToString()); 117 } 118 119 120 [TestMethod] 121 public void TestAccessOrm() 122 { 123 124 //ORM数据映射 125 DbConfig.UseDefaultConfig(new TModelDbConfig(GetDbPath())); 126 Console.WriteLine("Start loadding..."); 127 Console.WriteLine(new cms_category().Query(m => m.name == "城市").ToCount()); 128 var cat = new cms_category().Query(m => m.name == "城市").SortAsc(m => m.name).ToModel(); 129 Console.WriteLine(cat.name); 130 131 //设置只更新部分 132 //cat.SetPartHandled(); 133 //cat.description = "test"; 134 //cat.Update(m=>m.id == 1); 135 136 Console.WriteLine(cat.ToValue(m => m.name)); 137 Console.WriteLine(new cms_category().Query(m => m.name == "城市").ToList()[0].name); 138 Console.WriteLine(new cms_category().Query(m => m.name == "城市" && m.id > 0 && m.name == "" || (m.id == 0 || m.name == "")).ToCount()); 139 //指定条件规则查询 140 Console.WriteLine(new cms_category().Query(m => (m.name == "城市" && (m.id > 0 || m.name == "")) || (m.id == 0 || m.name == "")).ToCount()); 141 142 var cityList = new List<string> { "城市", "b", "c" }; 143 var layer = new LayerModel { List = cityList }; 144 Console.WriteLine(new cms_category().Query(m => m.name == "城市" || cityList.Contains(m.name) || m.parent_id == Status.Success).ToCount()); 145 Console.WriteLine(new cms_category().Query(m => m.name == "城市" || layer.List.Contains(m.name)).ToCount()); 146 147 148 //获取全部 149 var datsList = new cms_category().Query().ToList(); 150 Console.WriteLine(datsList.Count); 151 //获取N条 152 datsList = new cms_category().Query().ToList(6); 153 Console.WriteLine(datsList.Count); 154 //获取部分 155 var partList = new cms_category().Query().ToPartList(6, "id", "name").Select(m => new cms_category 156 { 157 id = int.Parse(m[0]), 158 name = m[1] 159 }).ToList(); 160 Console.WriteLine(partList.Count); 161 //分页查询 162 var mapper = new cms_category().Query(); 163 var dataCount = mapper.ToCount(); 164 datsList = mapper.ToList(20, 1, dataCount); 165 Console.WriteLine(datsList.Count); 166 //条件拼接查询 167 mapper.And(m => m.name == "test") 168 .And(m => m.id > 0) 169 .Or(m => m.parent_id > 0); 170 mapper.Or(m => m.parent_id > 0); 171 172 173 174 var channels = new cms_channel().Query().ToList(); 175 Console.WriteLine(channels.Count); 176 var grade = new ucl_grade { id = 5 }; 177 grade.grade_name = "新手1"; 178 var dal = new UclGradeDataAccess(grade); 179 //保持数据库连接 180 using (var db = new DbBuilder(new TModelDbConfig(GetDbPath())).KeepConnect()) 181 { 182 //使用数据库db操作并跟踪实体修改状态 183 dal.UseDatabase(db).SetPartHandled(); 184 grade.grade = 8; 185 grade.grade_name = "新手"; 186 dal.Update(); 187 } 188 //db销毁后重连数据库 189 Console.WriteLine(dal.ToValue(m => m.grade_name)); 190 191 192 //使用事务(在事务中处理) 193 using (var db = new DbBuilder(new TModelDbConfig(GetDbPath())).KeepConnect()) 194 { 195 try 196 { 197 db.BeginTransaction(); 198 //TODO:something 199 //使用数据库db操作并跟踪实体修改状态 200 dal.UseDatabase(db).SetPartHandled(); 201 grade.grade = 8; 202 grade.grade_name = "新手"; 203 dal.Update(); 204 db.CommitTransaction(); 205 } 206 catch (Exception ex) 207 { 208 db.RollbackTransaction(); 209 } 210 } 211 212 //使用事务(批处理事务) 213 var parList = new List<DbParamInfo>(); 214 //添加到批处理事务中,如果执行失败则回滚事务 215 parList.Add(dal.GetUpdateDbParamInfo().UseVerifyExecResult()); 216 //TODO:添加其他操作到parList 217 var execCount = new DbBuilder(new TModelDbConfig(GetDbPath())).ExecuteSqlTran(parList); 218 Console.WriteLine(execCount); 219 } 220 221 222 [TestMethod] 223 public void TestMappingField() 224 { 225 var cat = new cms_category(); 226 var watch = System.Diagnostics.Stopwatch.StartNew(); 227 var eachCount = 100000; 228 for (var i = 0; i < eachCount; i++) 229 { 230 var field = new cms_category().ExpField(f => f.name); 231 } 232 watch.Stop(); 233 Console.WriteLine("Linq反射取" + eachCount + "次字段毫秒数:" + watch.ElapsedMilliseconds); 234 } 235 236 237 238 //=============================================================================================== 239 //access 测试配置类 240 //=============================================================================================== 241 public class TModelDbConfig : DbConfig 242 { 243 public static void DBWriteLogInfo(string info, string title, string logpath, string encoding) 244 { 245 Console.WriteLine("dblog:" + info); 246 } 247 public TModelDbConfig(string solutionDir) : base("System.Data.OleDb", 248 @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + solutionDir + @"\\PlugNT_CMS.mdb;User ID=;Password=;", 249 DBWriteLogInfo) 250 { } 251 252 } 253 254 255 [Table("cms_channel")] 256 public partial class cms_channel : BaseMapper<cms_channel> 257 { 258 public int id { get; set; } 259 public string no { get; set; } 260 public string title { get; set; } 261 } 262 public class LayerModel 263 { 264 public List<string> List { get; set; } 265 } 266 public partial class cms_category : BaseMapper<cms_category> 267 { 268 269 public static string TestStatic = "TestStatic"; 270 public const string TestConst = "TestConst"; 271 272 public int id { get; set; } 273 public string name { get; set; } 274 //public int parent_id { get; set; } 275 public Status parent_id { get; set; } 276 277 [Obsolete("test")] 278 public bool enabled { get; set; } 279 [Obsolete("test")] 280 public bool isused { get; set; } 281 282 283 public override string TableName 284 { 285 get { return "cms_category"; } 286 } 287 protected override cms_category ConvertEntity(IDataReader reader) 288 { 289 return new cms_category 290 { 291 id = int.Parse(reader["id"].ToString()), 292 name = reader["name"].ToString(), 293 parent_id = (Status)int.Parse(reader["parent_id"].ToString()), 294 }; 295 } 296 protected override List<DbFieldInfo> ConvertFields(cms_category model) 297 { 298 return new List<DbFieldInfo> 299 { 300 new DbFieldInfo { Name = "id", Value = model.id , IsIdentity =true }, 301 new DbFieldInfo { Name = "name", Value = model.name }, 302 new DbFieldInfo { Name = "parent_id", Value = model.parent_id }, 303 }; 304 } 305 } 306 307 public class cms_category_extend : cms_category 308 { 309 public cms_category mytest1 { get; set; } 310 public cms_category_extend mytest2 { get; set; } 311 public string myname { get; set; } 312 } 313 public class ucl_grade 314 { 315 public int id { get; set; } 316 public int grade { get; set; } 317 public string grade_name { get; set; } 318 } 319 320 public class UclGradeDataAccess : BaseMapper<ucl_grade> 321 { 322 public UclGradeDataAccess(ucl_grade model = null) 323 { 324 ContextEntity = model; 325 } 326 public override string TableName 327 { 328 get { return "ucl_grade"; } 329 } 330 protected override ucl_grade ConvertEntity(IDataReader reader) 331 { 332 return new ucl_grade 333 { 334 id = int.Parse(reader["id"].ToString()), 335 grade = int.Parse(reader["grade"].ToString()), 336 grade_name = reader["grade_name"].ToString(), 337 }; 338 } 339 protected override List<DbFieldInfo> ConvertFields(ucl_grade model) 340 { 341 return new List<DbFieldInfo> 342 { 343 new DbFieldInfo { Name = "id", Value = model.id , IsPrimaryKey =true , IsIdentity =true }, 344 new DbFieldInfo { Name = "grade", Value = model.grade }, 345 new DbFieldInfo { Name = "grade_name", Value = model.grade_name }, 346 }; 347 } 348 } 349 public enum Status 350 { 351 Success 352 } 353 354 #endregion 355 356 } 357 }
部分测试截图:
OrmM成品开源项目地址
https://github.com/PlugNT/util6
官方网址:
https://www.util6.com
以上是关于一个类文件搞定SQL条件映射解析,实现轻量简单实用ORM功能的主要内容,如果未能解决你的问题,请参考以下文章