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.

200 lines
6.3 KiB

2 years ago
  1. namespace SqlSugar
  2. {
  3. public class SqlServerDbMaintenance : DbMaintenanceProvider
  4. {
  5. #region DML
  6. protected override string GetColumnInfosByTableNameSql
  7. {
  8. get
  9. {
  10. var sql = @"SELECT sysobjects.name AS TableName,
  11. syscolumns.Id AS TableId,
  12. syscolumns.name AS DbColumnName,
  13. systypes.name AS DataType,
  14. syscolumns.length AS [Length],
  15. sys.extended_properties.[value] AS [ColumnDescription],
  16. syscomments.text AS DefaultValue,
  17. syscolumns.isnullable AS IsNullable,
  18. columnproperty(syscolumns.id,syscolumns.name,'IsIdentity')as IsIdentity,
  19. (CASE
  20. WHEN EXISTS
  21. (
  22. select 1
  23. from sysindexes i
  24. join sysindexkeys k on i.id = k.id and i.indid = k.indid
  25. join sysobjects o on i.id = o.id
  26. join syscolumns c on i.id=c.id and k.colid = c.colid
  27. where o.xtype = 'U'
  28. and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)
  29. and o.name=sysobjects.name and c.name=syscolumns.name
  30. ) THEN 1
  31. ELSE 0
  32. END) AS IsPrimaryKey
  33. FROM syscolumns
  34. INNER JOIN systypes ON syscolumns.xtype = systypes.xtype
  35. LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id
  36. LEFT OUTER JOIN sys.extended_properties ON (sys.extended_properties.minor_id = syscolumns.colid
  37. AND sys.extended_properties.major_id = syscolumns.id)
  38. LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
  39. WHERE syscolumns.id IN
  40. (SELECT id
  41. FROM sysobjects
  42. WHERE xtype IN('u',
  43. 'v') )
  44. AND (systypes.name <> 'sysname')
  45. AND sysobjects.name='{0}'
  46. AND systypes.name<>'geometry'
  47. AND systypes.name<>'geography'
  48. ORDER BY syscolumns.colid";
  49. return sql;
  50. }
  51. }
  52. protected override string GetTableInfoListSql
  53. {
  54. get
  55. {
  56. return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
  57. FROM sysobjects s
  58. LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('U') ";
  59. }
  60. }
  61. protected override string GetViewInfoListSql
  62. {
  63. get
  64. {
  65. return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
  66. FROM sysobjects s
  67. LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('V') ";
  68. }
  69. }
  70. #endregion
  71. #region DDL
  72. protected override string AddPrimaryKeySql
  73. {
  74. get
  75. {
  76. return "ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY({2})";
  77. }
  78. }
  79. protected override string AddColumnToTableSql
  80. {
  81. get
  82. {
  83. return "ALTER TABLE {0} ADD {1} {2}{3} {4} {5} {6}";
  84. }
  85. }
  86. protected override string AlterColumnToTableSql
  87. {
  88. get
  89. {
  90. return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
  91. }
  92. }
  93. protected override string BackupDataBaseSql
  94. {
  95. get
  96. {
  97. return @"USE master;BACKUP DATABASE {0} TO disk = '{1}'";
  98. }
  99. }
  100. protected override string CreateTableSql
  101. {
  102. get
  103. {
  104. return "CREATE TABLE {0}(\r\n{1})";
  105. }
  106. }
  107. protected override string CreateTableColumn
  108. {
  109. get
  110. {
  111. return "{0} {1}{2} {3} {4} {5}";
  112. }
  113. }
  114. protected override string TruncateTableSql
  115. {
  116. get
  117. {
  118. return "TRUNCATE TABLE {0}";
  119. }
  120. }
  121. protected override string BackupTableSql
  122. {
  123. get
  124. {
  125. return "SELECT TOP {0} * INTO {1} FROM {2}";
  126. }
  127. }
  128. protected override string DropTableSql
  129. {
  130. get
  131. {
  132. return "DROP TABLE {0}";
  133. }
  134. }
  135. protected override string DropColumnToTableSql
  136. {
  137. get
  138. {
  139. return "ALTER TABLE {0} DROP COLUMN {1}";
  140. }
  141. }
  142. protected override string DropConstraintSql
  143. {
  144. get
  145. {
  146. return "ALTER TABLE {0} DROP CONSTRAINT {1}";
  147. }
  148. }
  149. protected override string RenameColumnSql
  150. {
  151. get
  152. {
  153. return "exec sp_rename '{0}.{1}','{2}','column';";
  154. }
  155. }
  156. #endregion
  157. #region Check
  158. protected override string CheckSystemTablePermissionsSql
  159. {
  160. get
  161. {
  162. return "select top 1 id from sysobjects";
  163. }
  164. }
  165. #endregion
  166. #region Scattered
  167. protected override string CreateTableNull
  168. {
  169. get
  170. {
  171. return "NULL";
  172. }
  173. }
  174. protected override string CreateTableNotNull
  175. {
  176. get
  177. {
  178. return "NOT NULL";
  179. }
  180. }
  181. protected override string CreateTablePirmaryKey
  182. {
  183. get
  184. {
  185. return "PRIMARY KEY";
  186. }
  187. }
  188. protected override string CreateTableIdentity
  189. {
  190. get
  191. {
  192. return "IDENTITY(1,1)";
  193. }
  194. }
  195. #endregion
  196. }
  197. }