Browse Source

Update .net core project

SqlSugar5
sunkaixuan 2 years ago
parent
commit
fafdaaccf4
  1. 100
      Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs
  2. 29
      Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  3. 4
      Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs
  4. 1
      Src/Asp.NetCore2/SqlSugar/Interface/Insertable.cs
  5. 4
      Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLInsertBuilder.cs
  6. 9
      Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerInsertBuilder.cs

100
Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs

@ -512,5 +512,105 @@ namespace SqlSugar
} }
#endregion #endregion
#region Insert PkList
private List<Type> InsertPkListWithFunc<Type>(EntityColumnInfo pkInfo)
{
InsertBuilder.IsReturnPkList = true;
InsertBuilder.IsNoPage = true;
string sql = _ExecuteCommand();
sql = this.InsertBuilder.ConvertInsertReturnIdFunc(SqlBuilder.GetTranslationColumnName(pkInfo.DbColumnName), sql);
var result = Ado.SqlQuery<Type>(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
After(sql, null);
return result;
}
private List<Type> InsertPkListNoFunc<Type>(EntityColumnInfo pkInfo)
{
if (this.Ado.Transaction != null)
{
return ReturnDefaultIdentity<Type>(pkInfo);
}
else
{
try
{
this.Context.Ado.BeginTran();
var result = ReturnDefaultIdentity<Type>(pkInfo);
this.Context.Ado.CommitTran();
return result;
}
catch (Exception ex)
{
this.Context.Ado.RollbackTran();
throw ex;
}
}
}
private List<Type> InsertPkListIdentityCount1<Type>(EntityColumnInfo pkInfo)
{
if (pkInfo.UnderType == UtilConstants.IntType)
{
return new List<Type> { (Type)(object)this.ExecuteReturnIdentity() };
}
else
{
return new List<Type> { (Type)(object)this.ExecuteReturnBigIdentity() };
}
}
private List<Type> InsertPkListLong<Type>()
{
var list = this.ExecuteReturnSnowflakeIdList();
try
{
return list.Cast<Type>().ToList();
}
catch
{
Check.ExceptionEasy($"long to ExecuteReturnPkList<{typeof(Type).Name}> error ", $" long 转换成ExecuteReturnPkList<{typeof(Type).Name}>失败");
return null;
}
}
private List<Type> InsertPkListGuid<Type>(EntityColumnInfo pkInfo)
{
Check.ExceptionEasy(pkInfo.UnderType.Name != typeof(Type).Name, $"{pkInfo.UnderType.Name} to ExecuteReturnPkList<{typeof(Type).Name}> error ", $" {pkInfo.UnderType.Name} 转换成ExecuteReturnPkList<{typeof(Type).Name}>失败");
this.ExecuteCommand();
List<Type> result = new List<Type>();
if (InsertBuilder.DbColumnInfoList.HasValue())
{
foreach (var item in InsertBuilder.DbColumnInfoList)
{
var isPk = item.DbColumnName.EqualCase(pkInfo.DbColumnName);
if (isPk)
{
result.Add((Type)item.Value);
}
}
}
return result;
}
private List<Type> ReturnDefaultIdentity<Type>(EntityColumnInfo pkInfo)
{
List<Type> result = new List<Type>();
foreach (var item in this.InsertObjs)
{
var insertable = this.Context.Insertable(item)
.InsertColumns(this.InsertBuilder.DbColumnInfoList.Select(it => it.DbColumnName).Distinct().ToArray());
if (pkInfo.UnderType == UtilConstants.IntType)
{
result.Add((Type)(object)insertable.ExecuteReturnIdentity());
}
else
{
result.Add((Type)(object)insertable.ExecuteReturnBigIdentity());
}
}
return result;
}
#endregion
} }
} }

