This commit is contained in:
sunkaixuan
2017-05-01 23:00:05 +08:00
parent f3128e88e0
commit f86f2127c5
12 changed files with 69 additions and 24 deletions

View File

@@ -73,6 +73,7 @@
<Compile Include="Models\Student.cs" /> <Compile Include="Models\Student.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnitTest\Setting\Attribute.cs" />
<Compile Include="UnitTest\Setting\AutoClose.cs" /> <Compile Include="UnitTest\Setting\AutoClose.cs" />
<Compile Include="UnitTest\Setting\MapColumn.cs" /> <Compile Include="UnitTest\Setting\MapColumn.cs" />
<Compile Include="UnitTest\Setting\MapTable.cs" /> <Compile Include="UnitTest\Setting\MapTable.cs" />

View File

@@ -21,8 +21,8 @@ namespace OrmTest.UnitTest
var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now }; var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now };
var insertObjs = new List<Student>() { insertObj }.ToArray(); var insertObjs = new List<Student>() { insertObj }.ToArray();
db.IgnoreColumns.Add("TestId", "Student"); db.IgnoreColumns.Add("TestId", "Student");
db.MappingColumns.Add("id","dbid", "Student"); //db.MappingColumns.Add("id","dbid", "Student");
db.MappingTables.Add("student","dbstudent");
var s1= db.Insertable<Student>(insertObj).ToSql(); var s1= db.Insertable<Student>(insertObj).ToSql();
//Insert reutrn Command Count //Insert reutrn Command Count

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.UnitTest
{
public class Attribute : ExpTestBase
{
public Attribute(int eachCount)
{
this.Count = eachCount;
}
public void Init()
{
}
}
}

View File

@@ -121,10 +121,16 @@ namespace SqlSugar
} }
} }
public virtual string GetSinglePrimaryFiled(string tableName) public virtual List<string> GetIsIdentities(string tableName)
{ {
var pkColumnInfo = GetColumnInfosByTableName(tableName).FirstOrDefault(it => it.IsPrimarykey); var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
return pkColumnInfo == null ? null : pkColumnInfo.ColumnName; return result.Select(it=>it.ColumnName).ToList();
}
public virtual List<string> GetPrimaries(string tableName)
{
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
return result.Select(it => it.ColumnName).ToList();
} }
protected abstract string AddColumnToTableSql { get; } protected abstract string AddColumnToTableSql { get; }

View File

@@ -28,7 +28,7 @@ namespace SqlSugar
foreach (var item in result.Type.GetProperties()) foreach (var item in result.Type.GetProperties())
{ {
EntityColumnInfo column = new EntityColumnInfo(); EntityColumnInfo column = new EntityColumnInfo();
column.EntityName = item.Name; column.Name = item.Name;
column.PropertyInfo = item; column.PropertyInfo = item;
result.Columns.Add(column); result.Columns.Add(column);
} }

View File

@@ -15,7 +15,7 @@ namespace SqlSugar
public ISqlBuilder Builder { get; set; } public ISqlBuilder Builder { get; set; }
public StringBuilder sql { get; set; } public StringBuilder sql { get; set; }
public List<SugarParameter> Parameters { get; set; } public List<SugarParameter> Parameters { get; set; }
public string EntityName { get; set; } public string TableName { get; set; }
public string TableWithString { get; set; } public string TableWithString { get; set; }
public List<DbColumnInfo> DbColumnInfoList { get; set; } public List<DbColumnInfo> DbColumnInfoList { get; set; }
public bool IsInsertNull { get; set; } public bool IsInsertNull { get; set; }
@@ -39,7 +39,7 @@ namespace SqlSugar
{ {
get get
{ {
var result = Builder.GetTranslationTableName(EntityName); var result = Builder.GetTranslationTableName(TableName);
result += PubConst.Space; result += PubConst.Space;
if (this.TableWithString.IsValuable()) if (this.TableWithString.IsValuable())
{ {

View File

@@ -69,13 +69,28 @@ namespace SqlSugar
#region Private Methods #region Private Methods
private void PreToSql() private void PreToSql()
{ {
if (this.Context.IgnoreColumns != null && this.Context.IgnoreColumns.Any()) { #region Identities
List<string> identities = Db.DbMaintenance.GetIsIdentities(this.InsertBuilder.TableName);
if (identities != null && identities.Any())
{
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.Name).ToList(); var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.Name).ToList();
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it =>
{ {
return !currentIgnoreColumns.Any(i => it.EntityPropertyName == i.EntityPropertyName); return !identities.Any(i => it.ColumnName.Equals(i, StringComparison.CurrentCultureIgnoreCase));
}).ToList(); }).ToList();
} }
#endregion
#region IgnoreColumns
if (this.Context.IgnoreColumns != null && this.Context.IgnoreColumns.Any())
{
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.Name).ToList();
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it =>
{
return !currentIgnoreColumns.Any(i => it.EntityPropertyName.Equals(i.EntityPropertyName,StringComparison.CurrentCulture));
}).ToList();
}
#endregion
if (this.IsSingle) if (this.IsSingle)
{ {
foreach (var item in this.InsertBuilder.DbColumnInfoList) foreach (var item in this.InsertBuilder.DbColumnInfoList)
@@ -87,13 +102,13 @@ namespace SqlSugar
} }
public void Init() public void Init()
{ {
this.InsertBuilder.EntityName = EntityInfo.Name; this.InsertBuilder.TableName = EntityInfo.Name;
if (IsMappingTable) if (IsMappingTable)
{ {
var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.Name); var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.Name);
if (mappingInfo != null) if (mappingInfo != null)
{ {
this.InsertBuilder.EntityName = mappingInfo.DbTableName; this.InsertBuilder.TableName = mappingInfo.DbTableName;
} }
} }
Check.Exception(InsertObjs == null || InsertObjs.Count() == 0, "InsertObjs is null"); Check.Exception(InsertObjs == null || InsertObjs.Count() == 0, "InsertObjs is null");
@@ -106,8 +121,8 @@ namespace SqlSugar
var columnInfo = new DbColumnInfo() var columnInfo = new DbColumnInfo()
{ {
Value = column.PropertyInfo.GetValue(item), Value = column.PropertyInfo.GetValue(item),
ColumnName = GetDbColumnName(column.EntityName), ColumnName = GetDbColumnName(column.Name),
EntityPropertyName = column.EntityName, EntityPropertyName = column.Name,
TableId = i TableId = i
}; };
insertItem.Add(columnInfo); insertItem.Add(columnInfo);
@@ -121,9 +136,9 @@ namespace SqlSugar
{ {
return entityName; return entityName;
} }
if (this.Context.MappingColumns.Any(it => it.EntityName == EntityInfo.Name)) if (this.Context.MappingColumns.Any(it => it.EntityName.Equals(EntityInfo.Name,StringComparison.CurrentCultureIgnoreCase)))
{ {
this.MappingColumnList = this.Context.MappingColumns.Where(it => it.EntityName == EntityInfo.Name).ToList(); this.MappingColumnList = this.Context.MappingColumns.Where(it => it.EntityName.Equals(EntityInfo.Name,StringComparison.CurrentCultureIgnoreCase)).ToList();
} }
if (MappingColumnList == null || !MappingColumnList.Any()) if (MappingColumnList == null || !MappingColumnList.Any())
{ {
@@ -131,7 +146,7 @@ namespace SqlSugar
} }
else else
{ {
var mappInfo = this.Context.MappingColumns.Single(it => it.EntityPropertyName == entityName); var mappInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityPropertyName.Equals(entityName,StringComparison.CurrentCultureIgnoreCase));
return mappInfo == null ? entityName : mappInfo.DbColumnName; return mappInfo == null ? entityName : mappInfo.DbColumnName;
} }
} }

