CodeGenUtil.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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)
  51. {
  52. var dbType = App.GetOptions<DbConnectionOptions>().ConnectionConfigs[0].DbType;
  53. var dataType = dbType switch
  54. {
  55. DbType.Oracle => ConvertDataType_OracleSQL(string.IsNullOrEmpty(dbColumnInfo.OracleDataType) ? dbColumnInfo.DataType : dbColumnInfo.OracleDataType, dbColumnInfo.Length, dbColumnInfo.Scale),
  56. DbType.PostgreSQL => ConvertDataType_PostgreSQL(dbColumnInfo.DataType),
  57. _ => ConvertDataType_Default(dbColumnInfo.DataType),
  58. };
  59. return dataType + (dbColumnInfo.IsNullable ? "?" : "");
  60. }
  61. public static string ConvertDataType_OracleSQL(string dataType, int? length, int? scale)
  62. {
  63. switch (dataType.ToLower())
  64. {
  65. case "interval year to month":
  66. return "int";
  67. case "interval day to second":
  68. return "TimeSpan";
  69. case "smallint":
  70. return "Int16";
  71. case "int":
  72. case "integer":
  73. return "int";
  74. case "long":
  75. return "long";
  76. case "float":
  77. return "float";
  78. case "decimal":
  79. return "decimal";
  80. case "number":
  81. if (length != null)
  82. {
  83. if (scale != null && scale > 0)
  84. {
  85. return "decimal";
  86. }
  87. else if ((scale != null && scale == 0) || scale == null)
  88. {
  89. if (length > 1 && length < 12)
  90. {
  91. return "int";
  92. }
  93. else if (length > 11)
  94. {
  95. return "long";
  96. }
  97. }
  98. if (length == 1)
  99. {
  100. return "bool";
  101. }
  102. }
  103. return "decimal";
  104. case "char":
  105. case "clob":
  106. case "nclob":
  107. case "nchar":
  108. case "nvarchar":
  109. case "varchar":
  110. case "nvarchar2":
  111. case "varchar2":
  112. case "rowid":
  113. return "string";
  114. case "timestamp":
  115. case "timestamp with time zone":
  116. case "timestamptz":
  117. case "timestamp without time zone":
  118. case "date":
  119. case "time":
  120. case "time with time zone":
  121. case "timetz":
  122. case "time without time zone":
  123. return "DateTime";
  124. case "bfile":
  125. case "blob":
  126. case "raw":
  127. return "byte[]";
  128. default:
  129. return "object";
  130. }
  131. }
  132. //PostgreSQL数据类型对应的字段类型
  133. public static string ConvertDataType_PostgreSQL(string dataType)
  134. {
  135. switch (dataType)
  136. {
  137. case "int2":
  138. case "smallint":
  139. return "Int16";
  140. case "int4":
  141. case "integer":
  142. return "int";
  143. case "int8":
  144. case "bigint":
  145. return "long";
  146. case "float4":
  147. case "real":
  148. return "float";
  149. case "float8":
  150. case "double precision":
  151. return "double";
  152. case "numeric":
  153. case "decimal":
  154. case "path":
  155. case "point":
  156. case "polygon":
  157. case "interval":
  158. case "lseg":
  159. case "macaddr":
  160. case "money":
  161. return "decimal";
  162. case "boolean":
  163. case "bool":
  164. case "box":
  165. case "bytea":
  166. return "bool";
  167. case "varchar":
  168. case "character varying":
  169. case "geometry":
  170. case "name":
  171. case "text":
  172. case "char":
  173. case "character":
  174. case "cidr":
  175. case "circle":
  176. case "tsquery":
  177. case "tsvector":
  178. case "txid_snapshot":
  179. case "xml":
  180. case "json":
  181. return "string";
  182. case "uuid":
  183. return "Guid";
  184. case "timestamp":
  185. case "timestamp with time zone":
  186. case "timestamptz":
  187. case "timestamp without time zone":
  188. case "date":
  189. case "time":
  190. case "time with time zone":
  191. case "timetz":
  192. case "time without time zone":
  193. return "DateTime";
  194. case "bit":
  195. case "bit varying":
  196. return "byte[]";
  197. case "varbit":
  198. return "byte";
  199. case "public.geometry":
  200. case "inet":
  201. return "object";
  202. default:
  203. return "object";
  204. }
  205. }
  206. public static string ConvertDataType_Default(string dataType)
  207. {
  208. return dataType.ToLower() switch
  209. {
  210. "text" or "varchar" or "char" or "nvarchar" or "nchar" or "timestamp" => "string",
  211. "int" => "int",
  212. "smallint" => "Int16",
  213. //"tinyint" => "byte",
  214. "tinyint" => "bool", // MYSQL
  215. "bigint" or "integer" => "long",
  216. "bit" => "bool",
  217. "money" or "smallmoney" or "numeric" or "decimal" => "decimal",
  218. "real" => "Single",
  219. "datetime" or "smalldatetime" => "DateTime",
  220. "float" => "double",
  221. "image" or "binary" or "varbinary" => "byte[]",
  222. "uniqueidentifier" => "Guid",
  223. _ => "object",
  224. };
  225. }
  226. /// <summary>
  227. /// 数据类型转显示类型
  228. /// </summary>
  229. /// <param name="dataType"></param>
  230. /// <returns></returns>
  231. public static string DataTypeToEff(string dataType)
  232. {
  233. if (string.IsNullOrEmpty(dataType)) return "";
  234. return dataType?.TrimEnd('?') switch
  235. {
  236. "string" => "Input",
  237. "int" => "InputNumber",
  238. "long" => "Input",
  239. "float" => "Input",
  240. "double" => "Input",
  241. "decimal" => "Input",
  242. "bool" => "Switch",
  243. "Guid" => "Input",
  244. "DateTime" => "DatePicker",
  245. _ => "Input",
  246. };
  247. }
  248. // 是否通用字段
  249. public static bool IsCommonColumn(string columnName)
  250. {
  251. var columnList = new List<string>()
  252. {
  253. "CreateOrgId", "TenantId", "CreateTime", "UpdateTime", "CreateUserId", "UpdateUserId", "IsDelete"
  254. };
  255. return columnList.Contains(columnName);
  256. }
  257. }