Skip to content

Commit

Permalink
Backing out of named params in matchesSql
Browse files Browse the repository at this point in the history
  • Loading branch information
elexisvenator committed Jan 10, 2025
1 parent 8027c88 commit 656931a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 68 deletions.
23 changes: 1 addition & 22 deletions docs/documents/querying/linq/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,7 @@ public async Task query_with_matches_sql()
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L267-L282' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_with_matches_sql' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Alternatively, named parameters can be used by passing in an anonymous object or a `Dictionary<string, object?>`. This can be done anywhere where you can pass in sql parameters:

<!-- snippet: sample_query_with_matches_sql_parameters -->
<a id='snippet-sample_query_with_matches_sql_parameters'></a>
```cs
[Fact]
public async Task query_with_matches_sql_and_parameters()
{
using var session = theStore.LightweightSession();
var u = new User { FirstName = "Eric", LastName = "Smith" };
session.Store(u);
await session.SaveChangesAsync();

var parameters = new { First = "Eric", Last = "Smith" };
var user = session.Query<User>().Single(x =>
x.MatchesSql("data->> 'FirstName' = @First and data->> 'LastName' = @Last", parameters));

user.Id.ShouldBe(u.Id);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L284-L301' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_with_matches_sql_parameters' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Named parameters are not supported here, and will throw at runtime if they are used.

**But**, if you want to take advantage of the more recent and very powerful JSONPath style querying, use this flavor of
the same functionality that behaves exactly the same, but uses the '^' character for parameter placeholders to disambiguate
Expand Down
19 changes: 0 additions & 19 deletions src/DocumentDbTests/Reading/query_by_sql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,6 @@ public async Task query_with_matches_sql()

#endregion

#region sample_query_with_matches_sql_parameters

[Fact]
public async Task query_with_matches_sql_and_parameters()
{
using var session = theStore.LightweightSession();
var u = new User { FirstName = "Eric", LastName = "Smith" };
session.Store(u);
await session.SaveChangesAsync();

var parameters = new { First = "Eric", Last = "Smith" };
var user = session.Query<User>().Single(x =>
x.MatchesSql("data->> 'FirstName' = @First and data->> 'LastName' = @Last", parameters));

user.Id.ShouldBe(u.Id);
}

#endregion

[Fact]
public async Task query_with_select_in_query()
{
Expand Down
29 changes: 2 additions & 27 deletions src/LinqTests/Acceptance/matches_sql_queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public async Task query_using_matches_sql()
var user3 = new User { UserName = "baz" };
var user4 = new User { UserName = "jack" };

var paramObject = new { Name1 = "foo", Name2 = "bar" };
var paramDict = new Dictionary<string, object> { { "Name1", "foo" }, { "Name2", "bar" } };

await using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
await session.SaveChangesAsync();
Expand All @@ -37,26 +34,6 @@ public async Task query_using_matches_sql()
.ToList()
.Select(x => x.UserName)
.Single().ShouldBe("jack");



// no where clause, named params
session.Query<User>().Where(x => x.MatchesSql("d.data ->> 'UserName' = @Name1 or d.data ->> 'UserName' = @Name2", paramObject)).OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("baz", "jack");
session.Query<User>().Where(x => x.MatchesSql("d.data ->> 'UserName' = @Name1 or d.data ->> 'UserName' = @Name2", paramDict)).OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("baz", "jack");

// with a where clause, named params
session.Query<User>().Where(x => x.UserName != "baz" && x.MatchesSql("d.data ->> 'UserName' != @Name1 and d.data ->> 'UserName' != @Name2", paramObject))
.OrderBy(x => x.UserName)
.ToList()
.Select(x => x.UserName)
.Single().ShouldBe("jack");
session.Query<User>().Where(x => x.UserName != "baz" && x.MatchesSql("d.data ->> 'UserName' != @Name1 and d.data ->> 'UserName' != @Name2", paramDict))
.OrderBy(x => x.UserName)
.ToList()
.Select(x => x.UserName)
.Single().ShouldBe("jack");
}

[Fact]
Expand All @@ -66,16 +43,14 @@ public async Task query_using_where_fragment()
var user2 = new User { UserName = "bar" };
var user3 = new User { UserName = "baz" };
var user4 = new User { UserName = "jack" };
var user5 = new User { UserName = "jill" };

await using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4, user5);
session.Store(user1, user2, user3, user4);
await session.SaveChangesAsync();

var whereFragment = CompoundWhereFragment.And();
whereFragment.Add(new WhereFragment("d.data ->> 'UserName' != ?", "baz"));
whereFragment.Add(new WhereFragment("d.data ->> 'UserName' != @Name1", new { Name1 = "jack" }));
whereFragment.Add(new WhereFragment("d.data ->> 'UserName' != @Name2", new Dictionary<string, string> { { "Name2", "jill" } }));
whereFragment.Add(new WhereFragment("d.data ->> 'UserName' != ?", "jack"));

// no where clause
session.Query<User>().Where(x => x.MatchesSql(whereFragment)).OrderBy(x => x.UserName).Select(x => x.UserName)
Expand Down

0 comments on commit 656931a

Please sign in to comment.