CodeGenUtil.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. using DbType = SqlSugar.DbType;
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// 代码生成帮助类
  12. /// </summary>
  13. public static class CodeGenUtil
  14. {
  15. /// <summary>
  16. /// 转换大驼峰法命名
  17. /// </summary>
  18. /// <param name="columnName">字段名</param>
  19. /// <param name="dbColumnNames">EntityBase 实体属性名称</param>
  20. /// <returns></returns>
  21. public static string CamelColumnName(string columnName, string[] dbColumnNames)
  22. {
  23. if (columnName.Contains('_'))
  24. {
  25. var arrColName = columnName.Split('_');
  26. var sb = new StringBuilder();
  27. foreach (var col in arrColName)
  28. {
  29. if (col.Length > 0)
  30. sb.Append(col[..1].ToUpper() + col[1..].ToLower());
  31. }
  32. columnName = sb.ToString();
  33. }
  34. else
  35. {
  36. var propertyName = dbColumnNames.FirstOrDefault(c => c.ToLower() == columnName.ToLower());
  37. if (!string.IsNullOrEmpty(propertyName))
  38. {
  39. columnName = propertyName;
  40. }
  41. else
  42. {
  43. columnName = columnName[..1].ToUpper() + columnName[1..].ToLower();
  44. }
  45. }
  46. return columnName;
  47. }
  48. // 根据数据库类型来处理对应的数据字段类型
  49. public static string ConvertDataType(DbColumnInfo dbColumnInfo, DbType dbType = DbType.Custom)
  50. {
  51. if (dbType == DbType.Custom)
  52. 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. default:
  200. return "object";
  201. }
  202. }
  203. public static string ConvertDataType_Default(string dataType)
  204. {
  205. return dataType.ToLower() switch
  206. {
  207. "tinytext" or "mediumtext" or "longtext" or "mid" or "text" or "varchar" or "char" or "nvarchar" or "nchar" or "timestamp" => "string",
  208. "int" => "int",
  209. "smallint" => "Int16",
  210. //"tinyint" => "byte",
  211. "tinyint" => "bool", // MYSQL
  212. "bigint" or "integer" => "long",
  213. "bit" => "bool",
  214. "money" or "smallmoney" or "numeric" or "decimal" => "decimal",
  215. "real" => "Single",
  216. "datetime" or "smalldatetime" => "DateTime",
  217. "float" or "double" => "double",
  218. "image" or "binary" or "varbinary" => "byte[]",
  219. "uniqueidentifier" => "Guid",
  220. _ => "object",
  221. };
  222. }
  223. /// <summary>
  224. /// 数据类型转显示类型
  225. /// </summary>
  226. /// <param name="dataType"></param>
  227. /// <returns></returns>
  228. public static string DataTypeToEff(string dataType)
  229. {
  230. if (string.IsNullOrEmpty(dataType)) return "";
  231. return dataType?.TrimEnd('?') switch
  232. {
  233. "string" => "Input",
  234. "int" => "InputNumber",
  235. "long" => "Input",
  236. "float" => "Input",
  237. "double" => "Input",
  238. "decimal" => "Input",
  239. "bool" => "Switch",
  240. "Guid" => "Input",
  241. "DateTime" => "DatePicker",
  242. _ => "Input",
  243. };
  244. }
  245. // 是否通用字段
  246. public static bool IsCommonColumn(string columnName)
  247. {
  248. var columnList = new List<string>()
  249. {
  250. nameof(EntityBaseData.CreateOrgId),
  251. nameof(EntityTenant.TenantId),
  252. nameof(EntityBase.CreateTime),
  253. nameof(EntityBase.UpdateTime),
  254. nameof(EntityBase.CreateUserId),
  255. nameof(EntityBase.UpdateUserId),
  256. nameof(EntityBase.CreateUserName),
  257. nameof(EntityBase.UpdateUserName),
  258. nameof(EntityBase.IsDelete)
  259. };
  260. return columnList.Contains(columnName);
  261. }
  262. }