OceanBase批量新增对序列的支持

This commit is contained in:
guoshun.du 2024-05-09 15:32:16 +08:00
parent 0ef7118013
commit 30283abe8c
5 changed files with 495 additions and 121 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlSugar;
namespace SqlSugar.OceanBaseForOracle
{
public class OceanBaseForOracleDeleteable<T> : DeleteableProvider<T> where T : class, new()
{
protected override List<string> GetIdentityKeys()
{
return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.DbColumnName).ToList();
}
}
}

View File

@ -0,0 +1,223 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar.OceanBaseForOracle
{
public class OceanBaseForOracleInsertable<T> : InsertableProvider<T> where T : class, new()
{
protected override List<string> GetIdentityKeys()
{
return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.DbColumnName).ToList();
}
protected string GetSeqName()
{
return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.OracleSequenceName).First();
}
protected List<string> GetSeqNames()
{
return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.OracleSequenceName).ToList();
}
public override int ExecuteReturnIdentity()
{
bool oldIsAuto = AutoBegin();
InsertBuilder.IsReturnIdentity = true;
PreToSql();
string sql = InsertBuilder.ToSqlString();
if (isIdEntityEnable())
{
if (sql?.StartsWith("INSERT ALL")==true)
{
return this.UseParameter().ExecuteCommand();
}
else
{
sql = sql + " RETURNING ID INTO :newId01 ";
}
InsertBuilder.Parameters.Add(new SugarParameter(":newId01", 0,true));
}
RestoreMapping();
var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
this.Context.Ado.IsDisableMasterSlaveSeparation = true;
var count = Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
var result = (this.GetIdentityKeys().IsNullOrEmpty() || count == 0) ? 0 : GetSeqValue(GetSeqName()).ObjToInt();
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
After(sql,result);
AutoEnd(oldIsAuto);
if (isIdEntityEnable())
{
return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToInt()??0;
}
return result;
}
private bool isIdEntityEnable()
{
return this.Context.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity == true;
}
public override long ExecuteReturnBigIdentity()
{
bool oldIsAuto = AutoBegin();
InsertBuilder.IsReturnIdentity = true;
PreToSql();
string sql = InsertBuilder.ToSqlString();
if (isIdEntityEnable())
{
if (sql?.StartsWith("INSERT ALL") == true)
{
return this.UseParameter().ExecuteCommand();
}
else
{
sql = sql + " RETURNING ID INTO :newId01 ";
}
InsertBuilder.Parameters.Add(new SugarParameter(":newId01", Convert.ToInt64(0), true));
}
RestoreMapping();
var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
this.Context.Ado.IsDisableMasterSlaveSeparation = true;
var count = Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
var result = (this.GetIdentityKeys().IsNullOrEmpty() || count == 0) ? 0 : Convert.ToInt64(GetSeqValue(GetSeqName()));
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
After(sql, result);
AutoEnd(oldIsAuto);
if (isIdEntityEnable())
{
return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToLong() ?? 0;
}
return result;
}
public async override Task<int> ExecuteReturnIdentityAsync()
{
bool oldIsAuto = AutoBegin();
InsertBuilder.IsReturnIdentity = true;
PreToSql();
string sql = InsertBuilder.ToSqlString();
if (isIdEntityEnable())
{
if (sql?.StartsWith("INSERT ALL") == true)
{
return await this.UseParameter().ExecuteCommandAsync();
}
else
{
sql = sql + " RETURNING ID INTO :newId01 ";
}
InsertBuilder.Parameters.Add(new SugarParameter(":newId01", 0, true));
}
RestoreMapping();
var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
this.Context.Ado.IsDisableMasterSlaveSeparation = true;
var count = await Ado.ExecuteCommandAsync(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
var result = (this.GetIdentityKeys().IsNullOrEmpty() || count == 0) ? 0 : GetSeqValue(GetSeqName()).ObjToInt();
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
After(sql, result);
AutoEnd(oldIsAuto);
if (isIdEntityEnable())
{
return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToInt() ?? 0;
}
return result;
}
public async override Task<long> ExecuteReturnBigIdentityAsync()
{
bool oldIsAuto = AutoBegin();
InsertBuilder.IsReturnIdentity = true;
PreToSql();
string sql = InsertBuilder.ToSqlString();
if (isIdEntityEnable())
{
if (sql?.StartsWith("INSERT ALL") == true)
{
return await this.UseParameter().ExecuteCommandAsync();
}
else
{
sql = sql + " RETURNING ID INTO :newId01 ";
}
InsertBuilder.Parameters.Add(new SugarParameter(":newId01", Convert.ToInt64(0), true));
}
RestoreMapping();
var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
this.Context.Ado.IsDisableMasterSlaveSeparation = true;
var count = await Ado.ExecuteCommandAsync(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
var result = (this.GetIdentityKeys().IsNullOrEmpty() || count == 0) ? 0 : Convert.ToInt64(GetSeqValue(GetSeqName()));
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
After(sql, result);
AutoEnd(oldIsAuto);
if (isIdEntityEnable())
{
return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToLong() ?? 0;
}
return result;
}
private void AutoEnd(bool oldIsAuto)
{
if (oldIsAuto)
{
this.Context.Context.CurrentConnectionConfig.IsAutoCloseConnection = oldIsAuto;
if (this.Ado.Transaction == null)
this.Context.Ado.Close();
}
}
private bool AutoBegin()
{
var oldIsAuto = this.Context.Context.CurrentConnectionConfig.IsAutoCloseConnection;
if (this.Context.Context.CurrentConnectionConfig.IsAutoCloseConnection)
{
this.Context.Context.CurrentConnectionConfig.IsAutoCloseConnection = false;
}
return oldIsAuto;
}
private object GetSeqValue(string seqName)
{
return Ado.GetScalar(" SELECT " + seqName + ".currval FROM DUAL");
}
protected override void PreToSql()
{
var identities = GetSeqNames();
var insertCount = InsertObjs.Count();
InsertBuilder.OracleSeqInfoList = new Dictionary<string, int>();
if ((identities.HasValue() && insertCount > 1)|| InsertBuilder.IsBlukCopy)
{
Check.Exception(identities.Count != identities.Distinct().Count(), "The field sequence needs to be unique");
foreach (var seqName in identities)
{
int seqBeginValue = 0;
seqBeginValue = this.Ado.GetInt("select " + seqName + ".Nextval from dual");
//Console.WriteLine(seqBeginValue);
var nextLength = insertCount - 1;
if (nextLength > 0)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(" select " + seqName + ".nextval,t.* from (");
for (int i = 0; i < nextLength; i++)
{
sb.AppendLine(" select 1 from dual");
if (i < (nextLength - 1))
{
sb.AppendLine("union all");
}
}
sb.AppendLine(" )t");
this.Ado.SqlQuery<int>(sb.ToString());
}
InsertBuilder.OracleSeqInfoList.Add(seqName, seqBeginValue);
}
}
base.PreToSql();
}
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar.OceanBaseForOracle
{
public class OceanBaseForOracleUpdateable<T> : UpdateableProvider<T> where T : class, new()
{
protected override List<string> GetIdentityKeys()
{
return this.EntityInfo.Columns.Where(it => it.OracleSequenceName.HasValue()).Select(it => it.DbColumnName).ToList();
}
public override int ExecuteCommand()
{
if (base.UpdateObjs.Count() == 1)
{
var resultl= base.ExecuteCommand();
if (resultl == -1)
{
return 1;
}
else
{
return resultl;
}
}
else if (base.UpdateObjs.Count() == 0)
{
return 0;
}
else
{
base.ExecuteCommand();
return base.UpdateObjs.Count();
}
}
public async override Task<int> ExecuteCommandAsync()
{
if (base.UpdateObjs.Count() == 1)
{
var result= await base.ExecuteCommandAsync();
if (result == -1)
{
return 1;
}
else
{
return result;
}
}
else if (base.UpdateObjs.Count() == 0)
{
return 0;
}
else
{
await base.ExecuteCommandAsync();
return base.UpdateObjs.Count();
}
}
}
}

View File

@ -9,17 +9,6 @@ namespace SqlSugar.OceanBaseForOracle
/// </summary>
public static class UtilExtensions
{
public static bool EqualCase(this string thisValue, string equalValue)
{
if (thisValue != null && equalValue != null)
{
return thisValue.ToLower() == equalValue.ToLower();
}
else
{
return thisValue == equalValue;
}
}
public static string ToLower(this string value, bool isLower)
{
if (isLower)
@ -28,116 +17,6 @@ namespace SqlSugar.OceanBaseForOracle
}
return value.ObjToString();
}
public static int ObjToInt(this object thisValue)
{
int reval = 0;
if (thisValue == null) return 0;
if (thisValue is Enum)
{
return (int)thisValue;
}
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static int ObjToInt(this object thisValue, int errorValue)
{
int reval = 0;
if (thisValue is Enum)
{
return (int)thisValue;
}
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static double ObjToMoney(this object thisValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static double ObjToMoney(this object thisValue, double errorValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
public static string ObjToString(this object thisValue, string errorValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return errorValue;
}
public static Decimal ObjToDecimal(this object thisValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static DateTime ObjToDate(this object thisValue)
{
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
reval = Convert.ToDateTime(thisValue);
}
return reval;
}
public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
{
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static bool ObjToBool(this object thisValue)
{
bool reval = false;
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static string ToUpper(this string value, bool isAutoToUpper)
{
if (value == null) return null;

View File

@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar.OceanBaseForOracle
{
internal static class UtilConvert
{
public static int ObjToInt(this object thisValue)
{
int reval = 0;
if (thisValue == null) return 0;
if (thisValue is Enum)
{
return Convert.ToInt32(thisValue);
}
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static long ObjToLong(this object thisValue)
{
long reval = 0;
if (thisValue == null) return 0;
if (thisValue is Enum)
{
return Convert.ToInt64(thisValue);
}
if (thisValue != null && thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static int ObjToInt(this object thisValue, int errorValue)
{
int reval = 0;
if (thisValue is Enum)
{
return (int)thisValue;
}
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static double ObjToMoney(this object thisValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static double ObjToMoney(this object thisValue, double errorValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static bool EqualCase(this string thisValue,string equalValue)
{
if ( thisValue!=null && equalValue != null)
{
return thisValue.ToLower() == equalValue.ToLower();
}
else
{
return thisValue == equalValue;
}
}
public static string ObjToString(this object thisValue,Func<DateTime,string> formatTime)
{
if (formatTime != null&&thisValue is DateTime)
{
var dt = Convert.ToDateTime(thisValue);
return formatTime(dt);
}
else
{
return thisValue.ObjToStringNoTrim();
}
}
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
public static string ObjToStringNoTrim(this object thisValue)
{
if (thisValue != null) return thisValue.ToString();
return "";
}
public static string ObjToStringNew(this object thisValue)
{
if (thisValue != null && thisValue is byte[])
{
return string.Join("|",thisValue as byte[]);
}
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
public static string ObjToString(this object thisValue, string errorValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return errorValue;
}
public static Decimal ObjToDecimal(this object thisValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static DateTime ObjToDate(this object thisValue)
{
if (thisValue is DateTime)
{
return (DateTime)thisValue;
}
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
reval = Convert.ToDateTime(thisValue);
}
return reval;
}
public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
{
if (thisValue is DateTime)
{
return (DateTime)thisValue;
}
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static bool ObjToBool(this object thisValue)
{
bool reval = false;
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
internal static MemberExpression ToMemberExpression(Expression parentIdExpression)
{
if (parentIdExpression is UnaryExpression)
{
return (parentIdExpression as UnaryExpression).Operand as MemberExpression;
}
else
{
return parentIdExpression as MemberExpression;
}
}
}
}