mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 01:05:27 +08:00 
			
		
		
		
	- 增加 神州通用 ShenTong 实现;#325
This commit is contained in:
		
							
								
								
									
										173
									
								
								Providers/FreeSql.Provider.ShenTong/ShenTongUtils.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										173
									
								
								Providers/FreeSql.Provider.ShenTong/ShenTongUtils.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,173 @@
 | 
			
		||||
using FreeSql.Internal;
 | 
			
		||||
using FreeSql.Internal.Model;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Collections.Concurrent;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data.Common;
 | 
			
		||||
using System.Data.OscarClient;
 | 
			
		||||
using System.Globalization;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql.ShenTong
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    class ShenTongUtils : CommonUtils
 | 
			
		||||
    {
 | 
			
		||||
        public ShenTongUtils(IFreeSql orm) : base(orm)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        static Array getParamterArrayValue(Type arrayType, object value, object defaultValue)
 | 
			
		||||
        {
 | 
			
		||||
            var valueArr = value as Array;
 | 
			
		||||
            var len = valueArr.GetLength(0);
 | 
			
		||||
            var ret = Array.CreateInstance(arrayType, len);
 | 
			
		||||
            for (var a = 0; a < len; a++)
 | 
			
		||||
            {
 | 
			
		||||
                var item = valueArr.GetValue(a);
 | 
			
		||||
                ret.SetValue(item == null ? defaultValue : getParamterValue(item.GetType(), item, 1), a);
 | 
			
		||||
            }
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
        static Dictionary<string, Func<object, object>> dicGetParamterValue = new Dictionary<string, Func<object, object>> {
 | 
			
		||||
            { typeof(uint).FullName, a => long.Parse(string.Concat(a)) }, { typeof(uint[]).FullName, a => getParamterArrayValue(typeof(long), a, 0) }, { typeof(uint?[]).FullName, a => getParamterArrayValue(typeof(long?), a, null) },
 | 
			
		||||
            { typeof(ulong).FullName, a => decimal.Parse(string.Concat(a)) }, { typeof(ulong[]).FullName, a => getParamterArrayValue(typeof(decimal), a, 0) }, { typeof(ulong?[]).FullName, a => getParamterArrayValue(typeof(decimal?), a, null) },
 | 
			
		||||
            { typeof(ushort).FullName, a => int.Parse(string.Concat(a)) }, { typeof(ushort[]).FullName, a => getParamterArrayValue(typeof(int), a, 0) }, { typeof(ushort?[]).FullName, a => getParamterArrayValue(typeof(int?), a, null) },
 | 
			
		||||
            { typeof(byte).FullName, a => short.Parse(string.Concat(a)) }, { typeof(byte[]).FullName, a => getParamterArrayValue(typeof(short), a, 0) }, { typeof(byte?[]).FullName, a => getParamterArrayValue(typeof(short?), a, null) },
 | 
			
		||||
            { typeof(sbyte).FullName, a => short.Parse(string.Concat(a)) }, { typeof(sbyte[]).FullName, a => getParamterArrayValue(typeof(short), a, 0) }, { typeof(sbyte?[]).FullName, a => getParamterArrayValue(typeof(short?), a, null) },
 | 
			
		||||
        };
 | 
			
		||||
        static object getParamterValue(Type type, object value, int level = 0)
 | 
			
		||||
        {
 | 
			
		||||
            if (type.FullName == "System.Byte[]") return value;
 | 
			
		||||
            if (type.IsArray && level == 0)
 | 
			
		||||
            {
 | 
			
		||||
                var elementType = type.GetElementType();
 | 
			
		||||
                Type enumType = null;
 | 
			
		||||
                if (elementType.IsEnum) enumType = elementType;
 | 
			
		||||
                else if (elementType.IsNullableType())
 | 
			
		||||
                {
 | 
			
		||||
                    var genericTypesFirst = elementType.GetGenericArguments().First();
 | 
			
		||||
                    if (genericTypesFirst.IsEnum) enumType = genericTypesFirst;
 | 
			
		||||
                }
 | 
			
		||||
                if (enumType != null) return enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
 | 
			
		||||
                    getParamterArrayValue(typeof(long), value, elementType.IsEnum ? null : enumType.CreateInstanceGetDefaultValue()) :
 | 
			
		||||
                    getParamterArrayValue(typeof(int), value, elementType.IsEnum ? null : enumType.CreateInstanceGetDefaultValue());
 | 
			
		||||
                return dicGetParamterValue.TryGetValue(type.FullName, out var trydicarr) ? trydicarr(value) : value;
 | 
			
		||||
            }
 | 
			
		||||
            if (type.IsNullableType()) type = type.GetGenericArguments().First();
 | 
			
		||||
            if (type.IsEnum) return (int)value;
 | 
			
		||||
            if (dicGetParamterValue.TryGetValue(type.FullName, out var trydic)) return trydic(value);
 | 
			
		||||
            return value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
 | 
			
		||||
        {
 | 
			
		||||
            if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
 | 
			
		||||
            if (value != null) value = getParamterValue(type, value);
 | 
			
		||||
            var ret = new OscarParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
 | 
			
		||||
            //if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) {
 | 
			
		||||
            //	ret.DataTypeName = "";
 | 
			
		||||
            //} else {
 | 
			
		||||
            var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
 | 
			
		||||
            if (tp != null) ret.OscarDbType = (OscarDbType)tp.Value;
 | 
			
		||||
            if (col != null)
 | 
			
		||||
            {
 | 
			
		||||
                var dbtype = (OscarDbType)_orm.DbFirst.GetDbType(new DatabaseModel.DbColumnInfo { DbTypeText = col.DbTypeText });
 | 
			
		||||
                if (dbtype != OscarDbType.Oidvector)
 | 
			
		||||
                {
 | 
			
		||||
                    ret.OscarDbType = dbtype;
 | 
			
		||||
                    if (col.DbSize != 0) ret.Size = col.DbSize;
 | 
			
		||||
                    if (col.DbPrecision != 0) ret.Precision = col.DbPrecision;
 | 
			
		||||
                    if (col.DbScale != 0) ret.Scale = col.DbScale;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            //}
 | 
			
		||||
            _params?.Add(ret);
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override DbParameter[] GetDbParamtersByObject(string sql, object obj) =>
 | 
			
		||||
            Utils.GetDbParamtersByObject<OscarParameter>(sql, obj, "@", (name, type, value) =>
 | 
			
		||||
            {
 | 
			
		||||
                if (value != null) value = getParamterValue(type, value);
 | 
			
		||||
                var ret = new OscarParameter { ParameterName = $"@{name}", Value = value };
 | 
			
		||||
                //if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) {
 | 
			
		||||
                //	ret.DataTypeName = "";
 | 
			
		||||
                //} else {
 | 
			
		||||
                var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
 | 
			
		||||
                if (tp != null) ret.OscarDbType = (OscarDbType)tp.Value;
 | 
			
		||||
                //}
 | 
			
		||||
                return ret;
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        public override string FormatSql(string sql, params object[] args) => sql?.FormatShenTong(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) => $"coalesce({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) => $"{left} % {right}";
 | 
			
		||||
        public override string Div(string left, string right, Type leftType, Type rightType) => $"{left} / {right}";
 | 
			
		||||
        public override string Now => "current_timestamp";
 | 
			
		||||
        public override string NowUtc => "(current_timestamp at time zone 'UTC')";
 | 
			
		||||
 | 
			
		||||
        public override string QuoteWriteParamter(Type type, string paramterName) => paramterName;
 | 
			
		||||
        public override string QuoteReadColumn(Type type, Type mapType, string columnName) => columnName;
 | 
			
		||||
 | 
			
		||||
        public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams, Type type, object value)
 | 
			
		||||
        {
 | 
			
		||||
            if (value == null) return "NULL";
 | 
			
		||||
            if (type.IsNumberType()) return string.Format(CultureInfo.InvariantCulture, "{0}", value);
 | 
			
		||||
            value = getParamterValue(type, value);
 | 
			
		||||
            var type2 = value.GetType();
 | 
			
		||||
            if (type2 == typeof(byte[])) return $"0x{CommonUtils.BytesSqlRaw(value as byte[])}";
 | 
			
		||||
            if (type2 == typeof(TimeSpan) || type2 == typeof(TimeSpan?))
 | 
			
		||||
            {
 | 
			
		||||
                var ts = (TimeSpan)value;
 | 
			
		||||
                var hh = Math.Min(24, (int)Math.Floor(ts.TotalHours));
 | 
			
		||||
                if (hh >= 24) hh = 0;
 | 
			
		||||
                value = $"{hh}:{ts.Minutes}:{ts.Seconds}.{ts.Milliseconds}";
 | 
			
		||||
            }
 | 
			
		||||
            else if (value is Array)
 | 
			
		||||
            {
 | 
			
		||||
                var valueArr = value as Array;
 | 
			
		||||
                var eleType = type2.GetElementType();
 | 
			
		||||
                var len = valueArr.GetLength(0);
 | 
			
		||||
                var sb = new StringBuilder().Append("ARRAY[");
 | 
			
		||||
                for (var a = 0; a < len; a++)
 | 
			
		||||
                {
 | 
			
		||||
                    var item = valueArr.GetValue(a);
 | 
			
		||||
                    if (a > 0) sb.Append(",");
 | 
			
		||||
                    sb.Append(GetNoneParamaterSqlValue(specialParams, eleType, item));
 | 
			
		||||
                }
 | 
			
		||||
                sb.Append("]");
 | 
			
		||||
                var dbinfo = _orm.CodeFirst.GetDbInfo(type);
 | 
			
		||||
                if (dbinfo != null) sb.Append("::").Append(dbinfo.dbtype);
 | 
			
		||||
                return sb.ToString();
 | 
			
		||||
            }
 | 
			
		||||
            return FormatSql("{0}", value, 1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user