Browse Source

CodeFirst adds database index and default value

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
7ef0bce3d4
  1. 1
      Src/Asp.Net/MySqlTest/MySqlTest.csproj
  2. 1
      Src/Asp.Net/MySqlTest/UnitTest/Main.cs
  3. 26
      Src/Asp.Net/MySqlTest/UnitTest/UCodeFirst.cs
  4. 1
      Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
  5. 1
      Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
  6. 25
      Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs
  7. 2
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  8. 65
      Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs
  9. 3
      Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs
  10. 1
      Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs
  11. 1
      Src/Asp.Net/SqlSugar/Entities/EntityColumnInfo.cs
  12. 7
      Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs
  13. 8
      Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs
  14. 39
      Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs
  15. 85
      Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs
  16. 21
      Src/Asp.Net/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs
  17. 117
      Src/Asp.Net/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs
  18. 72
      Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs

1
Src/Asp.Net/MySqlTest/MySqlTest.csproj

@ -116,6 +116,7 @@
<Compile Include="OldTests\UnitTest\Update.cs" />
<Compile Include="UnitTest\Main.cs" />
<Compile Include="UnitTest\UAdo.cs" />
<Compile Include="UnitTest\UCodeFirst.cs" />
<Compile Include="UnitTest\UJson.cs" />
<Compile Include="UnitTest\Updateable.cs" />
<Compile Include="UnitTest\UQueryable.cs" />

1
Src/Asp.Net/MySqlTest/UnitTest/Main.cs

@ -25,6 +25,7 @@ namespace OrmTest
});
public static void Init()
{
CodeFirst();
Updateable();
Json();
Ado();

26
Src/Asp.Net/MySqlTest/UnitTest/UCodeFirst.cs

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void CodeFirst()
{
if (Db.DbMaintenance.IsAnyTable("UnitCodeTest1", false))
Db.DbMaintenance.DropTable("UnitCodeTest1");
Db.CodeFirst.InitTables<UnitCodeTest1>();
Db.CodeFirst.InitTables<UnitCodeTest1>();
}
public class UnitCodeTest1
{
[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })]
public int Id { get; set; }
[SqlSugar.SugarColumn(DefaultValue = "now()", IndexGroupNameList = new string[] { "group1" })]
public DateTime? CreateDate { get; set; }
}
}
}

1
Src/Asp.Net/SqlServerTest/SqlServerTest.csproj

@ -147,6 +147,7 @@
<Compile Include="OldTest\UnitTest\Setting\MapTable.cs" />
<Compile Include="OldTest\UnitTest\Update.cs" />
<Compile Include="Models\CarType.cs" />
<Compile Include="UnitTest\UCodeFirst.cs" />
<Compile Include="UnitTest\UAdo.cs" />
<Compile Include="UnitTest\Updateable.cs" />
<Compile Include="UnitTest\UQueryable.cs" />

1
Src/Asp.Net/SqlServerTest/UnitTest/Main.cs

@ -25,6 +25,7 @@ namespace OrmTest
});
public static void Init()
{
CodeFirst();
Updateable();
Json();
Ado();

25
Src/Asp.Net/SqlServerTest/UnitTest/UCodeFirst.cs

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void CodeFirst()
{
if (Db.DbMaintenance.IsAnyTable("UnitCodeTest1", false))
Db.DbMaintenance.DropTable("UnitCodeTest1");
Db.CodeFirst.InitTables<UnitCodeTest1>();
}
public class UnitCodeTest1
{
[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })]
public int Id { get; set; }
[SqlSugar.SugarColumn(DefaultValue="getdate()", IndexGroupNameList =new string[] {"group1" } )]
public DateTime? CreateDate { get; set; }
}
}
}

