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.

269 lines
9.0 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.SQLite;
  6. namespace SqlSugar
  7. {
  8. public class SqliteDbMaintenance : 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 Name from sqlite_master where type='table' and name<>'sqlite_sequence' order by name;";
  23. }
  24. }
  25. protected override string GetViewInfoListSql
  26. {
  27. get
  28. {
  29. return @"select Name from sqlite_master where type='view' order by name;";
  30. }
  31. }
  32. #endregion
  33. #region DDL
  34. protected override string AddPrimaryKeySql
  35. {
  36. get
  37. {
  38. throw new NotSupportedException();
  39. }
  40. }
  41. protected override string AddColumnToTableSql
  42. {
  43. get
  44. {
  45. throw new NotSupportedException();
  46. }
  47. }
  48. protected override string AlterColumnToTableSql
  49. {
  50. get
  51. {
  52. // return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
  53. throw new NotSupportedException();
  54. }
  55. }
  56. protected override string BackupDataBaseSql
  57. {
  58. get
  59. {
  60. throw new NotSupportedException();
  61. }
  62. }
  63. protected override string CreateTableSql
  64. {
  65. get
  66. {
  67. return "CREATE TABLE {0}(\r\n{1} )";
  68. }
  69. }
  70. protected override string CreateTableColumn
  71. {
  72. get
  73. {
  74. return "{0} {1}{2} {3} {4} {5}";
  75. }
  76. }
  77. protected override string TruncateTableSql
  78. {
  79. get
  80. {
  81. return "DELETE FROM {0}";
  82. }
  83. }
  84. protected override string BackupTableSql
  85. {
  86. get
  87. {
  88. return " CREATE TABLE {0} AS SELECT * FROM {1} limit 0,{2}";
  89. }
  90. }
  91. protected override string DropTableSql
  92. {
  93. get
  94. {
  95. return "DROP TABLE {0}";
  96. }
  97. }
  98. protected override string DropColumnToTableSql
  99. {
  100. get
  101. {
  102. throw new NotSupportedException();
  103. }
  104. }
  105. protected override string DropConstraintSql
  106. {
  107. get
  108. {
  109. throw new NotSupportedException();
  110. }
  111. }
  112. protected override string RenameColumnSql
  113. {
  114. get
  115. {
  116. throw new NotSupportedException();
  117. }
  118. }
  119. #endregion
  120. #region Check
  121. protected override string CheckSystemTablePermissionsSql
  122. {
  123. get
  124. {
  125. return "select Name from sqlite_master limit 0,1";
  126. }
  127. }
  128. #endregion
  129. #region Scattered
  130. protected override string CreateTableNull
  131. {
  132. get
  133. {
  134. return "NULL";
  135. }
  136. }
  137. protected override string CreateTableNotNull
  138. {
  139. get
  140. {
  141. return "NOT NULL";
  142. }
  143. }
  144. protected override string CreateTablePirmaryKey
  145. {
  146. get
  147. {
  148. return "PRIMARY KEY";
  149. }
  150. }
  151. protected override string CreateTableIdentity
  152. {
  153. get
  154. {
  155. return "AUTOINCREMENT";
  156. }
  157. }
  158. #endregion
  159. #region Methods
  160. public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
  161. {
  162. var cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
  163. cacheKey = GetCacheKey(cacheKey);
  164. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
  165. () =>
  166. {
  167. var sql = "select * from " + tableName + " limit 0,1";
  168. var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
  169. this.Context.Ado.IsEnableLogEvent = false;
  170. using (DbDataReader reader = (SQLiteDataReader)this.Context.Ado.GetDataReader(sql))
  171. {
  172. this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
  173. var result = new List<DbColumnInfo>();
  174. var schemaTable = reader.GetSchemaTable();
  175. foreach (DataRow row in schemaTable.Rows)
  176. {
  177. var column = new DbColumnInfo
  178. {
  179. TableName = tableName,
  180. DataType = row["DataTypeName"].ToString().Trim(),
  181. IsNullable = (bool)row["AllowDBNull"],
  182. IsIdentity = (bool)row["IsAutoIncrement"],
  183. ColumnDescription = null,
  184. DbColumnName = row["ColumnName"].ToString(),
  185. DefaultValue = row["defaultValue"].ToString(),
  186. IsPrimarykey = (bool)row["IsKey"],
  187. Length = Convert.ToInt32(row["ColumnSize"])
  188. };
  189. result.Add(column);
  190. }
  191. return result;
  192. }
  193. });
  194. }
  195. public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
  196. {
  197. if (columns.HasValue())
  198. {
  199. foreach (var item in columns)
  200. {
  201. if (item.DbColumnName.Equals("GUID", StringComparison.CurrentCultureIgnoreCase))
  202. {
  203. item.Length = 20;
  204. }
  205. if (item.IsIdentity && !item.IsPrimarykey)
  206. {
  207. item.IsPrimarykey = true;
  208. Check.Exception(item.DataType == "integer", "Identity only integer type");
  209. }
  210. }
  211. }
  212. var sql = GetCreateTableSql(tableName, columns);
  213. this.Context.Ado.ExecuteCommand(sql);
  214. return true;
  215. }
  216. protected override string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
  217. {
  218. var columnArray = new List<string>();
  219. Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
  220. foreach (var item in columns)
  221. {
  222. var columnName = item.DbColumnName;
  223. var dataType = item.DataType;
  224. if (dataType == "varchar" && item.Length == 0)
  225. {
  226. item.Length = 1;
  227. }
  228. var dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
  229. var nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
  230. var primaryKey = item.IsPrimarykey?this.CreateTablePirmaryKey:null;
  231. var identity = item.IsIdentity ? this.CreateTableIdentity : null;
  232. var addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
  233. columnArray.Add(addItem);
  234. }
  235. var tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
  236. tableString = tableString.Replace("`", "\"");
  237. return tableString;
  238. }
  239. public override bool IsAnyConstraint(string constraintName)
  240. {
  241. throw new NotSupportedException("MySql IsAnyConstraint NotSupportedException");
  242. }
  243. public override bool BackupDataBase(string databaseName, string fullFileName)
  244. {
  245. Check.ThrowNotSupportedException("MySql BackupDataBase NotSupported");
  246. return false;
  247. }
  248. private List<T> GetListOrCache<T>(string cacheKey, string sql)
  249. {
  250. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
  251. () =>
  252. {
  253. var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
  254. this.Context.Ado.IsEnableLogEvent = false;
  255. var reval = this.Context.Ado.SqlQuery<T>(sql);
  256. this.Context.Ado.IsEnableLogEvent = isEnableLogEvent;
  257. return reval;
  258. });
  259. }
  260. #endregion
  261. }
  262. }