mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Merge branch 'dev' of github.com:sunkaixuan/SqlSugar
This commit is contained in:
commit
3676ccc006
@ -124,6 +124,23 @@ new List<SugarParameter>() {
|
||||
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
|
||||
new SugarParameter("@Name","张三")
|
||||
}, t11.Key, t11.Value, "Insert t11 error");
|
||||
|
||||
|
||||
var t12 = db.Insertable<Student>(new { Name = "a" }).ToSql();
|
||||
base.Check(@"INSERT INTO [STudent]
|
||||
([Name])
|
||||
VALUES
|
||||
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
|
||||
new SugarParameter("@Name","a")
|
||||
}, t12.Key, t12.Value, "Insert t12 error");
|
||||
|
||||
var t13 = db.Insertable<Student>(new Dictionary<string, object>() { {"id",0 },{ "name","2"} }).ToSql();
|
||||
base.Check(@"INSERT INTO [STudent]
|
||||
([Name])
|
||||
VALUES
|
||||
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
|
||||
new SugarParameter("@Name","2")
|
||||
}, t13.Key, t13.Value, "Insert t13 error");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,6 +144,21 @@ namespace OrmTest.UnitTest
|
||||
"Update 12 error"
|
||||
);
|
||||
|
||||
|
||||
|
||||
var t13 = db.Updateable<Student>(new { Name = "a", id=1 }).ToSql();
|
||||
base.Check(@"UPDATE [STudent] SET
|
||||
[Name]=@Name WHERE [Id]=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@Name","a"),
|
||||
new SugarParameter("@ID",1)
|
||||
}, t13.Key, t13.Value, "Insert t13 error");
|
||||
|
||||
var t14 = db.Updateable<Student>(new Dictionary<string, object>() { { "id", 0 }, { "name", "2" } }).ToSql();
|
||||
base.Check(@"UPDATE [STudent] SET
|
||||
[Name]=@Name WHERE [Id]=@Id", new List<SugarParameter>() {
|
||||
new SugarParameter("@Name", "2"),
|
||||
new SugarParameter("@ID", 0)
|
||||
}, t14.Key, t14.Value, "Insert t14 error");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user