using SqlSugar;
using System;
using ;
using ;
using ;
namespace DDD
{
/// <summary>
///
/// SqlSugarClient Code-First Table Building
/// Create a SQL statement based on the given entity class, and then create a MySQL table.
///
/// Advantage:
/// Given a random C# entity class, you can build a table.
/// No need to add extra attributes to the properties of entity classes. E.g. the cumbersome [SugarColumn(IsPrimaryKey = true)], etc.
///
/// Usage:
/// (DB, typeof(YourType1),typeof(YourType2));
///
/// Follow-up practices:
/// If you are not satisfied with the field types, you can use HeidiSQL software to modify the fields and types of the table.
/// Add fields, modify field names, add indexes, and also use the software.
///
/// by lmp 2024.11.15
///
/// </summary>
public class CreateTableCodeFirstOnMySQL
{
/// <summary>
/// Create Table
/// </summary>
/// <param name="db"></param>
/// <param name="entityTypes"></param>
public static void CreateTablesBySQL(SqlSugarClient db, params Type[] entityTypes)
{
foreach (Type entityType in entityTypes)
{
string sql = GetCreateTableSQLOfType(entityType);
(sql);
($"Create table complete [{}]");
}
}
/// <summary>
/// Generate a table building SQL statement
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
public static string GetCreateTableSQLOfType(Type entityType)
{
string tableName = ;
string sqlPropertys = CreateOther(entityType);
StringBuilder sb = new StringBuilder();
($@"
CREATE TABLE `{tableName}` (
`Id` BIGINT NOT NULL AUTO_INCREMENT,
{sqlPropertys},
PRIMARY KEY (`Id`) USING BTREE
)
COLLATE='utf8mb4_0900_ai_ci'
ENGINE=InnoDB
");
return ();
}
/// <summary>
/// Create types other than Id
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private static string CreateOther(Type entityType)
{
PropertyInfo[] piArray = (); // All public properties of type T
List<string> items = new List<string>();
foreach (PropertyInfo pi in piArray)
{
string columnName = ;
if (() == "ID") // Exclude Id because the statement has already been created.
{
continue;
}
Type tp = ;
if (tp == typeof(DateTime) || tp == typeof(DateTime?))
{
($"`{columnName}` DATETIME NULL ");
}
else if (tp == typeof(float) || tp == typeof(float?))
{
($"`{columnName}` DOUBLE NULL DEFAULT 0 ");
}
else if (tp == typeof(double) || tp == typeof(double?))
{
($"`{columnName}` DOUBLE NULL DEFAULT 0 ");
}
else if (tp == typeof(decimal) || tp == typeof(decimal?))
{
($"`{columnName}` DOUBLE NULL DEFAULT 0 ");
}
else if (tp == typeof(int) || tp == typeof(int?))
{
($"`{columnName}` INT NULL DEFAULT 0 ");
}
else if (tp == typeof(long) || tp == typeof(long?))
{
($"`{columnName}` BIGINT NULL DEFAULT 0 ");
}
else if (tp == typeof(bool) || tp == typeof(bool?))
{
($"`{columnName}` TINYINT(1) NULL DEFAULT 0 ");
}
else if (tp == typeof(string))
{
($"`{columnName}` VARCHAR(255) NULL DEFAULT NULL ");
}
else if (tp == typeof(byte[]))
{
($"`{columnName}` LONGBLOB NULL DEFAULT NULL ");
}
else
{
throw new Exception($"The unknown type of {columnName} in {}:" + tp);
}
}
return string.Join(", \r\n", items);
}
}
}