Skip to content

Commit

Permalink
v0.9.13
Browse files Browse the repository at this point in the history
- 增加 FreeSql.Extensions.JsonMap 扩展包,实现快速将对象映射为json字符串的方法;
- 优化 表达式解析未实现的错误提醒,如 $"";
  • Loading branch information
28810 authored and 28810 committed Sep 12, 2019
1 parent 8520008 commit 62a095d
Show file tree
Hide file tree
Showing 33 changed files with 249 additions and 53 deletions.
31 changes: 31 additions & 0 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
using FreeSql;
using FreeSql.DataAnnotations;
using FreeSql.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace base_entity
{
class Program
{
class TestConfig
{
public int clicks { get; set; }
public string title { get; set; }
}
[Table(Name = "sysconfig")]
public class S_SysConfig<T> : BaseEntity<S_SysConfig<T>>
{
[Column(IsPrimary = true)]
public string Name { get; set; }

[JsonMap]
public T Config { get; set; }
}

static void Main(string[] args)
{
#region 初始化 IFreeSql
BaseEntity.Initialization(new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5")
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
.Build());
#endregion

BaseEntity.Orm.UseJsonMap();

new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11" } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }.Save();
var testconfigs11 = S_SysConfig<TestConfig>.Select.ToList();

Task.Run(async () =>
{
Expand Down
1 change: 1 addition & 0 deletions Examples/base_entity/base_entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.BaseEntity\FreeSql.Extensions.BaseEntity.csproj" />
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.JsonMap\FreeSql.Extensions.JsonMap.csproj" />
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void Initialization(IFreeSql fsql)
/// 创建时间
/// </summary>
[Column(Position = -4)]
public DateTime CreateTime { get; set; }
public DateTime CreateTime { get; set; } = DateTime.Now;
/// <summary>
/// 更新时间
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>0.9.12</Version>
<Version>0.9.15</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用.</Description>
Expand All @@ -12,6 +12,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>FreeSql;ORM;BaseEntity</PackageTags>
<PackageId>$(AssemblyName)</PackageId>
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Linq;

namespace FreeSql.DataAnnotations
{

/// <summary>
/// 当实体类属性为【对象】时,以JSON形式映射存储
/// </summary>
public class JsonMapAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.9.15</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql 扩展包,可实现实体类属性为对象时,以JSON形式映射存储.</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>FreeSql;ORM</PackageTags>
<PackageId>$(AssemblyName)</PackageId>
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using FreeSql.DataAnnotations;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace FreeSql.Extensions
{
public static class JsonMapCore
{
static bool _isAoped = false;
static object _isAopedLock = new object();
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>();
static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) });
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod("SerializeObject", new[] { typeof(object) });

/// <summary>
/// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时,该属性将以JSON形式映射存储
/// </summary>
/// <returns></returns>
public static void UseJsonMap(this IFreeSql that)
{
if (_isAoped == false)
lock(_isAopedLock)
if (_isAoped == false)
{
_isAoped = true;

FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) =>
{
if (_dicTypes.ContainsKey(type)) return Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type));
return null;
});

that.Aop.ConfigEntityProperty += new EventHandler<Aop.ConfigEntityPropertyEventArgs>((s, e) =>
{
if (e.Property.GetCustomAttribute<JsonMapAttribute>(false) != null)
{
e.ModifyResult.MapType = typeof(string);
if (_dicTypes.TryAdd(e.Property.PropertyType, true))
{
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionObjectToStringIfThenElse.Add((LabelTarget returnTarget, Expression valueExp, Expression elseExp, Type type) =>
{
return Expression.IfThenElse(
Expression.TypeEqual(valueExp, e.Property.PropertyType),
Expression.Return(returnTarget, Expression.Call(MethodJsonConvertSerializeObject, Expression.Convert(valueExp, typeof(object))), typeof(object)),
elseExp);
});
}
}
});
}
}
}
}
23 changes: 23 additions & 0 deletions Extensions/FreeSql.Extensions.JsonMap/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FreeSql 扩展包,将值对象映射成 typeof(string),安装扩展包:

