Support Json

This commit is contained in:
sunkaixuan 2019-05-16 20:25:11 +08:00
parent 57d087f413
commit bcd187ec58
8 changed files with 71 additions and 6 deletions

View File

@ -8,7 +8,7 @@ namespace OrmTest
{
static void Main(string[] args)
{
OldTestMain.Init();
//OldTestMain.Init();
//Demo
Demo1_SqlSugarClient.Init();

View File

@ -140,6 +140,7 @@
<Compile Include="OldTest\UnitTest\Setting\MapColumn.cs" />
<Compile Include="OldTest\UnitTest\Setting\MapTable.cs" />
<Compile Include="OldTest\UnitTest\Update.cs" />
<Compile Include="UnitTest\UJson.cs" />
<Compile Include="UnitTest\Main.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -7,9 +8,23 @@ namespace OrmTest
{
public partial class NewUnitTest
{
public static SqlSugarClient Db=> new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
}
}
});
public static void Init()
{
Json();
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public partial class NewUnitTest
{
public static void Json()
{
Db.CodeFirst.InitTables<JsonTest>();
Db.DbMaintenance.TruncateTable("JsonTest");
Db.Insertable(new JsonTest() { Order = new Order { Id = 1, Name = "order1" } }).ExecuteCommand();
var list = Db.Queryable<JsonTest>().ToList();
Db.Updateable(new JsonTest() { Id=1,Order = new Order { Id = 2, Name = "order2" } }).ExecuteCommand();
var list2 = Db.Queryable<JsonTest>().ToList();
}
}
public class JsonTest
{
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SqlSugar.SugarColumn(ColumnDataType = "varchar(max)", IsJson = true)]
public Order Order { get; set; }
}
}

View File

@ -63,6 +63,7 @@ namespace SqlSugar
private static readonly MethodInfo getConvertdatetimeoffsetDate = typeof(IDataRecordExtensions).GetMethod("GetConvertdatetimeoffsetDate");
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
private static readonly MethodInfo getJson = typeof(IDataRecordExtensions).GetMethod("GetJson");
private static readonly MethodInfo getSqliteTypeNull = typeof(IDataRecordExtensions).GetMethod("GetSqliteTypeNull");
private static readonly MethodInfo getSqliteType = typeof(IDataRecordExtensions).GetMethod("GetSqliteType");
private static readonly MethodInfo getEntity = typeof(IDataRecordExtensions).GetMethod("GetEntity", new Type[] { typeof(SqlSugarProvider) });
@ -140,7 +141,7 @@ namespace SqlSugar
{
if (columnInfo.IsJson)
{
MethodInfo method = null;
MethodInfo jsonMethod = getJson.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
int i = DataRecord.GetOrdinal(fieldName);
Label endIfLabel = generator.DefineLabel();
generator.Emit(OpCodes.Ldarg_0);
@ -150,7 +151,7 @@ namespace SqlSugar
generator.Emit(OpCodes.Ldloc, result);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Call, method);
generator.Emit(OpCodes.Call, jsonMethod);
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod());
generator.MarkLabel(endIfLabel);
}

View File

@ -225,6 +225,15 @@ namespace SqlSugar
return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
}
public static T GetJson<T>(this IDataReader dr, int i)
{
var obj = dr.GetValue(i);
if (obj == null)
return default(T);
var value = obj.ObjToString();
return new SerializeService().DeserializeObject<T>(value);
}
public static Nullable<T> GetConvertEnum_Null<T>(this IDataReader dr, int i) where T : struct
{
if (dr.IsDBNull(i))
@ -303,7 +312,7 @@ namespace SqlSugar
{
return (T)Convert.ChangeType((dr.GetString(i)), type);
}
}
}
#endregion
}
}

View File

@ -348,6 +348,10 @@ namespace SqlSugar
{
columnInfo.Value = Convert.ToInt64(columnInfo.Value);
}
if (column.IsJson&& columnInfo.Value!=null)
{
columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value);
}
var tranColumn=EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
if (tranColumn!=null&&columnInfo.Value.HasValue()) {
columnInfo.Value = UtilMethods.EncodeBase64(columnInfo.Value.ToString());

View File

@ -455,6 +455,10 @@ namespace SqlSugar
{
columnInfo.Value = Convert.ToInt64(columnInfo.Value);
}
if (column.IsJson)
{
columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value);
}
var tranColumn = EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
if (tranColumn != null && columnInfo.Value.HasValue())
{