From 8263c86a209876f8b2ca448e2a4678e484526efd Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 6 Aug 2024 14:34:12 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20OracleUs7ascii=20?= =?UTF-8?q?=E5=86=99=E5=85=A5=E5=A4=84=E7=90=86=E7=89=B9=E6=80=A7=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OracleAdo/OracleAdo.cs | 14 +++++++- .../OracleOledbUtils.cs | 34 +++++++++++++++++-- .../OracleUS7AsciiAttribute.cs | 19 +++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 Providers/FreeSql.Provider.OracleOledb/OracleUS7AsciiAttribute.cs diff --git a/Providers/FreeSql.Provider.Oracle/OracleAdo/OracleAdo.cs b/Providers/FreeSql.Provider.Oracle/OracleAdo/OracleAdo.cs index f0c4ff7c..c9bb06f7 100644 --- a/Providers/FreeSql.Provider.Oracle/OracleAdo/OracleAdo.cs +++ b/Providers/FreeSql.Provider.Oracle/OracleAdo/OracleAdo.cs @@ -1,4 +1,5 @@ -using FreeSql.Internal; +using FreeSql.DataAnnotations; +using FreeSql.Internal; using FreeSql.Internal.CommonProvider; using FreeSql.Internal.Model; using FreeSql.Internal.ObjectPool; @@ -6,6 +7,7 @@ using System; using System.Collections; using System.Data.Common; using System.Linq; +using System.Reflection; using System.Threading; namespace FreeSql.Oracle @@ -50,7 +52,17 @@ namespace FreeSql.Oracle 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) diff --git a/Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs b/Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs index 575774d6..c34bc7f2 100644 --- a/Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs +++ b/Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs @@ -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 { @@ -147,6 +151,12 @@ namespace FreeSql.Oracle 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("'", "''"), "'"); @@ -166,5 +176,25 @@ namespace FreeSql.Oracle } 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(); + } + } } } diff --git a/Providers/FreeSql.Provider.OracleOledb/OracleUS7AsciiAttribute.cs b/Providers/FreeSql.Provider.OracleOledb/OracleUS7AsciiAttribute.cs new file mode 100644 index 00000000..4476c590 --- /dev/null +++ b/Providers/FreeSql.Provider.OracleOledb/OracleUS7AsciiAttribute.cs @@ -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; + } + + /// + /// 编码 + /// + public string Encoding { get; set; } = "GB2312"; + } +}