Skip to content

Commit 02ab692

Browse files
committed
Added collection param to IQuery.For and IQuery.ForIndex method
1 parent 738a796 commit 02ab692

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/YesSql.Abstractions/IQuery.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ public interface IQuery
1212
/// Adds a filter on the document type
1313
/// </summary>
1414
/// <param name="filterType">If <c>false</c> the document type won't be filtered.</param>
15+
/// <param name="collection">The name of the collection to load the object.</param>
1516
/// <typeparam name="T">The type of document to return</typeparam>
16-
IQuery<T> For<T>(bool filterType = true) where T : class;
17+
IQuery<T> For<T>(bool filterType = true, string collection = null) where T : class;
1718

1819
/// <summary>
1920
/// Defines what type of index should be returned
2021
/// </summary>
22+
/// <param name="collection">The name of the collection to load the object.</param>
2123
/// <typeparam name="T">The type of index to return</typeparam>
22-
IQueryIndex<T> ForIndex<T>() where T : class, IIndex;
24+
25+
IQueryIndex<T> ForIndex<T>(string collection = null) where T : class, IIndex;
2326

2427
/// <summary>
2528
/// Returns documents from any type

src/YesSql.Core/Services/DefaultQuery.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public QueryState(ISqlBuilder sqlBuilder, IStore store, string collection)
3030

3131
public string _bindingName = "a1";
3232
public Dictionary<string, List<Type>> _bindings = new Dictionary<string, List<Type>>();
33-
public readonly string _documentTable;
33+
public string _documentTable;
3434
public string _lastParameterName;
3535
public ISqlBuilder _sqlBuilder;
3636
public List<Action<object, ISqlBuilder>> _parameterBindings;
@@ -57,6 +57,11 @@ public void FlushFilters()
5757
_predicate = null;
5858
}
5959
}
60+
public void SetCurrentCollection(string collection)
61+
{
62+
_collection = collection;
63+
_documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(collection);
64+
}
6065

6166
public string GetTableAlias(string tableName)
6267
{
@@ -1039,8 +1044,13 @@ public async Task<int> CountAsync()
10391044
}
10401045
}
10411046

1042-
IQuery<T> IQuery.For<T>(bool filterType)
1047+
IQuery<T> IQuery.For<T>(bool filterType, string collection)
10431048
{
1049+
if (collection != null)
1050+
{
1051+
_collection = collection;
1052+
_queryState.SetCurrentCollection(collection);
1053+
}
10441054
_queryState.GetBindings().Clear();
10451055
_queryState.AddBinding(typeof(Document));
10461056

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

1059-
IQueryIndex<TIndex> IQuery.ForIndex<TIndex>()
1069+
IQueryIndex<TIndex> IQuery.ForIndex<TIndex>(string collection)
10601070
{
1071+
if (collection != null)
1072+
{
1073+
_collection = collection;
1074+
_queryState.SetCurrentCollection(collection);
1075+
}
10611076
_queryState.GetBindings().Clear();
10621077
_queryState.AddBinding(typeof(TIndex));
10631078
_queryState._sqlBuilder.Select();

src/YesSql.Core/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ private void BatchCommands()
781781
// holds the queries, parameters and actions returned by an IIndexCommand, until we know we can
782782
// add it to a batch if it fits the limits (page size and parameters boundaries)
783783
var localDbCommand = _connection.CreateCommand();
784-
var localQueries = new List<string>();
784+
var localQueries = new List<string>();
785785
var localActions = new List<Action<DbDataReader>>();
786786

787787
var batch = new BatchCommand(_connection.CreateCommand());

test/YesSql.Tests/CoreTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4155,6 +4155,8 @@ public async Task ShouldQueryInnerSelectWithCollection()
41554155
Assert.Equal(0, await session.Query<Person, PersonByNameCol>(collection: "Col1").Where(x => x.Name.IsNotInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
41564156

41574157
Assert.Equal(2, await session.Query("Col1").For<Person>().With<PersonByNameCol>().Where(x => x.Name.IsInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
4158+
Assert.Equal(2, await session.Query().For<Person>(collection:"Col1").With<PersonByNameCol>().Where(x => x.Name.IsInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
4159+
Assert.Equal(2, await session.Query("Col2").For<Person>(collection: "Col1").With<PersonByNameCol>().Where(x => x.Name.IsInAny<PersonByBothNamesCol>(y => y.Firstname)).CountAsync());
41584160

41594161
}
41604162
}

0 commit comments

Comments
 (0)