From 3b7b9c2de1e195ce1736a0982f183a6b3e042964 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 24 Jun 2017 02:15:35 +0800 Subject: [PATCH] Update MySql --- Src/Asp.Net/MySqlTest/Config.cs | 2 +- .../CodeFirstProvider/CodeFirstProvider.cs | 22 +++++----- .../Abstract/DbMaintenanceProvider/Methods.cs | 10 ++--- .../DbMaintenanceProvider/Properties.cs | 13 ++++++ .../MySql/CodeFirst/MySqlCodeFirst.cs | 15 +++++++ .../MySql/DbMaintenance/MySqlDbMaintenance.cs | 43 +++++++++++++++++-- 6 files changed, 84 insertions(+), 21 deletions(-) diff --git a/Src/Asp.Net/MySqlTest/Config.cs b/Src/Asp.Net/MySqlTest/Config.cs index c5da2fdad..d017f8af4 100644 --- a/Src/Asp.Net/MySqlTest/Config.cs +++ b/Src/Asp.Net/MySqlTest/Config.cs @@ -8,6 +8,6 @@ namespace OrmTest { 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"; } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 786c64e6f..4b3157250 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -14,13 +14,13 @@ namespace SqlSugar #endregion #region Public methods - public ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue) + public virtual ICodeFirst BackupTable(int maxBackupDataRows = int.MaxValue) { this.IsBackupTable = true; this.MaxBackupDataRows = maxBackupDataRows; return this; } - public void InitTables(Type entityType) + public virtual void InitTables(Type entityType) { if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) { @@ -33,7 +33,7 @@ namespace SqlSugar }); Check.Exception(!executeResult.IsSuccess, executeResult.Messaage); } - public void InitTables(Type[] entityTypes) + public virtual void InitTables(Type[] entityTypes) { 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(); InitTables(types); } - public void InitTables(params string[] entitiesNamespaces) + public virtual void InitTables(params string[] entitiesNamespaces) { if (entitiesNamespaces.IsValuable()) { @@ -61,7 +61,7 @@ namespace SqlSugar #endregion #region Core Logic - private void Execute(Type entityType) + protected virtual void Execute(Type entityType) { var entityInfo = this.Context.EntityProvider.GetEntityInfo(entityType); var tableName = GetTableName(entityInfo); @@ -71,7 +71,7 @@ namespace SqlSugar else NoExistLogic(entityInfo); } - private void NoExistLogic(EntityInfo entityInfo) + public virtual void NoExistLogic(EntityInfo 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"); @@ -91,7 +91,7 @@ namespace SqlSugar this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); } } - private void ExistLogic(EntityInfo entityInfo) + public virtual void ExistLogic(EntityInfo entityInfo) { if (entityInfo.Columns.IsValuable()) { @@ -192,11 +192,11 @@ namespace SqlSugar var tableName = GetTableName(entityInfo); return result.ToString(); } - private static string GetTableName(EntityInfo entityInfo) + protected string GetTableName(EntityInfo entityInfo) { 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() { @@ -214,7 +214,7 @@ namespace SqlSugar 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 dataType = dc.DataType; diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 541616ed5..c4ca8db5d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -201,7 +201,7 @@ namespace SqlSugar return reval; }); } - private string GetCreateTableSql(string tableName, List columns) + protected virtual string GetCreateTableSql(string tableName, List columns) { List columnArray = new List(); Check.Exception(columns.IsNullOrEmpty(), "No columns found "); @@ -213,13 +213,13 @@ namespace SqlSugar string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull; string primaryKey = 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); } - 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; } - private string GetAddColumnSql(string tableName, DbColumnInfo columnInfo) + protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo) { string columnName = columnInfo.DbColumnName; string dataType = columnInfo.DataType; @@ -230,7 +230,7 @@ namespace SqlSugar string result = string.Format(this.AddColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity); return result; } - private string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo) + protected virtual string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo) { string columnName = columnInfo.DbColumnName; string dataType = columnInfo.DataType; diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs index d7c2acfda..da9ac12b7 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs @@ -8,7 +8,20 @@ namespace SqlSugar public abstract partial class DbMaintenanceProvider : IDbMaintenance { #region Context + private ISqlBuilder _SqlBuilder; 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 #region DML diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs index 9a9e0c930..b2d50ab3c 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs @@ -7,5 +7,20 @@ namespace SqlSugar { 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 columns = new List(); + 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); + } } } diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs index 7ee52841b..bea148263 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs @@ -5,7 +5,7 @@ using System.Text; namespace SqlSugar { - public class MySqlDbMaintenance:DbMaintenanceProvider + public class MySqlDbMaintenance : DbMaintenanceProvider { #region DML protected override string GetColumnInfosByTableNameSql @@ -81,7 +81,7 @@ namespace SqlSugar { get { - return "CREATE TABLE {0}(\r\n{1})"; + return "CREATE TABLE {0}(\r\n{1}, PRIMARY KEY (`{2}`))"; } } protected override string CreateTableColumn @@ -150,7 +150,7 @@ namespace SqlSugar { get { - return "NULL"; + return "DEFAULT NULL"; } } protected override string CreateTableNotNull @@ -171,9 +171,44 @@ namespace SqlSugar { get { - return "IDENTITY(1,1)"; + return "AUTO_INCREMENT"; } } #endregion + + #region Methods + public override bool CreateTable(string tableName, List 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 columns) + { + List columnArray = new List(); + 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 } }