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.

115 lines
4.8 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace SqlSugar
  7. {
  8. public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
  9. {
  10. #region Properties
  11. public SqlSugarClient Context { get; set; }
  12. public CommandType CommandType { get; set; }
  13. public DeleteBuilder DeleteBuilder { get; set; }
  14. public InsertBuilder InsertBuilder { get; set; }
  15. public QueryBuilder QueryBuilder { get; set; }
  16. public UpdateBuilder UpdateBuilder { get; set; }
  17. public SqlQueryBuilder SqlQueryBuilder
  18. {
  19. get
  20. {
  21. base._SqlQueryBuilder = UtilMethods.IsNullReturnNew(base._SqlQueryBuilder);
  22. return base._SqlQueryBuilder;
  23. }
  24. set { base._SqlQueryBuilder = value; }
  25. }
  26. #endregion
  27. #region abstract Methods
  28. public virtual string GetTranslationTableName(string name)
  29. {
  30. Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
  31. if (name.IsContainsIn("(", ")", SqlTranslationLeft))
  32. {
  33. return name;
  34. }
  35. var context = this.Context;
  36. var mappingInfo = context
  37. .MappingTables
  38. .FirstOrDefault(it => it.EntityName.Equals(name, StringComparison.CurrentCultureIgnoreCase));
  39. name = (mappingInfo == null ? name : mappingInfo.DbTableName);
  40. if (name.IsContainsIn("(", ")", SqlTranslationLeft))
  41. {
  42. return name;
  43. }
  44. if (name.Contains("."))
  45. {
  46. return string.Join(".", name.Split('.').Select(it => SqlTranslationLeft + it + SqlTranslationRight));
  47. }
  48. else
  49. {
  50. return SqlTranslationLeft + name + SqlTranslationRight;
  51. }
  52. }
  53. public virtual string GetTranslationColumnName(string entityName, string propertyName)
  54. {
  55. Check.ArgumentNullException(entityName, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
  56. Check.ArgumentNullException(propertyName, string.Format(ErrorMessage.ObjNotExist, "Column Name"));
  57. var context = this.Context;
  58. var mappingInfo = context
  59. .MappingColumns
  60. .FirstOrDefault(it =>
  61. it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase) &&
  62. it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
  63. return (mappingInfo == null ? SqlTranslationLeft + propertyName + SqlTranslationRight : SqlTranslationLeft + mappingInfo.DbColumnName + SqlTranslationRight);
  64. }
  65. public virtual string GetTranslationColumnName(string propertyName)
  66. {
  67. if (propertyName.Contains(SqlTranslationLeft)) return propertyName;
  68. else
  69. return SqlTranslationLeft + propertyName + SqlTranslationRight;
  70. }
  71. public virtual string GetNoTranslationColumnName(string name)
  72. {
  73. if (!name.Contains(SqlTranslationLeft)) return name;
  74. return name == null ? string.Empty : Regex.Match(name, @".*" + "\\" + SqlTranslationLeft + "(.*?)" + "\\" + SqlTranslationRight + "").Groups[1].Value;
  75. }
  76. public virtual string GetPackTable(string sql, string shortName)
  77. {
  78. return UtilMethods.GetPackTable(sql, shortName);
  79. }
  80. public virtual string GetDefaultShortName()
  81. {
  82. return "t";
  83. }
  84. public virtual string GetUnionAllSql(List<string> sqlList)
  85. {
  86. return string.Join("UNION ALL \r\n", sqlList);
  87. }
  88. public virtual void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
  89. {
  90. UtilMethods.RepairReplicationParameters(ref appendSql, parameters, addIndex);
  91. }
  92. #endregion
  93. #region Common SqlTemplate
  94. public string AppendWhereOrAnd(bool isWhere, string sqlString)
  95. {
  96. return isWhere ? (" WHERE " + sqlString) : (" AND " + sqlString);
  97. }
  98. public string AppendHaving(string sqlString)
  99. {
  100. return " HAVING " + sqlString;
  101. }
  102. public virtual string SqlParameterKeyWord { get { return "@"; } }
  103. public abstract string SqlTranslationLeft { get; }
  104. public abstract string SqlTranslationRight { get; }
  105. public virtual string SqlFalse { get { return "1=2 "; } }
  106. public virtual string SqlDateNow { get { return "GETDATE()"; } }
  107. public virtual string FullSqlDateNow { get { return "SELECT GETDATE()"; } }
  108. public virtual string SqlSelectAll { get { return "*"; } }
  109. #endregion
  110. }
  111. }