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

Merge pull request !75 from 杜子腾/master
This commit is contained in:
阿妮亚 2025-04-01 06:04:19 +00:00 committed by Gitee
commit c094f1a7d4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
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)
{
@ -40,7 +48,7 @@ namespace SqlSugar.OceanBaseForOracle
private string GetOracleUpdateColums(DbColumnInfo m)
{
return string.Format("\"{0}\"={1}", m.DbColumnName.ToUpper(IsUppper), base.GetDbColumn(m, FormatValue(m.Value, m.IsPrimarykey, m.PropertyName)));
return string.Format("\"{0}\"={1} ", m.DbColumnName.ToUpper(IsUppper), base.GetDbColumn(m, FormatValue(m.Value, m.IsPrimarykey, m.PropertyName)));
}
int i = 0;
public object FormatValue(object value, bool isPrimaryKey, string name)

View File

@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
@ -34,7 +35,7 @@ namespace SqlSugar.TDSQLForPGODBC
public override Func<string, string, string> ConvertInsertReturnIdFunc { get; set; } = (name, sql) =>
{
return sql.Trim().TrimEnd(';')+ $"returning {name} ";
return sql.Trim().TrimEnd(';') + $"returning {name} ";
};
public override string ToSqlString()
{
@ -47,7 +48,7 @@ namespace SqlSugar.TDSQLForPGODBC
string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
if (isSingle)
{
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName)));
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName)));
ActionMinDate();
return GetIgnoreSql(string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString));
}
@ -56,7 +57,7 @@ namespace SqlSugar.TDSQLForPGODBC
StringBuilder batchInsetrSql = new StringBuilder();
int pageSize = 200;
int pageIndex = 1;
if (IsNoPage&&IsReturnPkList)
if (IsNoPage && IsReturnPkList)
{
pageSize = groupList.Count;
}
@ -75,7 +76,7 @@ namespace SqlSugar.TDSQLForPGODBC
}
batchInsetrSql.Append("\r\n ( " + string.Join(",", columns.Select(it =>
{
if (it.InsertServerTime || it.InsertSql.HasValue()||it.SqlParameterDbType is Type|| it?.PropertyType?.Name=="DateOnly" || it?.PropertyType?.Name == "TimeOnly")
if (it.InsertServerTime || it.InsertSql.HasValue() || it.SqlParameterDbType is Type || it?.PropertyType?.Name == "DateOnly" || it?.PropertyType?.Name == "TimeOnly")
{
return GetDbColumn(it, null);
}
@ -84,18 +85,22 @@ namespace SqlSugar.TDSQLForPGODBC
{
var date = ((DateTime)it.Value);
value = date.ToString("O");
if (date==DateTime.MaxValue)
if (date == DateTime.MaxValue)
{
value = "9999-12-31T23:59:59.999999";
}
}
else if (it.Value is DateTimeOffset)
else if (it.Value is DateTimeOffset)
{
return FormatDateTimeOffset(it.Value);
}
else if (it.IsArray&&it.Value!=null)
else if (it.Value is decimal v)
{
return FormatValue(it.Value,it.PropertyName,i,it);
return v.ToString(CultureInfo.InvariantCulture);
}
else if (it.IsArray && it.Value != null)
{
return FormatValue(it.Value, it.PropertyName, i, it);
}
else if (it.Value is byte[])
{
@ -105,7 +110,7 @@ namespace SqlSugar.TDSQLForPGODBC
{
value = it.Value;
}
if (value == null||value==DBNull.Value)
if (value == null || value == DBNull.Value)
{
return string.Format(SqlTemplateBatchSelect, "NULL");
}
@ -114,7 +119,7 @@ namespace SqlSugar.TDSQLForPGODBC
++i;
}
pageIndex++;
batchInsetrSql.Remove(batchInsetrSql.Length - 1,1).Append("\r\n;\r\n");
batchInsetrSql.Remove(batchInsetrSql.Length - 1, 1).Append("\r\n;\r\n");
}
return GetIgnoreSql(batchInsetrSql.ToString());
}
@ -129,9 +134,9 @@ namespace SqlSugar.TDSQLForPGODBC
else
{
var type = value.GetType();
if (type == UtilConstants.ByteArrayType||type == UtilConstants.DateType || columnInfo.IsArray || columnInfo.IsJson)
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,17 +177,82 @@ 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") + "'";
}
private string GetIgnoreSql(string sql)
{
if (this.ConflictNothing?.Any() == true)

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
@ -34,7 +35,7 @@ namespace SqlSugar.TDSQLForPGODBC
}
}
public object FormatValue(object value,string name,int i,DbColumnInfo columnInfo)
public object FormatValue(object value, string name, int i, DbColumnInfo columnInfo)
{
if (value == null)
{
@ -42,16 +43,16 @@ namespace SqlSugar.TDSQLForPGODBC
}
else
{
var type =UtilMethods.GetUnderType(value.GetType());
if (type == UtilConstants.ByteArrayType||type == UtilConstants.DateType||columnInfo.IsArray||columnInfo.IsJson)
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)
if (columnInfo.IsJson)
{
paramter.IsJson = true;
}
if (columnInfo.IsArray)
if (columnInfo.IsArray)
{
paramter.IsArray = true;
}
@ -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() + "'";
@ -133,7 +138,7 @@ namespace SqlSugar.TDSQLForPGODBC
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
int i = 0;
var tableColumnList = this.Context.DbMaintenance.GetColumnInfosByTableName(GetTableNameStringNoWith);
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
{
var isFirst = i == 0;
@ -145,11 +150,12 @@ 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)
if (columnInfo == null && it.PropertyType.IsEnum)
{
if (this.Context.CurrentConnectionConfig?.MoreSettings?.TableEnumIsString!=true)
if (this.Context.CurrentConnectionConfig?.MoreSettings?.TableEnumIsString != true)
{
typeName = "int";
}
@ -163,20 +169,21 @@ 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";
}
}
if(it?.PropertyType?.FullName == "NetTopologySuite.Geometries.Geometry")
if (it?.PropertyType?.FullName == "NetTopologySuite.Geometries.Geometry")
{
return string.Format(" {0} ", base.GetDbColumn(it, FormatValue(it.Value, it.DbColumnName, i + (pageIndex - 1) * 100000, it)), dbType);
}
return string.Format("CAST({0} AS {1})", base.GetDbColumn(it,FormatValue(it.Value,it.DbColumnName,i+(pageIndex-1)*100000,it)), dbType);
return string.Format("CAST({0} AS {1})", base.GetDbColumn(it, FormatValue(it.Value, it.DbColumnName, i + (pageIndex - 1) * 100000, it)), dbType);
})) + ")");
++i;
@ -224,7 +231,7 @@ namespace SqlSugar.TDSQLForPGODBC
}
else
{
if (item.Value?.Sql?.StartsWith("( CASE WHEN")==true)
if (item.Value?.Sql?.StartsWith("( CASE WHEN") == true)
{
result = result.Replace($"{dbColumnName}=T.{dbColumnName}", $"{dbColumnName}={item.Value.Sql.Replace(" \"", $" {Builder.GetTranslationColumnName(this.TableName)}.\"")}");
}
@ -241,22 +248,22 @@ namespace SqlSugar.TDSQLForPGODBC
}
protected override string GetJoinUpdate(string columnsString, ref string whereString)
{
if (this.JoinInfos?.Count > 1)
if (this.JoinInfos?.Count > 1)
{
return this.GetJoinUpdateMany(columnsString,whereString);
return this.GetJoinUpdateMany(columnsString, whereString);
}
var formString = $" {Builder.GetTranslationColumnName(this.TableName)} AS {Builder.GetTranslationColumnName(this.ShortName)} ";
var joinString = "";
foreach (var item in this.JoinInfos)
{
whereString += " AND "+item.JoinWhere;
whereString += " AND " + item.JoinWhere;
joinString += $"\r\n FROM {Builder.GetTranslationColumnName(item.TableName)} {Builder.GetTranslationColumnName(item.ShortName)} ";
}
var tableName = formString + "\r\n ";
columnsString = columnsString.Replace(Builder.GetTranslationColumnName(this.ShortName)+".","")+joinString;
columnsString = columnsString.Replace(Builder.GetTranslationColumnName(this.ShortName) + ".", "") + joinString;
return string.Format(SqlTemplate, tableName, columnsString, whereString);
}
private string GetJoinUpdateMany(string columnsString,string where)
private string GetJoinUpdateMany(string columnsString, string where)
{
var formString = $" {Builder.GetTranslationColumnName(this.TableName)} AS {Builder.GetTranslationColumnName(this.ShortName)} ";
var joinString = "";

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