2
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -116,6 +116,8 @@ namespace SqlSugar
NoExistLogic(entityInfo);
this.Context.DbMaintenance.AddRemark(entityInfo);
this.Context.DbMaintenance.AddIndex(entityInfo);
this.Context.DbMaintenance.AddDefaultValue(entityInfo);
}
public virtual void NoExistLogic(EntityInfo entityInfo)
{

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

@ -292,6 +292,23 @@ namespace SqlSugar
var dt=this.Context.Ado.GetDataTable(sql);
return dt.Rows != null && dt.Rows.Count > 0;
}
public virtual bool AddDefaultValue(string tableName, string columnName, string defaultValue)
{
string sql = string.Format(AddDefaultValueSql, tableName, columnName,defaultValue);
this.Context.Ado.ExecuteCommand(sql);
return true;
}
public virtual bool CreateIndex(string tableName, string[] columnNames)
{
string sql = string.Format(CreateIndexSql,tableName,string.Join(",",columnNames), string.Join("_", columnNames));
this.Context.Ado.ExecuteCommand(sql);
return true;
}
public virtual bool IsAnyIndex(string indexName)
{
string sql = string.Format(this.IsAnyIndexSql, indexName);
return this.Context.Ado.GetInt(sql)>0;
}
public virtual bool AddRemark(EntityInfo entity)
{
var db = this.Context;
@ -330,6 +347,54 @@ namespace SqlSugar
return true;
}
public virtual void AddIndex(EntityInfo entityInfo)
{
var db = this.Context;
var columns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
var indexColumns = columns.Where(it => it.IndexGroupNameList.HasValue()).ToList();
if (indexColumns.HasValue())
{
var groups = indexColumns.SelectMany(it => it.IndexGroupNameList).GroupBy(it => it).Select(it=>it.Key).ToList();
foreach (var item in groups)
{
var columnNames = indexColumns.Where(it => it.IndexGroupNameList.Any(i => i.Equals(item, StringComparison.CurrentCultureIgnoreCase))).Select(it=>it.DbColumnName).ToArray();
var indexName = string.Format("Index_{0}_{1}",entityInfo.DbTableName, string.Join("_", columnNames));
if (!IsAnyIndex(indexName))
{
CreateIndex(entityInfo.DbTableName, columnNames);
}
}
}
}
protected virtual bool IsAnyDefaultValue(string tableName, string columnName,List<DbColumnInfo> columns)
{
var defaultValue = columns.Where(it => it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase)).First().DefaultValue;
return defaultValue.HasValue();
}
public virtual bool IsAnyDefaultValue(string tableName, string columnName)
{
return IsAnyDefaultValue(tableName, columnName, this.GetColumnInfosByTableName(tableName, false));
}
public virtual void AddDefaultValue(EntityInfo entityInfo)
{
var dbColumns=this.GetColumnInfosByTableName(entityInfo.DbTableName, false);
var db = this.Context;
var columns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
foreach (var item in columns)
{
if (item.DefaultValue.HasValue())
{
if (!IsAnyDefaultValue(entityInfo.DbTableName,item.DbColumnName,dbColumns))
{
this.AddDefaultValue(entityInfo.DbTableName, item.DbColumnName, item.DefaultValue);
}
}
}
}
public virtual bool RenameTable(string oldTableName, string newTableName)
{
string sql = string.Format(this.RenameTableSql, oldTableName,newTableName);

3
Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs

@ -32,6 +32,9 @@ namespace SqlSugar
#endregion
#region DDL
protected abstract string CreateIndexSql { get; }
protected abstract string IsAnyIndexSql { get; }
protected abstract string AddDefaultValueSql { get; }
protected abstract string CreateDataBaseSql { get; }
protected abstract string AddColumnToTableSql { get; }
protected abstract string AlterColumnToTableSql { get; }

1
Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs

@ -172,6 +172,7 @@ namespace SqlSugar
column.IsJson = sugarColumn.IsJson;
column.NoSerialize = sugarColumn.NoSerialize;
column.DefaultValue = sugarColumn.DefaultValue;
column.IndexGroupNameList = sugarColumn.IndexGroupNameList;
}
else
{

1
Src/Asp.Net/SqlSugar/Entities/EntityColumnInfo.cs

@ -31,5 +31,6 @@ namespace SqlSugar
public string SerializeDateTimeFormat { get; set; }
public bool IsJson { get; set; }
public bool NoSerialize { get; set; }
public string[] IndexGroupNameList { get; set; }
}
}

