diff --git a/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/DbMaintenance/MySqlDbMaintenance.cs b/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/DbMaintenance/MySqlDbMaintenance.cs index cf09d222d..89f368236 100644 --- a/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/DbMaintenance/MySqlDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/DbMaintenance/MySqlDbMaintenance.cs @@ -340,10 +340,22 @@ namespace SqlSugar.MySqlConnector } var oldDatabaseName = this.Context.Ado.Connection.Database; var connection = this.Context.CurrentConnectionConfig.ConnectionString; - Check.ExceptionEasy(Regex.Split(connection,oldDatabaseName).Length > 2 - , "The user name and password cannot be the same as the database name ", - " 创建数据库失败, 请换一个库名,库名不能 password 或者 username 有重叠 "); - connection = connection.Replace(oldDatabaseName, "mysql"); + if (Regex.Split(connection, oldDatabaseName).Length > 2) + { + var name = Regex.Match(connection, @"database\=\w+|datasource\=\w+", RegexOptions.IgnoreCase).Value; + if (!string.IsNullOrEmpty(name)) + { + connection = connection.Replace(name, "database=mysql"); + } + else + { + Check.ExceptionEasy("Failed to create the database. The database name has a keyword. Please change the name", "建库失败,库名存在关键字,请换一个名字"); + } + } + else + { + connection = connection.Replace(oldDatabaseName, "mysql"); + } var newDb = new SqlSugarClient(new ConnectionConfig() { DbType = this.Context.CurrentConnectionConfig.DbType, @@ -428,6 +440,48 @@ namespace SqlSugar.MySqlConnector return tableString; } + + public override bool AddColumn(string tableName, DbColumnInfo columnInfo) + { + tableName = this.SqlBuilder.GetTranslationTableName(tableName); + var isAddNotNUll = columnInfo.IsNullable == false && columnInfo.DefaultValue.HasValue(); + if (isAddNotNUll) + { + columnInfo = this.Context.Utilities.TranslateCopy(columnInfo); + columnInfo.IsNullable = true; + } + string sql = GetAddColumnSql(tableName, columnInfo); + if (sql != null && columnInfo.ColumnDescription.HasValue() &&!sql.ToLower().Contains("comment")) + { + sql = $"{sql}{" COMMENT '"+ columnInfo.ColumnDescription.ToSqlFilter()+ "' "}"; + } + this.Context.Ado.ExecuteCommand(sql); + if (isAddNotNUll) + { + var dtColums = this.Context.Queryable().AS(columnInfo.TableName).Where("1=2") + .Select(this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName)).ToDataTable().Columns.Cast(); + var dtColumInfo = dtColums.First(it => it.ColumnName.EqualCase(columnInfo.DbColumnName)); + var type = UtilMethods.GetUnderType(dtColumInfo.DataType); + var value = type == UtilConstants.StringType ? (object)"" : Activator.CreateInstance(type); + if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle) + { + value = columnInfo.DefaultValue; + if (value.Equals("")) + { + value = "empty"; + } + } + var dt = new Dictionary(); + dt.Add(columnInfo.DbColumnName, value); + this.Context.Updateable(dt) + .AS(tableName) + .Where($"{columnInfo.DbColumnName} is null ").ExecuteCommand(); + columnInfo.IsNullable = false; + UpdateColumn(tableName, columnInfo); + } + return true; + } + protected override string GetSize(DbColumnInfo item) { string dataSize = null; diff --git a/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/SqlBuilder/MySqlInsertBuilder.cs b/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/SqlBuilder/MySqlInsertBuilder.cs index 5fc7b2bee..d680c7e28 100644 --- a/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/SqlBuilder/MySqlInsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar.MySqlConnector/MySql/SqlBuilder/MySqlInsertBuilder.cs @@ -140,7 +140,7 @@ namespace SqlSugar.MySqlConnector foreach (var item in groupList) { batchInsetrSql.Append("("); - insertColumns = string.Join(",", item.Select(it => base.GetDbColumn(it,FormatValue(it.Value,it.PropertyName)))); + insertColumns = string.Join(",", item.Select(it =>base.GetDbColumn(it, FormatValue(it.Value,it.PropertyName)))); batchInsetrSql.Append(insertColumns); if (groupList.Last() == item) {