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.

284 lines
14 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace SqlSugar
  5. {
  6. public abstract partial class DbMaintenanceProvider : IDbMaintenance
  7. {
  8. #region DML
  9. public virtual List<DbTableInfo> GetViewInfoList()
  10. {
  11. var cacheKey = "DbMaintenanceProvider.GetViewInfoList";
  12. cacheKey = GetCacheKey(cacheKey);
  13. var result = GetListOrCache<DbTableInfo>(cacheKey, this.GetViewInfoListSql);
  14. foreach (var item in result)
  15. {
  16. item.DbObjectType = DbObjectType.View;
  17. }
  18. return result;
  19. }
  20. public virtual List<DbTableInfo> GetTableInfoList()
  21. {
  22. var cacheKey = "DbMaintenanceProvider.GetTableInfoList";
  23. cacheKey = GetCacheKey(cacheKey);
  24. var result = GetListOrCache<DbTableInfo>(cacheKey, this.GetTableInfoListSql);
  25. foreach (var item in result)
  26. {
  27. item.DbObjectType = DbObjectType.Table;
  28. }
  29. return result;
  30. }
  31. public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
  32. {
  33. if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>();
  34. var cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
  35. cacheKey = GetCacheKey(cacheKey);
  36. return GetListOrCache<DbColumnInfo>(cacheKey, string.Format(this.GetColumnInfosByTableNameSql, tableName));
  37. }
  38. public virtual List<string> GetIsIdentities(string tableName)
  39. {
  40. var cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
  41. cacheKey = GetCacheKey(cacheKey);
  42. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,() =>
  43. {
  44. var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
  45. return result.Select(it => it.DbColumnName).ToList();
  46. });
  47. }
  48. public virtual List<string> GetPrimaries(string tableName)
  49. {
  50. var cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
  51. cacheKey = GetCacheKey(cacheKey);
  52. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,() =>
  53. {
  54. var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
  55. return result.Select(it => it.DbColumnName).ToList();
  56. });
  57. }
  58. #endregion
  59. #region Check
  60. public virtual bool IsAnyTable(string tableName)
  61. {
  62. tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
  63. var tables = GetTableInfoList();
  64. if (tables == null) return false;
  65. else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
  66. }
  67. public virtual bool IsAnyColumn(string tableName, string columnName)
  68. {
  69. columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
  70. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  71. var isAny = IsAnyTable(tableName);
  72. Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
  73. var columns = GetColumnInfosByTableName(tableName);
  74. if (columns.IsNullOrEmpty()) return false;
  75. return columns.Any(it => it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
  76. }
  77. public virtual bool IsPrimaryKey(string tableName, string columnName)
  78. {
  79. columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
  80. var isAny = IsAnyTable(tableName);
  81. Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
  82. var columns = GetColumnInfosByTableName(tableName);
  83. if (columns.IsNullOrEmpty()) return false;
  84. return columns.Any(it => it.IsPrimarykey = true && it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
  85. }
  86. public virtual bool IsIdentity(string tableName, string columnName)
  87. {
  88. columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
  89. var isAny = IsAnyTable(tableName);
  90. Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
  91. var columns = GetColumnInfosByTableName(tableName);
  92. if (columns.IsNullOrEmpty()) return false;
  93. return columns.Any(it => it.IsIdentity = true && it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
  94. }
  95. public virtual bool IsAnyConstraint(string constraintName)
  96. {
  97. return this.Context.Ado.GetInt("select object_id('" + constraintName + "')") > 0;
  98. }
  99. public virtual bool IsAnySystemTablePermissions()
  100. {
  101. this.Context.Ado.CheckConnection();
  102. var sql = this.CheckSystemTablePermissionsSql;
  103. try
  104. {
  105. var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
  106. this.Context.Ado.IsEnableLogEvent = false;
  107. this.Context.Ado.ExecuteCommand(sql);
  108. this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
  109. return true;
  110. }
  111. catch
  112. {
  113. return false;
  114. }
  115. }
  116. #endregion
  117. #region DDL
  118. public virtual bool AddPrimaryKey(string tableName, string columnName)
  119. {
  120. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  121. columnName = this.SqlBuilder.GetTranslationTableName(columnName);
  122. var sql = string.Format(this.AddPrimaryKeySql, tableName, string.Format("PK_{0}_{1}", this.SqlBuilder.GetNoTranslationColumnName(tableName), this.SqlBuilder.GetNoTranslationColumnName(columnName)), columnName);
  123. this.Context.Ado.ExecuteCommand(sql);
  124. return true;
  125. }
  126. public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
  127. {
  128. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  129. var sql = GetAddColumnSql(tableName, columnInfo);
  130. this.Context.Ado.ExecuteCommand(sql);
  131. return true;
  132. }
  133. public virtual bool UpdateColumn(string tableName, DbColumnInfo column)
  134. {
  135. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  136. var sql = GetUpdateColumnSql(tableName, column);
  137. this.Context.Ado.ExecuteCommand(sql);
  138. return true;
  139. }
  140. public virtual bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
  141. {
  142. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  143. var sql = GetCreateTableSql(tableName, columns);
  144. this.Context.Ado.ExecuteCommand(sql);
  145. return true;
  146. }
  147. public virtual bool DropTable(string tableName)
  148. {
  149. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  150. this.Context.Ado.ExecuteCommand(string.Format(this.DropTableSql, tableName));
  151. return true;
  152. }
  153. public virtual bool DropColumn(string tableName, string columnName)
  154. {
  155. columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
  156. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  157. this.Context.Ado.ExecuteCommand(string.Format(this.DropColumnToTableSql, tableName, columnName));
  158. return true;
  159. }
  160. public virtual bool DropConstraint(string tableName, string constraintName)
  161. {
  162. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  163. var sql = string.Format(this.DropConstraintSql, tableName, constraintName);
  164. this.Context.Ado.ExecuteCommand(sql);
  165. return true;
  166. }
  167. public virtual bool TruncateTable(string tableName)
  168. {
  169. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  170. this.Context.Ado.ExecuteCommand(string.Format(this.TruncateTableSql, tableName));
  171. return true;
  172. }
  173. public virtual bool BackupDataBase(string databaseName, string fullFileName)
  174. {
  175. var directory = FileHelper.GetDirectoryFromFilePath(fullFileName);
  176. if (!FileHelper.IsExistDirectory(directory))
  177. {
  178. FileHelper.CreateDirectory(directory);
  179. }
  180. this.Context.Ado.ExecuteCommand(string.Format(this.BackupDataBaseSql, databaseName, fullFileName));
  181. return true;
  182. }
  183. public virtual bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue)
  184. {
  185. oldTableName = this.SqlBuilder.GetTranslationTableName(oldTableName);
  186. newTableName = this.SqlBuilder.GetTranslationTableName(newTableName);
  187. var sql = string.Format(this.BackupTableSql, newTableName, oldTableName, maxBackupDataRows);
  188. this.Context.Ado.ExecuteCommand(sql);
  189. return true;
  190. }
  191. public virtual bool RenameColumn(string tableName, string oldColumnName, string newColumnName)
  192. {
  193. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  194. oldColumnName = this.SqlBuilder.GetTranslationColumnName(oldColumnName);
  195. newColumnName = this.SqlBuilder.GetTranslationColumnName(newColumnName);
  196. var sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName);
  197. this.Context.Ado.ExecuteCommand(sql);
  198. return true;
  199. }
  200. #endregion
  201. #region Private
  202. private List<T> GetListOrCache<T>(string cacheKey, string sql)
  203. {
  204. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
  205. () =>
  206. {
  207. var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
  208. this.Context.Ado.IsEnableLogEvent = false;
  209. var result = this.Context.Ado.SqlQuery<T>(sql);
  210. this.Context.Ado.IsEnableLogEvent = isEnableLogEvent;
  211. return result;
  212. });
  213. }
  214. protected virtual string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
  215. {
  216. var columnArray = new List<string>();
  217. Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
  218. foreach (var item in columns)
  219. {
  220. var columnName = this.SqlBuilder.GetTranslationTableName(item.DbColumnName);
  221. var dataType = item.DataType;
  222. var dataSize = GetSize(item);
  223. var nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
  224. string primaryKey = null;
  225. var identity = item.IsIdentity ? this.CreateTableIdentity : null;
  226. var addItem = string.Format(this.CreateTableColumn, columnName, dataType, dataSize, nullType, primaryKey, identity);
  227. columnArray.Add(addItem);
  228. }
  229. var tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
  230. return tableString;
  231. }
  232. protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo)
  233. {
  234. var columnName = this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName);
  235. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  236. var dataType = columnInfo.DataType;
  237. var dataSize = GetSize(columnInfo);
  238. var nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull;
  239. string primaryKey = null;
  240. string identity = null;
  241. var result = string.Format(this.AddColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
  242. return result;
  243. }
  244. protected virtual string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo)
  245. {
  246. var columnName = this.SqlBuilder.GetTranslationTableName(columnInfo.DbColumnName);
  247. tableName = this.SqlBuilder.GetTranslationTableName(tableName);
  248. var dataType = columnInfo.DataType;
  249. var dataSize = GetSize(columnInfo);
  250. var nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull;
  251. string primaryKey = null;
  252. string identity = null;
  253. var result = string.Format(this.AlterColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
  254. return result;
  255. }
  256. protected virtual string GetCacheKey(string cacheKey)
  257. {
  258. return this.Context.CurrentConnectionConfig.DbType + "." + this.Context.Ado.Connection.Database + "." + cacheKey;
  259. }
  260. protected virtual string GetSize(DbColumnInfo item)
  261. {
  262. string dataSize = null;
  263. var isMax = item.Length > 4000 || item.Length == -1;
  264. if (isMax)
  265. {
  266. dataSize = item.Length > 0 ? string.Format("({0})", "max") : null;
  267. }
  268. else if (item.Length > 0 && item.DecimalDigits == 0)
  269. {
  270. dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
  271. }
  272. else if (item.Length > 0 && item.DecimalDigits > 0)
  273. {
  274. dataSize = item.Length > 0 ? string.Format("({0},{1})", item.Length, item.DecimalDigits) : null;
  275. }
  276. return dataSize;
  277. }
  278. #endregion
  279. }
  280. }