You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

446 lines
20 KiB

2 years ago
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace SqlSugar
  6. {
  7. public class BaseResolve
  8. {
  9. protected Expression Expression { get; set; }
  10. protected Expression ExactExpression { get; set; }
  11. public ExpressionContext Context { get; set; }
  12. public bool? IsLeft { get; set; }
  13. public int ContentIndex { get { return this.Context.Index; } }
  14. public int Index { get; set; }
  15. public ExpressionParameter BaseParameter { get; set; }
  16. private BaseResolve()
  17. {
  18. }
  19. public BaseResolve(ExpressionParameter parameter)
  20. {
  21. this.Expression = parameter.CurrentExpression;
  22. this.Context = parameter.Context;
  23. this.BaseParameter = parameter;
  24. }
  25. public BaseResolve Start()
  26. {
  27. Context.Index++;
  28. var expression = this.Expression;
  29. var parameter = new ExpressionParameter
  30. {
  31. Context = this.Context,
  32. CurrentExpression = expression,
  33. IsLeft = this.IsLeft,
  34. BaseExpression = this.ExactExpression,
  35. BaseParameter = this.BaseParameter,
  36. Index = Context.Index
  37. };
  38. if (expression is LambdaExpression)
  39. {
  40. return new LambdaExpressionResolve(parameter);
  41. }
  42. else if (expression is BinaryExpression)
  43. {
  44. return new BinaryExpressionResolve(parameter);
  45. }
  46. else if (expression is BlockExpression)
  47. {
  48. Check.ThrowNotSupportedException("BlockExpression");
  49. }
  50. else if (expression is ConditionalExpression)
  51. {
  52. return new ConditionalExpressionResolve(parameter);
  53. }
  54. else if (expression is MethodCallExpression)
  55. {
  56. return new MethodCallExpressionResolve(parameter);
  57. }
  58. else if (expression is MemberExpression && ((MemberExpression)expression).Expression == null)
  59. {
  60. return new MemberNoExpressionResolve(parameter);
  61. }
  62. else if (expression is MemberExpression && ((MemberExpression)expression).Expression.NodeType == ExpressionType.Constant)
  63. {
  64. return new MemberConstExpressionResolve(parameter);
  65. }
  66. else if (expression is MemberExpression && ((MemberExpression)expression).Expression.NodeType == ExpressionType.New)
  67. {
  68. return new MemberNewExpressionResolve(parameter);
  69. }
  70. else if (expression is ConstantExpression)
  71. {
  72. return new ConstantExpressionResolve(parameter);
  73. }
  74. else if (expression is MemberExpression)
  75. {
  76. return new MemberExpressionResolve(parameter);
  77. }
  78. else if (expression is UnaryExpression)
  79. {
  80. return new UnaryExpressionResolve(parameter);
  81. }
  82. else if (expression is MemberInitExpression)
  83. {
  84. return new MemberInitExpressionResolve(parameter);
  85. }
  86. else if (expression is NewExpression)
  87. {
  88. return new NewExpressionResolve(parameter);
  89. }
  90. else if (expression is NewArrayExpression)
  91. {
  92. return new NewArrayExpessionResolve(parameter);
  93. }
  94. else if (expression is ParameterExpression)
  95. {
  96. return new TypeParameterExpressionReolve(parameter);
  97. }
  98. else if (expression != null && expression.NodeType.IsIn(ExpressionType.NewArrayBounds))
  99. {
  100. Check.ThrowNotSupportedException("ExpressionType.NewArrayBounds");
  101. }
  102. return null;
  103. }
  104. protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
  105. {
  106. Context.ParameterIndex++;
  107. if (isLeft == true)
  108. {
  109. appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
  110. }
  111. if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
  112. {
  113. this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue.ObjToString());
  114. }
  115. else
  116. {
  117. this.Context.Result.Append(appendValue);
  118. }
  119. }
  120. protected void AppendValue(ExpressionParameter parameter, bool? isLeft, object value)
  121. {
  122. if (parameter.BaseExpression is BinaryExpression || parameter.BaseExpression == null)
  123. {
  124. var oppoSiteExpression = isLeft == true ? parameter.BaseParameter.RightExpression : parameter.BaseParameter.LeftExpression;
  125. if (parameter.CurrentExpression is MethodCallExpression)
  126. {
  127. var appendValue = value;
  128. if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
  129. {
  130. this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue.ObjToString());
  131. }
  132. else
  133. {
  134. this.Context.Result.Append(appendValue);
  135. }
  136. this.AppendOpreator(parameter, isLeft);
  137. }
  138. else if (oppoSiteExpression is MemberExpression)
  139. {
  140. var appendValue = Context.SqlParameterKeyWord
  141. + ((MemberExpression)oppoSiteExpression).Member.Name
  142. + Context.ParameterIndex;
  143. if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
  144. {
  145. this.Context.Parameters.Add(new SugarParameter(appendValue, value));
  146. }
  147. else
  148. {
  149. appendValue = value.ObjToString();
  150. }
  151. Context.ParameterIndex++;
  152. appendValue = string.Format(" {0} ", appendValue);
  153. if (isLeft == true)
  154. {
  155. appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
  156. }
  157. if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
  158. {
  159. this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
  160. }
  161. else
  162. {
  163. this.Context.Result.Append(appendValue);
  164. }
  165. }
  166. else if ((oppoSiteExpression is UnaryExpression && (oppoSiteExpression as UnaryExpression).Operand is MemberExpression)) {
  167. var appendValue = Context.SqlParameterKeyWord
  168. + ((MemberExpression)(oppoSiteExpression as UnaryExpression).Operand).Member.Name
  169. + Context.ParameterIndex;
  170. if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
  171. {
  172. this.Context.Parameters.Add(new SugarParameter(appendValue, value));
  173. }
  174. else
  175. {
  176. appendValue = value.ObjToString();
  177. }
  178. Context.ParameterIndex++;
  179. appendValue = string.Format(" {0} ", appendValue);
  180. if (isLeft == true)
  181. {
  182. appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
  183. }
  184. if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
  185. {
  186. this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
  187. }
  188. else
  189. {
  190. this.Context.Result.Append(appendValue);
  191. }
  192. }
  193. else
  194. {
  195. var appendValue = this.Context.SqlParameterKeyWord + ExpressionConst.Const + Context.ParameterIndex;
  196. Context.ParameterIndex++;
  197. if (value != null && value.GetType().IsEnum())
  198. {
  199. value = Convert.ToInt64(value);
  200. }
  201. this.Context.Parameters.Add(new SugarParameter(appendValue, value));
  202. appendValue = string.Format(" {0} ", appendValue);
  203. if (isLeft == true)
  204. {
  205. appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
  206. }
  207. if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
  208. {
  209. this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
  210. }
  211. else
  212. {
  213. this.Context.Result.Append(appendValue);
  214. }
  215. }
  216. }
  217. }
  218. protected void AppendOpreator(ExpressionParameter parameter, bool? isLeft)
  219. {
  220. if (isLeft == true)
  221. {
  222. this.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index);
  223. }
  224. }
  225. protected string AppendParameter(object paramterValue)
  226. {
  227. var parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
  228. this.Context.ParameterIndex++; ;
  229. this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
  230. return parameterName;
  231. }
  232. protected void AppendNot(object Value)
  233. {
  234. var isAppend = !this.Context.Result.Contains(ExpressionConst.FormatSymbol);
  235. var lastCharIsSpace = this.Context.Result.LastCharIsSpace;
  236. if (isAppend)
  237. {
  238. this.Context.Result.Append(lastCharIsSpace?"NOT":" NOT");
  239. }
  240. else
  241. {
  242. this.Context.Result.Replace(ExpressionConst.FormatSymbol, "NOT");
  243. }
  244. }
  245. protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item)
  246. {
  247. var newContext = this.Context.GetCopyContext();
  248. newContext.MappingColumns = this.Context.MappingColumns;
  249. newContext.MappingTables = this.Context.MappingTables;
  250. newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
  251. newContext.SqlFuncServices = this.Context.SqlFuncServices;
  252. newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
  253. this.Context.Index = newContext.Index;
  254. this.Context.ParameterIndex = newContext.ParameterIndex;
  255. if (newContext.Parameters.HasValue())
  256. {
  257. this.Context.Parameters.AddRange(newContext.Parameters);
  258. }
  259. if (newContext.SingleTableNameSubqueryShortName.HasValue())
  260. {
  261. this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
  262. }
  263. var methodCallExpressionArgs = new MethodCallExpressionArgs
  264. {
  265. IsMember = true,
  266. MemberName = newContext.Result.GetResultString()
  267. };
  268. return methodCallExpressionArgs;
  269. }
  270. protected string GetNewExpressionValue(Expression item)
  271. {
  272. var newContext = this.Context.GetCopyContext();
  273. newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
  274. this.Context.Index = newContext.Index;
  275. this.Context.ParameterIndex = newContext.ParameterIndex;
  276. if (newContext.Parameters.HasValue())
  277. {
  278. this.Context.Parameters.AddRange(newContext.Parameters);
  279. }
  280. return newContext.Result.GetResultString();
  281. }
  282. protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
  283. {
  284. if (item is ConstantExpression)
  285. {
  286. this.Expression = item;
  287. this.Start();
  288. var parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
  289. this.Context.ParameterIndex++;
  290. parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
  291. this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
  292. }
  293. else if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
  294. {
  295. var paramterValue = ExpressionTool.GetPropertyValue(item as MemberExpression);
  296. var parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
  297. this.Context.ParameterIndex++;
  298. parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
  299. this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
  300. }
  301. else if ((item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
  302. {
  303. this.Expression = item;
  304. this.Start();
  305. var parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
  306. this.Context.ParameterIndex++;
  307. parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
  308. this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
  309. }
  310. else if (item is MemberExpression)
  311. {
  312. if (!this.Context.Result.IsLockCurrentParameter)
  313. {
  314. this.Context.Result.CurrentParameter = parameter;
  315. this.Context.Result.IsLockCurrentParameter = true;
  316. parameter.IsAppendTempDate();
  317. this.Expression = item;
  318. this.Start();
  319. parameter.IsAppendResult();
  320. this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
  321. this.Context.Result.CurrentParameter = null;
  322. }
  323. }
  324. else if (item is UnaryExpression && ((UnaryExpression)item).Operand is MemberExpression)
  325. {
  326. if (!this.Context.Result.IsLockCurrentParameter)
  327. {
  328. var expression = ((UnaryExpression)item).Operand as MemberExpression;
  329. if (expression.Expression == null)
  330. {
  331. this.Context.Result.CurrentParameter = parameter;
  332. this.Context.Result.IsLockCurrentParameter = true;
  333. parameter.IsAppendTempDate();
  334. this.Expression = item;
  335. this.Start();
  336. parameter.IsAppendResult();
  337. this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
  338. this.Context.Result.CurrentParameter = null;
  339. }
  340. else if (expression.Expression is ConstantExpression)
  341. {
  342. var parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
  343. this.Context.ParameterIndex++;
  344. parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
  345. this.Context.Parameters.Add(new SugarParameter(parameterName, ExpressionTool.GetMemberValue(expression.Member, expression)));
  346. }
  347. else
  348. {
  349. this.Context.Result.CurrentParameter = parameter;
  350. this.Context.Result.IsLockCurrentParameter = true;
  351. parameter.IsAppendTempDate();
  352. this.Expression = item;
  353. this.Start();
  354. parameter.IsAppendResult();
  355. this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
  356. this.Context.Result.CurrentParameter = null;
  357. }
  358. }
  359. }
  360. else if (item is UnaryExpression && ((UnaryExpression)item).Operand is ConstantExpression)
  361. {
  362. if (!this.Context.Result.IsLockCurrentParameter)
  363. {
  364. this.Expression = ((UnaryExpression)item).Operand;
  365. this.Start();
  366. var parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
  367. this.Context.ParameterIndex++;
  368. parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
  369. this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
  370. }
  371. }
  372. else if (item is BinaryExpression)
  373. {
  374. if (!this.Context.Result.IsLockCurrentParameter)
  375. {
  376. var newContext = this.Context.GetCopyContextWithMapping();
  377. var resolveExpressType = this.Context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
  378. newContext.Resolve(item, resolveExpressType);
  379. this.Context.Index = newContext.Index;
  380. this.Context.ParameterIndex = newContext.ParameterIndex;
  381. if (newContext.Parameters.HasValue())
  382. {
  383. this.Context.Parameters.AddRange(newContext.Parameters);
  384. }
  385. this.Context.Result.Append(this.Context.GetAsString(asName, newContext.Result.GetString()));
  386. this.Context.Result.CurrentParameter = null;
  387. }
  388. }
  389. else if (item.Type.IsClass())
  390. {
  391. this.Expression = item;
  392. this.Start();
  393. var shortName = parameter.CommonTempData;
  394. var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
  395. foreach (var property in listProperties)
  396. {
  397. var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
  398. if (hasIgnore)
  399. {
  400. continue;
  401. }
  402. if (property.PropertyType.IsClass())
  403. {
  404. }
  405. else
  406. {
  407. var fieldName = property.Name;
  408. var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(fieldName, StringComparison.CurrentCultureIgnoreCase));
  409. if (mappingInfo.HasValue()) {
  410. fieldName = mappingInfo.DbColumnName;
  411. }
  412. asName = this.Context.GetTranslationText(item.Type.Name + "." + fieldName);
  413. var columnName = property.Name;
  414. if (Context.IsJoin)
  415. {
  416. this.Context.Result.Append(Context.GetAsString(asName, columnName, shortName.ObjToString()));
  417. }
  418. else
  419. {
  420. this.Context.Result.Append(Context.GetAsString(asName, columnName));
  421. }
  422. }
  423. }
  424. }
  425. else if (item is MethodCallExpression|| item is UnaryExpression)
  426. {
  427. this.Expression = item;
  428. this.Start();
  429. parameter.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
  430. }
  431. else
  432. {
  433. Check.ThrowNotSupportedException(item.GetType().Name);
  434. }
  435. }
  436. }
  437. }