2017-01-07 21:54:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
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
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2018-11-08 16:19:16 +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;
|
2018-11-08 16:19:16 +08:00
|
|
|
|
string newName = name +append+ addIndex;
|
2017-08-26 08:57:18 +08:00
|
|
|
|
appendSql = appendSql.Replace(name, newName);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 05:44:02 +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
|
|
|
|
}
|
2018-10-13 05:44:02 +08:00
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|