mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 增加 FreeSql.Provider.OracleOledb 解决US7ASCII 中文乱码问题;
This commit is contained in:
@ -0,0 +1,52 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>FreeSql;ncc;YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库 Oracle 基于 oledb 实现,解决 US7ASCII 中文乱码问题</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageTags>FreeSql;ORM;Oracle;Oledb;US7ASCII</PackageTags>
|
||||
<PackageId>$(AssemblyName)</PackageId>
|
||||
<PackageIcon>logo.png</PackageIcon>
|
||||
<Title>$(AssemblyName)</Title>
|
||||
<IsPackable>true</IsPackable>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
|
||||
<DelaySign>false</DelaySign>
|
||||
<Version>3.2.666-preview20220823</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../../logo.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<PackageReference Include="System.Data.OleDb" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="../FreeSql.Provider.Oracle/**/*.cs" Exclude="../FreeSql.Provider.Oracle/obj/**/*" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Remove="..\FreeSql.Provider.Oracle\OracleUtils.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<DefineConstants>oledb</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'net40'">
|
||||
<DefineConstants>net40;oledb</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
156
Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs
Normal file
156
Providers/FreeSql.Provider.OracleOledb/OracleOledbUtils.cs
Normal file
@ -0,0 +1,156 @@
|
||||
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.Globalization;
|
||||
|
||||
namespace FreeSql.Oracle
|
||||
{
|
||||
|
||||
class OracleUtils : CommonUtils
|
||||
{
|
||||
public OracleUtils(IFreeSql orm) : base(orm)
|
||||
{
|
||||
}
|
||||
|
||||
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
|
||||
var dbtype = (OleDbType?)_orm.CodeFirst.GetDbInfo(type)?.type;
|
||||
if (dbtype == OleDbType.Boolean)
|
||||
{
|
||||
if (value == null) value = null;
|
||||
else value = (bool)value == true ? 1 : 0;
|
||||
dbtype = OleDbType.SmallInt;
|
||||
}
|
||||
var ret = new OleDbParameter { ParameterName = QuoteParamterName(parameterName) };
|
||||
if (dbtype != null) ret.OleDbType = dbtype.Value;
|
||||
ret.Value = value;
|
||||
if (col != null)
|
||||
{
|
||||
var dbtype2 = (OleDbType)_orm.DbFirst.GetDbType(new DatabaseModel.DbColumnInfo { DbTypeTextFull = col.Attribute.DbType?.Replace("NOT NULL", "").Replace(" NULL", "").Trim(), DbTypeText = col.DbTypeText });
|
||||
switch (dbtype2)
|
||||
{
|
||||
case OleDbType.Char:
|
||||
case OleDbType.VarChar:
|
||||
case OleDbType.WChar:
|
||||
case OleDbType.LongVarChar:
|
||||
case OleDbType.LongVarWChar:
|
||||
case OleDbType.Decimal:
|
||||
dbtype = dbtype2;
|
||||
//if (col.DbSize != 0) ret.Size = col.DbSize;
|
||||
if (col.DbPrecision != 0) ret.Precision = col.DbPrecision;
|
||||
if (col.DbScale != 0) ret.Scale = col.DbScale;
|
||||
break;
|
||||
}
|
||||
}
|
||||
_params?.Add(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
|
||||
Utils.GetDbParamtersByObject<OleDbParameter>(sql, obj, ":", (name, type, value) =>
|
||||
{
|
||||
var dbtypeint = _orm.CodeFirst.GetDbInfo(type)?.type;
|
||||
var dbtype = dbtypeint != null ? (OleDbType?)dbtypeint : null;
|
||||
if (dbtype == OleDbType.Boolean)
|
||||
{
|
||||
if (value == null) value = null;
|
||||
else value = (bool)value == true ? 1 : 0;
|
||||
dbtype = OleDbType.SmallInt;
|
||||
}
|
||||
var ret = new OleDbParameter { ParameterName = $":{name}" };
|
||||
if (dbtype != null) ret.OleDbType = dbtype.Value;
|
||||
if (value is IList valueList && value is Array == false && valueList.Count > 0)
|
||||
{
|
||||
var valueItemType = valueList[0]?.GetType();
|
||||
if (valueItemType == typeof(int)) LocalSetListValue<int>();
|
||||
else if (valueItemType == typeof(long)) LocalSetListValue<long>();
|
||||
else if (valueItemType == typeof(short)) LocalSetListValue<short>();
|
||||
else if (valueItemType == typeof(string)) LocalSetListValue<string>();
|
||||
else if (valueItemType == typeof(Guid)) LocalSetListValue<Guid>();
|
||||
else if (valueItemType == typeof(char)) LocalSetListValue<char>();
|
||||
else if (valueItemType == typeof(bool)) LocalSetListValue<bool>();
|
||||
else if (valueItemType == typeof(uint)) LocalSetListValue<uint>();
|
||||
else if (valueItemType == typeof(ulong)) LocalSetListValue<ulong>();
|
||||
else if (valueItemType == typeof(ushort)) LocalSetListValue<ushort>();
|
||||
else if (valueItemType == typeof(decimal)) LocalSetListValue<decimal>();
|
||||
else if (valueItemType == typeof(double)) LocalSetListValue<double>();
|
||||
else if (valueItemType == typeof(float)) LocalSetListValue<float>();
|
||||
else if (valueItemType == typeof(DateTime)) LocalSetListValue<DateTime>();
|
||||
|
||||
void LocalSetListValue<T>()
|
||||
{
|
||||
var valueCopy = new List<T>();
|
||||
foreach (var valueItem in valueList) valueCopy.Add((T)Utils.GetDataReaderValue(valueItemType, valueItem));
|
||||
value = valueCopy.ToArray();
|
||||
}
|
||||
}
|
||||
ret.Value = value; //IList 赋值会报错
|
||||
return ret;
|
||||
});
|
||||
|
||||
public override string FormatSql(string sql, params object[] args) => sql?.FormatOracle(args);
|
||||
public override string QuoteSqlName(params string[] name)
|
||||
{
|
||||
if (name.Length == 1)
|
||||
{
|
||||
var nametrim = name[0].Trim();
|
||||
if (nametrim.StartsWith("(") && nametrim.EndsWith(")"))
|
||||
return nametrim; //原生SQL
|
||||
if (nametrim.StartsWith("\"") && nametrim.EndsWith("\""))
|
||||
return nametrim;
|
||||
return $"\"{nametrim.Replace(".", "\".\"")}\"";
|
||||
}
|
||||
return $"\"{string.Join("\".\"", name)}\"";
|
||||
}
|
||||
public override string TrimQuoteSqlName(string name)
|
||||
{
|
||||
var nametrim = name.Trim();
|
||||
if (nametrim.StartsWith("(") && nametrim.EndsWith(")"))
|
||||
return nametrim; //原生SQL
|
||||
return $"{nametrim.Trim('"').Replace("\".\"", ".").Replace(".\"", ".")}";
|
||||
}
|
||||
public override string[] SplitTableName(string name) => GetSplitTableNames(name, '"', '"', 2);
|
||||
public override string QuoteParamterName(string name) => $":{name}";
|
||||
public override string IsNull(string sql, object value) => $"nvl({sql}, {value})";
|
||||
public override string StringConcat(string[] objs, Type[] types) => $"{string.Join(" || ", objs)}";
|
||||
public override string Mod(string left, string right, Type leftType, Type rightType) => $"mod({left}, {right})";
|
||||
public override string Div(string left, string right, Type leftType, Type rightType) => $"trunc({left} / {right})";
|
||||
public override string Now => "systimestamp";
|
||||
public override string NowUtc => "sys_extract_utc(systimestamp)";
|
||||
|
||||
public override string QuoteWriteParamterAdapter(Type type, string paramterName) => paramterName;
|
||||
protected override string QuoteReadColumnAdapter(Type type, Type mapType, string columnName) => columnName;
|
||||
|
||||
public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams, string specialParamFlag, ColumnInfo col, Type type, object value)
|
||||
{
|
||||
if (value == null) return "NULL";
|
||||
if (type.IsNumberType()) return string.Format(CultureInfo.InvariantCulture, "{0}", value);
|
||||
if (type == typeof(string))
|
||||
{
|
||||
var valueString = value as string;
|
||||
if (valueString != null)
|
||||
{
|
||||
if (valueString.Length < 4000) return string.Concat("'", valueString.Replace("'", "''"), "'");
|
||||
var pam = AppendParamter(specialParams, $"p_{specialParams?.Count}{specialParamFlag}", col, type, value);
|
||||
return pam.ParameterName;
|
||||
}
|
||||
}
|
||||
if (type == typeof(byte[]))
|
||||
{
|
||||
var valueBytes = value as byte[];
|
||||
if (valueBytes != null)
|
||||
{
|
||||
if (valueBytes.Length < 4000) return $"hextoraw('{CommonUtils.BytesSqlRaw(valueBytes)}')";
|
||||
var pam = AppendParamter(specialParams, $"p_{specialParams?.Count}{specialParamFlag}", col, type, value);
|
||||
return pam.ParameterName;
|
||||
}
|
||||
}
|
||||
return FormatSql("{0}", value, 1);
|
||||
}
|
||||
}
|
||||
}
|
BIN
Providers/FreeSql.Provider.OracleOledb/key.snk
Normal file
BIN
Providers/FreeSql.Provider.OracleOledb/key.snk
Normal file
Binary file not shown.
Reference in New Issue
Block a user