CodeGenUtil.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using DbType = SqlSugar.DbType;
  2. namespace Admin.NET.Core;
  3. /// <summary>
  4. /// 代码生成帮助类
  5. /// </summary>
  6. public static class CodeGenUtil
  7. {
  8. /// <summary>
  9. /// 转换大驼峰法命名
  10. /// </summary>
  11. /// <param name="columnName">字段名</param>
  12. /// <param name="dbColumnNames">EntityBase 实体属性名称</param>
  13. /// <returns></returns>
  14. public static string CamelColumnName(string columnName, string[] dbColumnNames)
  15. {
  16. if (columnName.Contains('_'))
  17. {
  18. var arrColName = columnName.Split('_');
  19. var sb = new StringBuilder();
  20. foreach (var col in arrColName)
  21. {
  22. sb.Append(col[..1].ToUpper() + col[1..].ToLower());
  23. }
  24. columnName = sb.ToString();
  25. }
  26. else
  27. {
  28. var propertyName = dbColumnNames.FirstOrDefault(c => c.ToLower() == columnName.ToLower());
  29. if (!string.IsNullOrEmpty(propertyName))
  30. {
  31. columnName = propertyName;
  32. }
  33. else
  34. {
  35. columnName = columnName[..1].ToUpper() + columnName[1..].ToLower();
  36. }
  37. }
  38. return columnName;
  39. }
  40. // 根据数据库类型来处理对应的数据字段类型
  41. public static string ConvertDataType(DbColumnInfo dbColumnInfo)
  42. {
  43. var dbType = App.GetOptions<DbConnectionOptions>().ConnectionConfigs[0].DbType;
  44. var dataType = dbType switch
  45. {
  46. DbType.Oracle => ConvertDataType_OracleSQL(string.IsNullOrEmpty(dbColumnInfo.OracleDataType) ? dbColumnInfo.DataType : dbColumnInfo.OracleDataType, dbColumnInfo.Length, dbColumnInfo.Scale),
  47. DbType.PostgreSQL => ConvertDataType_PostgreSQL(dbColumnInfo.DataType),
  48. _ => ConvertDataType_Default(dbColumnInfo.DataType),
  49. };
  50. return dataType + (dbColumnInfo.IsNullable ? "?" : "");
  51. }
  52. public static string ConvertDataType_OracleSQL(string dataType, int? length, int? scale)
  53. {
  54. switch (dataType.ToLower())
  55. {
  56. case "interval year to month":
  57. return "int";
  58. case "interval day to second":
  59. return "TimeSpan";
  60. case "smallint":
  61. return "Int16";
  62. case "int":
  63. case "integer":
  64. return "int";
  65. case "long":
  66. return "long";
  67. case "float":
  68. return "float";
  69. case "decimal":
  70. return "decimal";
  71. case "number":
  72. if (length != null)
  73. {
  74. if (scale != null && scale > 0)
  75. {
  76. return "decimal";
  77. }
  78. else if ((scale != null && scale == 0) || scale == null)
  79. {
  80. if (length > 1 && length < 12)
  81. {
  82. return "int";
  83. }
  84. else if (length > 11)
  85. {
  86. return "long";
  87. }
  88. }
  89. if (length == 1)
  90. {
  91. return "bool";
  92. }
  93. }
  94. return "decimal";
  95. case "char":
  96. case "clob":
  97. case "nclob":
  98. case "nchar":
  99. case "nvarchar":
  100. case "varchar":
  101. case "nvarchar2":
  102. case "varchar2":
  103. case "rowid":
  104. return "string";
  105. case "timestamp":
  106. case "timestamp with time zone":
  107. case "timestamptz":
  108. case "timestamp without time zone":
  109. case "date":
  110. case "time":
  111. case "time with time zone":
  112. case "timetz":
  113. case "time without time zone":
  114. return "DateTime";
  115. case "bfile":
  116. case "blob":
  117. case "raw":
  118. return "byte[]";
  119. default:
  120. return "object";
  121. }
  122. }
  123. //PostgreSQL数据类型对应的字段类型
  124. public static string ConvertDataType_PostgreSQL(string dataType)
  125. {
  126. switch (dataType)
  127. {
  128. case "int2":
  129. case "smallint":
  130. return "Int16";
  131. case "int4":
  132. case "integer":
  133. return "int";
  134. case "int8":
  135. case "bigint":
  136. return "long";
  137. case "float4":
  138. case "real":
  139. return "float";
  140. case "float8":
  141. case "double precision":
  142. return "double";
  143. case "numeric":
  144. case "decimal":
  145. case "path":
  146. case "point":
  147. case "polygon":
  148. case "interval":
  149. case "lseg":
  150. case "macaddr":
  151. case "money":
  152. return "decimal";
  153. case "boolean":
  154. case "bool":
  155. case "box":
  156. case "bytea":
  157. return "bool";
  158. case "varchar":
  159. case "character varying":
  160. case "geometry":
  161. case "name":
  162. case "text":
  163. case "char":
  164. case "character":
  165. case "cidr":
  166. case "circle":
  167. case "tsquery":
  168. case "tsvector":
  169. case "txid_snapshot":
  170. case "xml":
  171. case "json":
  172. return "string";
  173. case "uuid":
  174. return "Guid";
  175. case "timestamp":
  176. case "timestamp with time zone":
  177. case "timestamptz":
  178. case "timestamp without time zone":
  179. case "date":
  180. case "time":
  181. case "time with time zone":
  182. case "timetz":
  183. case "time without time zone":
  184. return "DateTime";
  185. case "bit":
  186. case "bit varying":
  187. return "byte[]";
  188. case "varbit":
  189. return "byte";
  190. case "public.geometry":
  191. case "inet":
  192. return "object";
  193. default:
  194. return "object";
  195. }
  196. }
  197. public static string ConvertDataType_Default(string dataType)
  198. {
  199. return dataType.ToLower() switch
  200. {
  201. "text" or "varchar" or "char" or "nvarchar" or "nchar" or "timestamp" => "string",
  202. "int" => "int",
  203. "smallint" => "Int16",
  204. //"tinyint" => "byte",
  205. "tinyint" => "bool", // MYSQL
  206. "bigint" or "integer" => "long",
  207. "bit" => "bool",
  208. "money" or "smallmoney" or "numeric" or "decimal" => "decimal",
  209. "real" => "Single",
  210. "datetime" or "smalldatetime" => "DateTime",
  211. "float" => "double",
  212. "image" or "binary" or "varbinary" => "byte[]",
  213. "uniqueidentifier" => "Guid",
  214. _ => "object",
  215. };
  216. }
  217. /// <summary>
  218. /// 数据类型转显示类型
  219. /// </summary>
  220. /// <param name="dataType"></param>
  221. /// <returns></returns>
  222. public static string DataTypeToEff(string dataType)
  223. {
  224. if (string.IsNullOrEmpty(dataType)) return "";
  225. return dataType?.TrimEnd('?') switch
  226. {
  227. "string" => "Input",
  228. "int" => "InputNumber",
  229. "long" => "Input",
  230. "float" => "Input",
  231. "double" => "Input",
  232. "decimal" => "Input",
  233. "bool" => "Switch",
  234. "Guid" => "Input",
  235. "DateTime" => "DatePicker",
  236. _ => "Input",
  237. };
  238. }
  239. // 是否通用字段
  240. public static bool IsCommonColumn(string columnName)
  241. {
  242. var columnList = new List<string>()
  243. {
  244. "CreateOrgId", "TenantId", "CreateTime", "UpdateTime", "CreateUserId", "UpdateUserId", "IsDelete"
  245. };
  246. return columnList.Contains(columnName);
  247. }
  248. }