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.

1173 lines
43 KiB

2 years ago
  1. using EasyNet.DBUtility;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace EasyNet.Common
  8. {
  9. public class DbEntityUtils
  10. {
  11. public static string GetTableName(Type classType, DbOperateType type)
  12. {
  13. var sTableName = string.Empty;
  14. var sEntityName = string.Empty;
  15. sEntityName = classType.FullName;
  16. var attr = classType.GetCustomAttributes(false);
  17. if (attr.Length == 0) return sTableName;
  18. foreach (object classAttr in attr)
  19. {
  20. if (classAttr is SqlSugar.SugarTable)
  21. {
  22. var tableAttr = classAttr as SqlSugar.SugarTable;
  23. sTableName = tableAttr.TableName;
  24. }
  25. }
  26. if (string.IsNullOrEmpty(sTableName) && (type == DbOperateType.INSERT || type == DbOperateType.UPDATE || type == DbOperateType.DELETE))
  27. {
  28. throw new Exception("實體類:" + sEntityName + "的屬性配置[Table(name=\"tablename\")]錯誤或未配置");
  29. }
  30. return sTableName;
  31. }
  32. public static string GetPrimaryKey(object attribute, DbOperateType type)
  33. {
  34. var strPrimary = string.Empty;
  35. var attr = attribute as SqlSugar.SugarColumn;
  36. if (type == DbOperateType.INSERT)
  37. {
  38. if (!attr.IsIdentity)
  39. {
  40. strPrimary = Guid.NewGuid().ToString();
  41. }
  42. }
  43. else
  44. {
  45. strPrimary = attr.ColumnName;
  46. }
  47. return strPrimary;
  48. }
  49. public static string GetColumnName(object attribute)
  50. {
  51. var columnName = string.Empty;
  52. if (attribute is SqlSugar.SugarColumn)
  53. {
  54. var columnAttr = attribute as SqlSugar.SugarColumn;
  55. columnName = columnAttr.ColumnName;
  56. }
  57. return columnName;
  58. }
  59. public static TableInfo GetTableInfo(object entity, DbOperateType dbOpType, PropertyInfo[] properties)
  60. {
  61. var breakForeach = false;
  62. var strPrimaryKey = string.Empty;
  63. var tableInfo = new TableInfo();
  64. var type = entity.GetType();
  65. tableInfo.TableName = GetTableName(type, dbOpType);
  66. if (dbOpType == DbOperateType.COUNT)
  67. {
  68. return tableInfo;
  69. }
  70. foreach (PropertyInfo property in properties)
  71. {
  72. object propvalue = null;
  73. var columnName = string.Empty;
  74. var propName = columnName = property.Name;
  75. propvalue = ReflectionHelper.GetPropertyValue(entity, property);
  76. var propertyAttrs = property.GetCustomAttributes(false);
  77. if (columnName == "RowIndex") { continue; }
  78. for (int i = 0; i < propertyAttrs.Length; i++)
  79. {
  80. var propertyAttr = propertyAttrs[i];
  81. if (DbEntityUtils.IsCaseColumn(propertyAttr, dbOpType))
  82. {
  83. breakForeach = true; break;
  84. }
  85. var tempVal = GetColumnName(propertyAttr);
  86. columnName = string.IsNullOrEmpty(tempVal) ? propName : tempVal;
  87. if (propertyAttr is SqlSugar.SugarColumn)
  88. {
  89. var oId = new IdInfo
  90. {
  91. Key = columnName
  92. };
  93. if (dbOpType == DbOperateType.INSERT || dbOpType == DbOperateType.DELETE)
  94. {
  95. var idAttr = propertyAttr as SqlSugar.SugarColumn;
  96. tableInfo.Strategy = idAttr.IsIdentity;
  97. oId.Strategy = idAttr.IsIdentity;
  98. if (CommonUtils.IsNullOrEmpty(propvalue))
  99. {
  100. strPrimaryKey = DbEntityUtils.GetPrimaryKey(propertyAttr, dbOpType);
  101. if (!string.IsNullOrEmpty(strPrimaryKey))
  102. propvalue = strPrimaryKey;
  103. }
  104. }
  105. oId.Value = propvalue;
  106. if (tableInfo.Id.Key == null)
  107. {
  108. tableInfo.Id = oId;
  109. }
  110. tableInfo.Ids.Add(oId);
  111. tableInfo.Keycolumns.Put(propName, columnName);
  112. tableInfo.PropToColumn.Put(propName, columnName);
  113. breakForeach = true;
  114. }
  115. }
  116. //if (breakForeach && dbOpType == DbOperateType.DELETE) break;
  117. if (breakForeach) { breakForeach = false; continue; }
  118. tableInfo.Columns.Put(columnName, propvalue);
  119. tableInfo.PropToColumn.Put(propName, columnName);
  120. }
  121. if (dbOpType == DbOperateType.UPDATE)
  122. {
  123. tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  124. }
  125. return tableInfo;
  126. }
  127. public static List<T> ToList<T>(IDataReader sdr, TableInfo tableInfo, PropertyInfo[] properties) where T : new()
  128. {
  129. var list = new List<T>();
  130. while (sdr.Read())
  131. {
  132. var entity = new T();
  133. foreach (PropertyInfo property in properties)
  134. {
  135. if (property.Name == EasyNetGlobalConstWord.ROWINDEX) continue;
  136. var name = tableInfo.PropToColumn[property.Name].ToString();
  137. if (tableInfo.TableName == string.Empty)
  138. {
  139. if (DbEntityUtils.IsCaseColumn(property, DbOperateType.SELECT)) continue;
  140. ReflectionHelper.SetPropertyValue(entity, property, sdr[name]);
  141. continue;
  142. }
  143. ReflectionHelper.SetPropertyValue(entity, property, sdr[name]);
  144. }
  145. list.Add(entity);
  146. }
  147. return list;
  148. }
  149. public static List<T> ToList<T>(IDataReader sdr) where T : new()
  150. {
  151. var list = new List<T>();
  152. var properties = ReflectionHelper.GetProperties(new T().GetType());
  153. while (sdr.Read())
  154. {
  155. var entity = new T();
  156. foreach (PropertyInfo property in properties)
  157. {
  158. var name = property.Name;
  159. if (name == EasyNetGlobalConstWord.ROWINDEX) continue;
  160. ReflectionHelper.SetPropertyValue(entity, property, sdr[name]);
  161. }
  162. list.Add(entity);
  163. }
  164. return list;
  165. }
  166. public static List<T> ToListForQuery<T>(IDataReader sdr) where T : new()
  167. {
  168. var list = new List<T>();
  169. var properties = ReflectionHelper.GetProperties(new T().GetType());
  170. while (sdr.Read())
  171. {
  172. var entity = new T();
  173. for (int index = 0; index < sdr.FieldCount; index++)
  174. {
  175. var name = sdr.GetName(index);
  176. var property = ReflectionHelper.GetProperty(properties, name);
  177. if (property == null) continue;
  178. ReflectionHelper.SetPropertyValue(entity, property, sdr.GetValue(index));
  179. }
  180. list.Add(entity);
  181. }
  182. return list;
  183. }
  184. public static List<Dictionary<string, object>> ToListForQuery(IDataReader sdr, PropertyInfo[] properties)
  185. {
  186. var list = new List<Dictionary<string, object>>();
  187. while (sdr.Read())
  188. {
  189. var dic = new Dictionary<string, object>();
  190. for (int index = 0; index < sdr.FieldCount; index++)
  191. {
  192. var name = sdr.GetName(index);
  193. var property = ReflectionHelper.GetProperty(properties, name);
  194. if (property == null) continue;
  195. dic.Add(property.Name, sdr.GetValue(index));
  196. }
  197. list.Add(dic);
  198. }
  199. return list;
  200. }
  201. public static string GetQuerySql(TableInfo tableInfo, DbCondition condition)
  202. {
  203. var sbColumns = new StringBuilder();
  204. tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  205. foreach (String key in tableInfo.Columns.Keys)
  206. {
  207. var nKey = DbKeywords.FormatColumnName(key.Trim());
  208. sbColumns.Append(nKey).Append(",");
  209. }
  210. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  211. var sSqls = String.Empty;
  212. if (String.IsNullOrEmpty(condition.queryString))
  213. {
  214. sSqls = "SELECT {0} FROM {1}";
  215. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo));
  216. sSqls += condition.ToString();
  217. }
  218. else
  219. {
  220. sSqls = condition.ToString();
  221. }
  222. sSqls = sSqls.ToUpper();
  223. return sSqls;
  224. }
  225. public static string GetQueryAllSql(TableInfo tableInfo)
  226. {
  227. var sbColumns = new StringBuilder();
  228. tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  229. foreach (String key in tableInfo.Columns.Keys)
  230. {
  231. var nKey = DbKeywords.FormatColumnName(key.Trim());
  232. sbColumns.Append(nKey).Append(",");
  233. }
  234. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  235. var sSqls = "SELECT {0} FROM {1}";
  236. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo));
  237. return sSqls;
  238. }
  239. public static string GetQueryByIdSql(TableInfo tableInfo)
  240. {
  241. var sbColumns = new StringBuilder();
  242. //if (tableInfo.Columns.ContainsKey(tableInfo.Id.Key))
  243. // tableInfo.Columns[tableInfo.Id.Key] = tableInfo.Id.Value;
  244. //else
  245. // tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  246. foreach (String key in tableInfo.PropToColumn.Keys)
  247. {
  248. var nKey = DbKeywords.FormatColumnName(key.Trim());
  249. sbColumns.Append(nKey).Append(",");
  250. }
  251. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  252. var sSqls = "SELECT {0} FROM {1} WHERE {2} = " + AdoHelper.DbParmChar + "{2}";
  253. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo), tableInfo.Id.Key);
  254. return sSqls;
  255. }
  256. public static string GetQueryByFieldSql(TableInfo tableInfo, string field)
  257. {
  258. var sbColumns = new StringBuilder();
  259. foreach (String key in tableInfo.PropToColumn.Keys)
  260. {
  261. var nKey = DbKeywords.FormatColumnName(key.Trim());
  262. sbColumns.Append(nKey).Append(",");
  263. }
  264. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  265. var sSqls = "SELECT {0} FROM {1} WHERE {2} = " + AdoHelper.DbParmChar + "{2}";
  266. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo), field);
  267. return sSqls;
  268. }
  269. public static string GetQueryByObjSql(TableInfo tableInfo, ref Dictionary<string, object> wh, Dictionary<string, object> sort = null)
  270. {
  271. var sbColumns = new StringBuilder();
  272. var sWheres = new StringBuilder();
  273. var sSorts = new StringBuilder();
  274. foreach (String key in tableInfo.PropToColumn.Keys)
  275. {
  276. var nKey = DbKeywords.FormatColumnName(key.Trim());
  277. sbColumns.Append(nKey).Append(",");
  278. }
  279. if (wh != null)
  280. {
  281. var newWh = new Dictionary<string, object>();
  282. var pm_wh = new ParamMap();
  283. pm_wh.Paramters(wh);
  284. foreach (string key in pm_wh.Keys)
  285. {
  286. if (key != EasyNetGlobalConstWord.SORT)
  287. {
  288. var sParmChar = new StringBuilder();
  289. var sKey = "";
  290. object sVal = "";
  291. var nKey = DbKeywords.FormatColumnName(key.Trim());
  292. switch (nKey)
  293. {
  294. case "_OR_":
  295. sParmChar = GetWhsOrChid(key, pm_wh, ref sKey, ref sVal);
  296. var oOr = sVal as Object[];
  297. foreach (Object jo in oOr)
  298. {
  299. var dic = jo as Dictionary<string, object>;
  300. foreach (string keyNew in dic.Keys)
  301. {
  302. if (!newWh.ContainsKey(keyNew))
  303. {
  304. newWh.Add(keyNew, dic[keyNew]);
  305. }
  306. }
  307. }
  308. break;
  309. case "_AND_":
  310. sParmChar = GetWhsAndChid(key, pm_wh, ref sKey, ref sVal);
  311. if (sVal != null && sVal.ToString() != "")
  312. {
  313. var oAnd = sVal as Object[];
  314. foreach (Object jo in oAnd)
  315. {
  316. if (jo != null)
  317. {
  318. var dic = jo as Dictionary<string, object>;
  319. foreach (string keyNew in dic.Keys)
  320. {
  321. if (!newWh.ContainsKey(keyNew))
  322. {
  323. newWh.Add(keyNew, dic[keyNew]);
  324. }
  325. }
  326. }
  327. }
  328. }
  329. break;
  330. default:
  331. sParmChar = GetWhsAnd(key, pm_wh, ref sKey, ref sVal);
  332. newWh.Add(sKey, sVal);
  333. break;
  334. }
  335. if (sParmChar.Length > 0)
  336. {
  337. sWheres.Append(sParmChar).Append(" AND ");
  338. }
  339. }
  340. }
  341. wh = newWh;
  342. }
  343. if (sort != null)
  344. {
  345. foreach (string key in sort.Keys)
  346. {
  347. var nKey = DbKeywords.FormatColumnName(key.Trim());
  348. sSorts.Append(nKey).Append(" " + sort[nKey]).Append(",");
  349. }
  350. }
  351. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  352. if (sWheres.Length > 0) sWheres.Remove(sWheres.ToString().Length - 4, 4);
  353. if (sSorts.Length > 0) sSorts.Remove(sSorts.ToString().Length - 1, 1);
  354. var sSqls = "SELECT {0} FROM {1}" + ((sWheres.Length == 0) ? "" : " WHERE {2} ") + ((sSorts.Length == 0) ? "" : " ORDER BY {3} ");
  355. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo), sWheres.ToString(), sSorts.ToString());
  356. //if (sWheres.Length == 0) sSqls.Remove(sSqls.ToString().Length - 8, 8);
  357. return sSqls;
  358. }
  359. public static string GetQueryCountSql(TableInfo tableInfo)
  360. {
  361. var sbColumns = new StringBuilder();
  362. var sSqls = "SELECT COUNT(0) FROM {1} ";
  363. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo));
  364. foreach (String key in tableInfo.Columns.Keys)
  365. {
  366. var nKey = DbKeywords.FormatColumnName(key.Trim());
  367. sbColumns.Append(nKey).Append("=").Append(AdoHelper.DbParmChar).Append(key);
  368. }
  369. if (sbColumns.Length > 0)
  370. {
  371. sSqls += " WHERE " + sbColumns.ToString();
  372. }
  373. return sSqls;
  374. }
  375. public static string GetQueryCountSql(TableInfo tableInfo, DbCondition condition)
  376. {
  377. var sSqls = "SELECT COUNT(0) FROM {0}";
  378. sSqls = string.Format(sSqls, GetFullTableName(tableInfo));
  379. sSqls += condition.ToString();
  380. return sSqls;
  381. }
  382. public static string GetQueryByPropertySql(TableInfo tableInfo)
  383. {
  384. var sbColumns = new StringBuilder();
  385. tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  386. foreach (String key in tableInfo.Columns.Keys)
  387. {
  388. var nKey = DbKeywords.FormatColumnName(key.Trim());
  389. sbColumns.Append(nKey).Append(",");
  390. }
  391. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  392. var sSqls = "SELECT {0} FROM {1} WHERE {2} = " + AdoHelper.DbParmChar + "{2}";
  393. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo), tableInfo.Id.Key);
  394. return sSqls;
  395. }
  396. public static string GetQueryByPage(TableInfo tableInfo, ref ParamMap pm)
  397. {
  398. var sbColumns = new StringBuilder();
  399. var sbWhs = new StringBuilder();
  400. //tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  401. foreach (String key in tableInfo.PropToColumn.Keys)
  402. {
  403. var nKey = DbKeywords.FormatColumnName(key.Trim());
  404. sbColumns.Append(nKey).Append(",");
  405. }
  406. if (pm.Keys.Count > 0)
  407. {
  408. var newPm = new ParamMap();
  409. var BRemoveOr = false;
  410. var BRemoveAnd = false;
  411. foreach (string key in pm.Keys)
  412. {
  413. if (key != EasyNetGlobalConstWord.SORT)
  414. {
  415. var sParmChar = new StringBuilder();
  416. var sKey = "";
  417. object sVal = "";
  418. if (key == "pageStart" || key == "pageEnd")
  419. {
  420. continue;
  421. }
  422. switch (key)
  423. {
  424. case "_OR_":
  425. sParmChar = GetWhsOrChid(key, pm, ref sKey, ref sVal);
  426. var oOr = sVal as Object[];
  427. foreach (Object jo in oOr)
  428. {
  429. var dic = jo as Dictionary<string, object>;
  430. foreach (string keyNew in dic.Keys)
  431. {
  432. if (!newPm.ContainsKey(keyNew))
  433. {
  434. newPm.Put(keyNew, dic[keyNew]);
  435. }
  436. }
  437. }
  438. BRemoveOr = true;
  439. break;
  440. case "_AND_":
  441. sParmChar = GetWhsAndChid(key, pm, ref sKey, ref sVal);
  442. if (sVal != null && sVal.ToString() != "")
  443. {
  444. var oAnd = sVal as Object[];
  445. foreach (Object jo in oAnd)
  446. {
  447. if (jo != null)
  448. {
  449. var dic = jo as Dictionary<string, object>;
  450. foreach (string keyNew in dic.Keys)
  451. {
  452. if (!newPm.ContainsKey(keyNew))
  453. {
  454. newPm.Put(keyNew, dic[keyNew]);
  455. }
  456. }
  457. }
  458. }
  459. }
  460. BRemoveAnd = true;
  461. break;
  462. default:
  463. sParmChar = GetWhsAnd(key, pm, ref sKey, ref sVal);
  464. newPm.Put(sKey, sVal);
  465. break;
  466. }
  467. if (sParmChar.Length > 0)
  468. {
  469. sbWhs.Append(sParmChar).Append(" AND ");
  470. }
  471. }
  472. }
  473. if (BRemoveOr)
  474. {
  475. pm.Remove("_OR_");
  476. }
  477. if (BRemoveAnd)
  478. {
  479. pm.Remove("_AND_");
  480. }
  481. foreach (String key in newPm.Keys)
  482. {
  483. pm.Put(key, newPm[key]);
  484. }
  485. //pm = newPm;
  486. }
  487. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  488. var sSqls = "SELECT {0} FROM {1}";
  489. if (sbWhs.Length > 0)
  490. {
  491. sbWhs.Remove(sbWhs.ToString().Length - 5, 5);
  492. sSqls += " WHERE {2}";
  493. }
  494. sSqls = string.Format(sSqls, sbColumns.ToString(), GetFullTableName(tableInfo), sbWhs);
  495. return sSqls;
  496. }
  497. public static string GetAutoSql()
  498. {
  499. var autoSQL = "";
  500. if (AdoHelper.DbType == DatabaseType.SQLSERVER)
  501. {
  502. autoSQL = " select scope_identity() as AutoId ";
  503. }
  504. if (AdoHelper.DbType == DatabaseType.ACCESS)
  505. {
  506. autoSQL = " select @@IDENTITY as AutoId ";
  507. }
  508. if (AdoHelper.DbType == DatabaseType.MYSQL)
  509. {
  510. autoSQL = " ;select @@identity ";
  511. }
  512. return autoSQL;
  513. }
  514. public static string GetFullTableName(TableInfo tbinfo)
  515. {
  516. var sTableName = "";
  517. if (AdoHelper.DbType == DatabaseType.SQLSERVER)
  518. {
  519. sTableName = tbinfo.TableName;
  520. }
  521. if (AdoHelper.DbType == DatabaseType.ACCESS)
  522. {
  523. }
  524. if (AdoHelper.DbType == DatabaseType.MYSQL)
  525. {
  526. }
  527. return sTableName;
  528. }
  529. public static string GetInsertSql(TableInfo tableInfo)
  530. {
  531. var sbColumns = new StringBuilder();
  532. var sbValues = new StringBuilder();
  533. foreach (IdInfo Id in tableInfo.Ids)
  534. {
  535. if (!Id.Strategy)
  536. {
  537. if (!Id.Strategy && tableInfo.Id.Value == null)
  538. {
  539. Id.Value = Guid.NewGuid().ToString();
  540. }
  541. if (Id.Value != null)
  542. {
  543. tableInfo.Columns.Put(Id.Key, Id.Value);
  544. }
  545. }
  546. }
  547. foreach (String key in tableInfo.Columns.Keys)
  548. {
  549. var value = tableInfo.Columns[key];
  550. if (!string.IsNullOrEmpty(key.Trim()) && value != null)
  551. {
  552. var nKey = DbKeywords.FormatColumnName(key.Trim());
  553. sbColumns.Append(nKey).Append(",");
  554. sbValues.Append(AdoHelper.DbParmChar).Append(key).Append(",");
  555. }
  556. }
  557. if (sbColumns.Length > 0 && sbValues.Length > 0)
  558. {
  559. sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  560. sbValues.Remove(sbValues.ToString().Length - 1, 1);
  561. }
  562. var sSqls = "INSERT INTO {0}({1}) VALUES({2})";
  563. sSqls = string.Format(sSqls, GetFullTableName(tableInfo), sbColumns.ToString(), sbValues.ToString());
  564. //if (tableInfo.Strategy == GenerationType.INDENTITY && (AdoHelper.DbType == DatabaseType.SQLSERVER || AdoHelper.DbType == DatabaseType.MYSQL))
  565. //{
  566. // string autoSql = DbEntityUtils.GetAutoSql();
  567. // sSqls = sSqls + autoSql;
  568. //}
  569. return sSqls;
  570. }
  571. public static string GetUpdateSql(TableInfo tableInfo)
  572. {
  573. var sbBody = new StringBuilder();
  574. foreach (String key in tableInfo.Columns.Keys)
  575. {
  576. var value = tableInfo.Columns[key];
  577. if (IsCaseColumn(tableInfo, key)) continue;
  578. if (!string.IsNullOrEmpty(key.Trim()) && value != null)
  579. {
  580. var nKey = DbKeywords.FormatColumnName(key.Trim());
  581. sbBody.Append(nKey).Append("=").Append(AdoHelper.DbParmChar + key).Append(",");
  582. }
  583. }
  584. if (sbBody.Length > 0) sbBody.Remove(sbBody.ToString().Length - 1, 1);
  585. tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value);
  586. var sSqls = "update {0} set {1} where {2} =" + AdoHelper.DbParmChar + tableInfo.Id.Key;
  587. sSqls = string.Format(sSqls, GetFullTableName(tableInfo), sbBody.ToString(), tableInfo.Id.Key);
  588. return sSqls;
  589. }
  590. public static string GetDeleteByIdSql(TableInfo tableInfo)
  591. {
  592. var sSql = "delete from {0} where {1}";
  593. var sWheres = new StringBuilder();
  594. foreach (IdInfo oId in tableInfo.Ids)
  595. {
  596. sWheres.Append(oId.Key).Append(" = " + AdoHelper.DbParmChar + oId.Key).Append(" and ");
  597. }
  598. if (sWheres.Length > 0) sWheres.Remove(sWheres.ToString().Length - 4, 4);
  599. sSql = string.Format(sSql, GetFullTableName(tableInfo), sWheres);
  600. return sSql;
  601. }
  602. public static string GetDeleteByFieldSql(TableInfo tableInfo, string FieldName)
  603. {
  604. var sSqls = "delete from {0} where {1} =" + AdoHelper.DbParmChar + FieldName;
  605. sSqls = string.Format(sSqls, GetFullTableName(tableInfo), FieldName);
  606. return sSqls;
  607. }
  608. public static string GetInsertByObjSql(TableInfo tableInfo, Dictionary<string, object> val)
  609. {
  610. var sbColumns = new StringBuilder();
  611. var sbValues = new StringBuilder();
  612. foreach (IdInfo Id in tableInfo.Ids)
  613. {
  614. var sGuid = Guid.NewGuid().ToString();
  615. if (Id.Key != null && Id.Strategy)
  616. {
  617. continue;
  618. }
  619. if (val.ContainsKey(Id.Key))
  620. {
  621. if (val[Id.Key] == null || val[Id.Key].ToString() == "")
  622. {
  623. val[Id.Key] = sGuid;
  624. }
  625. }
  626. else
  627. {
  628. val.Add(Id.Key, sGuid);
  629. }
  630. }
  631. foreach (String key in val.Keys)
  632. {
  633. var nKey = DbKeywords.FormatColumnName(key.Trim());
  634. if (tableInfo.PropToColumn.ContainsKey(key))
  635. {
  636. sbColumns.Append(nKey).Append(",");
  637. sbValues.Append(AdoHelper.DbParmChar).Append(key).Append(",");
  638. }
  639. }
  640. if (sbColumns.Length > 0 && sbValues.Length > 0)
  641. {
  642. sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  643. sbValues.Remove(sbValues.ToString().Length - 1, 1);
  644. }
  645. var sSqls = "INSERT INTO {0}({1}) VALUES({2})";
  646. sSqls = string.Format(sSqls, GetFullTableName(tableInfo), sbColumns.ToString(), sbValues.ToString());
  647. return sSqls;
  648. }
  649. public static string GetUpdateByObjSql(TableInfo tableInfo, ref Dictionary<string, object> val, Dictionary<string, object> wh)
  650. {
  651. var dic = new Dictionary<string, object>();
  652. var sBody = new StringBuilder();
  653. var sWheres = new StringBuilder();
  654. foreach (String key in val.Keys)
  655. {
  656. if (IsCaseColumn(tableInfo, key) || wh.ContainsKey(key)) continue;
  657. var nKey = DbKeywords.FormatColumnName(key.Trim());
  658. sBody.Append(nKey).Append("=").Append(AdoHelper.DbParmChar + key).Append(",");
  659. dic.Add(key, val[key]);
  660. }
  661. foreach (String key in wh.Keys)
  662. {
  663. var nKey = DbKeywords.FormatColumnName(key.Trim());
  664. sWheres.Append(nKey).Append(" = " + AdoHelper.DbParmChar + nKey).Append(" and ");
  665. }
  666. if (sBody.Length > 0) sBody.Remove(sBody.ToString().Length - 1, 1);
  667. if (sWheres.Length > 0) sWheres.Remove(sWheres.ToString().Length - 4, 4);
  668. var sSql = "update {0} set {1} where {2}";
  669. sSql = string.Format(sSql, GetFullTableName(tableInfo), sBody, sWheres);
  670. val = dic;
  671. return sSql;
  672. }
  673. public static string GetDeleteByObjSql(TableInfo tableInfo, object parms)
  674. {
  675. var sWheres = new StringBuilder();
  676. var childparms = parms as Dictionary<string, object>;
  677. var pm_wh = new ParamMap();
  678. pm_wh.Paramters(childparms);
  679. foreach (String key in childparms.Keys)
  680. {
  681. var sParmChar = new StringBuilder();
  682. var sKey = DbKeywords.FormatColumnName(key.Trim());
  683. object sVal = null;
  684. //sWheres.Append(nKey).Append(" = " + AdoHelper.DbParmChar + nKey).Append(" and ");
  685. sParmChar = GetWhsAnd(key, pm_wh, ref sKey, ref sVal);
  686. if (sParmChar.Length > 0)
  687. {
  688. sWheres.Append(sParmChar).Append(" AND ");
  689. }
  690. }
  691. if (sWheres.Length > 0) sWheres.Remove(sWheres.ToString().Length - 4, 4);
  692. //string sSqls = "SELECT count(0) FROM {0} WHERE {1}" + System.Environment.NewLine;
  693. var sSqls = "delete from {0} where {1}";
  694. sSqls = string.Format(sSqls, GetFullTableName(tableInfo), sWheres);
  695. return sSqls;
  696. }
  697. public static void SetParameters(Map columns, params IDbDataParameter[] parms)
  698. {
  699. var i = 0;
  700. foreach (string key in columns.Keys)
  701. {
  702. if (!string.IsNullOrEmpty(key.Trim()))
  703. {
  704. var value = columns[key];
  705. if (value == null) value = DBNull.Value;
  706. parms[i].ParameterName = key;
  707. parms[i].Value = value;
  708. i++;
  709. }
  710. }
  711. }
  712. public static bool IsCaseColumn(object attribute, DbOperateType dbOperateType)
  713. {
  714. if (attribute is SqlSugar.SugarColumn)
  715. {
  716. var columnAttr = attribute as SqlSugar.SugarColumn;
  717. if (columnAttr.IsIgnore)
  718. {
  719. return true;
  720. }
  721. if (!columnAttr.IsOnlyIgnoreInsert && dbOperateType == DbOperateType.INSERT)
  722. {
  723. return true;
  724. }
  725. }
  726. return false;
  727. }
  728. public static bool IsCaseColumn(PropertyInfo property, DbOperateType dbOperateType)
  729. {
  730. var isBreak = false;
  731. var propertyAttrs = property.GetCustomAttributes(false);
  732. foreach (object propertyAttr in propertyAttrs)
  733. {
  734. if (DbEntityUtils.IsCaseColumn(propertyAttr, dbOperateType))
  735. {
  736. isBreak = true; break;
  737. }
  738. }
  739. return isBreak;
  740. }
  741. public static bool IsCaseColumn(TableInfo tableInfo, string col)
  742. {
  743. var isBreak = false;
  744. do
  745. {
  746. if (col == "RowIndex" || col == "CreateUser" || col == "CreateDate" || col == tableInfo.Id.Key)
  747. {
  748. isBreak = true; break;
  749. }
  750. foreach (IdInfo Id in tableInfo.Ids)
  751. {
  752. if (Id.Key == col)
  753. {
  754. isBreak = true; break;
  755. }
  756. }
  757. }
  758. while (false);
  759. return isBreak;
  760. }
  761. private static StringBuilder GetWhsAnd(string sKey, ParamMap pm, ref string r_sKey, ref object r_sVal, string dkey = null)
  762. {
  763. var sParmChar = new StringBuilder();
  764. do
  765. {
  766. var nKey = sKey.Trim();
  767. var pmKey = nKey;
  768. if (dkey != null)
  769. {
  770. pmKey = dkey.Trim();
  771. }
  772. var sValue = pm[pmKey].ToString().Trim();
  773. var sColType = "";
  774. if (nKey.StartsWith("INCLUDNULL_"))
  775. {
  776. sColType = "includenull";
  777. nKey = nKey.Replace("INCLUDNULL_", "");
  778. }
  779. else if (nKey.StartsWith("INCLUDNULLINT_"))
  780. {
  781. sColType = "includenullint";
  782. nKey = nKey.Replace("INCLUDNULLINT_", "");
  783. }
  784. else if (nKey.StartsWith("ISNULL_"))
  785. {
  786. sColType = "isnull";
  787. nKey = nKey.Replace("ISNULL_", "");
  788. }
  789. else if (nKey.StartsWith("ISBLANK_"))
  790. {
  791. sColType = "isblank";
  792. nKey = nKey.Replace("ISBLANK_", "");
  793. }
  794. else if (nKey.StartsWith("CHARINDEX_"))
  795. {
  796. sColType = "_charindex";
  797. nKey = nKey.Replace("CHARINDEX_", "");
  798. }
  799. else if (nKey.StartsWith("_CHARINDEX_"))
  800. {
  801. sColType = "charindex_";
  802. nKey = nKey.Replace("_CHARINDEX_", "");
  803. }
  804. else if (nKey.StartsWith("IN_"))
  805. {
  806. sColType = "in";
  807. nKey = nKey.Replace("IN_", "");
  808. }
  809. if (nKey.IndexOf("+") > -1)
  810. {
  811. sColType = "multicolumn";
  812. nKey = nKey.Split('+')[0].ToString().Trim();
  813. }
  814. nKey = DbKeywords.FormatColumnName(nKey);
  815. var sCn = " = ";
  816. if (-1 != sValue.IndexOf("|"))
  817. {
  818. }
  819. else if (sValue.StartsWith("=="))
  820. {
  821. }
  822. else if (sValue.StartsWith(">="))
  823. {
  824. sCn = " >= ";
  825. sValue = sValue.Replace(">=", "").Trim();
  826. sColType = "gt_eq";
  827. }
  828. else if (sValue.StartsWith("<="))
  829. {
  830. sCn = " <= ";
  831. sValue = sValue.Replace("<=", "").Trim();
  832. sColType = "lt_eq";
  833. }
  834. else if (sValue.StartsWith(">>="))
  835. {
  836. sCn = " >= ";
  837. sValue = sValue.Replace(">>=", "").Trim();
  838. sColType = "agt_eq";
  839. }
  840. else if (sValue.StartsWith("<<="))
  841. {
  842. sCn = " <= ";
  843. sValue = sValue.Replace("<<=", "").Trim();
  844. sColType = "alt_eq";
  845. }
  846. else if (sValue.StartsWith("<>") || sValue.StartsWith("!="))
  847. {
  848. sCn = " <> ";
  849. sValue = sValue.Replace("<>", "").Replace("!=", "").Trim();
  850. }
  851. else if (sValue.StartsWith(">"))
  852. {
  853. sCn = " > ";
  854. sValue = sValue.Replace(">", "").Trim();
  855. }
  856. else if (sValue.StartsWith("<"))
  857. {
  858. sCn = " < ";
  859. sValue = sValue.Replace("<", "").Trim();
  860. }
  861. else if (-1 != sValue.IndexOf(".."))
  862. {
  863. }
  864. else if (sValue.StartsWith("%"))
  865. {
  866. sCn = " LIKE ";
  867. }
  868. if (sCn == " <> ")
  869. {
  870. sParmChar.Append(nKey + sCn + AdoHelper.DbParmChar + pmKey);
  871. }
  872. else if (sColType == "isnull")
  873. {
  874. sParmChar.Append(nKey + " IS NULL ");
  875. }
  876. else if (sColType == "isblank")
  877. {
  878. sParmChar.Append("ISNULL(" + nKey + ",'')" + " = " + AdoHelper.DbParmChar + pmKey);
  879. }
  880. else if (sValue != "")
  881. {
  882. switch (sColType)
  883. {
  884. case "gt_eq":
  885. case "lt_eq":
  886. sParmChar.Append("(" + nKey + sCn + AdoHelper.DbParmChar + pmKey + " OR ISNULL(" + nKey + ",'')=''" + ")");
  887. break;
  888. case "agt_eq":
  889. case "alt_eq":
  890. sParmChar.Append("(" + nKey + sCn + AdoHelper.DbParmChar + pmKey + ")");
  891. break;
  892. case "includenull":
  893. sParmChar.Append("ISNULL(" + nKey + ",'')" + sCn + AdoHelper.DbParmChar + pmKey);
  894. break;
  895. case "includenullint":
  896. sParmChar.Append("ISNULL(" + nKey + ",0)" + sCn + AdoHelper.DbParmChar + pmKey);
  897. break;
  898. case "multicolumn":
  899. sParmChar.Append(sKey + sCn + AdoHelper.DbParmChar + pmKey);
  900. break;
  901. case "_charindex":
  902. sParmChar.Append("CHARINDEX(" + AdoHelper.DbParmChar + pmKey + "," + nKey + ") > 0");
  903. break;
  904. case "charindex_":
  905. sParmChar.Append("CHARINDEX(" + nKey + "," + AdoHelper.DbParmChar + pmKey + ") > 0");
  906. break;
  907. case "in":
  908. sParmChar.Append(nKey + " IN (" + AdoHelper.DbParmChar + pmKey + ")");
  909. break;
  910. default:
  911. sParmChar.Append(nKey + sCn + AdoHelper.DbParmChar + pmKey);
  912. break;
  913. }
  914. }
  915. r_sKey = pmKey;
  916. r_sVal = sValue;
  917. }
  918. while (false);
  919. return sParmChar;
  920. }
  921. private static StringBuilder GetWhsOrChid(string sKey, ParamMap pm, ref string r_sKey, ref object r_oVal)
  922. {
  923. var sParmChar = new StringBuilder();
  924. sParmChar.Append("(");
  925. do
  926. {
  927. var nKey = sKey.Trim();
  928. var oVal = pm[sKey];
  929. var saPm = oVal as Object[];
  930. var oValNew = new Object[saPm.Length];
  931. var iIndex = 0;
  932. foreach (Object jo in saPm)
  933. {
  934. var dic = jo as Dictionary<string, object>;
  935. var dicNew = new Dictionary<string, object>();
  936. var pm_wh = new ParamMap();
  937. pm_wh.Paramters(dic);
  938. sParmChar.Append("(");
  939. foreach (string key in dic.Keys)
  940. {
  941. var sKeyNew = "";
  942. object sValNew = "";
  943. var nKeyNew = DbKeywords.FormatColumnName(key.Trim());
  944. var sPmChar = GetWhsAnd(key, pm_wh, ref sKeyNew, ref sValNew);
  945. if (sPmChar.Length > 0)
  946. {
  947. sParmChar.Append(sPmChar + " AND ");
  948. }
  949. dicNew.Add(sKeyNew, sValNew);
  950. }
  951. sParmChar.Remove(sParmChar.Length - 4, 4);
  952. sParmChar.Append(") OR ");
  953. oValNew[iIndex] = dicNew;
  954. iIndex++;
  955. }
  956. sParmChar.Remove(sParmChar.Length - 3, 3);
  957. sParmChar.Append(")");
  958. r_sKey = nKey;
  959. r_oVal = oValNew;
  960. }
  961. while (false);
  962. return sParmChar;
  963. }
  964. private static StringBuilder GetWhsAndChid(string sKey, ParamMap pm, ref string r_sKey, ref object r_oVal)
  965. {
  966. var sParmChar = new StringBuilder();
  967. sParmChar.Append("(");
  968. do
  969. {
  970. var nKey = sKey.Trim();
  971. var oVal = pm[sKey];
  972. var saPm = oVal as Object[];
  973. var oValNew = new Object[saPm.Length];
  974. var iIndex = 0;
  975. foreach (Object jo in saPm)
  976. {
  977. var dic = jo as Dictionary<string, object>;
  978. var dicNew = new Dictionary<string, object>();
  979. var pm_wh = new ParamMap();
  980. var key = dic["key"].ToString().Trim();
  981. var name = dic["name"].ToString().Trim();
  982. var value = dic["value"];
  983. pm_wh.Add(name, value);
  984. var sKeyNew = "";
  985. object sValNew = "";
  986. var nKeyNew = DbKeywords.FormatColumnName(key);
  987. var sPmChar = GetWhsAnd(key, pm_wh, ref sKeyNew, ref sValNew, name);
  988. dicNew.Add(sKeyNew, sValNew);
  989. if (sPmChar.Length == 0)
  990. {
  991. continue;
  992. }
  993. sParmChar.Append(sPmChar + " AND ");
  994. oValNew[iIndex] = dicNew;
  995. iIndex++;
  996. }
  997. if (sParmChar.Length == 1) { sParmChar = new StringBuilder(); break; }
  998. sParmChar.Remove(sParmChar.Length - 4, 4);
  999. sParmChar.Append(")");
  1000. r_sKey = nKey;
  1001. r_oVal = oValNew;
  1002. }
  1003. while (false);
  1004. return sParmChar;
  1005. }
  1006. }
  1007. }