CodeGenUtil.cs 8.8 KB

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