mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 23:13:42 +08:00
-
This commit is contained in:
parent
ec6bd36b41
commit
8ef15c0697
@ -18,14 +18,14 @@ namespace OrmTest
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//Unit Test
|
||||
new Field(1).Init();
|
||||
new Where(1).Init();
|
||||
new Method(1).Init();
|
||||
new JoinQuery(1).Init();
|
||||
new SingleQuery(1).Init();
|
||||
new SelectQuery(1).Init();
|
||||
new AutoClose(1).Init();
|
||||
|
||||
//new Field(1).Init();
|
||||
//new Where(1).Init();
|
||||
//new Method(1).Init();
|
||||
//new JoinQuery(1).Init();
|
||||
//new SingleQuery(1).Init();
|
||||
//new SelectQuery(1).Init();
|
||||
//new AutoClose(1).Init();
|
||||
new Insert(1).Init();
|
||||
//Performance Test
|
||||
//new SqlSugarPerformance(100).Select();
|
||||
}
|
||||
|
@ -21,8 +21,7 @@ namespace OrmTest.UnitTest
|
||||
var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now };
|
||||
var insertObjs = new List<Student>() { insertObj }.ToArray();
|
||||
|
||||
//Insert reutrn identity
|
||||
db.Insertable<Student>(insertObj).ExecuteReutrnIdentity();
|
||||
var s1= db.Insertable<Student>(insertObj).ToSql();
|
||||
|
||||
//Insert reutrn Command Count
|
||||
db.Insertable<Student>(insertObj).ExecuteCommand();
|
||||
|
@ -11,7 +11,7 @@ namespace SqlSugar
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public EntityInfo GetEntityInfo<T>()where T:class,new()
|
||||
{
|
||||
string cacheKey = "GetEntityInfo";
|
||||
string cacheKey = "GetEntityInfo"+typeof(T).FullName;
|
||||
return CacheFactory.Func<EntityInfo>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
@ -27,9 +27,10 @@ namespace SqlSugar
|
||||
result.Columns = new List<EntityColumnInfo>();
|
||||
foreach (var item in result.Type.GetProperties())
|
||||
{
|
||||
EntityColumnInfo columns = new EntityColumnInfo();
|
||||
columns.ColumnName = item.Name;
|
||||
columns.PropertyInfo = item;
|
||||
EntityColumnInfo column = new EntityColumnInfo();
|
||||
column.EntityName = item.Name;
|
||||
column.PropertyInfo = item;
|
||||
result.Columns.Add(column);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
@ -8,6 +8,7 @@ namespace SqlSugar
|
||||
{
|
||||
public InsertBuilder() {
|
||||
this.sql = new StringBuilder();
|
||||
this.DbColumnInfoList = new List<DbColumnInfo>();
|
||||
}
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public ILambdaExpressions LambdaExpressions { get; set; }
|
||||
@ -16,7 +17,8 @@ namespace SqlSugar
|
||||
public List<SugarParameter> Parameters { get; set; }
|
||||
public string EntityName { get; set; }
|
||||
public string TableWithString { get; set; }
|
||||
public List<string> ColumNames{ get; set; }
|
||||
public List<DbColumnInfo> DbColumnInfoList { get; set; }
|
||||
public bool IsInsertNull { get; set; }
|
||||
|
||||
public virtual string SqlTemplate
|
||||
{
|
||||
@ -49,9 +51,9 @@ namespace SqlSugar
|
||||
|
||||
public virtual string ToSqlString()
|
||||
{
|
||||
string columnsString =string.Join("," ,this.ColumNames.Select(it => Builder.GetTranslationColumnName(it)));
|
||||
string columnParametersString = string.Join(",", this.ColumNames.Select(it =>Builder.SqlParameterKeyWord+it));
|
||||
return string.Format(this.sql.ToString(),columnsString, columnParametersString);
|
||||
string columnsString =string.Join("," ,this.DbColumnInfoList.Select(it => Builder.GetTranslationColumnName(it.ColumnName)));
|
||||
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>Builder.SqlParameterKeyWord+it.ColumnName));
|
||||
return string.Format(SqlTemplate,GetTableNameString,columnsString, columnParametersString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,28 +9,36 @@ namespace SqlSugar
|
||||
{
|
||||
public class InsertableProvider<T> : IInsertable<T> where T : class, new()
|
||||
{
|
||||
|
||||
public SqlSugarClient Context { 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 IDb Db { get { return Context.Database; } }
|
||||
public ISqlBuilder SqlBuilder { get; set; }
|
||||
public InsertBuilder InsertBuilder { get { return this.InsertBuilder; } }
|
||||
public InsertBuilder InsertBuilder { 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.InsertObjs.Length == 1; } }
|
||||
|
||||
public EntityInfo EntityInfo { get; set; }
|
||||
private List<string> IgnoreColumnList { get; set; }
|
||||
private List<string> InsertColumList { get; set; }
|
||||
public List<MappingColumn> MappingColumnList { get; set; }
|
||||
private List<string> IgnoreColumnNameList { get; set; }
|
||||
public T[] InsertObjs { get; set; }
|
||||
|
||||
public int ExecuteCommand()
|
||||
{
|
||||
PreToSql();
|
||||
return Db.ExecuteCommand(InsertBuilder.ToSqlString(), InsertBuilder.Parameters);
|
||||
}
|
||||
public KeyValuePair<string, List<SugarParameter>> ToSql()
|
||||
{
|
||||
PreToSql();
|
||||
string sql = InsertBuilder.ToSqlString();
|
||||
return new KeyValuePair<string, List<SugarParameter>>(sql, InsertBuilder.Parameters);
|
||||
}
|
||||
|
||||
public int ExecuteReutrnIdentity()
|
||||
{
|
||||
PreToSql();
|
||||
return Db.GetInt(InsertBuilder.ToSqlString(), InsertBuilder.Parameters);
|
||||
}
|
||||
|
||||
@ -46,12 +54,78 @@ namespace SqlSugar
|
||||
|
||||
public IInsertable<T> With(string lockString)
|
||||
{
|
||||
this.InsertBuilder.TableWithString = lockString;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IInsertable<T> Where(bool isInsertNull)
|
||||
{
|
||||
this.InsertBuilder.IsInsertNull = isInsertNull;
|
||||
return this;
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
private void PreToSql()
|
||||
{
|
||||
if (this.IsSingle)
|
||||
{
|
||||
foreach (var item in this.InsertBuilder.DbColumnInfoList)
|
||||
{
|
||||
if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List<SugarParameter>();
|
||||
this.InsertBuilder.Parameters.Add(new SugarParameter(this.SqlBuilder.SqlParameterKeyWord+item.ColumnName,item.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Init()
|
||||
{
|
||||
this.InsertBuilder.EntityName = EntityInfo.Name;
|
||||
if (IsMappingTable)
|
||||
{
|
||||
var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.Name);
|
||||
if (mappingInfo != null)
|
||||
{
|
||||
this.InsertBuilder.EntityName = mappingInfo.DbTableName;
|
||||
}
|
||||
}
|
||||
Check.Exception(InsertObjs == null || InsertObjs.Count() == 0, "InsertObjs is null");
|
||||
int i = 0;
|
||||
foreach (var item in InsertObjs)
|
||||
{
|
||||
List<DbColumnInfo> insertItem = new List<DbColumnInfo>();
|
||||
foreach (var column in EntityInfo.Columns)
|
||||
{
|
||||
var columnInfo = new DbColumnInfo()
|
||||
{
|
||||
Value = column.PropertyInfo.GetValue(item),
|
||||
ColumnName = GetDbColumnName(column.EntityName),
|
||||
EntityPropertyName = column.EntityName,
|
||||
TableId=i
|
||||
};
|
||||
insertItem.Add(columnInfo);
|
||||
}
|
||||
this.InsertBuilder.DbColumnInfoList.AddRange(insertItem);
|
||||
}
|
||||
}
|
||||
private string GetDbColumnName(string entityName)
|
||||
{
|
||||
if (!IsMappingColumns)
|
||||
{
|
||||
return entityName;
|
||||
}
|
||||
if (this.Context.MappingColumns.Any(it => it.EntityName == EntityInfo.Name))
|
||||
{
|
||||
this.MappingColumnList = this.Context.MappingColumns.Where(it => it.EntityName == EntityInfo.Name).ToList();
|
||||
}
|
||||
if (MappingColumnList == null || !MappingColumnList.Any())
|
||||
{
|
||||
return entityName;
|
||||
}
|
||||
else
|
||||
{
|
||||
var mappInfo = this.Context.MappingColumns.Single(it => it.EntityPropertyName == entityName);
|
||||
return mappInfo == null ? entityName : mappInfo.DbColumnName;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ namespace SqlSugar
|
||||
public string TableName { get; set; }
|
||||
public int TableId { get; set; }
|
||||
public string ColumnName { get; set; }
|
||||
public string EntityPropertyName { get; set; }
|
||||
public string DataType { get; set; }
|
||||
public int Length { get; set; }
|
||||
public string ColumnDescription { get; set; }
|
||||
@ -16,5 +17,6 @@ namespace SqlSugar
|
||||
public bool IsNullable { get; set; }
|
||||
public bool IsIdentity { get; set; }
|
||||
public bool IsPrimarykey { get; set; }
|
||||
public object Value { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ namespace SqlSugar
|
||||
{
|
||||
public PropertyInfo PropertyInfo { get; set; }
|
||||
public string EntityName { get; set; }
|
||||
public string ColumnName { get; set; }
|
||||
public int Length { get; set; }
|
||||
public string ColumnDescription { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
|
@ -236,11 +236,11 @@ namespace SqlSugar
|
||||
var sqlBuilder = InstanceFactory.GetSqlbuilder(base.CurrentConnectionConfig); ;
|
||||
reval.SqlBuilder = sqlBuilder;
|
||||
reval.InsertObjs = insertObjs;
|
||||
reval.SqlBuilder.InsertBuilder = InstanceFactory.GetInsertBuilder(base.CurrentConnectionConfig);
|
||||
reval.SqlBuilder.InsertBuilder.Builder = sqlBuilder;
|
||||
reval.SqlBuilder.Context = reval.SqlBuilder.QueryBuilder.Context = this;
|
||||
reval.SqlBuilder.InsertBuilder.EntityName = typeof(T).Name;
|
||||
reval.SqlBuilder.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(base.CurrentConnectionConfig);
|
||||
sqlBuilder.InsertBuilder =reval.InsertBuilder = InstanceFactory.GetInsertBuilder(base.CurrentConnectionConfig);
|
||||
sqlBuilder.InsertBuilder.Builder = sqlBuilder;
|
||||
sqlBuilder.Context = reval.SqlBuilder.InsertBuilder.Context = this;
|
||||
sqlBuilder.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(base.CurrentConnectionConfig);
|
||||
reval.Init();
|
||||
return reval;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user