2024-11-27 10:53:00 +08:00
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2024-06-15 13:02:35 +08:00
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using DbType = SqlSugar . DbType ;
namespace Admin.NET.Core ;
/// <summary>
/// 代码生成帮助类
/// </summary>
2025-06-16 00:19:30 +08:00
public static class CodeGenHelper
2024-06-15 13:02:35 +08:00
{
/// <summary>
/// 转换大驼峰法命名
/// </summary>
/// <param name="columnName">字段名</param>
/// <param name="dbColumnNames">EntityBase 实体属性名称</param>
/// <returns></returns>
public static string CamelColumnName ( string columnName , string [ ] dbColumnNames )
{
if ( columnName . Contains ( '_' ) )
{
var arrColName = columnName . Split ( '_' ) ;
var sb = new StringBuilder ( ) ;
foreach ( var col in arrColName )
{
if ( col . Length > 0 )
sb . Append ( col [ . . 1 ] . ToUpper ( ) + col [ 1. . ] . ToLower ( ) ) ;
}
columnName = sb . ToString ( ) ;
}
else
{
2025-06-16 00:19:30 +08:00
var propertyName = dbColumnNames . FirstOrDefault ( u = > u . ToLower ( ) = = columnName . ToLower ( ) ) ;
2024-06-15 13:02:35 +08:00
if ( ! string . IsNullOrEmpty ( propertyName ) )
{
columnName = propertyName ;
}
else
{
columnName = columnName [ . . 1 ] . ToUpper ( ) + columnName [ 1. . ] . ToLower ( ) ;
}
}
return columnName ;
}
// 根据数据库类型来处理对应的数据字段类型
public static string ConvertDataType ( DbColumnInfo dbColumnInfo , DbType dbType = DbType . Custom )
{
if ( dbType = = DbType . Custom )
dbType = App . GetOptions < DbConnectionOptions > ( ) . ConnectionConfigs [ 0 ] . DbType ;
var dataType = dbType switch
{
DbType . Oracle = > ConvertDataType_OracleSQL ( string . IsNullOrEmpty ( dbColumnInfo . OracleDataType ) ? dbColumnInfo . DataType : dbColumnInfo . OracleDataType , dbColumnInfo . Length , dbColumnInfo . Scale ) ,
2024-11-27 10:53:00 +08:00
DbType . Dm = > ConvertDataType_Dm ( string . IsNullOrEmpty ( dbColumnInfo . OracleDataType ) ? dbColumnInfo . DataType : dbColumnInfo . OracleDataType , dbColumnInfo . Length , dbColumnInfo . Scale ) ,
2024-06-15 13:02:35 +08:00
DbType . PostgreSQL = > ConvertDataType_PostgreSQL ( dbColumnInfo . DataType ) ,
_ = > ConvertDataType_Default ( dbColumnInfo . DataType ) ,
} ;
return dataType + ( dbColumnInfo . IsNullable ? "?" : "" ) ;
}
2024-11-27 10:53:00 +08:00
// 达梦(DM)数据类型对应的字段类型
public static string ConvertDataType_Dm ( string dataType , int? length , int? scale )
{
return ConvertDataType_OracleSQL ( dataType , length , scale ) ; //达梦兼容Oracle, 目前先这样实现
}
2024-11-28 02:47:41 +08:00
2024-11-25 16:27:14 +08:00
// OracleSQL数据类型对应的字段类型
2024-06-15 13:02:35 +08:00
public static string ConvertDataType_OracleSQL ( string dataType , int? length , int? scale )
{
switch ( dataType . ToLower ( ) )
{
2024-11-25 16:27:14 +08:00
case "interval year to month" : return "int" ;
2024-06-15 13:02:35 +08:00
2024-11-25 16:27:14 +08:00
case "interval day to second" : return "TimeSpan" ;
2024-06-15 13:02:35 +08:00
2024-11-25 16:27:14 +08:00
case "smallint" : return "Int16" ;
2024-06-15 13:02:35 +08:00
case "int" :
2024-11-25 16:27:14 +08:00
case "integer" : return "int" ;
2024-06-15 13:02:35 +08:00
2024-11-25 16:27:14 +08:00
case "long" : return "long" ;
2024-06-15 13:02:35 +08:00
2024-11-25 16:27:14 +08:00
case "float" : return "float" ;
2024-06-15 13:02:35 +08:00
2024-11-25 16:27:14 +08:00
case "decimal" : return "decimal" ;
2024-06-15 13:02:35 +08:00
case "number" :
2024-11-25 16:27:14 +08:00
if ( length = = null ) return "decimal" ;
return scale switch
2024-06-15 13:02:35 +08:00
{
2024-11-25 16:27:14 +08:00
> 0 = > "decimal" ,
0 or null when length is > 1 and < 12 = > "int" ,
0 or null when length > 11 = > "long" ,
_ = > length = = 1 ? "bool" : "decimal"
} ;
2024-06-15 13:02:35 +08:00
case "char" :
case "clob" :
case "nclob" :
case "nchar" :
case "nvarchar" :
case "varchar" :
case "nvarchar2" :
case "varchar2" :
case "rowid" :
return "string" ;
case "timestamp" :
case "timestamp with time zone" :
case "timestamptz" :
case "timestamp without time zone" :
case "date" :
case "time" :
case "time with time zone" :
case "timetz" :
case "time without time zone" :
return "DateTime" ;
case "bfile" :
case "blob" :
case "raw" :
return "byte[]" ;
default :
return "object" ;
}
}
2024-11-25 16:27:14 +08:00
// PostgreSQL数据类型对应的字段类型
2024-06-15 13:02:35 +08:00
public static string ConvertDataType_PostgreSQL ( string dataType )
{
2025-06-16 00:19:30 +08:00
return dataType switch
2024-06-15 13:02:35 +08:00
{
2025-06-16 00:19:30 +08:00
"int2" or "smallint" = > "Int16" ,
"int4" or "integer" = > "int" ,
"int8" or "bigint" = > "long" ,
"float4" or "real" = > "float" ,
"float8" or "double precision" = > "double" ,
"numeric" or "decimal" or "path" or "point" or "polygon" or "interval" or "lseg" or "macaddr" or "money" = > "decimal" ,
"boolean" or "bool" or "box" or "bytea" = > "bool" ,
"varchar" or "character varying" or "geometry" or "name" or "text" or "char" or "character" or "cidr" or "circle" or "tsquery" or "tsvector" or "txid_snapshot" or "xml" or "json" = > "string" ,
"uuid" = > "Guid" ,
"timestamp" or "timestamp with time zone" or "timestamptz" or "timestamp without time zone" or "date" or "time" or "time with time zone" or "timetz" or "time without time zone" = > "DateTime" ,
"bit" or "bit varying" = > "byte[]" ,
"varbit" = > "byte" ,
_ = > "object" ,
} ;
2024-06-15 13:02:35 +08:00
}
2024-11-25 21:35:23 +08:00
// 默认数据类型
2024-06-15 13:02:35 +08:00
public static string ConvertDataType_Default ( string dataType )
{
return dataType . ToLower ( ) switch
{
2025-06-19 01:42:57 +08:00
"tinytext" or "mediumtext" or "longtext" or "mid" or "text" or "varchar" or "char" or "nvarchar" or "nchar" or "string" or "timestamp" = > "string" ,
"int" or "integer" or "int32" = > "int" ,
2024-06-15 13:02:35 +08:00
"smallint" = > "Int16" ,
//"tinyint" => "byte",
2025-06-19 01:42:57 +08:00
"tinyint" = > "bool" , // MYSQL
"bigint" or "int64" = > "long" ,
"bit" or "boolean" = > "bool" ,
2024-06-15 13:02:35 +08:00
"money" or "smallmoney" or "numeric" or "decimal" = > "decimal" ,
"real" = > "Single" ,
2024-06-18 01:58:17 +08:00
"datetime" or "datetime2" or "smalldatetime" = > "DateTime" ,
2024-06-15 13:02:35 +08:00
"float" or "double" = > "double" ,
"image" or "binary" or "varbinary" = > "byte[]" ,
"uniqueidentifier" = > "Guid" ,
_ = > "object" ,
} ;
}
/// <summary>
/// 数据类型转显示类型
/// </summary>
/// <param name="dataType"></param>
/// <returns></returns>
public static string DataTypeToEff ( string dataType )
{
if ( string . IsNullOrEmpty ( dataType ) ) return "" ;
return dataType ? . TrimEnd ( '?' ) switch
{
"string" = > "Input" ,
"int" = > "InputNumber" ,
2024-11-25 16:27:14 +08:00
"long" = > "InputNumber" ,
"float" = > "InputNumber" ,
"double" = > "InputNumber" ,
"decimal" = > "InputNumber" ,
2024-06-15 13:02:35 +08:00
"bool" = > "Switch" ,
"Guid" = > "Input" ,
"DateTime" = > "DatePicker" ,
_ = > "Input" ,
} ;
}
// 是否通用字段
public static bool IsCommonColumn ( string columnName )
{
var columnList = new List < string > ( )
{
nameof ( EntityBaseData . CreateOrgId ) ,
2024-07-16 10:53:15 +08:00
nameof ( EntityBaseData . CreateOrgName ) ,
2024-06-15 13:02:35 +08:00
nameof ( EntityTenant . TenantId ) ,
nameof ( EntityBase . CreateTime ) ,
nameof ( EntityBase . UpdateTime ) ,
nameof ( EntityBase . CreateUserId ) ,
nameof ( EntityBase . UpdateUserId ) ,
nameof ( EntityBase . CreateUserName ) ,
nameof ( EntityBase . UpdateUserName ) ,
nameof ( EntityBase . IsDelete )
} ;
return columnList . Contains ( columnName ) ;
}
}