7
Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs

@ -157,6 +157,13 @@ namespace SqlSugar
set { _DefaultValue = value; }
}
private string[] _IndexGroupNameList;
public string[] IndexGroupNameList
{
get { return _IndexGroupNameList; }
set { _IndexGroupNameList = value; }
}
}
}

8
Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs

@ -9,6 +9,7 @@ namespace SqlSugar
SqlSugarProvider Context { get; set; }
#region DML
List<string> GetDataBaseList(SqlSugarClient db);
List<DbTableInfo> GetViewInfoList(bool isCache=true);
List<DbTableInfo> GetTableInfoList(bool isCache=true);
List<DbColumnInfo> GetColumnInfosByTableName(string tableName,bool isCache=true);
@ -26,6 +27,8 @@ namespace SqlSugar
#endregion
#region DDL
bool AddDefaultValue(string tableName,string columnName,string defaultValue);
bool CreateIndex(string tableName, string [] columnNames);
bool DropTable(string tableName);
bool TruncateTable(string tableName);
bool TruncateTable<T>();
@ -40,6 +43,10 @@ namespace SqlSugar
bool DropColumn(string tableName,string columnName);
bool RenameColumn(string tableName, string oldColumnName, string newColumnName);
bool AddRemark(EntityInfo entity);
void AddIndex(EntityInfo entityInfo);
void AddDefaultValue(EntityInfo entityInfo);
bool IsAnyDefaultValue(string tableName, string columnName);
bool IsAnyIndex(string indexName);
bool AddColumnRemark(string columnName,string tableName,string description);
bool DeleteColumnRemark(string columnName, string tableName);
bool IsAnyColumnRemark(string columnName, string tableName);
@ -60,7 +67,6 @@ namespace SqlSugar
/// <param name="databaseDirectory"></param>
/// <returns></returns>
bool CreateDatabase(string databaseName,string databaseDirectory = null);
List<string> GetDataBaseList(SqlSugarClient db);
#endregion
}
}

39
Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs

