CodeGenUtil.cs 4.3 KB

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