> dotnet add package FreeSql.Extensions.JsonMap
```csharp
fsql.UseJsonMap(); //开启功能
class TestConfig
{
public int clicks { get; set; }
public string title { get; set; }
}

[Table(Name = "sysconfig")]
public class S_SysConfig<T>
{
[Column(IsPrimary = true)]
public string Name { get; set; }

[JsonMap]
public T Config { get; set; }
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.9.12</Version>
<Version>0.9.15</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql 扩展包,可实现【延时加载】属性.</Description>
Expand All @@ -12,6 +12,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>FreeSql;ORM</PackageTags>
<PackageId>$(AssemblyName)</PackageId>
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
Expand Down
3 changes: 2 additions & 1 deletion FreeSql.DbContext/FreeSql.DbContext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.9.12</Version>
<Version>0.9.15</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
Expand All @@ -11,6 +11,7 @@
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageId>$(AssemblyName)</PackageId>
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
Expand Down
3 changes: 2 additions & 1 deletion FreeSql.Repository/FreeSql.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.9.12</Version>
<Version>0.9.15</Version>
<Authors>YeXiangQin</Authors>
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
Expand All @@ -11,6 +11,7 @@
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageId>$(AssemblyName)</PackageId>
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
Expand Down
3 changes: 3 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ public class AuthorTest
[Fact]
public void Test1()
{

var testssargs1 = "10100";
var testformatsql1 = g.mysql.Select<TaskBuild>().Where(a => a.NamespaceName == $"1_{10100}").ToSql();
var testorderbysql = g.mysql.Select<TaskBuild>().OrderByDescending(a => a.OptionsEntity04 + (a.score ?? 0)).ToSql();

var testincludeMemberssql1 = g.sqlite.Select<TaskBuild>().Where(a => a.Templates.Title == "1").ToList();
Expand Down
15 changes: 15 additions & 0 deletions FreeSql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "base_entity", "Examples\bas
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.BaseEntity", "Extensions\FreeSql.Extensions.BaseEntity\FreeSql.Extensions.BaseEntity.csproj", "{FE0CB06E-493F-4CE8-B2D7-BF48CA8015C6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.JsonMap", "Extensions\FreeSql.Extensions.JsonMap\FreeSql.Extensions.JsonMap.csproj", "{3043DEF1-85DF-47AD-8D5D-327270794356}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -332,6 +334,18 @@ Global
{FE0CB06E-493F-4CE8-B2D7-BF48CA8015C6}.Release|x64.Build.0 = Release|Any CPU
{FE0CB06E-493F-4CE8-B2D7-BF48CA8015C6}.Release|x86.ActiveCfg = Release|Any CPU
{FE0CB06E-493F-4CE8-B2D7-BF48CA8015C6}.Release|x86.Build.0 = Release|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Debug|x64.ActiveCfg = Debug|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Debug|x64.Build.0 = Debug|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Debug|x86.ActiveCfg = Debug|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Debug|x86.Build.0 = Debug|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Release|Any CPU.Build.0 = Release|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Release|x64.ActiveCfg = Release|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Release|x64.Build.0 = Release|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Release|x86.ActiveCfg = Release|Any CPU
{3043DEF1-85DF-47AD-8D5D-327270794356}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -352,6 +366,7 @@ Global
{4C0973CB-BD49-4A5B-A6FE-EE0594BDD513} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
{D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
{FE0CB06E-493F-4CE8-B2D7-BF48CA8015C6} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
{3043DEF1-85DF-47AD-8D5D-327270794356} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}
Expand Down
4 changes: 3 additions & 1 deletion FreeSql/DataAnnotations/ColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public string Unique
public object DbDefautValue { get; internal set; }

/// <summary>
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
/// 类型映射,除了可做基本的类型映射外,特别介绍的功能:<para></para>
/// 1、将 enum 属性映射成 typeof(string)<para></para>
/// 2、将 对象 属性映射成 typeof(string),请安装扩展包 FreeSql.Extensions.JsonMap
/// </summary>
public Type MapType { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion FreeSql/FreeSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.9.12</Version>
<Version>0.9.15</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
Expand All @@ -12,6 +12,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>FreeSql;ORM</PackageTags>
<PackageId>$(AssemblyName)</PackageId>
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
Expand Down
4 changes: 3 additions & 1 deletion FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,16 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
tsc.mapType = null;
var exp3 = exp as MethodCallExpression;
var callType = exp3.Object?.Type ?? exp3.Method.DeclaringType;
string other3Exp = null;
switch (callType.FullName)
{
case "System.String": return ExpressionLambdaToSqlCallString(exp3, tsc);
case "System.Math": return ExpressionLambdaToSqlCallMath(exp3, tsc);
case "System.DateTime": return ExpressionLambdaToSqlCallDateTime(exp3, tsc);
case "System.TimeSpan": return ExpressionLambdaToSqlCallTimeSpan(exp3, tsc);
case "System.Convert": return ExpressionLambdaToSqlCallConvert(exp3, tsc);
case "System.String": other3Exp = ExpressionLambdaToSqlCallString(exp3, tsc); break;
case "System.Math": other3Exp = ExpressionLambdaToSqlCallMath(exp3, tsc); break;
case "System.DateTime": other3Exp = ExpressionLambdaToSqlCallDateTime(exp3, tsc); break;
case "System.TimeSpan": other3Exp = ExpressionLambdaToSqlCallTimeSpan(exp3, tsc); break;
case "System.Convert": other3Exp = ExpressionLambdaToSqlCallConvert(exp3, tsc); break;
}
if (string.IsNullOrEmpty(other3Exp) == false) return other3Exp;
if (exp3.Method.Name == "Equals" && exp3.Object != null && exp3.Arguments.Count > 0)
return ExpressionBinary("=", exp3.Object, exp3.Arguments[0], tsc);
if (callType.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`"))
Expand Down Expand Up @@ -849,7 +851,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)

// }
//}
var other3Exp = ExpressionLambdaToSqlOther(exp3, tsc);
other3Exp = ExpressionLambdaToSqlOther(exp3, tsc);
if (string.IsNullOrEmpty(other3Exp) == false) return other3Exp;
if (exp3.IsParameter() == false) return formatSql(Expression.Lambda(exp3).Compile().DynamicInvoke(), tsc.mapType);
throw new Exception($"未实现函数表达式 {exp3} 解析");
Expand Down
Loading

0 comments on commit 62a095d

Please sign in to comment.