29
Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -70,6 +70,35 @@ namespace SqlSugar
RestoreMapping(); RestoreMapping();
return new KeyValuePair<string, List<SugarParameter>>(sql, InsertBuilder.Parameters); return new KeyValuePair<string, List<SugarParameter>>(sql, InsertBuilder.Parameters);
} }
public virtual List<Type> ExecuteReturnPkList<Type>()
{
var pkInfo= this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
Check.ExceptionEasy(pkInfo==null,"ExecuteReturnPkList need primary key", "ExecuteReturnPkList需要主键");
Check.ExceptionEasy(this.EntityInfo.Columns.Count(it => it.IsPrimarykey == true)>1, "ExecuteReturnPkList ,Only support technology single primary key", "ExecuteReturnPkList只支技单主键");
var isIdEntity = pkInfo.IsIdentity|| (pkInfo.OracleSequenceName.HasValue()&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle);
if (pkInfo.UnderType == UtilConstants.LongType)
{
return InsertPkListLong<Type>();
}
else if (isIdEntity&&this.InsertObjs.Length==1)
{
return InsertPkListIdentityCount1<Type>(pkInfo);
}
else if (isIdEntity && this.InsertBuilder.ConvertInsertReturnIdFunc == null)
{
return InsertPkListNoFunc<Type>(pkInfo);
}
else if (isIdEntity && this.InsertBuilder.ConvertInsertReturnIdFunc != null)
{
return InsertPkListWithFunc<Type>(pkInfo);
}
else
{
return InsertPkListGuid<Type>(pkInfo);
}
}
public virtual int ExecuteReturnIdentity() public virtual int ExecuteReturnIdentity()
{ {
if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null) if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null)

4
Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs

@ -31,6 +31,10 @@ namespace SqlSugar
public Dictionary<string, int> OracleSeqInfoList { get; set; } public Dictionary<string, int> OracleSeqInfoList { get; set; }
public bool IsBlukCopy { get; set; } public bool IsBlukCopy { get; set; }
public virtual bool IsOleDb { get; set; } public virtual bool IsOleDb { get; set; }
public virtual Func<string, string, string> ConvertInsertReturnIdFunc { get; set; }
public virtual bool IsNoPage { get; set; }
public virtual bool IsReturnPkList { get; set; }
#endregion #endregion
#region SqlTemplate #region SqlTemplate

1
Src/Asp.NetCore2/SqlSugar/Interface/Insertable.cs

@ -12,6 +12,7 @@ namespace SqlSugar
InsertBuilder InsertBuilder { get; set; } InsertBuilder InsertBuilder { get; set; }
int ExecuteCommand(); int ExecuteCommand();
Task<int> ExecuteCommandAsync(); Task<int> ExecuteCommandAsync();
List<Type> ExecuteReturnPkList<Type>();
long ExecuteReturnSnowflakeId(); long ExecuteReturnSnowflakeId();
List<long> ExecuteReturnSnowflakeIdList(); List<long> ExecuteReturnSnowflakeIdList();
Task<long> ExecuteReturnSnowflakeIdAsync(); Task<long> ExecuteReturnSnowflakeIdAsync();

4
Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLInsertBuilder.cs

@ -32,6 +32,10 @@ namespace SqlSugar
public override string SqlTemplateBatchSelect => " {0} "; public override string SqlTemplateBatchSelect => " {0} ";
public override Func<string, string, string> ConvertInsertReturnIdFunc { get; set; } = (name, sql) =>
{
return sql.Trim().TrimEnd(';')+ $"returning {name} ";
};
public override string ToSqlString() public override string ToSqlString()
{ {
if (IsNoInsertNull) if (IsNoInsertNull)

9
Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerInsertBuilder.cs

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SqlServerInsertBuilder:InsertBuilder public class SqlServerInsertBuilder:InsertBuilder
{ {
public override Func<string, string, string> ConvertInsertReturnIdFunc { get; set; } = (name, sql) =>
{
return sql.Replace("select SCOPE_IDENTITY();", "").Replace(")\r\n SELECT", $")\r\n OUTPUT INSERTED.{name} as {name} \r\nSELECT");
};
public override bool IsNoPage { get; set; } = true;
public override string ToSqlString() public override string ToSqlString()
{ {
if (IsNoInsertNull) if (IsNoInsertNull)
@ -34,6 +39,10 @@ namespace SqlSugar
{ {
pageSize = 100; pageSize = 100;
} }
if (IsNoPage && IsReturnPkList)
{
pageSize = groupList.Count;
}
int pageIndex = 1; int pageIndex = 1;
int totalRecord = groupList.Count; int totalRecord = groupList.Count;
int pageCount = (totalRecord + pageSize - 1) / pageSize; int pageCount = (totalRecord + pageSize - 1) / pageSize;

Loading…
Cancel
Save