CodeGenUtil.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. namespace Admin.NET.Core.Util;
  2. /// <summary>
  3. /// 代码生成帮助类
  4. /// </summary>
  5. public static class CodeGenUtil
  6. {
  7. //根据数据库类型来处理对应的数据字段类型
  8. public static string ConvertDataType(string dataType, string DbType)
  9. {
  10. switch (DbType)
  11. {
  12. case "PostgreSQL":
  13. return ConvertDataType_PostgreSQL(dataType);
  14. default:
  15. return ConvertDataType(dataType);
  16. }
  17. }
  18. //PostgreSQL数据类型对应的字段类型
  19. public static string ConvertDataType_PostgreSQL(string dataType)
  20. {
  21. switch (dataType)
  22. {
  23. case "int2":
  24. case "smallint":
  25. return "Int16";
  26. case "int4":
  27. case "integer":
  28. return "int";
  29. case "int8":
  30. case "bigint":
  31. return "long";
  32. case "float4":
  33. case "real":
  34. return "float";
  35. case "float8":
  36. case "double precision":
  37. return "double";
  38. case "numeric":
  39. case "decimal":
  40. case "path":
  41. case "point":
  42. case "polygon":
  43. case "interval":
  44. case "lseg":
  45. case "macaddr":
  46. case "money":
  47. return "decimal";
  48. case "boolean":
  49. case "bool":
  50. case "box":
  51. case "bytea":
  52. return "bool";
  53. case "varchar":
  54. case "character varying":
  55. case "geometry":
  56. case "name":
  57. case "text":
  58. case "char":
  59. case "character":
  60. case "cidr":
  61. case "circle":
  62. case "tsquery":
  63. case "tsvector":
  64. case "txid_snapshot":
  65. case "xml":
  66. case "json":
  67. return "string";
  68. case "uuid":
  69. return "Guid";
  70. case "timestamp":
  71. case "timestamp with time zone":
  72. case "timestamptz":
  73. case "timestamp without time zone":
  74. case "date":
  75. case "time":
  76. case "time with time zone":
  77. case "timetz":
  78. case "time without time zone":
  79. return "DateTime";
  80. case "bit":
  81. case "bit varying":
  82. return "byte[]";
  83. case "varbit":
  84. return "byte";
  85. case "public.geometry":
  86. case "inet":
  87. return "object";
  88. default:
  89. return "object";
  90. }
  91. }
  92. public static string ConvertDataType(string dataType)
  93. {
  94. switch (dataType)
  95. {
  96. case "text":
  97. case "varchar":
  98. case "char":
  99. case "nvarchar":
  100. case "nchar":
  101. case "timestamp":
  102. return "string";
  103. case "int":
  104. return "int";
  105. case "smallint":
  106. return "Int16";
  107. case "tinyint":
  108. return "byte";
  109. case "bigint":
  110. case "integer": // sqlite数据库
  111. return "long";
  112. case "bit":
  113. return "bool";
  114. case "money":
  115. case "smallmoney":
  116. case "numeric":
  117. case "decimal":
  118. return "decimal";
  119. case "real":
  120. return "Single";
  121. case "datetime":
  122. case "smalldatetime":
  123. return "DateTime";
  124. case "float":
  125. return "double";
  126. case "image":
  127. case "binary":
  128. case "varbinary":
  129. return "byte[]";
  130. case "uniqueidentifier":
  131. return "Guid";
  132. default:
  133. return "object";
  134. }
  135. }
  136. /// <summary>
  137. /// 数据类型转显示类型
  138. /// </summary>
  139. /// <param name="dataType"></param>
  140. /// <returns></returns>
  141. public static string DataTypeToEff(string dataType)
  142. {
  143. if (string.IsNullOrEmpty(dataType)) return "";
  144. return dataType switch
  145. {
  146. "string" => "Input",
  147. "int" => "InputNumber",
  148. "long" => "Input",
  149. "float" => "Input",
  150. "double" => "Input",
  151. "decimal" => "Input",
  152. "bool" => "Switch",
  153. "Guid" => "Input",
  154. "DateTime" => "DatePicker",
  155. _ => "Input",
  156. };
  157. }
  158. // 是否通用字段
  159. public static bool IsCommonColumn(string columnName)
  160. {
  161. var columnList = new List<string>()
  162. {
  163. "CreateTime", "UpdateTime", "CreateUserId", "UpdateUserId", "IsDelete"
  164. };
  165. return columnList.Contains(columnName);
  166. }
  167. }