@ -245,6 +245,29 @@ namespace SqlSugar
return "alter table {0} rename {1}";
}
}
protected override string CreateIndexSql
{
get
{
return "CREATE INDEX Index_{0}_{2} ON {0} ({1})";
}
}
protected override string AddDefaultValueSql
{
get
{
return "ALTER TABLE {0} ALTER COLUMN {1} SET DEFAULT '{2}'";
}
}
protected override string IsAnyIndexSql
{
get
{
return "SELECT count(*) FROM information_schema.statistics WHERE index_name = '{0}'";
}
}
#endregion
#region Methods
@ -397,7 +420,21 @@ namespace SqlSugar
return false;
}
}
public override bool AddDefaultValue(string tableName, string columnName, string defaultValue)
{
if (defaultValue.ToLower().IsIn("now()", "current_timestamp"))
{
string template = "ALTER table {0} CHANGE COLUMN {1} {1} {3} default {2}";
var dbColumnInfo=this.Context.DbMaintenance.GetColumnInfosByTableName(tableName).First(it => it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
string sql = string.Format(template, tableName, columnName, defaultValue, dbColumnInfo.DataType);
this.Context.Ado.ExecuteCommand(sql);
return true;
}
else
{
return base.AddDefaultValue(tableName,columnName,defaultValue);
}
}
public override bool IsAnyConstraint(string constraintName)
{
throw new NotSupportedException("MySql IsAnyConstraint NotSupportedException");

85
Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs

@ -51,6 +51,27 @@ namespace SqlSugar
#endregion
#region DDL
protected override string IsAnyIndexSql
{
get
{
return "";
}
}
protected override string CreateIndexSql
{
get
{
return "";
}
}
protected override string AddDefaultValueSql
{
get
{
return "";
}
}
protected override string CreateDataBaseSql
{
get
@ -142,100 +163,100 @@ namespace SqlSugar
return "ALTER TABLE {0} rename column {1} to {2}";
}
}
#endregion
#region Check
protected override string CheckSystemTablePermissionsSql
protected override string AddColumnRemarkSql
{
get
{
return "select t.table_name from user_tables t where rownum=1";
return "comment on column {1}.{0} is '{2}'";
}
}
#endregion
#region Scattered
protected override string CreateTableNull
protected override string DeleteColumnRemarkSql
{
get
{
return "";
return "comment on column {1}.{0} is ''";
}
}
protected override string CreateTableNotNull
protected override string IsAnyColumnRemarkSql
{
get
{
return "";
return "select * from user_col_comments where Table_Name='{1}' AND COLUMN_NAME='{0}' order by column_name";
}
}
protected override string CreateTablePirmaryKey
protected override string AddTableRemarkSql
{
get
{
return "PRIMARY KEY";
return "comment on table {0} is '{1}'";
}
}
protected override string CreateTableIdentity
protected override string DeleteTableRemarkSql
{
get
{
return "";
return "comment on table {0} is ''";
}
}
protected override string AddColumnRemarkSql
protected override string IsAnyTableRemarkSql
{
get
{
return "comment on column {1}.{0} is '{2}'";
return "select * from user_tab_comments where Table_Name='{0}'order by Table_Name";
}
}
protected override string DeleteColumnRemarkSql
protected override string RenameTableSql
{
get
{
return "comment on column {1}.{0} is ''";
return "alter table {0} rename to {1}";
}
}
#endregion
protected override string IsAnyColumnRemarkSql
#region Check
protected override string CheckSystemTablePermissionsSql
{
get
{
return "select * from user_col_comments where Table_Name='{1}' AND COLUMN_NAME='{0}' order by column_name";
return "select t.table_name from user_tables t where rownum=1";
}
}
#endregion
protected override string AddTableRemarkSql
#region Scattered
protected override string CreateTableNull
{
get
{
return "comment on table {0} is '{1}'";
return "";
}
}
protected override string DeleteTableRemarkSql
protected override string CreateTableNotNull
{
get
{
return "comment on table {0} is ''";
return "";
}
}
protected override string IsAnyTableRemarkSql
protected override string CreateTablePirmaryKey
{
get
{
return "select * from user_tab_comments where Table_Name='{0}'order by Table_Name";
return "PRIMARY KEY";
}
}
protected override string RenameTableSql {
protected override string CreateTableIdentity
{
get
{
return "alter table {0} rename to {1}";
return "";
}
}
#endregion

21
Src/Asp.Net/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs

@ -216,6 +216,27 @@ namespace SqlSugar
protected override string RenameTableSql => "alter table 表名 {0} to {1}";
protected override string CreateIndexSql
{
get
{
return "";
}
}
protected override string AddDefaultValueSql
{
get
{
return "";
}
}
protected override string IsAnyIndexSql
{
get
{
return "select count(*) from sys.indexes where name='{0}'";
}
}
#endregion
#region Methods

117
Src/Asp.Net/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs

@ -173,48 +173,6 @@ namespace SqlSugar
return "exec sp_rename '{0}.{1}','{2}','column';";
}
}
#endregion
#region Check
protected override string CheckSystemTablePermissionsSql
{
get
{
return "select top 1 id from sysobjects";
}
}
#endregion
#region Scattered
protected override string CreateTableNull
{
get
{
return "NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "IDENTITY(1,1)";
}
}
protected override string AddColumnRemarkSql
{
get
@ -287,7 +245,73 @@ namespace SqlSugar
}
}
protected override string CreateIndexSql
{
get
{
return "CREATE NONCLUSTERED INDEX Index_{0}_{2} ON {0}({1})";
}
}
protected override string AddDefaultValueSql
{
get
{
return "alter table {0} ADD DEFAULT '{2}' FOR {1}";
}
}
protected override string IsAnyIndexSql
{
get
{
return "select count(*) from sys.indexes where name='{0}'";
}
}
#endregion
#region Check
protected override string CheckSystemTablePermissionsSql
{
get
{
return "select top 1 id from sysobjects";
}
}
#endregion
#region Scattered
protected override string CreateTableNull
{
get
{
return "NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "IDENTITY(1,1)";
}
}
#endregion
#region Methods
/// <summary>
///by current connection string
/// </summary>
@ -305,7 +329,8 @@ namespace SqlSugar
var oldDatabaseName = this.Context.Ado.Connection.Database;
var connection = this.Context.CurrentConnectionConfig.ConnectionString;
connection = connection.Replace(oldDatabaseName, "master");
var newDb = new SqlSugarClient(new ConnectionConfig() {
var newDb = new SqlSugarClient(new ConnectionConfig()
{
DbType = this.Context.CurrentConnectionConfig.DbType,
IsAutoCloseConnection = true,
ConnectionString = connection
@ -353,7 +378,7 @@ namespace SqlSugar
var pkColumns = columns.Where(it => it.IsPrimarykey).ToList();
if (pkColumns.Count > 1)
{
this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it=>it.DbColumnName).ToArray());
this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it => it.DbColumnName).ToArray());
}
else
{
@ -365,7 +390,6 @@ namespace SqlSugar
}
return true;
}
public override bool RenameColumn(string tableName, string oldColumnName, string newColumnName)
{
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
@ -374,6 +398,7 @@ namespace SqlSugar
string sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName);
this.Context.Ado.ExecuteCommand(sql);
return true;
}
}
#endregion
}
}

