Skip to content

Commit

Permalink
JsonExtract
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Qu authored and Victor Qu committed Oct 12, 2024
1 parent 5cae4f1 commit 51f07b9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ProjectCommon.targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>0.0.2.0</Version>
<Version>0.0.2.1</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)$(VersionSuffix)</Version>
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ such filter operater just make api more restful (`Where=urlencode(complex condit
#### Func Fields:

- `Fields` return some Fields , no Fields or `Fields=*` is return all
- query string `?Fields=name,age`
- body `{"Fields":"name,age"}`
- query string `?Fields=name,age,json(data,'$.age')`
- body `{"Fields":"name,age,json(data,'$.age')"}`
- `TotalCount` return total count
- query string `?TotalCount=true`
- body `{"TotalCount":"true"}`
Expand All @@ -176,8 +176,8 @@ such filter operater just make api more restful (`Where=urlencode(complex condit
- query string `?Rows=100`
- body `{"Rows":100}`
- `OrderBy` sort result
- query string `?OrderBy=name:asc,age:desc`
- body `{"OrderBy":"name:asc,age:desc"}`
- query string `?OrderBy=name asc,age desc,json(data,'$.age') desc`
- body `{"OrderBy":"name asc,age desc,json(data,'$.age') desc"}`
- `Where` complex condition filter
- query string `?Where=urlencode( not(name like 'H%') or name like '%v%' )`
- body `{"Where":"not(name like 'H%') or name like '%v%'"}`
Expand Down Expand Up @@ -217,4 +217,6 @@ such filter operater just make api more restful (`Where=urlencode(complex condit
- `or`
- example ` age <= 30 or age > 60`
- `()`
- example ` (age <= 30 or age > 60) and name = 'killer'`
- example ` (age <= 30 or age > 60) and name = 'killer'`
- support json
- example ` json(data,'$.age') > 60`
38 changes: 34 additions & 4 deletions src/SV.Db.Sloth/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Linq.Expressions;
using System.Linq.Expressions;

namespace SV.Db
{
Expand Down Expand Up @@ -63,12 +62,12 @@ public static bool In<R>(this object o, params R[] source)
throw new NotImplementedException();
}

public static object JsonExtract(this object o, string path)
public static Any JsonExtract(this object o, string path)
{
throw new NotImplementedException();
}

public static object JsonExtract(this object o, string path, string aS)
public static Any JsonExtract(this object o, string path, string aS)
{
throw new NotImplementedException();
}
Expand All @@ -78,4 +77,35 @@ public static object Desc(this object o)
throw new NotImplementedException();
}
}

public class Any
{
public static bool operator ==(Any obj1, Any obj2) => throw new NotImplementedException();

public static bool operator !=(Any obj1, Any obj2) => throw new NotImplementedException();

public static bool operator >=(Any obj1, Any obj2) => throw new NotImplementedException();

public static bool operator <=(Any obj1, Any obj2) => throw new NotImplementedException();

public static bool operator >(Any obj1, Any obj2) => throw new NotImplementedException();

public static bool operator <(Any obj1, Any obj2) => throw new NotImplementedException();

public static implicit operator int(Any d) => throw new NotImplementedException();

public static implicit operator decimal(Any d) => throw new NotImplementedException();

public static implicit operator double(Any d) => throw new NotImplementedException();

public static implicit operator string(Any d) => throw new NotImplementedException();

public static implicit operator float(Any d) => throw new NotImplementedException();

public static implicit operator long(Any d) => throw new NotImplementedException();

public static implicit operator bool(Any d) => throw new NotImplementedException();

//public static explicit operator Any(byte b) => new Digit(b);
}
}
8 changes: 8 additions & 0 deletions src/SV.Db.Sloth/From.Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ private static ValueStatement ConvertValueStatement(Expression v)
}
else if (v.NodeType == ExpressionType.Call)
{
if (v is MethodCallExpression mc && mc.Method.Name == nameof(ExpressionExtensions.JsonExtract))
{
var f = new JsonFieldStatement();
f.Field = ExpressionExtensions.GetMemberName(mc.Arguments[0]);
f.Path = Expression.Lambda(mc.Arguments[1]).Compile().DynamicInvoke().ToString();
f.As = mc.Arguments.Count > 2 ? "," + Expression.Lambda(mc.Arguments[2]).Compile().DynamicInvoke().ToString() : string.Empty;
return f;
}
var o = Expression.Lambda(v).Compile().DynamicInvoke();
return ConvertConstantStatement(o);
}
Expand Down
11 changes: 11 additions & 0 deletions test/UT/Sloth/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public void SelectOrderByFields()
[Fact]
public void Where()
{
AssertWhere<QueryTest, OperaterStatement>(i => i.A.JsonExtract("$.a") == 1, o =>
{
Assert.Equal("=", o.Operater);
var f = Assert.IsType<JsonFieldStatement>(o.Left);
Assert.Equal("A", f.Field);
Assert.Equal("$.a", f.Path);
Assert.Empty(f.As);
var v = Assert.IsType<NumberValueStatement>(o.Right);
Assert.Equal(1, v.Value);
});

AssertWhere<QueryTest, OperaterStatement>(i => i.A > 1, o =>
{
Assert.Equal(">", o.Operater);
Expand Down

0 comments on commit 51f07b9

Please sign in to comment.