Skip to content
Permalink
Browse files

v0.9.13

- 增加 FreeSql.Extensions.JsonMap 扩展包,实现快速将对象映射为json字符串的方法;
- 优化 表达式解析未实现的错误提醒,如 $"";
  • Loading branch information
28810 28810
28810 authored and 28810 committed Sep 12, 2019
1 parent 8520008 commit 62a095df8f8a7a9c0bc66a8f65ae7d182d059a78
Showing with 249 additions and 53 deletions.
  1. +31 −0 Examples/base_entity/Program.cs
  2. +1 −0 Examples/base_entity/base_entity.csproj
  3. +1 −1 Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs
  4. +2 −1 Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj
  5. +13 −0 Extensions/FreeSql.Extensions.JsonMap/DataAnnotations/JsonMapAttribute.cs
  6. +29 −0 Extensions/FreeSql.Extensions.JsonMap/FreeSql.Extensions.JsonMap.csproj
  7. +58 −0 Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs
  8. +23 −0 Extensions/FreeSql.Extensions.JsonMap/readme.md
  9. +2 −1 Extensions/FreeSql.Extensions.LazyLoading/FreeSql.Extensions.LazyLoading.csproj
  10. +2 −1 FreeSql.DbContext/FreeSql.DbContext.csproj
  11. +2 −1 FreeSql.Repository/FreeSql.Repository.csproj
  12. +3 −0 FreeSql.Tests/FreeSql.Tests/UnitTest1.cs
  13. +15 −0 FreeSql.sln
  14. +3 −1 FreeSql/DataAnnotations/ColumnAttribute.cs
  15. +2 −1 FreeSql/FreeSql.csproj
  16. +3 −1 FreeSql/FreeSql.xml
  17. +8 −6 FreeSql/Internal/CommonExpression.cs
  18. +8 −4 FreeSql/Internal/UtilsExpressionTree.cs
  19. +2 −1 Providers/FreeSql.Provider.MySql/FreeSql.Provider.MySql.csproj
  20. +5 −5 Providers/FreeSql.Provider.MySql/MySqlExpression.cs
  21. +2 −2 Providers/FreeSql.Provider.MySql/MySqlProvider.cs
  22. +2 −1 Providers/FreeSql.Provider.MySqlConnector/FreeSql.Provider.MySqlConnector.csproj
  23. +2 −1 Providers/FreeSql.Provider.Oracle/FreeSql.Provider.Oracle.csproj
  24. +5 −5 Providers/FreeSql.Provider.Oracle/OracleExpression.cs
  25. +2 −1 Providers/FreeSql.Provider.PostgreSQL/FreeSql.Provider.PostgreSQL.csproj
  26. +5 −5 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs
  27. +2 −2 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLProvider.cs
  28. +2 −1 Providers/FreeSql.Provider.SqlServer/FreeSql.Provider.SqlServer.csproj
  29. +5 −5 Providers/FreeSql.Provider.SqlServer/SqlServerExpression.cs
  30. +2 −1 Providers/FreeSql.Provider.Sqlite/FreeSql.Provider.Sqlite.csproj
  31. +5 −5 Providers/FreeSql.Provider.Sqlite/SqliteExpression.cs
  32. BIN logo.png
  33. +2 −0 readme.md
@@ -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 () =>
{
@@ -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" />
@@ -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>
@@ -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>
@@ -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>
@@ -0,0 +1,13 @@
using System;
using System.Linq;

namespace FreeSql.DataAnnotations
{

/// <summary>
/// 当实体类属性为【对象】时,以JSON形式映射存储
/// </summary>
public class JsonMapAttribute : Attribute
{
}
}
@@ -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>
@@ -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);
});
}
}
});
}
}
}
}
@@ -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; }
}
```
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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();
@@ -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
@@ -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
@@ -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}
@@ -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; }

@@ -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>
@@ -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>

Some generated files are not rendered by default. Learn more.

@@ -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`"))
@@ -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} 解析");

0 comments on commit 62a095d

Please sign in to comment.
You can’t perform that action at this time.