Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryDefinition: Adds API to get query parameters #2210

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions Microsoft.Azure.Cosmos/src/Query/v3Query/QueryDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Azure.Cosmos.Query.Core;
using Newtonsoft.Json;
Expand All @@ -17,6 +18,8 @@ public class QueryDefinition
[JsonProperty(PropertyName = "parameters", NullValueHandling = NullValueHandling.Ignore, Order = 1)]
private List<SqlParameter> parameters { get; set; }

private ParametersListAdapter parametersAdapter;

/// <summary>
/// Create a <see cref="QueryDefinition"/>
/// </summary>
Expand Down Expand Up @@ -105,15 +108,60 @@ public QueryDefinition WithParameter(string name, object value)
return this;
}

/// <summary>
/// Returns the names and values of parameters in this <see cref="QueryDefinition"/>.
/// </summary>
/// <returns>
/// A list of name/value tuples representing the parameters of this <see cref="QueryDefinition"/>.
/// </returns>
public IReadOnlyList<(string Name, object Value)> GetQueryParameters()
{
return this.parametersAdapter ??= new ParametersListAdapter(this);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I exposed the parameters through a method because that's the API we agreed upon, but I think it might be more natural to expose them as a property.

If you agree, I can make that change. The name Parameters is already taken by an internal property, so we could either rename the existing property, or find another name for the new one (I lean toward the former).


internal SqlQuerySpec ToSqlQuerySpec()
{
return new SqlQuerySpec(this.QueryText, new SqlParameterCollection(this.parameters ?? new List<SqlParameter>()));
return new SqlQuerySpec(this.QueryText, new SqlParameterCollection(this.parameters ?? (IReadOnlyList<SqlParameter>)Array.Empty<SqlParameter>()));
}

/// <summary>
/// Gets the sql parameters for the class
/// </summary>
[JsonIgnore]
internal IReadOnlyList<SqlParameter> Parameters => this.parameters ?? new List<SqlParameter>();
internal IReadOnlyList<SqlParameter> Parameters => this.parameters ?? (IReadOnlyList<SqlParameter>)Array.Empty<SqlParameter>();
j82w marked this conversation as resolved.
Show resolved Hide resolved

private class ParametersListAdapter : IReadOnlyList<(string Name, object Value)>
{
private readonly QueryDefinition queryDefinition;

public ParametersListAdapter(QueryDefinition queryDefinition)
{
this.queryDefinition = queryDefinition;
}

public (string Name, object Value) this[int index]
{
get
{
SqlParameter param = this.queryDefinition.Parameters[index];
return (param.Name, param.Value);
}
}

public int Count => this.queryDefinition.Parameters.Count;

public IEnumerator<(string Name, object Value)> GetEnumerator()
{
foreach (SqlParameter param in this.queryDefinition.Parameters)
{
yield return (param.Name, param.Value);
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4426,6 +4426,11 @@
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.QueryDefinition WithParameter(System.String, System.Object);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Collections.Generic.IReadOnlyList`1[System.ValueTuple`2[System.String,System.Object]] GetQueryParameters()": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.Collections.Generic.IReadOnlyList`1[System.ValueTuple`2[System.String,System.Object]] GetQueryParameters();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.String get_QueryText()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Microsoft.Azure.Cosmos.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Cosmos.Query.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -65,5 +66,93 @@ public void ThrowOnNullConnectionString()
QueryDefinition sqlQueryDefinition = new QueryDefinition("select * from s where s.Account = 1234");
sqlQueryDefinition.WithParameter(null, null);
}

[TestMethod]
public void GetQueryParametersReturnsListOfTuples()
{
string query = "select * from s where s.Account = @account and s.Balance > @balance";
QueryDefinition queryDefinition = new QueryDefinition(query)
.WithParameter("@account", "12345")
.WithParameter("@balance", 42);

IReadOnlyList<(string Name, object Value)> parameters = queryDefinition.GetQueryParameters();

Assert.AreEqual(2, parameters.Count);
Assert.AreEqual("@account", parameters[0].Name);
Assert.AreEqual("12345", parameters[0].Value);
Assert.AreEqual("@balance", parameters[1].Name);
Assert.AreEqual(42, parameters[1].Value);
}

[TestMethod]
public void GetQueryParametersAlwaysReturnsTheSameInstance()
{
string query = "select * from s where s.Account = @account and s.Balance > @balance";
QueryDefinition queryDefinition = new QueryDefinition(query)
.WithParameter("@account", "12345")
.WithParameter("@balance", 42);

IReadOnlyList<(string Name, object Value)> parameters1 = queryDefinition.GetQueryParameters();
IReadOnlyList<(string Name, object Value)> parameters2 = queryDefinition.GetQueryParameters();

Assert.AreSame(parameters1, parameters2);
}

[TestMethod]
public void GetQueryParametersReflectsParametersAddedLater()
{
string query = "select * from s where s.Account = @account and s.Balance > @balance";
QueryDefinition queryDefinition = new QueryDefinition(query)
.WithParameter("@account", "12345");

IReadOnlyList<(string Name, object Value)> parameters = queryDefinition.GetQueryParameters();

queryDefinition.WithParameter("@balance", 42);

Assert.AreEqual(2, parameters.Count);
Assert.AreEqual("@account", parameters[0].Name);
Assert.AreEqual("12345", parameters[0].Value);
Assert.AreEqual("@balance", parameters[1].Name);
Assert.AreEqual(42, parameters[1].Value);
}

[TestMethod]
public void GetQueryParametersReflectsParametersChangedLater()
{
string query = "select * from s where s.Account = @account and s.Balance > @balance";
QueryDefinition queryDefinition = new QueryDefinition(query)
.WithParameter("@account", "12345")
.WithParameter("@balance", 42);

IReadOnlyList<(string Name, object Value)> parameters = queryDefinition.GetQueryParameters();

queryDefinition.WithParameter("@balance", 123);

Assert.AreEqual(2, parameters.Count);
Assert.AreEqual("@account", parameters[0].Name);
Assert.AreEqual("12345", parameters[0].Value);
Assert.AreEqual("@balance", parameters[1].Name);
Assert.AreEqual(123, parameters[1].Value);
}

[TestMethod]
public void GetQueryParametersGetEnumeratorEnumeratesParameters()
{
string query = "select * from s where s.Account = @account and s.Balance > @balance";
QueryDefinition queryDefinition = new QueryDefinition(query)
.WithParameter("@account", "12345")
.WithParameter("@balance", 42);

IReadOnlyList<(string Name, object Value)> parameters = queryDefinition.GetQueryParameters();
IEnumerator<(string Name, object Value)> enumerator = parameters.GetEnumerator();

Assert.IsTrue(enumerator.MoveNext());
Assert.AreEqual("@account", enumerator.Current.Name);
Assert.AreEqual("12345", enumerator.Current.Value);
Assert.IsTrue(enumerator.MoveNext());
Assert.AreEqual("@balance", enumerator.Current.Name);
Assert.AreEqual(42, enumerator.Current.Value);
Assert.IsFalse(enumerator.MoveNext());
}
}
}
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Preview features are treated as a separate branch and will not be included in th
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- [#2191](https://github.com/Azure/azure-cosmos-dotnet-v3/issues/2191) & [#796](https://github.com/Azure/azure-cosmos-dotnet-v3/issues/796) Query: Expose query parameters

### <a name="3.16.0"/> [3.16.0](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.16.0) - 2021-01-12

#### Added
Expand Down