mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-17 10:41:56 +08:00
Support snowflake id
This commit is contained in:
parent
e2fd7bb04f
commit
77d5c19cb6
@ -85,7 +85,62 @@ namespace SqlSugar
|
|||||||
return result;
|
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()
|
public virtual T ExecuteReturnEntity()
|
||||||
{
|
{
|
||||||
|
@ -4,21 +4,21 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SqlSugar.DistributedSystem.Snowflake
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public sealed class SnowFlakeSingle
|
public sealed class SnowFlakeSingle
|
||||||
{
|
{
|
||||||
public static readonly SnowFlakeSingle instance = new SnowFlakeSingle();
|
public static readonly SnowFlakeSingle instance = new SnowFlakeSingle();
|
||||||
private SnowFlakeSingle()
|
private SnowFlakeSingle()
|
||||||
{
|
{
|
||||||
worker = new Snowflake.IdWorker(1, 1);
|
worker = new DistributedSystem.Snowflake.IdWorker(1, 1);
|
||||||
}
|
}
|
||||||
static SnowFlakeSingle() { }
|
static SnowFlakeSingle() { }
|
||||||
public static SnowFlakeSingle Instance
|
public static SnowFlakeSingle Instance
|
||||||
{
|
{
|
||||||
get { return instance; }
|
get { return instance; }
|
||||||
}
|
}
|
||||||
private Snowflake.IdWorker worker;
|
private DistributedSystem.Snowflake.IdWorker worker;
|
||||||
public long getID()
|
public long getID()
|
||||||
{
|
{
|
||||||
return worker.NextId();
|
return worker.NextId();
|
||||||
|
@ -12,6 +12,10 @@ namespace SqlSugar
|
|||||||
InsertBuilder InsertBuilder { get; set; }
|
InsertBuilder InsertBuilder { get; set; }
|
||||||
int ExecuteCommand();
|
int ExecuteCommand();
|
||||||
Task<int> ExecuteCommandAsync();
|
Task<int> ExecuteCommandAsync();
|
||||||
|
long ExecuteReturnSnowflakeId();
|
||||||
|
List<long> ExecuteReturnSnowflakeIdList();
|
||||||
|
Task<long> ExecuteReturnSnowflakeIdAsync();
|
||||||
|
Task<List<long>> ExecuteReturnSnowflakeIdListAsync();
|
||||||
int ExecuteReturnIdentity();
|
int ExecuteReturnIdentity();
|
||||||
Task<int> ExecuteReturnIdentityAsync();
|
Task<int> ExecuteReturnIdentityAsync();
|
||||||
T ExecuteReturnEntity();
|
T ExecuteReturnEntity();
|
||||||
|
Loading…
Reference in New Issue
Block a user