mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 增加 QuestDB 时序数据库支持;(感谢 Daily 贡献)#1429
This commit is contained in:
@ -15,13 +15,13 @@ namespace FreeSql.QuestDb
|
||||
{
|
||||
class QuestDbAdo : FreeSql.Internal.CommonProvider.AdoProvider
|
||||
{
|
||||
public QuestDbAdo() : base(DataType.PostgreSQL, null, null) { }
|
||||
public QuestDbAdo(CommonUtils util, string masterConnectionString, string[] slaveConnectionStrings, Func<DbConnection> connectionFactory) : base(DataType.PostgreSQL, masterConnectionString, slaveConnectionStrings)
|
||||
public QuestDbAdo() : base(DataType.QuestDb, null, null) { }
|
||||
public QuestDbAdo(CommonUtils util, string masterConnectionString, string[] slaveConnectionStrings, Func<DbConnection> connectionFactory) : base(DataType.QuestDb, masterConnectionString, slaveConnectionStrings)
|
||||
{
|
||||
base._util = util;
|
||||
if (connectionFactory != null)
|
||||
{
|
||||
var pool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.PostgreSQL, connectionFactory);
|
||||
var pool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.QuestDb, connectionFactory);
|
||||
ConnectionString = pool.TestConnection?.ConnectionString;
|
||||
MasterPool = pool;
|
||||
return;
|
||||
|
@ -22,7 +22,7 @@ namespace FreeSql.QuestDb
|
||||
{
|
||||
this.availableHandler = availableHandler;
|
||||
this.unavailableHandler = unavailableHandler;
|
||||
var policy = new PostgreSQLConnectionPoolPolicy
|
||||
var policy = new QuestDbConnectionPoolPolicy
|
||||
{
|
||||
_pool = this,
|
||||
Name = name
|
||||
@ -42,11 +42,11 @@ namespace FreeSql.QuestDb
|
||||
}
|
||||
}
|
||||
|
||||
class PostgreSQLConnectionPoolPolicy : IPolicy<DbConnection>
|
||||
class QuestDbConnectionPoolPolicy : IPolicy<DbConnection>
|
||||
{
|
||||
|
||||
internal QuestDbConnectionPool _pool;
|
||||
public string Name { get; set; } = $"PostgreSQL NpgsqlConnection {CoreStrings.S_ObjectPool}";
|
||||
public string Name { get; set; } = $"QuestDb NpgsqlConnection {CoreStrings.S_ObjectPool}";
|
||||
public int PoolSize { get; set; } = 50;
|
||||
public TimeSpan SyncGetTimeout { get; set; } = TimeSpan.FromSeconds(10);
|
||||
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromSeconds(20);
|
||||
|
@ -1,157 +0,0 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Npgsql;
|
||||
using NpgsqlTypes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace Newtonsoft.Json
|
||||
{
|
||||
public class QuestDbTypesConverter : JsonConverter
|
||||
{
|
||||
private static readonly Type typeof_BitArray = typeof(BitArray);
|
||||
|
||||
private static readonly Type typeof_NpgsqlPoint = typeof(NpgsqlPoint);
|
||||
private static readonly Type typeof_NpgsqlLine = typeof(NpgsqlLine);
|
||||
private static readonly Type typeof_NpgsqlLSeg = typeof(NpgsqlLSeg);
|
||||
private static readonly Type typeof_NpgsqlBox = typeof(NpgsqlBox);
|
||||
private static readonly Type typeof_NpgsqlPath = typeof(NpgsqlPath);
|
||||
private static readonly Type typeof_NpgsqlPolygon = typeof(NpgsqlPolygon);
|
||||
private static readonly Type typeof_NpgsqlCircle = typeof(NpgsqlCircle);
|
||||
|
||||
private static readonly Type typeof_Cidr = typeof((IPAddress, int));
|
||||
private static readonly Type typeof_IPAddress = typeof(IPAddress);
|
||||
private static readonly Type typeof_PhysicalAddress = typeof(PhysicalAddress);
|
||||
|
||||
private static readonly Type typeof_String = typeof(string);
|
||||
|
||||
private static readonly Type typeof_NpgsqlRange_int = typeof(NpgsqlRange<int>);
|
||||
private static readonly Type typeof_NpgsqlRange_long = typeof(NpgsqlRange<long>);
|
||||
private static readonly Type typeof_NpgsqlRange_decimal = typeof(NpgsqlRange<decimal>);
|
||||
private static readonly Type typeof_NpgsqlRange_DateTime = typeof(NpgsqlRange<DateTime>);
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
Type ctype = objectType.IsArray ? objectType.GetElementType() : objectType;
|
||||
var ctypeGenericType1 = ctype.GenericTypeArguments.FirstOrDefault();
|
||||
|
||||
if (ctype == typeof_BitArray) return true;
|
||||
|
||||
if (ctype == typeof_NpgsqlPoint || ctypeGenericType1 == typeof_NpgsqlPoint) return true;
|
||||
if (ctype == typeof_NpgsqlLine || ctypeGenericType1 == typeof_NpgsqlLine) return true;
|
||||
if (ctype == typeof_NpgsqlLSeg || ctypeGenericType1 == typeof_NpgsqlLSeg) return true;
|
||||
if (ctype == typeof_NpgsqlBox || ctypeGenericType1 == typeof_NpgsqlBox) return true;
|
||||
if (ctype == typeof_NpgsqlPath || ctypeGenericType1 == typeof_NpgsqlPath) return true;
|
||||
if (ctype == typeof_NpgsqlPolygon || ctypeGenericType1 == typeof_NpgsqlPolygon) return true;
|
||||
if (ctype == typeof_NpgsqlCircle || ctypeGenericType1 == typeof_NpgsqlCircle) return true;
|
||||
|
||||
if (ctype == typeof_Cidr || ctypeGenericType1 == typeof_Cidr) return true;
|
||||
if (ctype == typeof_IPAddress) return true;
|
||||
if (ctype == typeof_PhysicalAddress) return true;
|
||||
|
||||
if (ctype == typeof_NpgsqlRange_int || ctypeGenericType1 == typeof_NpgsqlRange_int) return true;
|
||||
if (ctype == typeof_NpgsqlRange_long || ctypeGenericType1 == typeof_NpgsqlRange_long) return true;
|
||||
if (ctype == typeof_NpgsqlRange_decimal || ctypeGenericType1 == typeof_NpgsqlRange_decimal) return true;
|
||||
if (ctype == typeof_NpgsqlRange_DateTime || ctypeGenericType1 == typeof_NpgsqlRange_DateTime) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
private object YieldJToken(Type ctype, JToken jt, int rank)
|
||||
{
|
||||
if (jt.Type == JTokenType.Null) return null;
|
||||
if (rank == 0)
|
||||
{
|
||||
var ctypeGenericType1 = ctype.GenericTypeArguments.FirstOrDefault();//ctype.Namespace == "System" && ctype.Name.StartsWith("Nullable`") ? ctype.GenericTypeArguments.FirstOrDefault() : null;
|
||||
if (ctype == typeof_BitArray) return jt.ToString().ToBitArray();
|
||||
|
||||
if (ctype == typeof_NpgsqlPoint || ctypeGenericType1 == typeof_NpgsqlPoint) return NpgsqlPoint.Parse(jt.ToString());
|
||||
if (ctype == typeof_NpgsqlLine || ctypeGenericType1 == typeof_NpgsqlLine) return NpgsqlLine.Parse(jt.ToString());
|
||||
if (ctype == typeof_NpgsqlLSeg || ctypeGenericType1 == typeof_NpgsqlLSeg) return NpgsqlLSeg.Parse(jt.ToString());
|
||||
if (ctype == typeof_NpgsqlBox || ctypeGenericType1 == typeof_NpgsqlBox) return NpgsqlBox.Parse(jt.ToString());
|
||||
if (ctype == typeof_NpgsqlPath || ctypeGenericType1 == typeof_NpgsqlPath) return NpgsqlPath.Parse(jt.ToString());
|
||||
if (ctype == typeof_NpgsqlPolygon || ctypeGenericType1 == typeof_NpgsqlPolygon) return NpgsqlPolygon.Parse(jt.ToString());
|
||||
if (ctype == typeof_NpgsqlCircle || ctypeGenericType1 == typeof_NpgsqlCircle) return NpgsqlCircle.Parse(jt.ToString());
|
||||
|
||||
if (ctype == typeof_Cidr || ctypeGenericType1 == typeof_Cidr)
|
||||
{
|
||||
var cidrArgs = jt.ToString().Split(new[] { '/' }, 2);
|
||||
return (IPAddress.Parse(cidrArgs.First()), cidrArgs.Length >= 2 ? int.TryParse(cidrArgs[1], out var tryCdirSubnet) ? tryCdirSubnet : 0 : 0);
|
||||
}
|
||||
if (ctype == typeof_IPAddress) return IPAddress.Parse(jt.ToString());
|
||||
if (ctype == typeof_PhysicalAddress) return PhysicalAddress.Parse(jt.ToString());
|
||||
|
||||
if (ctype == typeof_NpgsqlRange_int || ctypeGenericType1 == typeof_NpgsqlRange_int) return jt.ToString().ToNpgsqlRange<int>();
|
||||
if (ctype == typeof_NpgsqlRange_long || ctypeGenericType1 == typeof_NpgsqlRange_long) return jt.ToString().ToNpgsqlRange<long>();
|
||||
if (ctype == typeof_NpgsqlRange_decimal || ctypeGenericType1 == typeof_NpgsqlRange_decimal) return jt.ToString().ToNpgsqlRange<decimal>();
|
||||
if (ctype == typeof_NpgsqlRange_DateTime || ctypeGenericType1 == typeof_NpgsqlRange_DateTime) return jt.ToString().ToNpgsqlRange<DateTime>();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
var jtarr = jt.ToArray();
|
||||
var ret = Array.CreateInstance(ctype, jtarr.Length);
|
||||
var jtarrIdx = 0;
|
||||
foreach (var a in jtarr)
|
||||
{
|
||||
var t2 = YieldJToken(ctype, a, rank - 1);
|
||||
ret.SetValue(t2, jtarrIdx++);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
int rank = objectType.IsArray ? objectType.GetArrayRank() : 0;
|
||||
Type ctype = objectType.IsArray ? objectType.GetElementType() : objectType;
|
||||
|
||||
var ret = YieldJToken(ctype, JToken.Load(reader), rank);
|
||||
if (ret != null && rank > 0) return ret;
|
||||
return ret;
|
||||
}
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
Type objectType = value.GetType();
|
||||
if (objectType.IsArray)
|
||||
{
|
||||
int rank = objectType.GetArrayRank();
|
||||
int[] indices = new int[rank];
|
||||
GetJObject(value as Array, indices, 0).WriteTo(writer);
|
||||
}
|
||||
else
|
||||
GetJObject(value).WriteTo(writer);
|
||||
}
|
||||
public static JToken GetJObject(object value)
|
||||
{
|
||||
if (value is BitArray) return JToken.FromObject((value as BitArray)?.To1010());
|
||||
if (value is IPAddress) return JToken.FromObject((value as IPAddress)?.ToString());
|
||||
if (value is ValueTuple<IPAddress, int> || value is ValueTuple<IPAddress, int>?)
|
||||
{
|
||||
ValueTuple<IPAddress, int>? cidrValue = (ValueTuple<IPAddress, int>?)value;
|
||||
return JToken.FromObject(cidrValue == null ? null : $"{cidrValue.Value.Item1.ToString()}/{cidrValue.Value.Item2.ToString()}");
|
||||
}
|
||||
return JToken.FromObject(value?.ToString());
|
||||
}
|
||||
public static JToken GetJObject(Array value, int[] indices, int idx)
|
||||
{
|
||||
if (idx == indices.Length)
|
||||
{
|
||||
return GetJObject(value.GetValue(indices));
|
||||
}
|
||||
JArray ja = new JArray();
|
||||
if (indices.Length == 1)
|
||||
{
|
||||
foreach (object a in value)
|
||||
ja.Add(GetJObject(a));
|
||||
return ja;
|
||||
}
|
||||
int lb = value.GetLowerBound(idx);
|
||||
int ub = value.GetUpperBound(idx);
|
||||
for (int b = lb; b <= ub; b++)
|
||||
{
|
||||
indices[idx] = b;
|
||||
ja.Add(GetJObject(value, indices, idx + 1));
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
using NpgsqlTypes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
public static partial class QuestDbTypesExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 测量两个经纬度的距离,返回单位:米
|
||||
/// </summary>
|
||||
/// <param name="that">经纬坐标1</param>
|
||||
/// <param name="point">经纬坐标2</param>
|
||||
/// <returns>返回距离(单位:米)</returns>
|
||||
public static double Distance(this NpgsqlPoint that, NpgsqlPoint point)
|
||||
{
|
||||
double radLat1 = (double)(that.Y) * Math.PI / 180d;
|
||||
double radLng1 = (double)(that.X) * Math.PI / 180d;
|
||||
double radLat2 = (double)(point.Y) * Math.PI / 180d;
|
||||
double radLng2 = (double)(point.X) * Math.PI / 180d;
|
||||
return 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin((radLat1 - radLat2) / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin((radLng1 - radLng2) / 2), 2))) * 6378137;
|
||||
}
|
||||
|
||||
#if net40
|
||||
#else
|
||||
/// <summary>
|
||||
/// 测量两个经纬度的距离,返回单位:米
|
||||
/// </summary>
|
||||
/// <param name="that">经纬坐标1</param>
|
||||
/// <param name="point">经纬坐标2</param>
|
||||
/// <returns>返回距离(单位:米)</returns>
|
||||
public static double Distance(this Npgsql.LegacyPostgis.PostgisPoint that, Npgsql.LegacyPostgis.PostgisPoint point)
|
||||
{
|
||||
double radLat1 = (double)(that.Y) * Math.PI / 180d;
|
||||
double radLng1 = (double)(that.X) * Math.PI / 180d;
|
||||
double radLat2 = (double)(point.Y) * Math.PI / 180d;
|
||||
double radLng2 = (double)(point.X) * Math.PI / 180d;
|
||||
return 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin((radLat1 - radLat2) / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin((radLng1 - radLng2) / 2), 2))) * 6378137;
|
||||
}
|
||||
|
||||
public static NpgsqlRange<T> ToNpgsqlRange<T>(this string that)
|
||||
{
|
||||
var s = that;
|
||||
if (string.IsNullOrEmpty(s) || s == "empty") return NpgsqlRange<T>.Empty;
|
||||
string s1 = s.Trim('(', ')', '[', ']');
|
||||
string[] ss = s1.Split(new char[] { ',' }, 2);
|
||||
if (ss.Length != 2) return NpgsqlRange<T>.Empty;
|
||||
T t1 = default(T);
|
||||
T t2 = default(T);
|
||||
if (!string.IsNullOrEmpty(ss[0])) t1 = (T)Convert.ChangeType(ss[0], typeof(T));
|
||||
if (!string.IsNullOrEmpty(ss[1])) t2 = (T)Convert.ChangeType(ss[1], typeof(T));
|
||||
return new NpgsqlRange<T>(t1, s[0] == '[', s[0] == '(', t2, s[s.Length - 1] == ']', s[s.Length - 1] == ')');
|
||||
}
|
||||
#endif
|
||||
|
||||
public static string To1010(this BitArray ba)
|
||||
{
|
||||
char[] ret = new char[ba.Length];
|
||||
for (int a = 0; a < ba.Length; a++) ret[a] = ba[a] ? '1' : '0';
|
||||
return new string(ret);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 1010101010 这样的二进制字符串转换成 BitArray
|
||||
/// </summary>
|
||||
/// <param name="_1010Str">1010101010</param>
|
||||
/// <returns></returns>
|
||||
public static BitArray ToBitArray(this string _1010Str)
|
||||
{
|
||||
if (_1010Str == null) return null;
|
||||
BitArray ret = new BitArray(_1010Str.Length);
|
||||
for (int a = 0; a < _1010Str.Length; a++) ret[a] = _1010Str[a] == '1';
|
||||
return ret;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user