mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 01:05:27 +08:00 
			
		
		
		
	增加虚谷支持,基础版本
This commit is contained in:
		
							
								
								
									
										107
									
								
								Providers/FreeSql.Provider.Xugu/XuguAdo/XuguAdo.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								Providers/FreeSql.Provider.Xugu/XuguAdo/XuguAdo.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,107 @@
 | 
			
		||||
using FreeSql.Internal;
 | 
			
		||||
using FreeSql.Internal.Model;
 | 
			
		||||
using FreeSql.Internal.ObjectPool;
 | 
			
		||||
using Newtonsoft.Json.Linq;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data.Common;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using XuguClient;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql.Xugu
 | 
			
		||||
{
 | 
			
		||||
    class XuguAdo : FreeSql.Internal.CommonProvider.AdoProvider
 | 
			
		||||
    {
 | 
			
		||||
        public XuguAdo() : base(DataType.PostgreSQL, null, null) { }
 | 
			
		||||
        public XuguAdo(CommonUtils util, string masterConnectionString, string[] slaveConnectionStrings, Func<DbConnection> connectionFactory) : base(DataType.PostgreSQL, masterConnectionString, slaveConnectionStrings)
 | 
			
		||||
        {
 | 
			
		||||
            base._util = util; 
 | 
			
		||||
            if (connectionFactory != null)
 | 
			
		||||
            {
 | 
			
		||||
                MasterPool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.PostgreSQL, connectionFactory);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            if (!string.IsNullOrEmpty(masterConnectionString))
 | 
			
		||||
                MasterPool = new XuguConnectionPool(CoreStrings.S_MasterDatabase, masterConnectionString, null, null);
 | 
			
		||||
            if (slaveConnectionStrings != null)
 | 
			
		||||
            {
 | 
			
		||||
                foreach (var slaveConnectionString in slaveConnectionStrings)
 | 
			
		||||
                {
 | 
			
		||||
                    var slavePool = new XuguConnectionPool($"{CoreStrings.S_SlaveDatabase}{SlavePools.Count + 1}", slaveConnectionString, () => Interlocked.Decrement(ref slaveUnavailables), () => Interlocked.Increment(ref slaveUnavailables));
 | 
			
		||||
                    SlavePools.Add(slavePool);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
 | 
			
		||||
        {
 | 
			
		||||
            if (param == null) return "NULL";
 | 
			
		||||
            if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false || param is JToken || param is JObject || param is JArray))
 | 
			
		||||
                param = Utils.GetDataReaderValue(mapType, param);
 | 
			
		||||
 | 
			
		||||
            bool isdic;
 | 
			
		||||
            if (param is bool || param is bool?)
 | 
			
		||||
                return (bool)param ? "'t'" : "'f'";
 | 
			
		||||
            else if (param is string)
 | 
			
		||||
                return string.Concat("'", param.ToString().Replace("'", "''"), "'");
 | 
			
		||||
            else if (param is char)
 | 
			
		||||
                return string.Concat("'", param.ToString().Replace("'", "''").Replace('\0', ' '), "'");
 | 
			
		||||
            else if (param is Enum)
 | 
			
		||||
                return ((Enum)param).ToInt64();
 | 
			
		||||
            else if (decimal.TryParse(string.Concat(param), out var trydec))
 | 
			
		||||
                return param;
 | 
			
		||||
            else if (param is DateTime || param is DateTime?)
 | 
			
		||||
                return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss.ffffff"), "'");
 | 
			
		||||
            else if (param is TimeSpan || param is TimeSpan?)
 | 
			
		||||
                return ((TimeSpan)param).Ticks / 10;
 | 
			
		||||
            else if (param is byte[])
 | 
			
		||||
                return $"'\\x{CommonUtils.BytesSqlRaw(param as byte[])}'";
 | 
			
		||||
            else if (param is JToken || param is JObject || param is JArray)
 | 
			
		||||
                return string.Concat("'", param.ToString().Replace("'", "''"), "'::jsonb");
 | 
			
		||||
            else if ((isdic = param is Dictionary<string, string>) ||
 | 
			
		||||
                param is IEnumerable<KeyValuePair<string, string>>)
 | 
			
		||||
            {
 | 
			
		||||
                var pgdics = isdic ? param as Dictionary<string, string> :
 | 
			
		||||
                    param as IEnumerable<KeyValuePair<string, string>>;
 | 
			
		||||
                
 | 
			
		||||
                var pghstore = new StringBuilder("'");
 | 
			
		||||
                var pairs = pgdics.ToArray();
 | 
			
		||||
                
 | 
			
		||||
                for (var i = 0; i < pairs.Length; i++)
 | 
			
		||||
                {
 | 
			
		||||
                    if (i != 0) pghstore.Append(",");
 | 
			
		||||
 | 
			
		||||
                    pghstore.AppendFormat("\"{0}\"=>", pairs[i].Key.Replace("'", "''"));
 | 
			
		||||
 | 
			
		||||
                    if (pairs[i].Value == null)
 | 
			
		||||
                        pghstore.Append("NULL");
 | 
			
		||||
                    else
 | 
			
		||||
                        pghstore.AppendFormat("\"{0}\"", pairs[i].Value.Replace("'", "''"));
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                return pghstore.Append("'::hstore");
 | 
			
		||||
            }
 | 
			
		||||
            else if (param is IEnumerable)
 | 
			
		||||
                return AddslashesIEnumerable(param, mapType, mapColumn);
 | 
			
		||||
 | 
			
		||||
            return string.Concat("'", param.ToString().Replace("'", "''"), "'");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override DbCommand CreateCommand()
 | 
			
		||||
        {
 | 
			
		||||
            return new XGCommand();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override void ReturnConnection(IObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex)
 | 
			
		||||
        {
 | 
			
		||||
            var rawPool = pool as XuguConnectionPool;
 | 
			
		||||
            if (rawPool != null) rawPool.Return(conn, ex);
 | 
			
		||||
            else pool.Return(conn);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override DbParameter[] GetDbParamtersByObject(string sql, object obj) => _util.GetDbParamtersByObject(sql, obj);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										239
									
								
								Providers/FreeSql.Provider.Xugu/XuguAdo/XuguConnectionPool.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										239
									
								
								Providers/FreeSql.Provider.Xugu/XuguAdo/XuguConnectionPool.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,239 @@
 | 
			
		||||
using FreeSql.Internal.ObjectPool;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Concurrent;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Data.Common;
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql.Xugu
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    class XuguConnectionPool : ObjectPool<DbConnection>
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        internal Action availableHandler;
 | 
			
		||||
        internal Action unavailableHandler;
 | 
			
		||||
 | 
			
		||||
        public XuguConnectionPool(string name, string connectionString, Action availableHandler, Action unavailableHandler) : base(null)
 | 
			
		||||
        {
 | 
			
		||||
            this.availableHandler = availableHandler;
 | 
			
		||||
            this.unavailableHandler = unavailableHandler;
 | 
			
		||||
            var policy = new XuguConnectionPoolPolicy
 | 
			
		||||
            {
 | 
			
		||||
                _pool = this,
 | 
			
		||||
                Name = name
 | 
			
		||||
            };
 | 
			
		||||
            this.Policy = policy;
 | 
			
		||||
            policy.ConnectionString = connectionString;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Return(Object<DbConnection> obj, Exception exception, bool isRecreate = false)
 | 
			
		||||
        {
 | 
			
		||||
            //if (exception != null && exception is NpgsqlException)
 | 
			
		||||
            //{
 | 
			
		||||
            //    if (obj.Value.Ping() == false)
 | 
			
		||||
            //        base.SetUnavailable(exception, obj.LastGetTimeCopy);
 | 
			
		||||
            //}
 | 
			
		||||
            base.Return(obj, isRecreate);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    class XuguConnectionPoolPolicy : IPolicy<DbConnection>
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        internal XuguConnectionPool _pool;
 | 
			
		||||
        public string Name { get; set; } = $"Xugu XuguConnection {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);
 | 
			
		||||
        public int AsyncGetCapacity { get; set; } = 10000;
 | 
			
		||||
        public bool IsThrowGetTimeoutException { get; set; } = true;
 | 
			
		||||
        public bool IsAutoDisposeWithSystem { get; set; } = true;
 | 
			
		||||
        public int CheckAvailableInterval { get; set; } = 2;
 | 
			
		||||
        public int Weight { get; set; } = 1;
 | 
			
		||||
 | 
			
		||||
        static ConcurrentDictionary<string, int> dicConnStrIncr = new ConcurrentDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
 | 
			
		||||
        private string _connectionString;
 | 
			
		||||
        public string ConnectionString
 | 
			
		||||
        {
 | 
			
		||||
            get => _connectionString;
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                _connectionString = value ?? "";
 | 
			
		||||
 | 
			
		||||
                var minPoolSize = 0;
 | 
			
		||||
                var pattern = @"Min(imum)?\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
                var m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
                if (m.Success)
 | 
			
		||||
                {
 | 
			
		||||
                    minPoolSize = int.Parse(m.Groups[2].Value);
 | 
			
		||||
                    _connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                pattern = @"Max(imum)?\s*pool\s*size\s*=\s*(\d+)";
 | 
			
		||||
                m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
                if (m.Success == false || int.TryParse(m.Groups[2].Value, out var poolsize) == false || poolsize <= 0) poolsize = Math.Max(50, minPoolSize);
 | 
			
		||||
                var connStrIncr = dicConnStrIncr.AddOrUpdate(_connectionString, 1, (oldkey, oldval) => Math.Min(5, oldval + 1));
 | 
			
		||||
                PoolSize = poolsize + connStrIncr;
 | 
			
		||||
                _connectionString = m.Success ?
 | 
			
		||||
                    Regex.Replace(_connectionString, pattern, $"Maximum pool size={PoolSize}", RegexOptions.IgnoreCase) :
 | 
			
		||||
                    $"{_connectionString};Maximum pool size={PoolSize}";
 | 
			
		||||
 | 
			
		||||
                pattern = @"Connection\s*LifeTime\s*=\s*(\d+)";
 | 
			
		||||
                m = Regex.Match(_connectionString, pattern, RegexOptions.IgnoreCase);
 | 
			
		||||
                if (m.Success)
 | 
			
		||||
                {
 | 
			
		||||
                    IdleTimeout = TimeSpan.FromSeconds(int.Parse(m.Groups[1].Value));
 | 
			
		||||
                    _connectionString = Regex.Replace(_connectionString, pattern, "", RegexOptions.IgnoreCase);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                FreeSql.Internal.CommonUtils.PrevReheatConnectionPool(_pool, minPoolSize);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool OnCheckAvailable(Object<DbConnection> obj)
 | 
			
		||||
        {
 | 
			
		||||
            if (obj.Value == null) return false;
 | 
			
		||||
            if (obj.Value.State == ConnectionState.Closed) obj.Value.Open();
 | 
			
		||||
            return obj.Value.Ping(true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public DbConnection OnCreate()
 | 
			
		||||
        {
 | 
			
		||||
            var conn = new XuguClient.XGConnection(_connectionString);
 | 
			
		||||
            return conn;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void OnDestroy(DbConnection obj)
 | 
			
		||||
        {
 | 
			
		||||
            try { if (obj.State != ConnectionState.Closed) obj.Close(); } catch { }
 | 
			
		||||
 | 
			
		||||
            //try { XuguClient.XGConnection.ClearPool(obj as XuguClient.XGConnection); } catch { }
 | 
			
		||||
            obj.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void OnGet(Object<DbConnection> obj)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            if (_pool.IsAvailable)
 | 
			
		||||
            {
 | 
			
		||||
                if (obj.Value == null)
 | 
			
		||||
                {
 | 
			
		||||
                    _pool.SetUnavailable(new Exception(CoreStrings.S_ConnectionStringError), obj.LastGetTimeCopy);
 | 
			
		||||
                    throw new Exception(CoreStrings.S_ConnectionStringError_Check(this.Name));
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && obj.Value.Ping() == false)
 | 
			
		||||
                {
 | 
			
		||||
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        obj.Value.Open();
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception ex)
 | 
			
		||||
                    {
 | 
			
		||||
                        if (_pool.SetUnavailable(ex, obj.LastGetTimeCopy) == true)
 | 
			
		||||
                            throw new Exception($"【{this.Name}】Block access and wait for recovery: {ex.Message}");
 | 
			
		||||
                        throw ex;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
#if net40
 | 
			
		||||
#else
 | 
			
		||||
        async public Task OnGetAsync(Object<DbConnection> obj)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            if (_pool.IsAvailable)
 | 
			
		||||
            {
 | 
			
		||||
                if (obj.Value == null)
 | 
			
		||||
                {
 | 
			
		||||
                    _pool.SetUnavailable(new Exception(CoreStrings.S_ConnectionStringError), obj.LastGetTimeCopy);
 | 
			
		||||
                    throw new Exception(CoreStrings.S_ConnectionStringError_Check(this.Name));
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (obj.Value.State != ConnectionState.Open || DateTime.Now.Subtract(obj.LastReturnTime).TotalSeconds > 60 && (await obj.Value.PingAsync()) == false)
 | 
			
		||||
                {
 | 
			
		||||
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        await obj.Value.OpenAsync();
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception ex)
 | 
			
		||||
                    {
 | 
			
		||||
                        if (_pool.SetUnavailable(ex, obj.LastGetTimeCopy) == true)
 | 
			
		||||
                            throw new Exception($"【{this.Name}】Block access and wait for recovery: {ex.Message}");
 | 
			
		||||
                        throw ex;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
        public void OnGetTimeout()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void OnReturn(Object<DbConnection> obj)
 | 
			
		||||
        {
 | 
			
		||||
            //if (obj?.Value != null && obj.Value.State != ConnectionState.Closed) try { obj.Value.Close(); } catch { }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void OnAvailable()
 | 
			
		||||
        {
 | 
			
		||||
            _pool.availableHandler?.Invoke();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void OnUnavailable()
 | 
			
		||||
        {
 | 
			
		||||
            _pool.unavailableHandler?.Invoke();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static class DbConnectionExtensions
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        static DbCommand PingCommand(DbConnection conn)
 | 
			
		||||
        {
 | 
			
		||||
            var cmd = conn.CreateCommand();
 | 
			
		||||
            cmd.CommandTimeout = 5;
 | 
			
		||||
            cmd.CommandText = "select 1";
 | 
			
		||||
            return cmd;
 | 
			
		||||
        }
 | 
			
		||||
        public static bool Ping(this DbConnection that, bool isThrow = false)
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                PingCommand(that).ExecuteNonQuery();
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
            catch
 | 
			
		||||
            {
 | 
			
		||||
                if (that.State != ConnectionState.Closed) try { that.Close(); } catch { }
 | 
			
		||||
                if (isThrow) throw;
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
#if net40
 | 
			
		||||
#else
 | 
			
		||||
        async public static Task<bool> PingAsync(this DbConnection that, bool isThrow = false)
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                await PingCommand(that).ExecuteNonQueryAsync();
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
            catch
 | 
			
		||||
            {
 | 
			
		||||
                if (that.State != ConnectionState.Closed) try { that.Close(); } catch { }
 | 
			
		||||
                if (isThrow) throw;
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										155
									
								
								Providers/FreeSql.Provider.Xugu/XuguAdo/XuguTypesConverter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								Providers/FreeSql.Provider.Xugu/XuguAdo/XuguTypesConverter.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,155 @@
 | 
			
		||||
using Newtonsoft.Json.Linq;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net;
 | 
			
		||||
using System.Net.NetworkInformation;
 | 
			
		||||
 | 
			
		||||
namespace Newtonsoft.Json
 | 
			
		||||
{
 | 
			
		||||
    public class XuguTypesConverter : 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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,25 @@
 | 
			
		||||
using System.Collections;
 | 
			
		||||
 | 
			
		||||
public static partial class XuguTypesExtensions
 | 
			
		||||
{
 | 
			
		||||
   
 | 
			
		||||
    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