72
Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs

@ -135,49 +135,48 @@ namespace SqlSugar
throw new NotSupportedException();
}
}
#endregion
#region Check
protected override string CheckSystemTablePermissionsSql
protected override string AddColumnRemarkSql
{
get
{
return "select Name from sqlite_master limit 0,1";
throw new NotSupportedException();
}
}
#endregion
#region Scattered
protected override string CreateTableNull
protected override string DeleteColumnRemarkSql
{
get
{
return "NULL";
throw new NotSupportedException();
}
}
protected override string CreateTableNotNull
protected override string IsAnyColumnRemarkSql
{
get
{
return "NOT NULL";
throw new NotSupportedException();
}
}
protected override string CreateTablePirmaryKey
protected override string AddTableRemarkSql
{
get
{
return "PRIMARY KEY";
throw new NotSupportedException();
}
}
protected override string CreateTableIdentity
protected override string DeleteTableRemarkSql
{
get
{
return "AUTOINCREMENT";
throw new NotSupportedException();
}
}
protected override string AddColumnRemarkSql
protected override string IsAnyTableRemarkSql
{
get
{
@ -185,7 +184,7 @@ namespace SqlSugar
}
}
protected override string DeleteColumnRemarkSql
protected override string RenameTableSql
{
get
{
@ -193,43 +192,66 @@ namespace SqlSugar
}
}
protected override string IsAnyColumnRemarkSql
protected override string CreateIndexSql
{
get
{
throw new NotSupportedException();
}
}
protected override string AddTableRemarkSql
protected override string AddDefaultValueSql
{
get
{
throw new NotSupportedException();
}
}
protected override string DeleteTableRemarkSql
protected override string IsAnyIndexSql
{
get
{
throw new NotSupportedException();
}
}
#endregion
protected override string IsAnyTableRemarkSql
#region Check
protected override string CheckSystemTablePermissionsSql
{
get
{
throw new NotSupportedException();
return "select Name from sqlite_master limit 0,1";
}
}
#endregion
protected override string RenameTableSql
#region Scattered
protected override string CreateTableNull
{
get
{
throw new NotSupportedException();
return "NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "AUTOINCREMENT";
}
}
#endregion

Loading…
Cancel
Save