View File

@@ -194,7 +194,9 @@ namespace SqlSugar
Where("1=2 "); Where("1=2 ");
return this; return this;
} }
string filed = Context.Database.DbMaintenance.GetSinglePrimaryFiled(this.SqlBuilder.GetTranslationTableName(typeof(T).Name)); var pks = Context.Database.DbMaintenance.GetPrimaries(this.SqlBuilder.GetTranslationTableName(typeof(T).Name));
Check.Exception(pks == null || pks.Count != 1, "Queryable.In(params object[] pkValues): Only one primary key");
string filed = pks.FirstOrDefault();
string shortName = QueryBuilder.TableShortName == null ? null : (QueryBuilder.TableShortName + "."); string shortName = QueryBuilder.TableShortName == null ? null : (QueryBuilder.TableShortName + ".");
filed = shortName + filed; filed = shortName + filed;
return In(filed, pkValues); return In(filed, pkValues);

View File

@@ -10,7 +10,7 @@ namespace SqlSugar
public class EntityColumnInfo public class EntityColumnInfo
{ {
public PropertyInfo PropertyInfo { get; set; } public PropertyInfo PropertyInfo { get; set; }
public string EntityName { get; set; } public string Name { get; set; }
public int Length { get; set; } public int Length { get; set; }
public string ColumnDescription { get; set; } public string ColumnDescription { get; set; }
public string DefaultValue { get; set; } public string DefaultValue { get; set; }

View File

@@ -9,7 +9,6 @@ namespace SqlSugar
{ {
public string EntityPropertyName { get; set; } public string EntityPropertyName { get; set; }
public string DbColumnName { get; set; } public string DbColumnName { get; set; }
public string DbTableName { get; set; }
public string EntityName { get; set; } public string EntityName { get; set; }
} }
} }

View File

@@ -39,10 +39,10 @@ namespace SqlSugar
public class MappingColumnList : List<MappingColumn> public class MappingColumnList : List<MappingColumn>
{ {
public void Add(string EntityPropertyName, string dbColumnName, string dbTableName) public void Add(string EntityPropertyName, string dbColumnName, string entityName)
{ {
this.RemoveAll(it => it.EntityPropertyName.Equals(EntityPropertyName, StringComparison.CurrentCultureIgnoreCase)); this.RemoveAll(it => it.EntityPropertyName.Equals(EntityPropertyName, StringComparison.CurrentCultureIgnoreCase));
this.Add(new MappingColumn() { EntityPropertyName = EntityPropertyName, DbColumnName = dbColumnName, DbTableName = dbTableName }); this.Add(new MappingColumn() { EntityPropertyName = EntityPropertyName, DbColumnName = dbColumnName, EntityName = entityName });
} }
public new void Clear() public new void Clear()
{ {

View File

@@ -14,7 +14,9 @@ namespace SqlSugar
List<DbColumnInfo> GetColumnInfosByTableName(string tableName); List<DbColumnInfo> GetColumnInfosByTableName(string tableName);
string GetSinglePrimaryFiled(string tableName); List<string> GetIsIdentities(string tableName);
List<string> GetPrimaries(string tableName);
bool TruncateTable(string tableName); bool TruncateTable(string tableName);