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

Added collection param to IQuery.For and IQuery.ForIndex method #389

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions src/YesSql.Abstractions/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ public interface IQuery
/// Adds a filter on the document type
/// </summary>
/// <param name="filterType">If <c>false</c> the document type won't be filtered.</param>
/// <param name="collection">The name of the collection to load the object.</param>
/// <typeparam name="T">The type of document to return</typeparam>
IQuery<T> For<T>(bool filterType = true) where T : class;
IQuery<T> For<T>(bool filterType = true, string collection = null) where T : class;

/// <summary>
/// Defines what type of index should be returned
/// </summary>
/// <param name="collection">The name of the collection to load the object.</param>
/// <typeparam name="T">The type of index to return</typeparam>
IQueryIndex<T> ForIndex<T>() where T : class, IIndex;

IQueryIndex<T> ForIndex<T>(string collection = null) where T : class, IIndex;

/// <summary>
/// Returns documents from any type
Expand Down
21 changes: 18 additions & 3 deletions src/YesSql.Core/Services/DefaultQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public QueryState(ISqlBuilder sqlBuilder, IStore store, string collection)

public string _bindingName = "a1";
public Dictionary<string, List<Type>> _bindings = new Dictionary<string, List<Type>>();
public readonly string _documentTable;
public string _documentTable;
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe remove it from there and only compute it when the query is executed?

public string _lastParameterName;
public ISqlBuilder _sqlBuilder;
public List<Action<object, ISqlBuilder>> _parameterBindings;
Expand All @@ -57,6 +57,11 @@ public void FlushFilters()
_predicate = null;
}
}
public void SetCurrentCollection(string collection)
{
_collection = collection;
_documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(collection);
}

public string GetTableAlias(string tableName)
{
Expand Down Expand Up @@ -1039,8 +1044,13 @@ public async Task<int> CountAsync()
}
}

IQuery<T> IQuery.For<T>(bool filterType)
IQuery<T> IQuery.For<T>(bool filterType, string collection)
{
if (collection != null)
{
_collection = collection;
_queryState.SetCurrentCollection(collection);
}
_queryState.GetBindings().Clear();
_queryState.AddBinding(typeof(Document));

Expand All @@ -1056,8 +1066,13 @@ IQuery<T> IQuery.For<T>(bool filterType)
return new Query<T>(this);
}

IQueryIndex<TIndex> IQuery.ForIndex<TIndex>()
IQueryIndex<TIndex> IQuery.ForIndex<TIndex>(string collection)
{
if (collection != null)
{
_collection = collection;
_queryState.SetCurrentCollection(collection);
}
_queryState.GetBindings().Clear();
_queryState.AddBinding(typeof(TIndex));
_queryState._sqlBuilder.Select();
Expand Down
2 changes: 1 addition & 1 deletion src/YesSql.Core/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ private void BatchCommands()
// holds the queries, parameters and actions returned by an IIndexCommand, until we know we can
// add it to a batch if it fits the limits (page size and parameters boundaries)
var localDbCommand = _connection.CreateCommand();
var localQueries = new List<string>();
var localQueries = new List<string>();
var localActions = new List<Action<DbDataReader>>();

var batch = new BatchCommand(_connection.CreateCommand());
Expand Down
2 changes: 2 additions & 0 deletions test/YesSql.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4155,6 +4155,8 @@ public async Task ShouldQueryInnerSelectWithCollection()
Assert.Equal(0, await session.Query<Person, PersonByNameCol>(collection: "Col1").Where(x => x.Name.IsNotInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());

Assert.Equal(2, await session.Query("Col1").For<Person>().With<PersonByNameCol>().Where(x => x.Name.IsInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
Assert.Equal(2, await session.Query().For<Person>(collection:"Col1").With<PersonByNameCol>().Where(x => x.Name.IsInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
Assert.Equal(2, await session.Query("Col2").For<Person>(collection: "Col1").With<PersonByNameCol>().Where(x => x.Name.IsInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
Copy link
Owner

Choose a reason for hiding this comment

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

Actually I am confused now. Query already has a constructor with a collection, so the For call should use this one. So I read the issue again and I see that you are trying to hack the state of the query. I'd prefer a solution where you can plug a custom "CollectionResolver" that could be configured instead. So the Query() constructor would either take a collection name, or a CollectionResolver, or nothing to use default Collectionresolver from the configuration. That might be even better for your design.


}
}
Expand Down