Browse Source

Support snowflake id

pull/4/MERGE
sunkaixuna 3 years ago
parent
commit
77d5c19cb6
  1. 57
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  2. 6
      Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs
  3. 4
      Src/Asp.Net/SqlSugar/Interface/Insertable.cs

57
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -85,7 +85,62 @@ namespace SqlSugar
return result;
}
public virtual long ExecuteReturnSnowflakeId()
{
var id = SnowFlakeSingle.instance.getID();
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var snowProperty=entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType);
Check.Exception(snowProperty==null, "The entity sets the primary key and is long");
foreach (var item in this.InsertObjs)
{
snowProperty.PropertyInfo.SetValue(item,id);
}
this.ExecuteCommand();
return id;
}
public List<long> ExecuteReturnSnowflakeIdList()
{
List<long> result = new List<long>();
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType);
Check.Exception(snowProperty == null, "The entity sets the primary key and is long");
foreach (var item in InsertObjs)
{
var id = SnowFlakeSingle.instance.getID();;
snowProperty.PropertyInfo.SetValue(item, id);
result.Add(id);
}
this.ExecuteCommand();
return result;
}
public async Task<long> ExecuteReturnSnowflakeIdAsync()
{
var id = SnowFlakeSingle.instance.getID();
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType);
Check.Exception(snowProperty == null, "The entity sets the primary key and is long");
foreach (var item in this.InsertObjs)
{
snowProperty.PropertyInfo.SetValue(item, id);
}
await this.ExecuteCommandAsync();
return id;
}
public async Task<List<long>> ExecuteReturnSnowflakeIdListAsync()
{
List<long> result = new List<long>();
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType);
Check.Exception(snowProperty == null, "The entity sets the primary key and is long");
foreach (var item in InsertObjs)
{
var id = SnowFlakeSingle.instance.getID(); ;
snowProperty.PropertyInfo.SetValue(item, id);
result.Add(id);
}
await this.ExecuteCommandAsync();
return result;
}
public virtual T ExecuteReturnEntity()
{

6
Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs

@ -4,21 +4,21 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar.DistributedSystem.Snowflake
namespace SqlSugar
{
public sealed class SnowFlakeSingle
{
public static readonly SnowFlakeSingle instance = new SnowFlakeSingle();
private SnowFlakeSingle()
{
worker = new Snowflake.IdWorker(1, 1);
worker = new DistributedSystem.Snowflake.IdWorker(1, 1);
}
static SnowFlakeSingle() { }
public static SnowFlakeSingle Instance
{
get { return instance; }
}
private Snowflake.IdWorker worker;
private DistributedSystem.Snowflake.IdWorker worker;
public long getID()
{
return worker.NextId();

4
Src/Asp.Net/SqlSugar/Interface/Insertable.cs

@ -12,6 +12,10 @@ namespace SqlSugar
InsertBuilder InsertBuilder { get; set; }
int ExecuteCommand();
Task<int> ExecuteCommandAsync();
long ExecuteReturnSnowflakeId();
List<long> ExecuteReturnSnowflakeIdList();
Task<long> ExecuteReturnSnowflakeIdAsync();
Task<List<long>> ExecuteReturnSnowflakeIdListAsync();
int ExecuteReturnIdentity();
Task<int> ExecuteReturnIdentityAsync();
T ExecuteReturnEntity();

Loading…
Cancel
Save