CodeGenUtil.cs 7.7 KB

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