mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-24 16:18:47 +08:00
Update MySql
This commit is contained in:
parent
41136bbba1
commit
3b7b9c2de1
@ -8,6 +8,6 @@ namespace OrmTest
|
|||||||
{
|
{
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
public static string ConnectionString = "server=localhost;Database=SqlSugarTest;Uid=root;Pwd=root";
|
public static string ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=root";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,13 +14,13 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public methods
|
#region Public methods
|
||||||
public ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue)
|
public virtual ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue)
|
||||||
{
|
{
|
||||||
this.IsBackupTable = true;
|
this.IsBackupTable = true;
|
||||||
this.MaxBackupDataRows = maxBackupDataRows;
|
this.MaxBackupDataRows = maxBackupDataRows;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public void InitTables(Type entityType)
|
public virtual void InitTables(Type entityType)
|
||||||
{
|
{
|
||||||
if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
|
if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
|
||||||
{
|
{
|
||||||
@ -33,7 +33,7 @@ namespace SqlSugar
|
|||||||
});
|
});
|
||||||
Check.Exception(!executeResult.IsSuccess, executeResult.Messaage);
|
Check.Exception(!executeResult.IsSuccess, executeResult.Messaage);
|
||||||
}
|
}
|
||||||
public void InitTables(Type[] entityTypes)
|
public virtual void InitTables(Type[] entityTypes)
|
||||||
{
|
{
|
||||||
if (entityTypes.IsValuable())
|
if (entityTypes.IsValuable())
|
||||||
{
|
{
|
||||||
@ -43,12 +43,12 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void InitTables(string entitiesNamespace)
|
public virtual void InitTables(string entitiesNamespace)
|
||||||
{
|
{
|
||||||
var types = Assembly.Load(entitiesNamespace).GetTypes();
|
var types = Assembly.Load(entitiesNamespace).GetTypes();
|
||||||
InitTables(types);
|
InitTables(types);
|
||||||
}
|
}
|
||||||
public void InitTables(params string[] entitiesNamespaces)
|
public virtual void InitTables(params string[] entitiesNamespaces)
|
||||||
{
|
{
|
||||||
if (entitiesNamespaces.IsValuable())
|
if (entitiesNamespaces.IsValuable())
|
||||||
{
|
{
|
||||||
@ -61,7 +61,7 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Core Logic
|
#region Core Logic
|
||||||
private void Execute(Type entityType)
|
protected virtual void Execute(Type entityType)
|
||||||
{
|
{
|
||||||
var entityInfo = this.Context.EntityProvider.GetEntityInfo(entityType);
|
var entityInfo = this.Context.EntityProvider.GetEntityInfo(entityType);
|
||||||
var tableName = GetTableName(entityInfo);
|
var tableName = GetTableName(entityInfo);
|
||||||
@ -71,7 +71,7 @@ namespace SqlSugar
|
|||||||
else
|
else
|
||||||
NoExistLogic(entityInfo);
|
NoExistLogic(entityInfo);
|
||||||
}
|
}
|
||||||
private void NoExistLogic(EntityInfo entityInfo)
|
public virtual void NoExistLogic(EntityInfo entityInfo)
|
||||||
{
|
{
|
||||||
var tableName = GetTableName(entityInfo);
|
var tableName = GetTableName(entityInfo);
|
||||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||||
@ -91,7 +91,7 @@ namespace SqlSugar
|
|||||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void ExistLogic(EntityInfo entityInfo)
|
public virtual void ExistLogic(EntityInfo entityInfo)
|
||||||
{
|
{
|
||||||
if (entityInfo.Columns.IsValuable())
|
if (entityInfo.Columns.IsValuable())
|
||||||
{
|
{
|
||||||
@ -192,11 +192,11 @@ namespace SqlSugar
|
|||||||
var tableName = GetTableName(entityInfo);
|
var tableName = GetTableName(entityInfo);
|
||||||
return result.ToString();
|
return result.ToString();
|
||||||
}
|
}
|
||||||
private static string GetTableName(EntityInfo entityInfo)
|
protected string GetTableName(EntityInfo entityInfo)
|
||||||
{
|
{
|
||||||
return entityInfo.DbTableName == null ? entityInfo.EntityName : entityInfo.DbTableName;
|
return entityInfo.DbTableName == null ? entityInfo.EntityName : entityInfo.DbTableName;
|
||||||
}
|
}
|
||||||
private DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
protected DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
||||||
{
|
{
|
||||||
var result = new DbColumnInfo()
|
var result = new DbColumnInfo()
|
||||||
{
|
{
|
||||||
@ -214,7 +214,7 @@ namespace SqlSugar
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
|
protected bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
|
||||||
{
|
{
|
||||||
var propType = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(ec.PropertyInfo).Name);
|
var propType = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(ec.PropertyInfo).Name);
|
||||||
var dataType = dc.DataType;
|
var dataType = dc.DataType;
|
||||||
|
@ -201,7 +201,7 @@ namespace SqlSugar
|
|||||||
return reval;
|
return reval;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
private string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
|
protected virtual string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
|
||||||
{
|
{
|
||||||
List<string> columnArray = new List<string>();
|
List<string> columnArray = new List<string>();
|
||||||
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
|
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
|
||||||
@ -213,13 +213,13 @@ namespace SqlSugar
|
|||||||
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
||||||
string primaryKey = null;
|
string primaryKey = null;
|
||||||
string identity = item.IsIdentity ? this.CreateTableIdentity : null;
|
string identity = item.IsIdentity ? this.CreateTableIdentity : null;
|
||||||
string addItem = string.Format(this.CreateTableColumn, columnName, dataType, dataSize, nullType, primaryKey, identity);
|
string addItem = string.Format(this.CreateTableColumn,this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
|
||||||
columnArray.Add(addItem);
|
columnArray.Add(addItem);
|
||||||
}
|
}
|
||||||
string tableString = string.Format(this.CreateTableSql, tableName, string.Join(",\r\n", columnArray));
|
string tableString = string.Format(this.CreateTableSql,this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
|
||||||
return tableString;
|
return tableString;
|
||||||
}
|
}
|
||||||
private string GetAddColumnSql(string tableName, DbColumnInfo columnInfo)
|
protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo)
|
||||||
{
|
{
|
||||||
string columnName = columnInfo.DbColumnName;
|
string columnName = columnInfo.DbColumnName;
|
||||||
string dataType = columnInfo.DataType;
|
string dataType = columnInfo.DataType;
|
||||||
@ -230,7 +230,7 @@ namespace SqlSugar
|
|||||||
string result = string.Format(this.AddColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
|
string result = string.Format(this.AddColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
private string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo)
|
protected virtual string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo)
|
||||||
{
|
{
|
||||||
string columnName = columnInfo.DbColumnName;
|
string columnName = columnInfo.DbColumnName;
|
||||||
string dataType = columnInfo.DataType;
|
string dataType = columnInfo.DataType;
|
||||||
|
@ -8,7 +8,20 @@ namespace SqlSugar
|
|||||||
public abstract partial class DbMaintenanceProvider : IDbMaintenance
|
public abstract partial class DbMaintenanceProvider : IDbMaintenance
|
||||||
{
|
{
|
||||||
#region Context
|
#region Context
|
||||||
|
private ISqlBuilder _SqlBuilder;
|
||||||
public SqlSugarClient Context { get; set; }
|
public SqlSugarClient Context { get; set; }
|
||||||
|
public ISqlBuilder SqlBuilder
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_SqlBuilder == null)
|
||||||
|
{
|
||||||
|
_SqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
|
||||||
|
_SqlBuilder.Context = this.Context;
|
||||||
|
}
|
||||||
|
return _SqlBuilder;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region DML
|
#region DML
|
||||||
|
@ -7,5 +7,20 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
public class MySqlCodeFirst:CodeFirstProvider
|
public class MySqlCodeFirst:CodeFirstProvider
|
||||||
{
|
{
|
||||||
|
public override void NoExistLogic(EntityInfo entityInfo)
|
||||||
|
{
|
||||||
|
var tableName = base.GetTableName(entityInfo);
|
||||||
|
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||||
|
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||||
|
if (entityInfo.Columns.IsValuable())
|
||||||
|
{
|
||||||
|
foreach (var item in entityInfo.Columns)
|
||||||
|
{
|
||||||
|
DbColumnInfo dbColumnInfo = base.EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||||
|
columns.Add(dbColumnInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.Context.DbMaintenance.CreateTable(tableName, columns);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ using System.Text;
|
|||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class MySqlDbMaintenance:DbMaintenanceProvider
|
public class MySqlDbMaintenance : DbMaintenanceProvider
|
||||||
{
|
{
|
||||||
#region DML
|
#region DML
|
||||||
protected override string GetColumnInfosByTableNameSql
|
protected override string GetColumnInfosByTableNameSql
|
||||||
@ -81,7 +81,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "CREATE TABLE {0}(\r\n{1})";
|
return "CREATE TABLE {0}(\r\n{1}, PRIMARY KEY (`{2}`))";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override string CreateTableColumn
|
protected override string CreateTableColumn
|
||||||
@ -150,7 +150,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "NULL";
|
return "DEFAULT NULL";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override string CreateTableNotNull
|
protected override string CreateTableNotNull
|
||||||
@ -171,9 +171,44 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "IDENTITY(1,1)";
|
return "AUTO_INCREMENT";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
public override bool CreateTable(string tableName, List<DbColumnInfo> columns)
|
||||||
|
{
|
||||||
|
if (columns.IsValuable())
|
||||||
|
{
|
||||||
|
foreach (var item in columns)
|
||||||
|
{
|
||||||
|
if (item.DbColumnName.Equals("GUID",StringComparison.CurrentCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
item.Length = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return base.CreateTable(tableName,columns);
|
||||||
|
}
|
||||||
|
protected override string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
|
||||||
|
{
|
||||||
|
List<string> columnArray = new List<string>();
|
||||||
|
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
|
||||||
|
foreach (var item in columns)
|
||||||
|
{
|
||||||
|
string columnName = item.DbColumnName;
|
||||||
|
string dataType = item.DataType;
|
||||||
|
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
||||||
|
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
||||||
|
string primaryKey = null;
|
||||||
|
string identity = item.IsIdentity ? this.CreateTableIdentity : null;
|
||||||
|
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
|
||||||
|
columnArray.Add(addItem);
|
||||||
|
}
|
||||||
|
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray), columns.First(it=>it.IsPrimarykey).DbColumnName);
|
||||||
|
return tableString;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user