OceanBaseForOracle和TDSQLForPGODBC,同步修改Oracle和PGSQL的改动

This commit is contained in:
guoshun.du 2025-04-01 14:00:26 +08:00
parent 3e29e86961
commit 4ccc7fe70a
6 changed files with 230 additions and 44 deletions

View File

@ -56,7 +56,7 @@ namespace SqlSugar.OceanBaseForOracle
else
{
var bigSize = 500;
if (groupList.Count < bigSize)
if (groupList.Count < bigSize || this.Context?.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity == true)
{
string result = Small(identities, groupList, columnsString);
return result;
@ -76,7 +76,7 @@ namespace SqlSugar.OceanBaseForOracle
var sql = Small(identities, groupListPasge, columnsString);
this.Context.Ado.ExecuteCommand(sql, this.Parameters);
});
if (identities != null & identities.Count > 0 && this.OracleSeqInfoList != null && this.OracleSeqInfoList.Any())
if (identities != null && identities.Count > 0 && this.OracleSeqInfoList != null && this.OracleSeqInfoList.Any())
{
return $"SELECT {this.OracleSeqInfoList.First().Value - 1} FROM DUAL";
}

View File

@ -22,8 +22,16 @@ namespace SqlSugar.OceanBaseForOracle
sb.AppendLine(string.Join("\r\n", groupList.Select(t =>
{
var updateTable = string.Format("UPDATE {0} SET", base.GetTableNameStringNoWith);
var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Select(m => GetOracleUpdateColums(m)).ToArray());
var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Where(s => OldPrimaryKeys == null || !OldPrimaryKeys.Contains(s.DbColumnName)).Select(m => GetOracleUpdateColums(m)).ToArray());
var pkList = t.Where(s => s.IsPrimarykey).ToList();
if (this.IsWhereColumns && this.PrimaryKeys?.Any() == true)
{
var whereColumns = pkList.Where(it => this.PrimaryKeys?.Any(p => p.EqualCase(it.PropertyName) || p.EqualCase(it.DbColumnName)) == true).ToList();
if (whereColumns.Any())
{
pkList = whereColumns;
}
}
List<string> whereList = new List<string>();
foreach (var item in pkList)
{

View File

@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
@ -93,6 +94,10 @@ namespace SqlSugar.TDSQLForPGODBC
{
return FormatDateTimeOffset(it.Value);
}
else if (it.Value is decimal v)
{
return v.ToString(CultureInfo.InvariantCulture);
}
else if (it.IsArray && it.Value != null)
{
return FormatValue(it.Value, it.PropertyName, i, it);
@ -131,7 +136,7 @@ namespace SqlSugar.TDSQLForPGODBC
var type = value.GetType();
if (type == UtilConstants.ByteArrayType || type == UtilConstants.DateType || columnInfo.IsArray || columnInfo.IsJson)
{
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
var parameterName = this.Builder.SqlParameterKeyWord + name + "_" + i;
var paramter = new SugarParameter(parameterName, value);
if (columnInfo.IsJson)
{
@ -172,12 +177,77 @@ namespace SqlSugar.TDSQLForPGODBC
{
return "'" + value.ToString().ToSqlFilter() + "'";
}
else if (value is decimal v)
{
return v.ToString(CultureInfo.InvariantCulture);
}
else
{
return "'" + value.ToString() + "'";
}
}
}
public override object FormatValue(object value)
{
var N = string.Empty;
if (value == null)
{
return "NULL";
}
else
{
var type = UtilMethods.GetUnderType(value.GetType());
if (type == UtilConstants.DateType)
{
var date = value.ObjToDate();
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
{
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
}
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
else if (type == UtilConstants.ByteArrayType)
{
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
return bytesString;
}
else if (type.IsEnum())
{
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
return value.ToSqlValue();
}
else
{
return Convert.ToInt64(value);
}
}
else if (type == UtilConstants.BoolType)
{
return value.ObjToBool() ? "1" : "0";
}
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
{
return N + "'" + value.ToString().ToSqlFilter() + "'";
}
else if (type == UtilConstants.DateTimeOffsetType)
{
return FormatDateTimeOffset(value);
}
else if (type == UtilConstants.FloatType)
{
return N + "'" + Convert.ToDouble(value).ToString() + "'";
}
else if (value is decimal v)
{
return v.ToString(CultureInfo.InvariantCulture);
}
else
{
return N + "'" + value.ToString() + "'";
}
}
}
public override string FormatDateTimeOffset(object value)
{
return "'" + ((DateTimeOffset)value).ToString("o") + "'";

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
@ -45,7 +46,7 @@ namespace SqlSugar.TDSQLForPGODBC
var type = UtilMethods.GetUnderType(value.GetType());
if (type == UtilConstants.ByteArrayType || type == UtilConstants.DateType || columnInfo.IsArray || columnInfo.IsJson)
{
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
var parameterName = this.Builder.SqlParameterKeyWord + name + "_" + i;
var paramter = new SugarParameter(parameterName, value);
if (columnInfo.IsJson)
{
@ -86,6 +87,10 @@ namespace SqlSugar.TDSQLForPGODBC
{
return "'" + value.ToString().ToSqlFilter() + "'";
}
else if (value is decimal v)
{
return v.ToString(CultureInfo.InvariantCulture);
}
else
{
return "'" + value.ToString() + "'";
@ -145,7 +150,8 @@ namespace SqlSugar.TDSQLForPGODBC
{
var columnInfo = tableColumnList.FirstOrDefault(x => x.DbColumnName.Equals(it.DbColumnName, StringComparison.OrdinalIgnoreCase));
var dbType = columnInfo?.DataType;
if (dbType == null) {
if (dbType == null)
{
var typeName = it.PropertyType.Name.ToLower();
if (columnInfo == null && it.PropertyType.IsEnum)
{
@ -163,12 +169,13 @@ namespace SqlSugar.TDSQLForPGODBC
if (typeName == "boolean")
typeName = "bool";
var isAnyType = TDSQLForPGODBCDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).Any();
var isAnyType = PostgreSQLDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).Any();
if (isAnyType)
{
dbType = TDSQLForPGODBCDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).FirstOrDefault().Key;
dbType = PostgreSQLDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).FirstOrDefault().Key;
}
else {
else
{
dbType = "varchar";
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar.TDSQLForPGODBC
{
internal class QueryableFormat
{
public Type Type { get; set; }
public string TypeString { get; set; }
public string Format { get; set; }
public string PropertyName { get; set; }
public string MethodName { get; set; }
public MethodInfo MethodInfo { get; set; }
}
}

View File

@ -253,7 +253,71 @@ namespace SqlSugar.TDSQLForPGODBC
value = value.TrimEnd(' ').TrimEnd('1').TrimEnd('=');
return value;
}
/// <summary>
/// Available only in Select,Handles logic that cannot be completed by an expression
/// </summary>
/// <param name="addValue"></param>
/// <param name="valueFomatInfo"></param>
/// <returns></returns>
internal static object GetFormatValue(object addValue, QueryableFormat valueFomatInfo)
{
if (valueFomatInfo.MethodName == "ToString")
{
if (valueFomatInfo.Type == UtilConstants.GuidType)
{
addValue = Guid.Parse(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.ByteType)
{
addValue = Convert.ToByte(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.IntType)
{
addValue = Convert.ToInt32(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.LongType)
{
addValue = Convert.ToInt64(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.UIntType)
{
addValue = Convert.ToUInt32(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.ULongType)
{
addValue = Convert.ToUInt64(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.DecType)
{
addValue = Convert.ToDecimal(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.Type == UtilConstants.DobType)
{
addValue = Convert.ToDouble(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.TypeString == "Enum")
{
addValue = ChangeType2(addValue, valueFomatInfo.Type)?.ToString();
}
}
else if (valueFomatInfo.MethodName == "OnlyInSelectConvertToString")
{
var methodInfo = valueFomatInfo.MethodInfo;
if (methodInfo != null)
{
// 如果方法是静态的传递null作为第一个参数否则传递类的实例
object instance = methodInfo.IsStatic ? null : Activator.CreateInstance(methodInfo.ReflectedType);
// 创建一个包含参数值的object数组
object[] parameters = new object[] { addValue };
// 调用方法
addValue = methodInfo.Invoke(instance, parameters);
}
}
return addValue;
}
public static int CountSubstringOccurrences(string mainString, string searchString)
{
string[] substrings = mainString.Split(new string[] { searchString }, StringSplitOptions.None);
@ -542,6 +606,10 @@ namespace SqlSugar.TDSQLForPGODBC
return result;
}
}
else if (value is byte[] bytes && bytes.Length == 1 && destinationType == typeof(char))
{
return (char)(bytes)[0];
}
var destinationConverter = TypeDescriptor.GetConverter(destinationType);
if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
return destinationConverter.ConvertFrom(null, culture, value);
@ -1336,6 +1404,14 @@ namespace SqlSugar.TDSQLForPGODBC
{
return Convert.ToInt64(item.FieldValue);
}
else if (item.CSharpTypeName.EqualCase("float"))
{
return Convert.ToSingle(item.FieldValue);
}
else if (item.CSharpTypeName.EqualCase("single"))
{
return Convert.ToSingle(item.FieldValue);
}
else if (item.CSharpTypeName.EqualCase("short"))
{
return Convert.ToInt16(item.FieldValue);
@ -1511,10 +1587,11 @@ namespace SqlSugar.TDSQLForPGODBC
}
public static string GetSqlString(ConnectionConfig connectionConfig, KeyValuePair<string, List<SugarParameter>> sqlObj)
{
var guid = Guid.NewGuid() + "";
var result = sqlObj.Key;
if (sqlObj.Value != null)
{
foreach (var item in sqlObj.Value.OrderByDescending(it => it.ParameterName.Length))
foreach (var item in UtilMethods.CopySugarParameters(sqlObj.Value).OrderByDescending(it => it.ParameterName.Length))
{
if (item.ParameterName.StartsWith(":") && !result.Contains(item.ParameterName))
{
@ -1580,23 +1657,23 @@ namespace SqlSugar.TDSQLForPGODBC
result = result.Replace(item.ParameterName, (Convert.ToBoolean(item.Value) ? 1 : 0) + "");
}
}
else if (item.Value.GetType() != UtilConstants.StringType && connectionConfig.DbType == DbType.PostgreSQL && TDSQLForPGODBCDbBind.MappingTypesConst.Any(x => x.Value.ToString().EqualCase(item.Value.GetType().Name)))
else if (item.Value.GetType() != UtilConstants.StringType && connectionConfig.DbType == DbType.PostgreSQL && PostgreSQLDbBind.MappingTypesConst.Any(x => x.Value.ToString().EqualCase(item.Value.GetType().Name)))
{
var type = TDSQLForPGODBCDbBind.MappingTypesConst.First(x => x.Value.ToString().EqualCase(item.Value.GetType().Name)).Key;
var type = PostgreSQLDbBind.MappingTypesConst.First(x => x.Value.ToString().EqualCase(item.Value.GetType().Name)).Key;
var replaceValue = string.Format("CAST('{0}' AS {1})", item.Value, type);
result = result.Replace(item.ParameterName, replaceValue);
}
else if (connectionConfig.MoreSettings?.DisableNvarchar == true || item.DbType == System.Data.DbType.AnsiString || connectionConfig.DbType == DbType.Sqlite)
{
result = result.Replace(item.ParameterName, $"'{item.Value.ObjToString().ToSqlFilter()}'");
result = result.Replace(item.ParameterName, $"'{item.Value.ObjToString().Replace("@", guid).ToSqlFilter()}'");
}
else
{
result = result.Replace(item.ParameterName, $"N'{item.Value.ObjToString().ToSqlFilter()}'");
result = result.Replace(item.ParameterName, $"N'{item.Value.ObjToString().Replace("@", guid).ToSqlFilter()}'");
}
}
}
result = result.Replace(guid, "@");
return result;
}
public static string ByteArrayToPostgreByteaLiteral(byte[] data)
@ -1751,5 +1828,10 @@ namespace SqlSugar.TDSQLForPGODBC
}
return true;
}
internal static ConnMoreSettings GetMoreSetting(ExpressionContext context)
{
return context?.SugarContext?.Context?.CurrentConnectionConfig?.MoreSettings ?? new ConnMoreSettings();
}
}
}