StringExtension.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. namespace Ant.ORM
  7. {
  8. public static class StringExtension
  9. {
  10. /// <summary>
  11. /// 在SQL语句后面追加注释
  12. /// </summary>
  13. /// <param name="sql">需追加注释的SQL语句</param>
  14. /// <param name="author">作者</param>
  15. /// <param name="forUse">SQL的功能描述</param>
  16. /// <param name="fileName">SQL所在的文件名</param>
  17. /// <param name="funName">SQL所在的方法名</param>
  18. /// <returns>添加注释后的SQL</returns>
  19. private static string SqlAppendComment(this string sql,
  20. string author, string forUse, string fileName, string funName, string projectname)
  21. {
  22. if (author == null)
  23. {
  24. throw new ArgumentNullException("author参数为null或空字符串");
  25. }
  26. if (string.IsNullOrEmpty(forUse))
  27. {
  28. throw new ArgumentNullException("forUse参数为null或空字符串");
  29. }
  30. if (string.IsNullOrEmpty(fileName))
  31. {
  32. throw new ArgumentNullException("fileName参数为null或空字符串");
  33. }
  34. if (string.IsNullOrEmpty(funName))
  35. {
  36. throw new ArgumentNullException("funName参数为null或空字符串");
  37. }
  38. return
  39. string.Format(
  40. @"{0}
  41. --Flat:{5}/Author:{1}/For:{2}/File:{3}/Fun:{4}",
  42. sql,
  43. author,
  44. forUse,
  45. fileName,
  46. funName,
  47. projectname);
  48. }
  49. /// <summary>
  50. /// 在SQL语句后面追加注释
  51. /// </summary>
  52. /// <param name="sql">
  53. /// 需追加注释的SQL语句
  54. /// </param>
  55. /// <param name="comment">
  56. /// 评论注释
  57. /// </param>
  58. /// <returns>
  59. /// 添加注释后的SQL
  60. /// </returns>
  61. public static string AppendComment(this string sql,
  62. SqlComment comment)
  63. {
  64. if (!SqlComment.IsTest)
  65. {
  66. return sql.SqlAppendComment(comment.Author, comment.ForUse, comment.FileName, comment.FunName,comment.ProjectName);
  67. }
  68. return sql;
  69. }
  70. /// <summary>
  71. /// 插入Sql评论
  72. /// </summary>
  73. public class SqlComment
  74. {
  75. /// <summary>
  76. /// 作者
  77. /// </summary>
  78. public string Author { get; set; }
  79. /// <summary>
  80. /// 用途
  81. /// </summary>
  82. public string ForUse { get; set; }
  83. /// <summary>
  84. /// 文件名
  85. /// </summary>
  86. public string FileName { get; set; }
  87. /// <summary>
  88. /// 函数名
  89. /// </summary>
  90. public string FunName { get; set; }
  91. /// <summary>
  92. /// 是否是测试
  93. /// </summary>
  94. public static bool IsTest { get; set; }
  95. /// <summary>
  96. /// 项目名称
  97. /// </summary>
  98. public string ProjectName { get; set; }
  99. }
  100. }
  101. }