Browse Source

Update MySql

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
3b7b9c2de1
  1. 2
      Src/Asp.Net/MySqlTest/Config.cs
  2. 22
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  3. 10
      Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs
  4. 13
      Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs
  5. 15
      Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs
  6. 43
      Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs

2
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";
}
}

22
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;

10
Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs

@ -201,7 +201,7 @@ namespace SqlSugar
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>();
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;

13
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

15
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<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);
}
}
}

43
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<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…
Cancel
Save