SpecHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Dapper;
  2. using MySql.Data.MySqlClient;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using Wicture.DbRESTFul.Configuration;
  10. using Wicture.DbRESTFul.Resources.Csi;
  11. namespace Wicture.DbRESTFul.PMT.SpecGenerators
  12. {
  13. public class ColumnSpec
  14. {
  15. public string table { get; set; }
  16. public string name { get; set; }
  17. public string type { get; set; }
  18. public string description { get; set; }
  19. public string @default { get; set; }
  20. public bool primary_key { get; set; }
  21. public bool nullable { get; set; }
  22. }
  23. public class TableSpec
  24. {
  25. public string title { get; set; }
  26. public string name { get; set; }
  27. public string description { get; set; }
  28. public List<ColumnSpec> columns { get; set; }
  29. }
  30. public class DbSpecSource
  31. {
  32. public string projectName { get; set; }
  33. public string moduleName { get; set; }
  34. public List<TableSpec> tables { get; set; }
  35. public DatabaseType databaseType { get; set; }
  36. public IDbConnection GetConnection(string connectionString, DatabaseType DatabaseType = DatabaseType.MySQL)
  37. {
  38. if (DatabaseType == DatabaseType.SQLServer) return new SqlConnection(connectionString) as IDbConnection;
  39. else return new MySqlConnection(connectionString) as IDbConnection;
  40. }
  41. }
  42. public class SpecHelper
  43. {
  44. private readonly string databaseName = string.Empty;
  45. private readonly string query_table_names = "SELECT `TABLE_NAME` AS `name`, `TABLE_COMMENT` AS description FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA` = '{0}' AND `TABLE_TYPE` = 'BASE TABLE';";
  46. private readonly string query_table_names_sqlserver = "SELECT st.name,sep.VALUE [description] FROM sys.tables st LEFT JOIN sys.extended_properties sep ON st.object_id = sep.major_id AND sep.minor_id=0;";
  47. private readonly string query_all_column_names = "SELECT `TABLE_NAME` AS `table`, `COLUMN_NAME` AS `name`, `COLUMN_TYPE` AS `type`, (CASE `IS_NULLABLE` WHEN 'NO' THEN 0 ELSE 1 END) AS nullable, (CASE `COLUMN_KEY` WHEN 'PRI' THEN 1 ELSE 0 END) AS primary_key, `COLUMN_DEFAULT` AS `default`,`COLUMN_COMMENT` AS description FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = '{0}';";
  48. private readonly string query_all_column_names_sqlserver = "SELECT st.name [table], sc.is_nullable [nullable], sc.name [name], sep.VALUE [description], TYPE_NAME(sc.system_type_id) [type],0 [default] FROM sys.tables st INNER JOIN sys.columns sc ON st.object_id = sc.object_id LEFT JOIN sys.extended_properties sep ON st.object_id = sep.major_id AND sc.column_id = sep.minor_id;";
  49. private readonly string query_api_for_project = "SELECT a.* FROM api AS a LEFT JOIN project AS p ON p.id = a.projectId WHERE p.name = @projectName;";
  50. private readonly string query_module_for_project = "select m.`name` as moduleName, m.description as moduleTitle from module as m join project as p on p.id = m.projectId where p.`name` = @projectName;";
  51. private readonly string get_databaseType = "SELECT databaseType FROM project WHERE id=@projectId;";
  52. private static readonly List<string> ObjectProperties = new List<string> { "cache", "implementation", "parameter", "result", "mock" };
  53. private readonly DbSpecSource source;
  54. private string pmtConncetString = "";
  55. private string projectConncetString = "";
  56. public SpecHelper(string connectionString, int projectId = -1)
  57. {
  58. source = new DbSpecSource();
  59. projectConncetString = connectionString;
  60. using (var conn = source.GetConnection(connectionString))
  61. {
  62. databaseName = conn.Database;
  63. }
  64. pmtConncetString = ConfigurationManager.Settings.GetConfig<CsiSectionConfig>("CSI").DbConnection.ReadConnectionString;
  65. if (projectId != -1)
  66. {
  67. using (var conn = source.GetConnection(pmtConncetString))
  68. {
  69. var databaseType = SqlMapper.Query<int?>(conn, get_databaseType, new { projectId = projectId }).FirstOrDefault();
  70. source.databaseType = databaseType == 1 ? DatabaseType.SQLServer : DatabaseType.MySQL;
  71. }
  72. }
  73. }
  74. public List<TableSpec> ListTables()
  75. {
  76. using (var conn = source.GetConnection(projectConncetString, source.databaseType))
  77. {
  78. if (conn.State == ConnectionState.Closed) conn.Open();
  79. //choose sqls
  80. var query_all_columns = source.databaseType == DatabaseType.MySQL ? query_all_column_names : query_all_column_names_sqlserver;
  81. var query_all_tables = source.databaseType == DatabaseType.MySQL ? query_table_names : query_table_names_sqlserver;
  82. //get columns
  83. var columns = SqlMapper.Query<ColumnSpec>(conn, string.Format(query_all_columns, databaseName)).ToList();
  84. //get tables
  85. var data = SqlMapper.Query<TableSpec>(conn, string.Format(query_all_tables, databaseName));
  86. //make result for return
  87. data.ForEach(t =>
  88. {
  89. t.title = t.description;
  90. t.columns = columns.Where(c => c.table == t.name).ToList();
  91. });
  92. return data.ToList();
  93. }
  94. }
  95. public List<JObject> ListAPIForProject(string projectName, string moduleName)
  96. {
  97. var result = new List<JObject>();
  98. using (var conn = source.GetConnection(pmtConncetString))
  99. {
  100. var param = new { projectName = projectName };
  101. IEnumerable<dynamic> apis = SqlMapper.Query(conn, query_api_for_project, param);
  102. foreach (var item in apis)
  103. {
  104. // If moduleName is not specified, list all.
  105. if (string.IsNullOrEmpty(moduleName) || item.module == moduleName)
  106. {
  107. JObject ca = JObject.FromObject(item);
  108. ObjectProperties.ForEach(p =>
  109. {
  110. ca[p] = JToken.Parse(ca.Value<string>(p) ?? "{}");
  111. //处理mock属性,将其input与output参数取出来
  112. if (p == "mock")
  113. {
  114. var mockerArr = ca.Value<JArray>(p) ?? new JArray();
  115. //var mockerArr = JArray.FromObject(ca.Value<string>(p));
  116. if (mockerArr != null && mockerArr.Count > 0)
  117. {
  118. ca["input"] = mockerArr[0].Value<JObject>("input");
  119. ca["output"] = mockerArr[0].Value<JObject>("output");
  120. }
  121. }
  122. }
  123. );
  124. result.Add(ca);
  125. }
  126. }
  127. }
  128. return result;
  129. }
  130. public dynamic ListModules(string projectName)
  131. {
  132. using (var conn = source.GetConnection(pmtConncetString))
  133. {
  134. var param = new { projectName = projectName };
  135. return SqlMapper.Query(conn, query_module_for_project, param);
  136. }
  137. }
  138. }
  139. }