Skip to content

Commit

Permalink
Added collection param to IQuery.For and IQuery.ForIndex method
Browse files Browse the repository at this point in the history
  • Loading branch information
jersiovic committed Jun 3, 2021
1 parent 738a796 commit 02ab692
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
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;
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());

}
}
Expand Down

0 comments on commit 02ab692

Please sign in to comment.