Skip to content

Commit

Permalink
- 增加 OracleUs7ascii 写入处理特性;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Aug 6, 2024
1 parent 934be9c commit 8263c86
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
14 changes: 13 additions & 1 deletion Providers/FreeSql.Provider.Oracle/OracleAdo/OracleAdo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using FreeSql.Internal;
using FreeSql.DataAnnotations;
using FreeSql.Internal;
using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model;
using FreeSql.Internal.ObjectPool;
using System;
using System.Collections;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Threading;

namespace FreeSql.Oracle
Expand Down Expand Up @@ -50,7 +52,17 @@ public override object AddslashesProcessParam(object param, Type mapType, Column
else if (param is bool || param is bool?)
return (bool)param ? 1 : 0;
else if (param is string)
{
#if oledb
if (mapColumn?.Table != null && mapColumn.Table.Properties.TryGetValue(mapColumn.CsName, out var prop))
{
var us7attr = prop.GetCustomAttributes(typeof(OracleUS7AsciiAttribute), false)?.FirstOrDefault() as OracleUS7AsciiAttribute;
if (us7attr != null) return OracleUtils.StringToAscii(param as string, us7attr.Encoding);
}
#endif

return string.Concat("'", param.ToString().Replace("'", "''"), "'");
}
else if (param is char)
return string.Concat("'", param.ToString().Replace("'", "''").Replace('\0', ' '), "'");
else if (param is Enum)
Expand Down
34 changes: 32 additions & 2 deletions Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using FreeSql.Internal;
using FreeSql.DataAnnotations;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.OleDb;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FreeSql.Oracle
{
Expand Down Expand Up @@ -147,6 +151,12 @@ public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams,
if (type == typeof(string))
{
var valueString = value as string;
if (col?.Table != null && col.Table.Properties.TryGetValue(col.CsName, out var prop))
{
var us7attr = prop.GetCustomAttributes(typeof(OracleUS7AsciiAttribute), false)?.FirstOrDefault() as OracleUS7AsciiAttribute;
if (us7attr != null) return StringToAscii(valueString, us7attr.Encoding);
}

if (valueString != null)
{
if (valueString.Length < 4000) return string.Concat("'", valueString.Replace("'", "''"), "'");
Expand All @@ -166,5 +176,25 @@ public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams,
}
return FormatSql("{0}", value, 1);
}

public static string StringToAscii(string value, string encoding) //US7ASCII
{
if (string.IsNullOrEmpty(value)) return "NULL";
var bytes = Encoding.GetEncoding(encoding).GetBytes(value);
var sb = new StringBuilder();
try
{
for (int i = 0; i < bytes.Length; i++)
{
if (i > 0) sb.Append("||");
sb.Append("chr(").Append(bytes[i].ToString()).Append(")");
}
return sb.ToString();
}
finally
{
sb.Clear();
}
}
}
}
19 changes: 19 additions & 0 deletions Providers/FreeSql.Provider.OracleOledb/OracleUS7AsciiAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace FreeSql.DataAnnotations
{
[AttributeUsage(AttributeTargets.Class)]
public class OracleUS7AsciiAttribute : Attribute
{
public OracleUS7AsciiAttribute() { }
public OracleUS7AsciiAttribute(string encoding)
{
this.Encoding = encoding;
}

/// <summary>
/// 编码
/// </summary>
public string Encoding { get; set; } = "GB2312";
}
}

0 comments on commit 8263c86

Please sign in to comment.