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.

235 lines
8.0 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace SqlSugar
  5. {
  6. public class MySqlDbMaintenance : DbMaintenanceProvider
  7. {
  8. #region DML
  9. protected override string GetColumnInfosByTableNameSql
  10. {
  11. get
  12. {
  13. var sql = @"SELECT
  14. 0 as TableId,
  15. TABLE_NAME as TableName,
  16. column_name AS DbColumnName,
  17. CASE WHEN left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1)='' THEN COLUMN_TYPE ELSE left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1) END AS DataType,
  18. CAST(SUBSTRING(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)+1,LOCATE(')',COLUMN_TYPE)-LOCATE('(',COLUMN_TYPE)-1) AS signed) AS Length,
  19. column_default AS `DefaultValue`,
  20. column_comment AS `ColumnDescription`,
  21. CASE WHEN COLUMN_KEY = 'PRI'
  22. THEN true ELSE false END AS `IsPrimaryKey`,
  23. CASE WHEN EXTRA='auto_increment' THEN true ELSE false END as IsIdentity,
  24. CASE WHEN is_nullable = 'YES'
  25. THEN true ELSE false END AS `IsNullable`
  26. FROM
  27. Information_schema.columns where TABLE_NAME='{0}' and TABLE_SCHEMA=(select database()) ORDER BY TABLE_NAME";
  28. return sql;
  29. }
  30. }
  31. protected override string GetTableInfoListSql
  32. {
  33. get
  34. {
  35. return @"select TABLE_NAME as Name,TABLE_COMMENT as Description from information_schema.tables
  36. where TABLE_SCHEMA=(select database()) AND TABLE_TYPE='BASE TABLE'";
  37. }
  38. }
  39. protected override string GetViewInfoListSql
  40. {
  41. get
  42. {
  43. return @"select TABLE_NAME as Name,TABLE_COMMENT as Description from information_schema.tables
  44. where TABLE_SCHEMA=(select database()) AND TABLE_TYPE='VIEW'
  45. ";
  46. }
  47. }
  48. #endregion
  49. #region DDL
  50. protected override string AddPrimaryKeySql
  51. {
  52. get
  53. {
  54. return "ALTER TABLE {0} ADD PRIMARY KEY({2}) /*{1}*/";
  55. }
  56. }
  57. protected override string AddColumnToTableSql
  58. {
  59. get
  60. {
  61. return "ALTER TABLE {0} ADD {1} {2}{3} {4} {5} {6}";
  62. }
  63. }
  64. protected override string AlterColumnToTableSql
  65. {
  66. get
  67. {
  68. // return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
  69. return "alter table {0} change column {1} {1} {2}{3} {4} {5} {6}";
  70. }
  71. }
  72. protected override string BackupDataBaseSql
  73. {
  74. get
  75. {
  76. return "mysqldump.exe {0} -uroot -p > {1} ";
  77. }
  78. }
  79. protected override string CreateTableSql
  80. {
  81. get
  82. {
  83. return "CREATE TABLE {0}(\r\n{1} $PrimaryKey)";
  84. }
  85. }
  86. protected override string CreateTableColumn
  87. {
  88. get
  89. {
  90. return "{0} {1}{2} {3} {4} {5}";
  91. }
  92. }
  93. protected override string TruncateTableSql
  94. {
  95. get
  96. {
  97. return "TRUNCATE TABLE {0}";
  98. }
  99. }
  100. protected override string BackupTableSql
  101. {
  102. get
  103. {
  104. return "SELECT * INTO {1} FROM {2} limit 0,{0}";
  105. }
  106. }
  107. protected override string DropTableSql
  108. {
  109. get
  110. {
  111. return "DROP TABLE {0}";
  112. }
  113. }
  114. protected override string DropColumnToTableSql
  115. {
  116. get
  117. {
  118. return "ALTER TABLE {0} DROP COLUMN {1}";
  119. }
  120. }
  121. protected override string DropConstraintSql
  122. {
  123. get
  124. {
  125. return "ALTER TABLE {0} drop primary key;";
  126. }
  127. }
  128. protected override string RenameColumnSql
  129. {
  130. get
  131. {
  132. return "exec sp_rename '{0}.{1}','{2}','column';";
  133. }
  134. }
  135. #endregion
  136. #region Check
  137. protected override string CheckSystemTablePermissionsSql
  138. {
  139. get
  140. {
  141. return "select 1 from Information_schema.columns limit 0,1";
  142. }
  143. }
  144. #endregion
  145. #region Scattered
  146. protected override string CreateTableNull
  147. {
  148. get
  149. {
  150. return "DEFAULT NULL";
  151. }
  152. }
  153. protected override string CreateTableNotNull
  154. {
  155. get
  156. {
  157. return "NOT NULL";
  158. }
  159. }
  160. protected override string CreateTablePirmaryKey
  161. {
  162. get
  163. {
  164. return "PRIMARY KEY";
  165. }
  166. }
  167. protected override string CreateTableIdentity
  168. {
  169. get
  170. {
  171. return "AUTO_INCREMENT";
  172. }
  173. }
  174. #endregion
  175. #region Methods
  176. public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
  177. {
  178. if (columns.HasValue())
  179. {
  180. foreach (var item in columns)
  181. {
  182. if (item.DbColumnName.Equals("GUID",StringComparison.CurrentCultureIgnoreCase)&&item.Length==0)
  183. {
  184. item.Length = 10;
  185. }
  186. }
  187. }
  188. var sql = GetCreateTableSql(tableName, columns);
  189. string primaryKeyInfo = null;
  190. if (columns.Any(it => it.IsPrimarykey)) {
  191. primaryKeyInfo =string.Format( ", Primary key({0})",string.Join(",",columns.Where(it=>it.IsPrimarykey).Select(it=>this.SqlBuilder.GetTranslationColumnName(it.DbColumnName))));
  192. }
  193. sql = sql.Replace("$PrimaryKey", primaryKeyInfo);
  194. this.Context.Ado.ExecuteCommand(sql);
  195. return true;
  196. }
  197. protected override string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
  198. {
  199. var columnArray = new List<string>();
  200. Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
  201. foreach (var item in columns)
  202. {
  203. var columnName = item.DbColumnName;
  204. var dataType = item.DataType;
  205. if (dataType == "varchar"&& item.Length==0) {
  206. item.Length = 1;
  207. }
  208. var dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
  209. var nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
  210. string primaryKey = null;
  211. var identity = item.IsIdentity ? this.CreateTableIdentity : null;
  212. var addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
  213. columnArray.Add(addItem);
  214. }
  215. var tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
  216. return tableString;
  217. }
  218. public override bool IsAnyConstraint(string constraintName)
  219. {
  220. throw new NotSupportedException("MySql IsAnyConstraint NotSupportedException");
  221. }
  222. public override bool BackupDataBase(string databaseName, string fullFileName)
  223. {
  224. Check.ThrowNotSupportedException("MySql BackupDataBase NotSupported");
  225. return false;
  226. }
  227. #endregion
  228. }
  229. }