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.

230 lines
7.7 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Linq;
  6. namespace SqlSugar
  7. {
  8. public class OracleDbMaintenance : DbMaintenanceProvider
  9. {
  10. #region DML
  11. protected override string GetColumnInfosByTableNameSql
  12. {
  13. get
  14. {
  15. throw new NotSupportedException();
  16. }
  17. }
  18. protected override string GetTableInfoListSql
  19. {
  20. get
  21. {
  22. return @"SELECT table_name name from user_tables where
  23. table_name!='HELP'
  24. AND table_name NOT LIKE '%$%'
  25. AND table_name NOT LIKE 'LOGMNRC_%'
  26. AND table_name!='LOGMNRP_CTAS_PART_MAP'
  27. AND table_name!='LOGMNR_LOGMNR_BUILDLOG'
  28. AND table_name!='SQLPLUS_PRODUCT_PROFILE'
  29. ";
  30. }
  31. }
  32. protected override string GetViewInfoListSql
  33. {
  34. get
  35. {
  36. return @"select view_name name from user_views
  37. WHERE VIEW_name NOT LIKE '%$%'
  38. AND VIEW_NAME !='PRODUCT_PRIVS'
  39. AND VIEW_NAME NOT LIKE 'MVIEW_%' ";
  40. }
  41. }
  42. #endregion
  43. #region DDL
  44. protected override string AddPrimaryKeySql
  45. {
  46. get
  47. {
  48. return "ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY({2})";
  49. }
  50. }
  51. protected override string AddColumnToTableSql
  52. {
  53. get
  54. {
  55. return "ALTER TABLE {0} ADD {1} {2}{3} {4} {5} {6}";
  56. }
  57. }
  58. protected override string AlterColumnToTableSql
  59. {
  60. get
  61. {
  62. return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
  63. }
  64. }
  65. protected override string BackupDataBaseSql
  66. {
  67. get
  68. {
  69. return @"USE master;BACKUP DATABASE {0} TO disk = '{1}'";
  70. }
  71. }
  72. protected override string CreateTableSql
  73. {
  74. get
  75. {
  76. return "CREATE TABLE {0}(\r\n{1})";
  77. }
  78. }
  79. protected override string CreateTableColumn
  80. {
  81. get
  82. {
  83. return "{0} {1}{2} {3} {4} {5}";
  84. }
  85. }
  86. protected override string TruncateTableSql
  87. {
  88. get
  89. {
  90. return "TRUNCATE TABLE {0}";
  91. }
  92. }
  93. protected override string BackupTableSql
  94. {
  95. get
  96. {
  97. return "SELECT TOP {0} * INTO {1} FROM {2}";
  98. }
  99. }
  100. protected override string DropTableSql
  101. {
  102. get
  103. {
  104. return "DROP TABLE {0}";
  105. }
  106. }
  107. protected override string DropColumnToTableSql
  108. {
  109. get
  110. {
  111. return "ALTER TABLE {0} DROP COLUMN {1}";
  112. }
  113. }
  114. protected override string DropConstraintSql
  115. {
  116. get
  117. {
  118. return "ALTER TABLE {0} DROP CONSTRAINT {1}";
  119. }
  120. }
  121. protected override string RenameColumnSql
  122. {
  123. get
  124. {
  125. return "exec sp_rename '{0}.{1}','{2}','column';";
  126. }
  127. }
  128. #endregion
  129. #region Check
  130. protected override string CheckSystemTablePermissionsSql
  131. {
  132. get
  133. {
  134. return "select t.table_name from user_tables t where rownum=1";
  135. }
  136. }
  137. #endregion
  138. #region Scattered
  139. protected override string CreateTableNull
  140. {
  141. get
  142. {
  143. return "NULL";
  144. }
  145. }
  146. protected override string CreateTableNotNull
  147. {
  148. get
  149. {
  150. return "NOT NULL";
  151. }
  152. }
  153. protected override string CreateTablePirmaryKey
  154. {
  155. get
  156. {
  157. return "PRIMARY KEY";
  158. }
  159. }
  160. protected override string CreateTableIdentity
  161. {
  162. get
  163. {
  164. return "IDENTITY(1,1)";
  165. }
  166. }
  167. #endregion
  168. #region Methods
  169. public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
  170. {
  171. var cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
  172. cacheKey = GetCacheKey(cacheKey);
  173. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
  174. () =>
  175. {
  176. var sql = "select * from " + tableName + " WHERE 1=2 ";
  177. var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
  178. this.Context.Ado.IsEnableLogEvent = false;
  179. using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql))
  180. {
  181. this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
  182. var result = new List<DbColumnInfo>();
  183. var schemaTable = reader.GetSchemaTable();
  184. foreach (DataRow row in schemaTable.Rows)
  185. {
  186. var column = new DbColumnInfo
  187. {
  188. TableName = tableName,
  189. DataType = row["DataType"].ToString().Replace("System.", "").Trim(),
  190. IsNullable = (bool)row["AllowDBNull"],
  191. //IsIdentity = (bool)row["IsAutoIncrement"],
  192. ColumnDescription = null,
  193. DbColumnName = row["ColumnName"].ToString(),
  194. //DefaultValue = row["defaultValue"].ToString(),
  195. IsPrimarykey = GetPrimaryKeyByTableNames(tableName).Any(it=>it.Equals(row["ColumnName"].ToString(), StringComparison.CurrentCultureIgnoreCase)),
  196. Length = row["ColumnSize"].ObjToInt(),
  197. Scale = row["numericscale"].ObjToInt()
  198. };
  199. result.Add(column);
  200. }
  201. return result;
  202. }
  203. });
  204. }
  205. private List<string> GetPrimaryKeyByTableNames(string tableName)
  206. {
  207. var cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
  208. cacheKey = GetCacheKey(cacheKey);
  209. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
  210. () =>
  211. {
  212. var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
  213. this.Context.Ado.IsEnableLogEvent = false;
  214. var sql = @" select distinct cu.COLUMN_name KEYNAME from user_cons_columns cu, user_constraints au
  215. where cu.constraint_name = au.constraint_name
  216. and au.constraint_type = 'P' and au.table_name = '" +tableName.ToUpper()+ @"'";
  217. var pks = this.Context.Ado.SqlQuery<string>(sql);
  218. this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
  219. return pks;
  220. });
  221. }
  222. #endregion
  223. }
  224. }