mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-31 15:56:25 +08:00
Add db. Insertable(list).ExecuteReturnPkList()
This commit is contained in:
parent
8f3f827d00
commit
f3bb1c17f3
@ -70,6 +70,68 @@ namespace SqlSugar
|
||||
RestoreMapping();
|
||||
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需要主键");
|
||||
var isIdEntity = pkInfo.IsIdentity|| (pkInfo.OracleSequenceName.HasValue()&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle);
|
||||
if (pkInfo.UnderType == UtilConstants.LongType)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
else if (isIdEntity&&this.InsertObjs.Length==1)
|
||||
{
|
||||
if (pkInfo.UnderType == UtilConstants.IntType)
|
||||
{
|
||||
return new List<Type> { (Type)(object)this.ExecuteReturnIdentity() };
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<Type> { (Type)(object)this.ExecuteReturnBigIdentity() };
|
||||
}
|
||||
}
|
||||
else if (isIdEntity && this.InsertBuilder.ConvertInsertReturnIdFunc == null)
|
||||
{
|
||||
Check.ExceptionEasy("The current database does not support batch auto increment", "当前数据库不支持批量返回自增");
|
||||
return null;
|
||||
}
|
||||
else if (isIdEntity && this.InsertBuilder.ConvertInsertReturnIdFunc != null)
|
||||
{
|
||||
string sql = _ExecuteCommand();
|
||||
sql= this.InsertBuilder.ConvertInsertReturnIdFunc(pkInfo.DbColumnName,sql);
|
||||
var result = Ado.SqlQuery<Type>(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
|
||||
After(sql, null);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
public virtual int ExecuteReturnIdentity()
|
||||
{
|
||||
if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null)
|
||||
|
@ -120,6 +120,9 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Func<string,string,string> ConvertInsertReturnIdFunc { get; set; }
|
||||
|
||||
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
|
||||
{
|
||||
ILambdaExpressions resolveExpress = this.LambdaExpressions;
|
||||
|
@ -12,6 +12,7 @@ namespace SqlSugar
|
||||
InsertBuilder InsertBuilder { get; set; }
|
||||
int ExecuteCommand();
|
||||
Task<int> ExecuteCommandAsync();
|
||||
List<Type> ExecuteReturnPkList<Type>();
|
||||
long ExecuteReturnSnowflakeId();
|
||||
List<long> ExecuteReturnSnowflakeIdList();
|
||||
Task<long> ExecuteReturnSnowflakeIdAsync();
|
||||
|
@ -32,6 +32,10 @@ namespace SqlSugar
|
||||
|
||||
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()
|
||||
{
|
||||
if (IsNoInsertNull)
|
||||
|
Loading…
Reference in New Issue
Block a user