- 修复 MySqlProvider .NetFramework 下可能报初始化类型错误;

This commit is contained in:
2881099
2023-05-20 17:02:07 +08:00
parent e7177991ad
commit 76b18d84a7
6 changed files with 300 additions and 270 deletions

View File

@ -21,104 +21,108 @@ namespace FreeSql.QuestDb
{
public class QuestDbProvider<TMark> : BaseDbProvider, IFreeSql<TMark>
{
static QuestDbProvider()
static int _firstInit = 1;
static void InitInternal()
{
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BigInteger)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BitArray)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlPoint)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlLine)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlLSeg)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlBox)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlPath)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlPolygon)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlCircle)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof((IPAddress Address, int Subnet))] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(IPAddress)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(PhysicalAddress)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<int>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<long>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<decimal>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<DateTime>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(Dictionary<string, string>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(JToken)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(JObject)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(JArray)] = true;
var MethodJTokenFromObject = typeof(JToken).GetMethod("FromObject", new[] { typeof(object) });
var MethodJObjectFromObject = typeof(JObject).GetMethod("FromObject", new[] { typeof(object) });
var MethodJArrayFromObject = typeof(JArray).GetMethod("FromObject", new[] { typeof(object) });
var MethodJTokenParse = typeof(JToken).GetMethod("Parse", new[] { typeof(string) });
var MethodJObjectParse = typeof(JObject).GetMethod("Parse", new[] { typeof(string) });
var MethodJArrayParse = typeof(JArray).GetMethod("Parse", new[] { typeof(string) });
var MethodJsonConvertDeserializeObject =
typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) });
var MethodToString = typeof(Utils).GetMethod("ToStringConcat", BindingFlags.Public | BindingFlags.Static,
null, new[] { typeof(object) }, null);
Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add(
(LabelTarget returnTarget, Expression valueExp, Type type) =>
{
switch (type.FullName)
{
case "Newtonsoft.Json.Linq.JToken":
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJTokenParse,
Expression.Convert(valueExp, typeof(string))), typeof(JToken))),
Expression.Return(returnTarget,
Expression.TypeAs(Expression.Call(MethodJTokenFromObject, valueExp),
typeof(JToken))));
case "Newtonsoft.Json.Linq.JObject":
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJObjectParse,
Expression.Convert(valueExp, typeof(string))), typeof(JObject))),
Expression.Return(returnTarget,
Expression.TypeAs(Expression.Call(MethodJObjectFromObject, valueExp),
typeof(JObject))));
case "Newtonsoft.Json.Linq.JArray":
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJArrayParse,
Expression.Convert(valueExp, typeof(string))), typeof(JArray))),
Expression.Return(returnTarget,
Expression.TypeAs(Expression.Call(MethodJArrayFromObject, valueExp),
typeof(JArray))));
case "Npgsql.LegacyPostgis.PostgisGeometry":
return Expression.Return(returnTarget, valueExp);
case "NetTopologySuite.Geometries.Geometry":
return Expression.Return(returnTarget, valueExp);
}
if (typeof(IList).IsAssignableFrom(type))
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJsonConvertDeserializeObject,
Expression.Convert(valueExp, typeof(string)),
Expression.Constant(type, typeof(Type))), type)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJsonConvertDeserializeObject,
Expression.Convert(Expression.Call(MethodToString, valueExp), typeof(string)),
Expression.Constant(type, typeof(Type))), type)));
return null;
});
Select0Provider._dicMethodDataReaderGetValue[typeof(Guid)] =
typeof(DbDataReader).GetMethod("GetGuid", new Type[] { typeof(int) });
QuestDbContainer.Initialize(service =>
if (Interlocked.Exchange(ref _firstInit, 0) == 1) //不能放在 static ctor .NetFramework 可能报初始化类型错误
{
service.AddHttpClient();
});
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BigInteger)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BitArray)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlPoint)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlLine)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlLSeg)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlBox)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlPath)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlPolygon)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlCircle)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof((IPAddress Address, int Subnet))] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(IPAddress)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(PhysicalAddress)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<int>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<long>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<decimal>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(NpgsqlRange<DateTime>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(Dictionary<string, string>)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(JToken)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(JObject)] = true;
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(JArray)] = true;
var MethodJTokenFromObject = typeof(JToken).GetMethod("FromObject", new[] { typeof(object) });
var MethodJObjectFromObject = typeof(JObject).GetMethod("FromObject", new[] { typeof(object) });
var MethodJArrayFromObject = typeof(JArray).GetMethod("FromObject", new[] { typeof(object) });
var MethodJTokenParse = typeof(JToken).GetMethod("Parse", new[] { typeof(string) });
var MethodJObjectParse = typeof(JObject).GetMethod("Parse", new[] { typeof(string) });
var MethodJArrayParse = typeof(JArray).GetMethod("Parse", new[] { typeof(string) });
var MethodJsonConvertDeserializeObject =
typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) });
var MethodToString = typeof(Utils).GetMethod("ToStringConcat", BindingFlags.Public | BindingFlags.Static,
null, new[] { typeof(object) }, null);
Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add(
(LabelTarget returnTarget, Expression valueExp, Type type) =>
{
switch (type.FullName)
{
case "Newtonsoft.Json.Linq.JToken":
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJTokenParse,
Expression.Convert(valueExp, typeof(string))), typeof(JToken))),
Expression.Return(returnTarget,
Expression.TypeAs(Expression.Call(MethodJTokenFromObject, valueExp),
typeof(JToken))));
case "Newtonsoft.Json.Linq.JObject":
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJObjectParse,
Expression.Convert(valueExp, typeof(string))), typeof(JObject))),
Expression.Return(returnTarget,
Expression.TypeAs(Expression.Call(MethodJObjectFromObject, valueExp),
typeof(JObject))));
case "Newtonsoft.Json.Linq.JArray":
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJArrayParse,
Expression.Convert(valueExp, typeof(string))), typeof(JArray))),
Expression.Return(returnTarget,
Expression.TypeAs(Expression.Call(MethodJArrayFromObject, valueExp),
typeof(JArray))));
case "Npgsql.LegacyPostgis.PostgisGeometry":
return Expression.Return(returnTarget, valueExp);
case "NetTopologySuite.Geometries.Geometry":
return Expression.Return(returnTarget, valueExp);
}
if (typeof(IList).IsAssignableFrom(type))
return Expression.IfThenElse(
Expression.TypeIs(valueExp, typeof(string)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJsonConvertDeserializeObject,
Expression.Convert(valueExp, typeof(string)),
Expression.Constant(type, typeof(Type))), type)),
Expression.Return(returnTarget,
Expression.TypeAs(
Expression.Call(MethodJsonConvertDeserializeObject,
Expression.Convert(Expression.Call(MethodToString, valueExp), typeof(string)),
Expression.Constant(type, typeof(Type))), type)));
return null;
});
Select0Provider._dicMethodDataReaderGetValue[typeof(Guid)] =
typeof(DbDataReader).GetMethod("GetGuid", new Type[] { typeof(int) });
QuestDbContainer.Initialize(service =>
{
service.AddHttpClient();
});
}
}
public override ISelect<T1> CreateSelectProvider<T1>(object dywhere) =>
@ -139,6 +143,7 @@ namespace FreeSql.QuestDb
public QuestDbProvider(string masterConnectionString, string[] slaveConnectionString,
Func<DbConnection> connectionFactory = null)
{
InitInternal();
this.InternalCommonUtils = new QuestDbUtils(this);
this.InternalCommonExpression = new QuestDbExpression(this.InternalCommonUtils);