mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-26 22:25:49 +08:00
Update Core
This commit is contained in:
@@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class RewritableMethods : IRewritableMethods
|
||||
{
|
||||
#region DataReader
|
||||
|
||||
/// <summary>
|
||||
///DataReader to Dynamic
|
||||
@@ -121,84 +122,6 @@ namespace SqlSugar
|
||||
return reval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize Object
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public string SerializeObject(object value)
|
||||
{
|
||||
return JsonConvert.SerializeObject(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize Object
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public T DeserializeObject<T>(string value)
|
||||
{
|
||||
if (value.IsValuable())
|
||||
{
|
||||
value = value.Replace(":{}", ":null");
|
||||
}
|
||||
return JsonConvert.DeserializeObject<T>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy new Object
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sourceObject"></param>
|
||||
/// <returns></returns>
|
||||
public T TranslateCopy<T>(T sourceObject)
|
||||
{
|
||||
if (sourceObject == null) return default(T);
|
||||
else
|
||||
{
|
||||
var jsonString = SerializeObject(sourceObject);
|
||||
return DeserializeObject<T>(jsonString);
|
||||
}
|
||||
}
|
||||
|
||||
public dynamic DataTableToDynamic(DataTable table)
|
||||
{
|
||||
List<Dictionary<string, object>> deserializeObject = new List<Dictionary<string, object>>();
|
||||
Dictionary<string, object> childRow;
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
childRow = new Dictionary<string, object>();
|
||||
foreach (DataColumn col in table.Columns)
|
||||
{
|
||||
childRow.Add(col.ColumnName, row[col]);
|
||||
}
|
||||
deserializeObject.Add(childRow);
|
||||
}
|
||||
return JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(deserializeObject));
|
||||
|
||||
}
|
||||
|
||||
public ICacheManager<T> GetCacheInstance<T>()
|
||||
{
|
||||
return CacheManager<T>.GetInstance();
|
||||
}
|
||||
|
||||
public void RemoveCacheAll()
|
||||
{
|
||||
CacheManager.RemoveAllCache();
|
||||
}
|
||||
|
||||
public void RemoveCacheAll<T>()
|
||||
{
|
||||
CacheManager<T>.GetInstance().RemoveAllCache();
|
||||
}
|
||||
|
||||
public void RemoveCache<T>(string key)
|
||||
{
|
||||
CacheManager<T>.GetInstance().Remove(key);
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
@@ -222,8 +145,94 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Serialize
|
||||
/// <summary>
|
||||
/// Serialize Object
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public string SerializeObject(object value)
|
||||
{
|
||||
return JsonConvert.SerializeObject(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize Object
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public T DeserializeObject<T>(string value)
|
||||
{
|
||||
if (value.IsValuable())
|
||||
{
|
||||
value = value.Replace(":{}", ":null");
|
||||
}
|
||||
return JsonConvert.DeserializeObject<T>(value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Copy Object
|
||||
/// <summary>
|
||||
/// Copy new Object
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sourceObject"></param>
|
||||
/// <returns></returns>
|
||||
public T TranslateCopy<T>(T sourceObject)
|
||||
{
|
||||
if (sourceObject == null) return default(T);
|
||||
else
|
||||
{
|
||||
var jsonString = SerializeObject(sourceObject);
|
||||
return DeserializeObject<T>(jsonString);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DataTable
|
||||
public dynamic DataTableToDynamic(DataTable table)
|
||||
{
|
||||
List<Dictionary<string, object>> deserializeObject = new List<Dictionary<string, object>>();
|
||||
Dictionary<string, object> childRow;
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
childRow = new Dictionary<string, object>();
|
||||
foreach (DataColumn col in table.Columns)
|
||||
{
|
||||
childRow.Add(col.ColumnName, row[col]);
|
||||
}
|
||||
deserializeObject.Add(childRow);
|
||||
}
|
||||
return JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(deserializeObject));
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Cache
|
||||
public ICacheManager<T> GetCacheInstance<T>()
|
||||
{
|
||||
return CacheManager<T>.GetInstance();
|
||||
}
|
||||
|
||||
public void RemoveCacheAll()
|
||||
{
|
||||
CacheManager.RemoveAllCache();
|
||||
}
|
||||
|
||||
public void RemoveCacheAll<T>()
|
||||
{
|
||||
CacheManager<T>.GetInstance().RemoveAllCache();
|
||||
}
|
||||
|
||||
public void RemoveCache<T>(string key)
|
||||
{
|
||||
CacheManager<T>.GetInstance().Remove(key);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +174,8 @@ namespace SqlSugar
|
||||
{
|
||||
var appendValue = this.Context.SqlParameterKeyWord + ExpressionConst.Const + Context.ParameterIndex;
|
||||
Context.ParameterIndex++;
|
||||
if (value != null && value.GetType().IsEnum()) {
|
||||
if (value != null && value.GetType().IsEnum())
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
}
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
@@ -204,7 +205,7 @@ namespace SqlSugar
|
||||
protected string AppendParameter(object paramterValue)
|
||||
{
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;;
|
||||
this.Context.ParameterIndex++; ;
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
|
||||
return parameterName;
|
||||
}
|
||||
@@ -254,7 +255,7 @@ namespace SqlSugar
|
||||
|
||||
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (item.NodeType == ExpressionType.Constant)
|
||||
if (item is ConstantExpression)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
@@ -280,12 +281,6 @@ namespace SqlSugar
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
else if (item is MethodCallExpression)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
else if (item is MemberExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
@@ -397,6 +392,12 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (item is MethodCallExpression|| item is UnaryExpression)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.ThrowNotSupportedException(item.GetType().Name);
|
||||
|
||||
@@ -261,6 +261,27 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Insertable(new T[] { insertObj });
|
||||
}
|
||||
public virtual IInsertable<T> Insertable<T>(Dictionary<string, object> columnDictionary) where T : class, new()
|
||||
{
|
||||
Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Insertable.columnDictionary can't be null");
|
||||
var insertObject = this.RewritableMethods.DeserializeObject<T>(this.RewritableMethods.SerializeObject(columnDictionary));
|
||||
var columns = columnDictionary.Select(it => it.Key).ToList();
|
||||
return this.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c,StringComparison.CurrentCultureIgnoreCase))); ;
|
||||
}
|
||||
public virtual IInsertable<T> Insertable<T>(dynamic insertDynamicObject) where T : class, new()
|
||||
{
|
||||
if (insertDynamicObject is T)
|
||||
{
|
||||
return this.Insertable((T)insertDynamicObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
var columns= ((object)insertDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList();
|
||||
Check.Exception(columns.IsNullOrEmpty(), "Insertable.updateDynamicObject can't be null");
|
||||
T insertObject = this.RewritableMethods.DeserializeObject<T>(this.RewritableMethods.SerializeObject(insertDynamicObject));
|
||||
return this.Insertable(insertObject).InsertColumns(it=> columns.Any(c=>it.Equals(c,StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Deleteable
|
||||
@@ -316,6 +337,27 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Updateable(new T[] { new T() });
|
||||
}
|
||||
public virtual IUpdateable<T> Updateable<T>(Dictionary<string, object> columnDictionary) where T : class, new()
|
||||
{
|
||||
Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Updateable.columnDictionary can't be null");
|
||||
var updateObject = this.RewritableMethods.DeserializeObject<T>(this.RewritableMethods.SerializeObject(columnDictionary));
|
||||
var columns = columnDictionary.Select(it => it.Key).ToList();
|
||||
return this.Updateable(updateObject).UpdateColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ;
|
||||
}
|
||||
public virtual IUpdateable<T> Updateable<T>(dynamic updateDynamicObject) where T : class, new()
|
||||
{
|
||||
if (updateDynamicObject is T)
|
||||
{
|
||||
return this.Updateable((T)updateDynamicObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
var columns = ((object)updateDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList();
|
||||
Check.Exception(columns.IsNullOrEmpty(), "Updateable.updateDynamicObject can't be null");
|
||||
T updateObject = this.RewritableMethods.DeserializeObject<T>(this.RewritableMethods.SerializeObject(updateDynamicObject));
|
||||
return this.Updateable(updateObject).UpdateColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DbFirst
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package >
|
||||
<metadata>
|
||||
<id>sqlSugarCore</id>
|
||||
<version>4.2.1.9</version>
|
||||
<version>4.2.2.1</version>
|
||||
<authors>sunkaixuan</authors>
|
||||
<owners>Landa</owners>
|
||||
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user