CodeGenUtil.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 dbType = App.GetOptions<DbConnectionOptions>().ConnectionConfigs[0].DbType;
  12. return dbType switch
  13. {
  14. DbType.PostgreSQL => ConvertDataType_PostgreSQL(dataType),
  15. _ => ConvertDataType_Default(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_Default(string dataType)
  93. {
  94. return dataType switch
  95. {
  96. "text" or "varchar" or "char" or "nvarchar" or "nchar" or "timestamp" => "string",
  97. "int" => "int",
  98. "smallint" => "Int16",
  99. "tinyint" => "byte",
  100. "bigint" or "integer" => "long",
  101. "bit" => "bool",
  102. "money" or "smallmoney" or "numeric" or "decimal" => "decimal",
  103. "real" => "Single",
  104. "datetime" or "smalldatetime" => "DateTime",
  105. "float" => "double",
  106. "image" or "binary" or "varbinary" => "byte[]",
  107. "uniqueidentifier" => "Guid",
  108. _ => "object",
  109. };
  110. }
  111. /// <summary>
  112. /// 数据类型转显示类型
  113. /// </summary>
  114. /// <param name="dataType"></param>
  115. /// <returns></returns>
  116. public static string DataTypeToEff(string dataType)
  117. {
  118. if (string.IsNullOrEmpty(dataType)) return "";
  119. return dataType switch
  120. {
  121. "string" => "Input",
  122. "int" => "InputNumber",
  123. "long" => "Input",
  124. "float" => "Input",
  125. "double" => "Input",
  126. "decimal" => "Input",
  127. "bool" => "Switch",
  128. "Guid" => "Input",
  129. "DateTime" => "DatePicker",
  130. _ => "Input",
  131. };
  132. }
  133. // 是否通用字段
  134. public static bool IsCommonColumn(string columnName)
  135. {
  136. var columnList = new List<string>()
  137. {
  138. "CreateTime", "UpdateTime", "CreateUserId", "UpdateUserId", "IsDelete"
  139. };
  140. return columnList.Contains(columnName);
  141. }
  142. }