|
|
@ -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<object>().AS(columnInfo.TableName).Where("1=2") |
|
|
|
.Select(this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName)).ToDataTable().Columns.Cast<System.Data.DataColumn>(); |
|
|
|
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<string, object>(); |
|
|
|
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; |
|
|
|