Support snowflake id

This commit is contained in:
sunkaixuna 2021-07-31 20:55:11 +08:00
parent e2fd7bb04f
commit 77d5c19cb6
3 changed files with 63 additions and 4 deletions

View File

@ -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()
{

View File

@ -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();

View File

@ -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();