CodeGenUtil.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. using DbType = SqlSugar.DbType;
  10. namespace Admin.NET.Core;
  11. /// <summary>
  12. /// 代码生成帮助类
  13. /// </summary>
  14. public static class CodeGenUtil
  15. {
  16. /// <summary>
  17. /// 转换大驼峰法命名
  18. /// </summary>
  19. /// <param name="columnName">字段名</param>
  20. /// <param name="dbColumnNames">EntityBase 实体属性名称</param>
  21. /// <returns></returns>
  22. public static string CamelColumnName(string columnName, string[] dbColumnNames)
  23. {
  24. if (columnName.Contains('_'))
  25. {
  26. var arrColName = columnName.Split('_');
  27. var sb = new StringBuilder();
  28. foreach (var col in arrColName)
  29. {
  30. if (col.Length > 0)
  31. sb.Append(col[..1].ToUpper() + col[1..].ToLower());
  32. }
  33. columnName = sb.ToString();
  34. }
  35. else
  36. {
  37. var propertyName = dbColumnNames.FirstOrDefault(c => c.ToLower() == columnName.ToLower());
  38. if (!string.IsNullOrEmpty(propertyName))
  39. {
  40. columnName = propertyName;
  41. }
  42. else
  43. {
  44. columnName = columnName[..1].ToUpper() + columnName[1..].ToLower();
  45. }
  46. }
  47. return columnName;
  48. }
  49. // 根据数据库类型来处理对应的数据字段类型
  50. public static string ConvertDataType(DbColumnInfo dbColumnInfo, DbType dbType = DbType.Custom)
  51. {
  52. if (dbType == DbType.Custom)
  53. dbType = App.GetOptions<DbConnectionOptions>().ConnectionConfigs[0].DbType;
  54. var dataType = dbType switch
  55. {
  56. DbType.Oracle => ConvertDataType_OracleSQL(string.IsNullOrEmpty(dbColumnInfo.OracleDataType) ? dbColumnInfo.DataType : dbColumnInfo.OracleDataType, dbColumnInfo.Length, dbColumnInfo.Scale),
  57. DbType.PostgreSQL => ConvertDataType_PostgreSQL(dbColumnInfo.DataType),
  58. _ => ConvertDataType_Default(dbColumnInfo.DataType),
  59. };
  60. return dataType + (dbColumnInfo.IsNullable ? "?" : "");
  61. }
  62. public static string ConvertDataType_OracleSQL(string dataType, int? length, int? scale)
  63. {
  64. switch (dataType.ToLower())
  65. {
  66. case "interval year to month":
  67. return "int";
  68. case "interval day to second":
  69. return "TimeSpan";
  70. case "smallint":
  71. return "Int16";
  72. case "int":
  73. case "integer":
  74. return "int";
  75. case "long":
  76. return "long";
  77. case "float":
  78. return "float";
  79. case "decimal":
  80. return "decimal";
  81. case "number":
  82. if (length != null)
  83. {
  84. if (scale != null && scale > 0)
  85. {
  86. return "decimal";
  87. }
  88. else if ((scale != null && scale == 0) || scale == null)
  89. {
  90. if (length > 1 && length < 12)
  91. {
  92. return "int";
  93. }
  94. else if (length > 11)
  95. {
  96. return "long";
  97. }
  98. }
  99. if (length == 1)
  100. {
  101. return "bool";
  102. }
  103. }
  104. return "decimal";
  105. case "char":
  106. case "clob":
  107. case "nclob":
  108. case "nchar":
  109. case "nvarchar":
  110. case "varchar":
  111. case "nvarchar2":
  112. case "varchar2":
  113. case "rowid":
  114. return "string";
  115. case "timestamp":
  116. case "timestamp with time zone":
  117. case "timestamptz":
  118. case "timestamp without time zone":
  119. case "date":
  120. case "time":
  121. case "time with time zone":
  122. case "timetz":
  123. case "time without time zone":
  124. return "DateTime";
  125. case "bfile":
  126. case "blob":
  127. case "raw":
  128. return "byte[]";
  129. default:
  130. return "object";
  131. }
  132. }
  133. //PostgreSQL数据类型对应的字段类型
  134. public static string ConvertDataType_PostgreSQL(string dataType)
  135. {
  136. switch (dataType)
  137. {
  138. case "int2":
  139. case "smallint":
  140. return "Int16";
  141. case "int4":
  142. case "integer":
  143. return "int";
  144. case "int8":
  145. case "bigint":
  146. return "long";
  147. case "float4":
  148. case "real":
  149. return "float";
  150. case "float8":
  151. case "double precision":
  152. return "double";
  153. case "numeric":
  154. case "decimal":
  155. case "path":
  156. case "point":
  157. case "polygon":
  158. case "interval":
  159. case "lseg":
  160. case "macaddr":
  161. case "money":
  162. return "decimal";
  163. case "boolean":
  164. case "bool":
  165. case "box":
  166. case "bytea":
  167. return "bool";
  168. case "varchar":
  169. case "character varying":
  170. case "geometry":
  171. case "name":
  172. case "text":
  173. case "char":
  174. case "character":
  175. case "cidr":
  176. case "circle":
  177. case "tsquery":
  178. case "tsvector":
  179. case "txid_snapshot":
  180. case "xml":
  181. case "json":
  182. return "string";
  183. case "uuid":
  184. return "Guid";
  185. case "timestamp":
  186. case "timestamp with time zone":
  187. case "timestamptz":
  188. case "timestamp without time zone":
  189. case "date":
  190. case "time":
  191. case "time with time zone":
  192. case "timetz":
  193. case "time without time zone":
  194. return "DateTime";
  195. case "bit":
  196. case "bit varying":
  197. return "byte[]";
  198. case "varbit":
  199. return "byte";
  200. case "public.geometry":
  201. case "inet":
  202. return "object";
  203. default:
  204. return "object";
  205. }
  206. }
  207. public static string ConvertDataType_Default(string dataType)
  208. {
  209. return dataType.ToLower() switch
  210. {
  211. "tinytext" or "mediumtext" or "longtext" or "mid" or "text" or "varchar" or "char" or "nvarchar" or "nchar" or "timestamp" => "string",
  212. "int" => "int",
  213. "smallint" => "Int16",
  214. //"tinyint" => "byte",
  215. "tinyint" => "bool", // MYSQL
  216. "bigint" or "integer" => "long",
  217. "bit" => "bool",
  218. "money" or "smallmoney" or "numeric" or "decimal" => "decimal",
  219. "real" => "Single",
  220. "datetime" or "smalldatetime" => "DateTime",
  221. "float" or "double" => "double",
  222. "image" or "binary" or "varbinary" => "byte[]",
  223. "uniqueidentifier" => "Guid",
  224. _ => "object",
  225. };
  226. }
  227. /// <summary>
  228. /// 数据类型转显示类型
  229. /// </summary>
  230. /// <param name="dataType"></param>
  231. /// <returns></returns>
  232. public static string DataTypeToEff(string dataType)
  233. {
  234. if (string.IsNullOrEmpty(dataType)) return "";
  235. return dataType?.TrimEnd('?') switch
  236. {
  237. "string" => "Input",
  238. "int" => "InputNumber",
  239. "long" => "Input",
  240. "float" => "Input",
  241. "double" => "Input",
  242. "decimal" => "Input",
  243. "bool" => "Switch",
  244. "Guid" => "Input",
  245. "DateTime" => "DatePicker",
  246. _ => "Input",
  247. };
  248. }
  249. // 是否通用字段
  250. public static bool IsCommonColumn(string columnName)
  251. {
  252. var columnList = new List<string>()
  253. {
  254. "CreateOrgId", "TenantId", "CreateTime", "UpdateTime", "CreateUserId", "UpdateUserId", "IsDelete"
  255. };
  256. return columnList.Contains(columnName);
  257. }
  258. }