2017-01-07 21:54:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-05-24 19:06:36 +08:00
|
|
|
|
using System.ComponentModel;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
using System.Data;
|
2020-11-18 01:15:04 +08:00
|
|
|
|
using System.Diagnostics;
|
2019-05-24 19:06:36 +08:00
|
|
|
|
using System.Globalization;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
using System.Linq;
|
2021-03-02 19:58:42 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
using System.Reflection;
|
2021-01-22 16:09:17 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2018-12-31 13:50:10 +08:00
|
|
|
|
using System.Security.Cryptography;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
using System.Text;
|
2017-10-10 14:41:08 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
namespace SqlSugar
|
|
|
|
|
{
|
2017-08-25 22:25:29 +08:00
|
|
|
|
public class UtilMethods
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
2019-05-24 19:06:36 +08:00
|
|
|
|
|
2021-12-07 18:44:17 +08:00
|
|
|
|
internal static DateTime ConvertFromDateTimeOffset(DateTimeOffset dateTime)
|
|
|
|
|
{
|
|
|
|
|
if (dateTime.Offset.Equals(TimeSpan.Zero))
|
|
|
|
|
return dateTime.UtcDateTime;
|
|
|
|
|
else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))
|
|
|
|
|
return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);
|
|
|
|
|
else
|
|
|
|
|
return dateTime.DateTime;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 19:06:36 +08:00
|
|
|
|
internal static object To(object value, Type destinationType)
|
|
|
|
|
{
|
|
|
|
|
return To(value, destinationType, CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
2020-11-18 01:15:04 +08:00
|
|
|
|
|
2019-05-24 19:06:36 +08:00
|
|
|
|
internal static object To(object value, Type destinationType, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2019-05-28 21:51:29 +08:00
|
|
|
|
destinationType = UtilMethods.GetUnderType(destinationType);
|
2019-05-24 19:06:36 +08:00
|
|
|
|
var sourceType = value.GetType();
|
|
|
|
|
|
|
|
|
|
var destinationConverter = TypeDescriptor.GetConverter(destinationType);
|
|
|
|
|
if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
|
|
|
|
|
return destinationConverter.ConvertFrom(null, culture, value);
|
|
|
|
|
|
|
|
|
|
var sourceConverter = TypeDescriptor.GetConverter(sourceType);
|
|
|
|
|
if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
|
|
|
|
|
return sourceConverter.ConvertTo(null, culture, value, destinationType);
|
|
|
|
|
|
|
|
|
|
if (destinationType.IsEnum && value is int)
|
|
|
|
|
return Enum.ToObject(destinationType, (int)value);
|
|
|
|
|
|
|
|
|
|
if (!destinationType.IsInstanceOfType(value))
|
|
|
|
|
return Convert.ChangeType(value, destinationType, culture);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2021-01-22 16:09:17 +08:00
|
|
|
|
public static bool IsAnyAsyncMethod(StackFrame[] methods)
|
|
|
|
|
{
|
|
|
|
|
bool isAsync = false;
|
|
|
|
|
foreach (var item in methods)
|
|
|
|
|
{
|
|
|
|
|
if (UtilMethods.IsAsyncMethod(item.GetMethod()))
|
|
|
|
|
{
|
|
|
|
|
isAsync = true;
|
2021-07-16 02:38:57 +08:00
|
|
|
|
break;
|
2021-01-22 16:09:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return isAsync;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 15:54:42 +08:00
|
|
|
|
|
|
|
|
|
public static ConnectionConfig CopyConfig(ConnectionConfig it)
|
|
|
|
|
{
|
|
|
|
|
return new ConnectionConfig()
|
|
|
|
|
{
|
2022-05-05 17:26:53 +08:00
|
|
|
|
AopEvents =it.AopEvents==null?null:new AopEvents() {
|
|
|
|
|
DataExecuting=it.AopEvents?.DataExecuting,
|
|
|
|
|
OnDiffLogEvent=it.AopEvents?.OnDiffLogEvent,
|
|
|
|
|
OnError=it.AopEvents?.OnError,
|
|
|
|
|
OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql,
|
|
|
|
|
OnLogExecuted=it.AopEvents?.OnLogExecuted,
|
|
|
|
|
OnLogExecuting= it.AopEvents?.OnLogExecuting,
|
|
|
|
|
},
|
2022-05-05 15:54:42 +08:00
|
|
|
|
ConfigId = it.ConfigId,
|
2022-05-05 17:26:53 +08:00
|
|
|
|
ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {
|
|
|
|
|
AppendDataReaderTypeMappings=it.ConfigureExternalServices.AppendDataReaderTypeMappings,
|
|
|
|
|
DataInfoCacheService=it.ConfigureExternalServices.DataInfoCacheService,
|
|
|
|
|
EntityNameService=it.ConfigureExternalServices.EntityNameService,
|
|
|
|
|
EntityService=it.ConfigureExternalServices.EntityService,
|
|
|
|
|
RazorService=it.ConfigureExternalServices.RazorService,
|
|
|
|
|
ReflectionInoCacheService=it.ConfigureExternalServices.ReflectionInoCacheService,
|
|
|
|
|
SerializeService=it.ConfigureExternalServices.SerializeService,
|
|
|
|
|
SplitTableService=it.ConfigureExternalServices.SplitTableService,
|
|
|
|
|
SqlFuncServices=it.ConfigureExternalServices.SqlFuncServices
|
|
|
|
|
},
|
2022-05-05 15:54:42 +08:00
|
|
|
|
ConnectionString = it.ConnectionString,
|
|
|
|
|
DbType = it.DbType,
|
|
|
|
|
IndexSuffix = it.IndexSuffix,
|
|
|
|
|
InitKeyType = it.InitKeyType,
|
|
|
|
|
IsAutoCloseConnection = it.IsAutoCloseConnection,
|
|
|
|
|
LanguageType = it.LanguageType,
|
|
|
|
|
MoreSettings = it.MoreSettings == null ? null : new ConnMoreSettings()
|
|
|
|
|
{
|
|
|
|
|
DefaultCacheDurationInSeconds = it.MoreSettings.DefaultCacheDurationInSeconds,
|
|
|
|
|
DisableNvarchar = it.MoreSettings.DisableNvarchar,
|
|
|
|
|
PgSqlIsAutoToLower = it.MoreSettings.PgSqlIsAutoToLower,
|
|
|
|
|
IsAutoRemoveDataCache = it.MoreSettings.IsAutoRemoveDataCache,
|
|
|
|
|
IsWithNoLockQuery = it.MoreSettings.IsWithNoLockQuery,
|
|
|
|
|
TableEnumIsString = it.MoreSettings.TableEnumIsString,
|
2022-05-22 13:55:42 +08:00
|
|
|
|
DisableMillisecond = it.MoreSettings.DisableMillisecond,
|
|
|
|
|
DbMinDate=it.MoreSettings.DbMinDate
|
2022-05-05 15:54:42 +08:00
|
|
|
|
},
|
|
|
|
|
SqlMiddle = it.SqlMiddle == null ? null : new SqlMiddle
|
|
|
|
|
{
|
|
|
|
|
IsSqlMiddle = it.SqlMiddle.IsSqlMiddle,
|
|
|
|
|
ExecuteCommand = it.SqlMiddle.ExecuteCommand,
|
|
|
|
|
ExecuteCommandAsync = it.SqlMiddle.ExecuteCommandAsync,
|
|
|
|
|
GetDataReader = it.SqlMiddle.GetDataReader,
|
|
|
|
|
GetDataReaderAsync = it.SqlMiddle.GetDataReaderAsync,
|
|
|
|
|
GetDataSetAll = it.SqlMiddle.GetDataSetAll,
|
|
|
|
|
GetDataSetAllAsync = it.SqlMiddle.GetDataSetAllAsync,
|
|
|
|
|
GetScalar = it.SqlMiddle.GetScalar,
|
|
|
|
|
GetScalarAsync = it.SqlMiddle.GetScalarAsync
|
|
|
|
|
},
|
|
|
|
|
SlaveConnectionConfigs = it.SlaveConnectionConfigs
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 16:09:17 +08:00
|
|
|
|
public static bool IsAsyncMethod(MethodBase method)
|
|
|
|
|
{
|
|
|
|
|
if (method == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-16 02:38:57 +08:00
|
|
|
|
if (method.DeclaringType != null)
|
|
|
|
|
{
|
|
|
|
|
if (method.DeclaringType.GetInterfaces().Contains(typeof(IAsyncStateMachine)))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var name = method.Name;
|
2021-01-22 16:54:28 +08:00
|
|
|
|
if (name.Contains("OutputAsyncCausalityEvents"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-01-22 19:29:26 +08:00
|
|
|
|
if (name.Contains("OutputWaitEtwEvents"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-01-22 19:52:40 +08:00
|
|
|
|
if (name.Contains("ExecuteAsync"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-16 02:38:57 +08:00
|
|
|
|
Type attType = typeof(AsyncStateMachineAttribute);
|
2021-01-22 16:09:17 +08:00
|
|
|
|
var attrib = (AsyncStateMachineAttribute)method.GetCustomAttribute(attType);
|
|
|
|
|
return (attrib != null);
|
|
|
|
|
}
|
2019-05-24 19:06:36 +08:00
|
|
|
|
|
2020-11-18 01:15:04 +08:00
|
|
|
|
public static StackTraceInfo GetStackTrace()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
StackTrace st = new StackTrace(true);
|
|
|
|
|
StackTraceInfo info = new StackTraceInfo();
|
|
|
|
|
info.MyStackTraceList = new List<StackTraceInfoItem>();
|
|
|
|
|
info.SugarStackTraceList = new List<StackTraceInfoItem>();
|
|
|
|
|
for (int i = 0; i < st.FrameCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var frame = st.GetFrame(i);
|
2021-07-16 02:38:57 +08:00
|
|
|
|
if (frame.GetMethod().Module.Name.ToLower() != "sqlsugar.dll" && frame.GetMethod().Name.First() != '<')
|
2020-11-18 01:15:04 +08:00
|
|
|
|
{
|
|
|
|
|
info.MyStackTraceList.Add(new StackTraceInfoItem()
|
|
|
|
|
{
|
|
|
|
|
FileName = frame.GetFileName(),
|
|
|
|
|
MethodName = frame.GetMethod().Name,
|
|
|
|
|
Line = frame.GetFileLineNumber()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
info.SugarStackTraceList.Add(new StackTraceInfoItem()
|
|
|
|
|
{
|
|
|
|
|
FileName = frame.GetFileName(),
|
|
|
|
|
MethodName = frame.GetMethod().Name,
|
|
|
|
|
Line = frame.GetFileLineNumber()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 19:06:36 +08:00
|
|
|
|
internal static T To<T>(object value)
|
|
|
|
|
{
|
|
|
|
|
return (T)To(value, typeof(T));
|
|
|
|
|
}
|
2022-05-13 21:53:22 +08:00
|
|
|
|
|
|
|
|
|
internal static DateTime GetMinDate(ConnectionConfig currentConnectionConfig)
|
|
|
|
|
{
|
|
|
|
|
if (currentConnectionConfig.MoreSettings == null)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToDateTime("1900-01-01");
|
|
|
|
|
}
|
|
|
|
|
else if (currentConnectionConfig.MoreSettings.DbMinDate == null)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToDateTime("1900-01-01");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return currentConnectionConfig.MoreSettings.DbMinDate.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-09 19:12:41 +08:00
|
|
|
|
internal static Type GetUnderType(Type oldType)
|
|
|
|
|
{
|
|
|
|
|
Type type = Nullable.GetUnderlyingType(oldType);
|
2018-10-13 03:31:18 +08:00
|
|
|
|
return type == null ? oldType : type;
|
2017-07-09 19:12:41 +08:00
|
|
|
|
}
|
2019-04-21 11:36:48 +08:00
|
|
|
|
public static string ReplaceSqlParameter(string itemSql, SugarParameter itemParameter, string newName)
|
|
|
|
|
{
|
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"{0} ", "\\" + itemParameter.ParameterName), newName + " ", RegexOptions.IgnoreCase);
|
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\)", "\\" + itemParameter.ParameterName), newName + ")", RegexOptions.IgnoreCase);
|
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\,", "\\" + itemParameter.ParameterName), newName + ",", RegexOptions.IgnoreCase);
|
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"{0}$", "\\" + itemParameter.ParameterName), newName, RegexOptions.IgnoreCase);
|
2020-11-18 01:15:04 +08:00
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0}\+", "\\" + itemParameter.ParameterName), "+" + newName + "+", RegexOptions.IgnoreCase);
|
2021-07-16 02:38:57 +08:00
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0} ", "\\" + itemParameter.ParameterName), "+" + newName + " ", RegexOptions.IgnoreCase);
|
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@" {0}\+", "\\" + itemParameter.ParameterName), " " + newName + "+", RegexOptions.IgnoreCase);
|
2021-10-05 16:49:18 +08:00
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"\|\|{0}\|\|", "\\" + itemParameter.ParameterName), "||" + newName + "||", RegexOptions.IgnoreCase);
|
2021-04-25 15:51:57 +08:00
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"\={0}\+", "\\" + itemParameter.ParameterName), "=" + newName + "+", RegexOptions.IgnoreCase);
|
2021-10-05 16:49:18 +08:00
|
|
|
|
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\|\|", "\\" + itemParameter.ParameterName), newName + "||", RegexOptions.IgnoreCase);
|
2019-04-21 11:36:48 +08:00
|
|
|
|
return itemSql;
|
|
|
|
|
}
|
2018-05-03 16:22:18 +08:00
|
|
|
|
internal static Type GetRootBaseType(Type entityType)
|
|
|
|
|
{
|
|
|
|
|
var baseType = entityType.BaseType;
|
|
|
|
|
while (baseType != null && baseType.BaseType != UtilConstants.ObjType)
|
|
|
|
|
{
|
|
|
|
|
baseType = baseType.BaseType;
|
|
|
|
|
}
|
|
|
|
|
return baseType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable)
|
|
|
|
|
{
|
|
|
|
|
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
|
|
|
|
|
isNullable = unType != null;
|
|
|
|
|
unType = unType ?? propertyInfo.PropertyType;
|
|
|
|
|
return unType;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 14:26:49 +08:00
|
|
|
|
internal static Type GetUnderType(PropertyInfo propertyInfo)
|
|
|
|
|
{
|
|
|
|
|
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
|
|
|
|
|
unType = unType ?? propertyInfo.PropertyType;
|
|
|
|
|
return unType;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 15:41:57 +08:00
|
|
|
|
internal static bool IsNullable(PropertyInfo propertyInfo)
|
|
|
|
|
{
|
|
|
|
|
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
|
2017-07-09 19:12:41 +08:00
|
|
|
|
return unType != null;
|
2017-06-12 15:41:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 12:40:29 +08:00
|
|
|
|
internal static bool IsNullable(Type type)
|
|
|
|
|
{
|
|
|
|
|
Type unType = Nullable.GetUnderlyingType(type);
|
|
|
|
|
return unType != null;
|
|
|
|
|
}
|
2017-07-09 19:12:41 +08:00
|
|
|
|
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
if (returnObj.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
returnObj = new T();
|
|
|
|
|
}
|
|
|
|
|
return returnObj;
|
|
|
|
|
}
|
2021-07-16 02:38:57 +08:00
|
|
|
|
public static object ChangeType2(object value, Type type)
|
2021-07-02 18:28:27 +08:00
|
|
|
|
{
|
|
|
|
|
if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
|
|
|
|
|
if (value == null) return null;
|
|
|
|
|
if (type == value.GetType()) return value;
|
|
|
|
|
if (type.IsEnum)
|
|
|
|
|
{
|
|
|
|
|
if (value is string)
|
|
|
|
|
return Enum.Parse(type, value as string);
|
|
|
|
|
else
|
|
|
|
|
return Enum.ToObject(type, value);
|
|
|
|
|
}
|
|
|
|
|
if (!type.IsInterface && type.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
Type innerType = type.GetGenericArguments()[0];
|
|
|
|
|
object innerValue = ChangeType(value, innerType);
|
|
|
|
|
return Activator.CreateInstance(type, new object[] { innerValue });
|
|
|
|
|
}
|
|
|
|
|
if (value is string && type == typeof(Guid)) return new Guid(value as string);
|
|
|
|
|
if (value is string && type == typeof(Version)) return new Version(value as string);
|
|
|
|
|
if (!(value is IConvertible)) return value;
|
|
|
|
|
return Convert.ChangeType(value, type);
|
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
2017-07-09 19:12:41 +08:00
|
|
|
|
internal static T ChangeType<T>(T obj, Type type)
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(obj, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static T ChangeType<T>(T obj)
|
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(obj, typeof(T));
|
|
|
|
|
}
|
2017-08-26 08:57:18 +08:00
|
|
|
|
|
2020-12-30 18:38:55 +08:00
|
|
|
|
internal static DateTimeOffset GetDateTimeOffsetByDateTime(DateTime date)
|
|
|
|
|
{
|
|
|
|
|
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
|
|
|
|
|
DateTimeOffset utcTime2 = date;
|
|
|
|
|
return utcTime2;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 01:15:04 +08:00
|
|
|
|
internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex, string append = null)
|
2017-08-26 08:57:18 +08:00
|
|
|
|
{
|
2017-10-10 19:49:16 +08:00
|
|
|
|
if (appendSql.HasValue() && parameters.HasValue())
|
2017-08-26 08:57:18 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var parameter in parameters.OrderByDescending(it => it.ParameterName.Length))
|
|
|
|
|
{
|
|
|
|
|
//Compatible with.NET CORE parameters case
|
|
|
|
|
var name = parameter.ParameterName;
|
2020-11-18 01:15:04 +08:00
|
|
|
|
string newName = name + append + addIndex;
|
2019-04-21 11:44:20 +08:00
|
|
|
|
appendSql = ReplaceSqlParameter(appendSql, parameter, newName);
|
2017-08-26 08:57:18 +08:00
|
|
|
|
parameter.ParameterName = newName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static string GetPackTable(string sql, string shortName)
|
|
|
|
|
{
|
|
|
|
|
return string.Format(" ({0}) {1} ", sql, shortName);
|
|
|
|
|
}
|
2017-10-10 14:41:08 +08:00
|
|
|
|
|
2021-07-19 12:35:51 +08:00
|
|
|
|
public static Func<string, object> GetTypeConvert(object value)
|
|
|
|
|
{
|
|
|
|
|
if (value is int || value is uint || value is int? || value is uint?)
|
|
|
|
|
{
|
|
|
|
|
return x => Convert.ToInt32(x);
|
|
|
|
|
}
|
|
|
|
|
else if (value is short || value is ushort || value is short? || value is ushort?)
|
|
|
|
|
{
|
|
|
|
|
return x => Convert.ToInt16(x);
|
|
|
|
|
}
|
|
|
|
|
else if (value is long || value is long? || value is ulong? || value is long?)
|
|
|
|
|
{
|
|
|
|
|
return x => Convert.ToInt64(x);
|
|
|
|
|
}
|
|
|
|
|
else if (value is DateTime|| value is DateTime?)
|
|
|
|
|
{
|
|
|
|
|
return x => Convert.ToDateTime(x);
|
|
|
|
|
}
|
|
|
|
|
else if (value is bool||value is bool?)
|
|
|
|
|
{
|
|
|
|
|
return x => Convert.ToBoolean(x);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 17:55:55 +08:00
|
|
|
|
internal static string GetTypeName(object value)
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return value.GetType().Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 14:41:08 +08:00
|
|
|
|
internal static string GetParenthesesValue(string dbTypeName)
|
|
|
|
|
{
|
|
|
|
|
if (Regex.IsMatch(dbTypeName, @"\(.+\)"))
|
|
|
|
|
{
|
|
|
|
|
dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", "");
|
|
|
|
|
}
|
|
|
|
|
dbTypeName = dbTypeName.Trim();
|
|
|
|
|
return dbTypeName;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 13:10:34 +08:00
|
|
|
|
internal static T GetOldValue<T>(T value, Action action)
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2018-10-13 03:31:18 +08:00
|
|
|
|
|
|
|
|
|
internal static object DefaultForType(Type targetType)
|
|
|
|
|
{
|
|
|
|
|
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 01:15:04 +08:00
|
|
|
|
internal static Int64 GetLong(byte[] bytes)
|
2018-10-13 03:31:18 +08:00
|
|
|
|
{
|
2018-10-13 05:44:02 +08:00
|
|
|
|
return Convert.ToInt64(string.Join("", bytes).PadRight(20, '0'));
|
2018-10-13 03:31:18 +08:00
|
|
|
|
}
|
2021-03-04 22:31:31 +08:00
|
|
|
|
public static object GetPropertyValue<T>(T t, string PropertyName)
|
|
|
|
|
{
|
|
|
|
|
return t.GetType().GetProperty(PropertyName).GetValue(t, null);
|
|
|
|
|
}
|
2018-12-31 13:50:10 +08:00
|
|
|
|
internal static string GetMD5(string myString)
|
|
|
|
|
{
|
|
|
|
|
MD5 md5 = new MD5CryptoServiceProvider();
|
|
|
|
|
byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
|
|
|
|
|
byte[] targetData = md5.ComputeHash(fromData);
|
|
|
|
|
string byte2String = null;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < targetData.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte2String += targetData[i].ToString("x");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return byte2String;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 07:32:54 +08:00
|
|
|
|
public static string EncodeBase64(string code)
|
|
|
|
|
{
|
2022-03-13 11:02:28 +08:00
|
|
|
|
if (StaticConfig.Encode != null)
|
|
|
|
|
{
|
|
|
|
|
return StaticConfig.Encode(code);
|
|
|
|
|
}
|
2019-04-10 07:32:54 +08:00
|
|
|
|
if (code.IsNullOrEmpty()) return code;
|
|
|
|
|
string encode = "";
|
|
|
|
|
byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(code);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
encode = Convert.ToBase64String(bytes);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
encode = code;
|
|
|
|
|
}
|
|
|
|
|
return encode;
|
|
|
|
|
}
|
2019-05-15 18:11:44 +08:00
|
|
|
|
public static string ConvertNumbersToString(string value)
|
|
|
|
|
{
|
|
|
|
|
string[] splitInt = value.Split(new char[] { '9' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
var splitChars = splitInt.Select(s => Convert.ToChar(
|
|
|
|
|
Convert.ToInt32(s, 8)
|
|
|
|
|
).ToString());
|
|
|
|
|
|
|
|
|
|
return string.Join("", splitChars);
|
|
|
|
|
}
|
|
|
|
|
public static string ConvertStringToNumbers(string value)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (char c in value)
|
|
|
|
|
{
|
|
|
|
|
int cAscil = (int)c;
|
|
|
|
|
sb.Append(Convert.ToString(c, 8) + "9");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 07:32:54 +08:00
|
|
|
|
public static string DecodeBase64(string code)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-03-13 11:02:28 +08:00
|
|
|
|
if (StaticConfig.Decode != null)
|
|
|
|
|
{
|
|
|
|
|
return StaticConfig.Decode(code);
|
|
|
|
|
}
|
2019-04-10 07:32:54 +08:00
|
|
|
|
if (code.IsNullOrEmpty()) return code;
|
|
|
|
|
string decode = "";
|
|
|
|
|
byte[] bytes = Convert.FromBase64String(code);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
decode = Encoding.GetEncoding("utf-8").GetString(bytes);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
decode = code;
|
|
|
|
|
}
|
|
|
|
|
return decode;
|
|
|
|
|
}
|
2020-11-18 01:15:04 +08:00
|
|
|
|
catch
|
2019-04-10 07:32:54 +08:00
|
|
|
|
{
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 19:58:42 +08:00
|
|
|
|
public static void DataInoveByExpresson<Type>(Type[] datas, MethodCallExpression callExpresion)
|
|
|
|
|
{
|
|
|
|
|
var methodInfo = callExpresion.Method;
|
|
|
|
|
foreach (var item in datas)
|
|
|
|
|
{
|
|
|
|
|
if (callExpresion.Arguments.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
methodInfo.Invoke(item, null);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
List<object> methodParameters = new List<object>();
|
|
|
|
|
foreach (var callItem in callExpresion.Arguments)
|
|
|
|
|
{
|
2021-04-01 17:15:07 +08:00
|
|
|
|
var parameter = callItem.GetType().GetProperties().FirstOrDefault(it => it.Name == "Value");
|
|
|
|
|
if (parameter == null)
|
|
|
|
|
{
|
|
|
|
|
var value = LambdaExpression.Lambda(callItem).Compile().DynamicInvoke();
|
|
|
|
|
methodParameters.Add(value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var value = parameter.GetValue(callItem, null);
|
|
|
|
|
methodParameters.Add(value);
|
|
|
|
|
}
|
2021-03-02 19:58:42 +08:00
|
|
|
|
}
|
|
|
|
|
methodInfo.Invoke(item, methodParameters.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-20 14:23:15 +08:00
|
|
|
|
|
|
|
|
|
public static Dictionary<string, T> EnumToDictionary<T>()
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, T> dic = new Dictionary<string, T>();
|
|
|
|
|
if (!typeof(T).IsEnum)
|
|
|
|
|
{
|
|
|
|
|
return dic;
|
|
|
|
|
}
|
|
|
|
|
string desc = string.Empty;
|
|
|
|
|
foreach (var item in Enum.GetValues(typeof(T)))
|
|
|
|
|
{
|
|
|
|
|
var key = item.ToString().ToLower();
|
|
|
|
|
if (!dic.ContainsKey(key))
|
|
|
|
|
dic.Add(key, (T)item);
|
|
|
|
|
}
|
|
|
|
|
return dic;
|
|
|
|
|
}
|
2022-01-09 17:55:55 +08:00
|
|
|
|
public static object ConvertDataByTypeName(string ctypename,string value)
|
|
|
|
|
{
|
|
|
|
|
var item = new ConditionalModel() {
|
|
|
|
|
CSharpTypeName = ctypename,
|
|
|
|
|
FieldValue = value
|
|
|
|
|
};
|
|
|
|
|
if (item.CSharpTypeName.EqualCase(UtilConstants.DecType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToDecimal(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.DobType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToDouble(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.DateType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToDateTime(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.IntType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt32(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.LongType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt64(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.ShortType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt16(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.DateTimeOffsetType.Name))
|
|
|
|
|
{
|
|
|
|
|
return UtilMethods.GetDateTimeOffsetByDateTime(Convert.ToDateTime(item.FieldValue));
|
|
|
|
|
}
|
2022-03-04 13:39:20 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.GuidType.Name))
|
|
|
|
|
{
|
|
|
|
|
return Guid.Parse(item.FieldValue);
|
|
|
|
|
}
|
2022-03-13 11:02:28 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("int"))
|
2022-03-04 13:39:20 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt32(item.FieldValue);
|
|
|
|
|
}
|
2022-05-13 20:36:22 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("long"))
|
2022-03-04 13:39:20 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt64(item.FieldValue);
|
|
|
|
|
}
|
2022-03-13 11:02:28 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("short"))
|
2022-03-04 13:39:20 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt16(item.FieldValue);
|
|
|
|
|
}
|
2022-03-13 11:02:28 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("byte"))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToByte(item.FieldValue);
|
|
|
|
|
}
|
2022-05-13 20:36:22 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint"))
|
2022-03-13 11:02:28 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToUInt32(item.FieldValue);
|
|
|
|
|
}
|
2022-05-13 20:36:22 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("ulong"))
|
2022-03-13 11:02:28 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToUInt64(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("ushort"))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToUInt16(item.FieldValue);
|
|
|
|
|
}
|
2022-05-13 20:36:22 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint32"))
|
2022-03-13 11:02:28 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToUInt32(item.FieldValue);
|
|
|
|
|
}
|
2022-05-13 20:36:22 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint64"))
|
2022-03-13 11:02:28 +08:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToUInt64(item.FieldValue);
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint16"))
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToUInt16(item.FieldValue);
|
|
|
|
|
}
|
2022-05-13 20:36:22 +08:00
|
|
|
|
else if (item.CSharpTypeName.EqualCase("byte[]")&&item.FieldValue!=null&&item.FieldValue.Contains("|"))
|
|
|
|
|
{
|
|
|
|
|
return item.FieldValue.Split('|').Select(it=>Convert.ToByte(it)).ToArray();
|
|
|
|
|
}
|
2022-01-09 17:55:55 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return item.FieldValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-09 13:05:01 +08:00
|
|
|
|
|
2022-03-13 11:02:28 +08:00
|
|
|
|
public static bool IsNumber(string ctypename)
|
|
|
|
|
{
|
|
|
|
|
if (ctypename.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
var item = new ConditionalModel()
|
|
|
|
|
{
|
|
|
|
|
CSharpTypeName = ctypename,
|
|
|
|
|
};
|
|
|
|
|
if (item.CSharpTypeName.EqualCase(UtilConstants.DecType.Name))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.DobType.Name))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.IntType.Name))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.LongType.Name))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase(UtilConstants.ShortType.Name))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("int"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("long"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("short"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("byte"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("ulong"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("ushort"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint32"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint64"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (item.CSharpTypeName.EqualCase("uint16"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 13:05:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get Week Last Day Sun
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="datetime"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static DateTime GetWeekLastDaySun(DateTime datetime)
|
|
|
|
|
{
|
|
|
|
|
//星期天为最后一天
|
|
|
|
|
int weeknow = Convert.ToInt32(datetime.DayOfWeek);
|
|
|
|
|
weeknow = (weeknow == 0 ? 7 : weeknow);
|
|
|
|
|
int daydiff = (7 - weeknow);
|
|
|
|
|
|
|
|
|
|
//本周最后一天
|
|
|
|
|
string LastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");
|
|
|
|
|
return Convert.ToDateTime(LastDay);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get Week First Day Mon
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="datetime"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static DateTime GetWeekFirstDayMon(DateTime datetime)
|
|
|
|
|
{
|
|
|
|
|
//星期一为第一天
|
|
|
|
|
int weeknow = Convert.ToInt32(datetime.DayOfWeek);
|
|
|
|
|
|
|
|
|
|
//因为是以星期一为第一天,所以要判断weeknow等于0时,要向前推6天。
|
|
|
|
|
weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));
|
|
|
|
|
int daydiff = (-1) * weeknow;
|
|
|
|
|
|
|
|
|
|
//本周第一天
|
|
|
|
|
string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");
|
|
|
|
|
return Convert.ToDateTime(FirstDay);
|
|
|
|
|
}
|
2022-05-21 12:33:17 +08:00
|
|
|
|
|
|
|
|
|
public static string GetSqlString(ConnectionConfig connectionConfig,KeyValuePair<string, List<SugarParameter>> sqlObj, string result)
|
|
|
|
|
{
|
|
|
|
|
if (sqlObj.Value != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in sqlObj.Value.OrderByDescending(it => it.ParameterName.Length))
|
|
|
|
|
{
|
|
|
|
|
if (item.Value == null || item.Value == DBNull.Value)
|
|
|
|
|
{
|
|
|
|
|
result = result.Replace(item.ParameterName, "null");
|
|
|
|
|
}
|
|
|
|
|
else if (UtilMethods.IsNumber(item.Value.GetType().Name))
|
|
|
|
|
{
|
|
|
|
|
result = result.Replace(item.ParameterName, item.Value.ObjToString());
|
|
|
|
|
}
|
2022-05-21 12:35:35 +08:00
|
|
|
|
else if (connectionConfig.MoreSettings?.DisableNvarchar == true || item.DbType == System.Data.DbType.AnsiString || connectionConfig.DbType == DbType.Sqlite)
|
2022-05-21 12:33:17 +08:00
|
|
|
|
{
|
|
|
|
|
result = result.Replace(item.ParameterName, $"'{item.Value.ObjToString()}'");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = result.Replace(item.ParameterName, $"N'{item.Value.ObjToString()}'");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|