CodeGenUtil.cs 7.9 KB

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