-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support raw SQL projections in Select and OrderBy statements.
- Loading branch information
1 parent
e03ecc8
commit 71ecf9d
Showing
6 changed files
with
228 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Marten.Testing/Linq/SqlProjection/SqlProjectionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using Marten.Linq.SqlProjection; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Marten.Testing.Linq.SqlProjection | ||
{ | ||
public class SqlProjectionTests | ||
{ | ||
[Fact] | ||
public void Throws_NotSupportedException_when_called_directly() | ||
{ | ||
Should.Throw<NotSupportedException>( | ||
() => new object().SqlProjection<string>("COALESCE(d.data ->> 'UserName', ?)", "baz")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Marten.Linq.SqlProjection | ||
{ | ||
public static class SqlProjectionExtensions | ||
{ | ||
public static readonly MethodInfo MethodInfo = typeof(SqlProjectionExtensions) | ||
.GetMethod(nameof(SqlProjection), | ||
BindingFlags.Public | BindingFlags.Static); | ||
|
||
public static T SqlProjection<T>(this object doc, string sql, params object[] parameters) | ||
{ | ||
throw new NotSupportedException( | ||
$"{nameof(SqlProjection)} extension method can only be used in Marten Linq queries."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Weasel.Postgresql.SqlGeneration; | ||
|
||
namespace Marten.Linq.SqlProjection | ||
{ | ||
public static class SqlProjectionSqlFragment | ||
{ | ||
public static ISqlFragment TryParse(MethodCallExpression node, Func<Expression, Expression> visit = null) | ||
{ | ||
if (node == null) | ||
{ | ||
return null; | ||
} | ||
|
||
visit ??= x => x; | ||
|
||
if (!node.Method.IsGenericMethod || | ||
node.Method.GetGenericMethodDefinition() != SqlProjectionExtensions.MethodInfo) | ||
{ | ||
return null; | ||
} | ||
|
||
if (visit(node.Arguments[1]) is not ConstantExpression { Value: string sql }) | ||
{ | ||
throw new NotSupportedException("SqlProjection first parameter needs to resolve to a string"); | ||
} | ||
|
||
if (visit(node.Arguments[2]) is not ConstantExpression { Value: object[] sqlArguments }) | ||
{ | ||
throw new NotSupportedException("SqlProjection second parameter needs to resolve to an object[]"); | ||
} | ||
|
||
var whereFragment = new WhereFragment(sql, sqlArguments); | ||
return whereFragment; | ||
} | ||
} | ||
} |