mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
1
This commit is contained in:
@@ -14,10 +14,16 @@ namespace SqlSugar
|
||||
public ISqlBuilder SqlBuilder { get; internal set; }
|
||||
public UpdateBuilder UpdateBuilder { get; internal set; }
|
||||
public IAdo Ado { get { return Context.Ado; } }
|
||||
public object[] UpdateObjs { get; internal set; }
|
||||
|
||||
public T[] UpdateObjs { get; set; }
|
||||
public bool IsMappingTable { get { return this.Context.MappingTables != null && this.Context.MappingTables.Any(); } }
|
||||
public bool IsMappingColumns { get { return this.Context.MappingColumns != null && this.Context.MappingColumns.Any(); } }
|
||||
public bool IsSingle { get { return this.UpdateObjs.Length == 1; } }
|
||||
public List<MappingColumn> MappingColumnList { get; set; }
|
||||
private List<string> IgnoreColumnNameList { get; set; }
|
||||
private bool IsOffIdentity { get; set; }
|
||||
public int ExecuteCommand()
|
||||
{
|
||||
PreToSql();
|
||||
return this.Ado.ExecuteCommand(UpdateBuilder.ToSqlString(), UpdateBuilder.Parameters.ToArray());
|
||||
}
|
||||
|
||||
@@ -50,6 +56,7 @@ namespace SqlSugar
|
||||
|
||||
public KeyValuePair<string, List<SugarParameter>> ToSql()
|
||||
{
|
||||
PreToSql();
|
||||
string sql = UpdateBuilder.ToSqlString();
|
||||
return new KeyValuePair<string, List<SugarParameter>>(sql, UpdateBuilder.Parameters);
|
||||
}
|
||||
@@ -89,7 +96,89 @@ namespace SqlSugar
|
||||
|
||||
internal void Init()
|
||||
{
|
||||
this.UpdateBuilder.TableName = EntityInfo.EntityName;
|
||||
if (IsMappingTable)
|
||||
{
|
||||
var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.EntityName);
|
||||
if (mappingInfo != null)
|
||||
{
|
||||
this.UpdateBuilder.TableName = mappingInfo.DbTableName;
|
||||
}
|
||||
}
|
||||
Check.Exception(UpdateObjs == null || UpdateObjs.Count() == 0, "UpdateObjs is null");
|
||||
int i = 0;
|
||||
foreach (var item in UpdateObjs)
|
||||
{
|
||||
List<DbColumnInfo> insertItem = new List<DbColumnInfo>();
|
||||
foreach (var column in EntityInfo.Columns)
|
||||
{
|
||||
var columnInfo = new DbColumnInfo()
|
||||
{
|
||||
Value = column.PropertyInfo.GetValue(item),
|
||||
ColumnName = GetDbColumnName(column.PropertyName),
|
||||
EntityPropertyName = column.PropertyName,
|
||||
TableId = i
|
||||
};
|
||||
insertItem.Add(columnInfo);
|
||||
}
|
||||
this.UpdateBuilder.DbColumnInfoList.AddRange(insertItem);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
private void PreToSql()
|
||||
{
|
||||
#region Identities
|
||||
if (!IsOffIdentity)
|
||||
{
|
||||
List<string> identities = GetIdentityKeys();
|
||||
if (identities != null && identities.Any())
|
||||
{
|
||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it =>
|
||||
{
|
||||
return !identities.Any(i => it.ColumnName.Equals(i, StringComparison.CurrentCultureIgnoreCase));
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IgnoreColumns
|
||||
if (this.Context.IgnoreColumns != null && this.Context.IgnoreColumns.Any())
|
||||
{
|
||||
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.EntityName).ToList();
|
||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it =>
|
||||
{
|
||||
return !currentIgnoreColumns.Any(i => it.EntityPropertyName.Equals(i.EntityPropertyName, StringComparison.CurrentCulture));
|
||||
}).ToList();
|
||||
}
|
||||
#endregion
|
||||
if (this.IsSingle)
|
||||
{
|
||||
foreach (var item in this.UpdateBuilder.DbColumnInfoList)
|
||||
{
|
||||
if (this.UpdateBuilder.Parameters == null) this.UpdateBuilder.Parameters = new List<SugarParameter>();
|
||||
this.UpdateBuilder.Parameters.Add(new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.ColumnName, item.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
private string GetDbColumnName(string entityName)
|
||||
{
|
||||
if (!IsMappingColumns)
|
||||
{
|
||||
return entityName;
|
||||
}
|
||||
if (this.Context.MappingColumns.Any(it => it.EntityName.Equals(EntityInfo.EntityName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
this.MappingColumnList = this.Context.MappingColumns.Where(it => it.EntityName.Equals(EntityInfo.EntityName, StringComparison.CurrentCultureIgnoreCase)).ToList();
|
||||
}
|
||||
if (MappingColumnList == null || !MappingColumnList.Any())
|
||||
{
|
||||
return entityName;
|
||||
}
|
||||
else
|
||||
{
|
||||
var mappInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityPropertyName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase));
|
||||
return mappInfo == null ? entityName : mappInfo.DbColumnName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user